// realpath.c, version 1.0.1 // OMKOV coreutils realpath // 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 inline int rpath(const char *path); static void hlp(void); static void ver(void); int main(int ac, char *av[]) { A0 = av[0]; struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { case 256: { hlp(); return 0; } case 257: { ver(); return 0; } default: { return 1; } } bool warned = false; if (opt.ind == ac) { rpath("."); } else for (char **p = &av[opt.ind]; *p; ++p) if (rpath(*p)) { warn("%s: %s\n", *p, serr()); warned = true; } return warned; } static inline int rpath(const char *file) { char *path; if (!(path = realpath(file, NULL))) { return 1; } fputs(path, stdout); fputc('\n', stdout); free(path); return 0; } static void hlp(void) { puts("realpath - resolve an absolute pathname\n"); puts("usage: realpath [file...]\n"); puts("options:"); puts(" --help Display help information"); puts(" --version Display version information"); } static void ver() { puts("OMKOV coreutils realpath, version " VERSION); puts("Copyright (C) 2020, Jakob Wakeling"); puts("MIT Licence (https://opensource.org/licenses/MIT)"); }