Author | Jamozed <[email protected]> |
Date | 2021-01-15 01:15:40 |
Commit | d9b7bcc957150589fd03a569bb6886da263531b3 |
Parent | 8e5fd79617a63e4877d24408528bc36594e2f6c9 |
Align pwd builtin with OMKOV coreutils version
Diffstat
M | src/bltns/pwd.c | | | 49 | +++++++++++++++++++++++++++++++++++++++++-------- |
1 files changed, 41 insertions, 8 deletions
diff --git a/src/bltns/pwd.c b/src/bltns/pwd.c index a21e380..02c0a03 100644 --- a/src/bltns/pwd.c +++ b/src/bltns/pwd.c @@ -1,5 +1,5 @@ -// pwd.c, version 1.0.2b -// OMKOV coreutils pwd adapted as a OSH builtin +// pwd.c, version 1.0.1b +// OMKOV coreutils pwd adapted as an OSH builtin // Copyright (C) 2020, Jakob Wakeling // All rights reserved. @@ -30,29 +30,62 @@ 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 "../lib/error.h" #include "../lib/optget.h" #include <unistd.h> -#include <errno.h> #include <stdio.h> #include <stdlib.h> -static int bltn_pwd(char *av[]) { int mode = 0; - struct opt opt = OPTGET_INIT; opt.str = "LP"; opt.lops = NULL; +#define VERSION "1.0.1b" + +static struct lop lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } +}; + +static int mode = 0; + +static void hlp(void); +static void ver(void); + +static int bltn_pwd(char *av[]) { A0 = av[0]; + struct opt opt = OPTGET_INIT; opt.str = "LP"; opt.lops = lops; for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { case 'L': { mode = 0; break; } case 'P': { mode = 1; break; } + case 256: { hlp(); return 0; } + case 257: { ver(); return 0; } default: { return 1; } } pwd:; char *cwd = mode ? getcwd(NULL, 0) : getenv("PWD"); if (!cwd && !mode) { mode = 1; goto pwd; } - else if (!cwd) { perror(av[0]); return 1; } + else if (!cwd) { error(1, "%s", av[0]); } fputs(cwd, stdout); fputc('\n', stdout); - if (mode) { free(cwd); } - return 0; + if (mode) { free(cwd); } return 0; +} + +static void hlp(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 ver(void) { + puts("OMKOV coreutils pwd, version " VERSION); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; }