Author | Jamozed <[email protected]> |
Date | 2020-06-26 14:13:22 |
Commit | 44d97c8bc4565b9e5dae11c7093801449885214d |
Parent | d798a0963d9f91e165bf1b426cab6d9f3cc1ab9f |
realpath: Add realpath utility
Diffstat
M | CMakeLists.txt | | | 1 | + |
M | README.md | | | 1 | + |
A | man/realpath.1 | | | 35 | +++++++++++++++++++++++++++++++++++ |
A | src/realpath.c | | | 98 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 135 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index aba9e71..ba773b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ ADD_EXECUTABLE(mkdir ${CMAKE_SOURCE_DIR}/src/mkdir.c) ADD_EXECUTABLE(nice ${CMAKE_SOURCE_DIR}/src/nice.c) ADD_EXECUTABLE(pwd ${CMAKE_SOURCE_DIR}/src/pwd.c) ADD_EXECUTABLE(rand ${CMAKE_SOURCE_DIR}/src/rand.c) +ADD_EXECUTABLE(realpath ${CMAKE_SOURCE_DIR}/src/realpath.c) ADD_EXECUTABLE(rmdir ${CMAKE_SOURCE_DIR}/src/rmdir.c) ADD_EXECUTABLE(sleep ${CMAKE_SOURCE_DIR}/src/sleep.c) ADD_EXECUTABLE(sync ${CMAKE_SOURCE_DIR}/src/sync.c) diff --git a/README.md b/README.md index 452027b..36c7d4f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ UNIX-like systems. | nice | Execute with an altered nice value | POSIX | | pwd | Print working directory | POSIX | | rand | Generate random string | | +| realpath | Resolve an absolute pathname | | | rmdir | Remove directories | POSIX | | sleep | Suspend execution for an interval | POSIX | | sync | Synchronise file system caches to disk | | diff --git a/man/realpath.1 b/man/realpath.1 new file mode 100644 index 0000000..c4e2c39 --- /dev/null +++ b/man/realpath.1 @@ -0,0 +1,35 @@ +.TH REALPATH 1 2020-06-27 "OMKOV coreutils" "General Commands Manual" +.SH NAME +realpath \(em resolve an absolute pathname +.SH SYNOPSYS +\fBrealpath\fR [\fIfile\fR...] +.SH DESCRIPTION +Resolve the absolute path for each input \fIfile\fR. If \fIfile\fR is +unspecified, the path of the current working directory will be resolved. +.SH OPTIONS +The following options are supported: +.TP +.B --help +Display help information. +.TP +.B --version +Display version information. +.SH OPERANDS +The following operand is supported: +.TP +.I file +A pathname of a file. +.SH EXIT STATUS +The following exit values will be returned: +.TP +\ 0 +Successful completion. +.TP +>0 +An error occurred. +.SH COPYRIGHT +.nf +Copyright (C) 2020, Jakob Wakeling +All rights reserved. +OMKOV Permissive Licence (https://www.omkov.net/OLPE) +.fi diff --git a/src/realpath.c b/src/realpath.c new file mode 100644 index 0000000..ff40e7c --- /dev/null +++ b/src/realpath.c @@ -0,0 +1,98 @@ +// realpath.c, version 1.0.0 +// OMKOV coreutils realpath +// 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 <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static inline int rpath(const char *path); + +static void help(void); +static void version(void); + +int main(int argc, char *argv[]) { + lop_t lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } + }; + + opt_t opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; int o; + while((o = optget(&opt, argv, 1)) != -1) switch (o) { + case 256: { help(); return 0; } + case 257: { version(); return 0; } + default: { return 1; } + } + + bool warned = false; + + if (opt.ind == argc) { rpath("."); } + else for (char **p = &argv[opt.ind]; *p; ++p) if (rpath(*p)) { + warn("%s: %s: %s\n", argv[0], *p, serrno); 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 help(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"); + return; +} + +static void version() { + puts("OMKOV coreutils realpath, version 1.0.0"); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}