Author | Jamozed <[email protected]> |
Date | 2020-06-26 10:55:54 |
Commit | b3fcfc938f3d168a7ec39e0ed747c37252efe204 |
Parent | 4e723d65558e4ee97508ae95617ba2b080577f34 |
pwd: Add POSIX pwd
Diffstat
M | CMakeLists.txt | | | 1 | + |
M | README.md | | | 1 | + |
A | man/pwd.1 | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
A | src/pwd.c | | | 94 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 141 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index aae602b..16551b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ ADD_EXECUTABLE(false ${CMAKE_SOURCE_DIR}/src/false.c) ADD_EXECUTABLE(link ${CMAKE_SOURCE_DIR}/src/link.c) ADD_EXECUTABLE(logname ${CMAKE_SOURCE_DIR}/src/logname.c) ADD_EXECUTABLE(mkdir ${CMAKE_SOURCE_DIR}/src/mkdir.c) +ADD_EXECUTABLE(pwd ${CMAKE_SOURCE_DIR}/src/pwd.c) ADD_EXECUTABLE(rmdir ${CMAKE_SOURCE_DIR}/src/rmdir.c) ADD_EXECUTABLE(tee ${CMAKE_SOURCE_DIR}/src/tee.c) ADD_EXECUTABLE(true ${CMAKE_SOURCE_DIR}/src/true.c) diff --git a/README.md b/README.md index 496bfca..c4666d3 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ UNIX-like systems. | link | Create a link to a file | POSIX | | logname | Return the user's login name | POSIX | | mkdir | Make directories | POSIX | +| pwd | Print working directory | POSIX | | rmdir | Remove directories | POSIX | | tee | Duplicate standard input | POSIX | | true | Return true value | POSIX | diff --git a/man/pwd.1 b/man/pwd.1 new file mode 100644 index 0000000..a653bc1 --- /dev/null +++ b/man/pwd.1 @@ -0,0 +1,45 @@ +.TH PWD 1 2020-06-26 "OMKOV coreutils" "General Commands Manual" +.SH NAME +pwd \(em print working directory name +.SH SYNOPSYS +\fBpwd\fR [-L|-P] +.SH DESCRIPTION +Write the absolute pathname of the current working directory to standard output, +which does not contain the filenames dot or dot-dot. If no option is specified, +\fIpwd\fR behaves as if the \fB-L\fR option was specified. +.SH OPTIONS +The following options are supported: +.TP +.B -L +Display the logical current working directory. +.TP +.B -P +Display the physical current working directory (avoid symbolic links). +.TP +.B --help +Display help information. +.TP +.B --version +Display version information. +.SH ENVIROMENT +The following enviroment variable is used by \fIpwd\fR: +.TP +.I PWD +An absolute pathname to the current working directory. +.SH EXIT STATUS +The following exit values will be returned: +.TP +\ 0 +Successful completion. +.TP +>0 +An error occurred. +.SH STANDARDS +The \fIpwd\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2") +specification. +.SH COPYRIGHT +.nf +Copyright (C) 2020, Jakob Wakeling +All rights reserved. +OMKOV Permissive Licence (https://www.omkov.net/OLPE) +.fi diff --git a/src/pwd.c b/src/pwd.c new file mode 100644 index 0000000..f53e81e --- /dev/null +++ b/src/pwd.c @@ -0,0 +1,94 @@ +// pwd.c, version 1.0.0 +// OMKOV coreutils implementation of POSIX pwd +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Copyright (C) 2020, Jakob Wakeling +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "error.h" +#include "optget.h" + +#include <unistd.h> + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static int mode = 0; + +static void help(void); +static void version(void); + +int main(int argc, char *argv[]) { (void)(argc); + lop_t lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } + }; + + opt_t opt = OPTGET_INIT; opt.str = "LP"; opt.lops = lops; int o; + while ((o = optget(&opt, argv, 1)) != -1) switch (o) { + case 'L': { mode = 0; break; } + case 'P': { mode = 1; break; } + case 256: { help(); return 0; } + case 257: { version(); return 0; } + default: { return 1; } + } + +pwd:; + char *cwd = mode ? getcwd(NULL, 0) : getenv("PWD"); + if (!cwd && !mode) { mode = 1; goto pwd; } + else if (!cwd) { error(1, "%s: %s", argv[0], serrno); } + + fputs(cwd, stdout); fputc('\n', stdout); + + if (mode) { free(cwd); } return 0; +} + +static void help(void) { + puts("pwd - print working directory name\n"); + puts("usage: pwd [-L|-P]\n"); + puts("options:"); + puts(" -L Display the logical current working directory"); + puts(" -P Display the physical current working directory"); + puts(" --help Display help information"); + puts(" --version Display version information"); + return; +} + +static void version(void) { + puts("OMKOV coreutils pwd, version 1.0.0"); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}