// pwd.c, version 1.0.1 // OMKOV coreutils implementation of POSIX pwd // Copyright (C) 2020, Jakob Wakeling // MIT Licence #include "util/error.h" #include "util/optget.h" #include #include #include #define VERSION "1.0.1" 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); int main(int ac, char *av[]) { (void)ac; 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) { error(1, "%s", av[0]); } fputs(cwd, stdout); fputc('\n', stdout); 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"); } static void ver(void) { puts("OMKOV coreutils pwd, version " VERSION); puts("Copyright (C) 2020, Jakob Wakeling"); puts("MIT Licence (https://opensource.org/licenses/MIT)"); }