logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: 03bf16a04f428b6a93403c1b91b6a960419abac5
parent ab0fb955dafeca34d6c7e7f9eced128ee08dd5c5
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 27 Sep 2024 16:16:13 +0200

cmd/realpath: add support for -s option

Diffstat:

ALICENSES/MIT.txt9+++++++++
MMakefile4++--
Mcmd/realpath.16+++++-
Mcmd/realpath.c13+++++++++++--
Mlib/fs.h3+++
Alib/offline_realpath.c162+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest-cmd/realpath.sh13++++++++++++-
7 files changed, 204 insertions(+), 6 deletions(-)

diff --git a/LICENSES/MIT.txt b/LICENSES/MIT.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) <year> <copyright holders> + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in 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: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +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 AUTHORS OR COPYRIGHT HOLDERS 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 IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -170,9 +170,9 @@ cmd/chown: cmd/chown.c lib/fs.c lib/fs.h lib/user_group_parse.c lib/user_group_p $(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) -o $@ cmd/chown.c lib/fs.c lib/user_group_parse.c $(LDFLAGS) $(LDSTATIC) -cmd/realpath: cmd/realpath.c lib/fs.c lib/fs.h Makefile +cmd/realpath: cmd/realpath.c lib/fs.c lib/offline_realpath.c lib/fs.h Makefile $(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} - $(CC) -std=c99 $(CFLAGS) -o $@ cmd/realpath.c lib/fs.c $(LDFLAGS) $(LDSTATIC) + $(CC) -std=c99 $(CFLAGS) -o $@ cmd/realpath.c lib/fs.c lib/offline_realpath.c $(LDFLAGS) $(LDSTATIC) cmd/expr.tab.c: cmd/expr.y Makefile $(YACC) -b cmd/expr cmd/expr.y diff --git a/cmd/realpath.1 b/cmd/realpath.1 @@ -11,6 +11,7 @@ .Nm .Op Fl E Ns | Ns Fl e .Op Fl n Ns | Ns Fl z +.Op Fl s .Ar path... .Sh DESCRIPTION The @@ -34,6 +35,8 @@ Fail when does not exists. .It Fl n Do not print a trailing separator. +.It Fl s +Do not resolve symlinks. .It Fl z Use NULL byte as separator instead of newlines. .El @@ -48,7 +51,8 @@ IEEE Std 1003.1-2024 (“POSIX.1”) specification. .Pp The options -.Fl n +.Fl n , +.Fl s and .Fl z and support for multiple diff --git a/cmd/realpath.c b/cmd/realpath.c @@ -16,6 +16,7 @@ #include <unistd.h> // getopt static bool must_exists = false; +static bool offline = false; static char *argv0 = NULL; static char sep = '\n'; @@ -23,7 +24,12 @@ static char sep = '\n'; static int print_realpath(char *path) { - char *file = realpath(path, NULL); + char *file = NULL; + if(offline) + file = offline_realpath(path, NULL); + else + file = realpath(path, NULL); + if(file) { if(printf("%s", file) < 0) @@ -92,7 +98,7 @@ main_realpath(int argc, char *argv[]) int offset_sep = 0; int c = -1; - while((c = getopt(argc, argv, ":Eemnz")) != -1) + while((c = getopt(argc, argv, ":Eemnsz")) != -1) { switch(c) { @@ -105,6 +111,9 @@ main_realpath(int argc, char *argv[]) case 'n': offset_sep = 1; break; + case 's': + offline = true; + break; case 'z': sep = '\0'; break; diff --git a/lib/fs.h b/lib/fs.h @@ -28,3 +28,6 @@ ssize_t auto_file_copy(int fd_in, int fd_out, off_t len, int flags); #else ssize_t auto_fd_copy(int fd_in, int fd_out, size_t len); #endif + +// lib/offline_realpath.c +char *offline_realpath(const char *restrict filename, char *restrict resolved); diff --git a/lib/offline_realpath.c b/lib/offline_realpath.c @@ -0,0 +1,162 @@ +// Based on the realpath(3) function from musl +// Copyright © 2005-2020 Rich Felker, et al. +// SPDX-License-Identifier: MIT + +#define _POSIX_C_SOURCE 200809L +#include <errno.h> +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static size_t +slash_len(const char *s) +{ + const char *s0 = s; + while(*s == '/') + s++; + return s - s0; +} + +static char * +__strchrnul(const char *s, int c) +{ + c = (unsigned char)c; + if(!c) return (char *)s + strlen(s); + + for(; *s && *(unsigned char *)s != c; s++) + ; + return (char *)s; +} + +// realpath(3) but without checking for symlinks +char * +offline_realpath(const char *restrict filename, char *restrict resolved) +{ + char stack[PATH_MAX + 1]; + char output[PATH_MAX]; + size_t p, q, l, l0, cnt = 0, nup = 0; + int check_dir = 0; + + if(!filename) + { + errno = EINVAL; + return 0; + } + l = strnlen(filename, sizeof stack); + if(!l) + { + errno = ENOENT; + return 0; + } + if(l >= PATH_MAX) goto toolong; + p = sizeof stack - l - 1; + q = 0; + memcpy(stack + p, filename, l + 1); + + /* Main loop. Each iteration pops the next part from stack of + * remaining path components and consumes any slashes that follow. + * If not a link, it's moved to output; if a link, contents are + * pushed to the stack. */ +restart: + for(;; p += slash_len(stack + p)) + { + /* If stack starts with /, the whole component is / or // + * and the output state must be reset. */ + if(stack[p] == '/') + { + check_dir = 0; + nup = 0; + q = 0; + output[q++] = '/'; + p++; + /* Initial // is special. */ + if(stack[p] == '/' && stack[p + 1] != '/') output[q++] = '/'; + continue; + } + + char *z = __strchrnul(stack + p, '/'); + l0 = l = z - (stack + p); + + if(!l && !check_dir) break; + + /* Skip any . component but preserve check_dir status. */ + if(l == 1 && stack[p] == '.') + { + p += l; + continue; + } + + /* Copy next component onto output at least temporarily, to + * call readlink, but wait to advance output position until + * determining it's not a link. */ + if(q && output[q - 1] != '/') + { + if(!p) goto toolong; + stack[--p] = '/'; + l++; + } + if(q + l >= PATH_MAX) goto toolong; + memcpy(output + q, stack + p, l); + output[q + l] = 0; + p += l; + + int up = 0; + if(l0 == 2 && stack[p - 2] == '.' && stack[p - 1] == '.') + { + up = 1; + /* Any non-.. path components we could cancel start + * after nup repetitions of the 3-byte string "../"; + * if there are none, accumulate .. components to + * later apply to cwd, if needed. */ + if(q <= 3 * nup) + { + nup++; + q += l; + continue; + } + } + check_dir = 0; + if(up) + { + while(q && output[q - 1] != '/') + q--; + if(q > 1 && (q > 2 || output[0] != '/')) q--; + continue; + } + if(l0) q += l; + check_dir = stack[p]; + } + + output[q] = 0; + + if(output[0] != '/') + { + if(!getcwd(stack, sizeof stack)) return 0; + l = strlen(stack); + /* Cancel any initial .. components. */ + p = 0; + while(nup--) + { + while(l > 1 && stack[l - 1] != '/') + l--; + if(l > 1) l--; + p += 2; + if(p < q) p++; + } + if(q - p && stack[l - 1] != '/') stack[l++] = '/'; + if(l + (q - p) + 1 >= PATH_MAX) goto toolong; + memmove(output + l, output + p, q - p + 1); + memcpy(output, stack, l); + q = l + q - p; + } + + if(resolved) + return memcpy(resolved, output, q + 1); + else + return strdup(output); + +toolong: + errno = ENAMETOOLONG; + return 0; +} diff --git a/test-cmd/realpath.sh b/test-cmd/realpath.sh @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MPL-2.0 target="$(dirname "$0")/../cmd/realpath" -plans=26 +plans=31 . "$(dirname "$0")/tap.sh" t . '.' "${PWD} @@ -73,3 +73,14 @@ t n:/dev/null_/var/empty '-n /dev/null /var/empty' '/dev/null # As required by POSIX.1-2024 -E and -e aren't errorneous t Ee:/dev/null '-E -e /dev/null' '/dev/null ' + +t s:foo '-s foo' "$PWD/foo +" +t s:foo/bar '-s foo/bar' "$PWD/foo/bar +" +t s:./foo/bar '-s ./foo/bar' "$PWD/foo/bar +" +t s:/foo/bar '-s /foo/bar' "/foo/bar +" +t s:/foo/del/../bar '-s /foo/del/../bar' "/foo/bar +"