diff options
author | Leah Rowe <leah@libreboot.org> | 2025-10-04 09:14:33 +0100 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2025-10-04 09:20:12 +0100 |
commit | e9a910b33c7837b4b868e3abda18eb4810df7f02 (patch) | |
tree | 749e1830cb0607952df1a1afc0ae09ec1db54140 /util/sbase/pathchk.c | |
parent | 2cfaba181b3c68761871fa47b32725c934423c14 (diff) |
config/git: import suckless sbase
i currently use the output of sha512sum in several
places of xbmk, which is a bit unreliable in case
output changes.
other cases where i use util outputs in variables
are probably reliable, because i'm using mostly
posix utilities in those.
to mitigate this, i now import suckless sbase, which
has a reasonable sha512sum implementation.
*every* binary it builds is being placed in build.list,
because i'll probably start using more of them.
for example, i may start modifying the "date"
implementation, adding the GNU-specific options that
i need as mentioned on init.sh
i'm importing it in util/ because the sha512sum
util is needed for verifying project sources, so
if sbase itself is a "project source", that means
we can into a chicken and egg bootstrapping problem.
this is sbase at revision:
055cc1ae1b3a13c3d8f25af0a4a3316590efcd48
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/sbase/pathchk.c')
-rw-r--r-- | util/sbase/pathchk.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/util/sbase/pathchk.c b/util/sbase/pathchk.c new file mode 100644 index 00000000..b6b0c91c --- /dev/null +++ b/util/sbase/pathchk.c @@ -0,0 +1,104 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/stat.h> + +#include <errno.h> +#include <limits.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +#define PORTABLE_CHARACTER_SET "0123456789._-qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" +/* If your system supports more other characters, but not all non-NUL characters, define SYSTEM_CHARACTER_SET. */ + +static int most = 0; +static int extra = 0; + +static int +pathchk(char *filename) +{ + char *invalid, *invalid_end, *p, *q; + const char *character_set; + size_t len, maxlen; + struct stat st; + + /* Empty? */ + if (extra && !*filename) + eprintf("empty filename\n"); + + /* Leading hyphen? */ + if (extra && ((*filename == '-') || strstr(filename, "/-"))) + eprintf("%s: leading '-' in component of filename\n", filename); + + /* Nonportable character? */ +#ifdef SYSTEM_CHARACTER_SET + character_set = "/"SYSTEM_CHARACTER_SET; +#else + character_set = 0; +#endif + if (most) + character_set = "/"PORTABLE_CHARACTER_SET; + if (character_set && *(invalid = filename + strspn(filename, character_set))) { + for (invalid_end = invalid + 1; *invalid_end & 0x80; invalid_end++); + p = estrdup(filename); + *invalid_end = 0; + eprintf("%s: nonportable character '%s'\n", p, invalid); + } + + /* Symlink error? Non-searchable directory? */ + if (lstat(filename, &st) && errno != ENOENT) { + /* lstat rather than stat, so that if filename is a bad symlink, but + * all parents are OK, no error will be detected. */ + eprintf("%s:", filename); + } + + /* Too long pathname? */ + maxlen = most ? _POSIX_PATH_MAX : PATH_MAX; + if (strlen(filename) >= maxlen) + eprintf("%s: is longer than %zu bytes\n", filename, maxlen); + + /* Too long component? */ + maxlen = most ? _POSIX_NAME_MAX : NAME_MAX; + for (p = filename; p; p = q) { + q = strchr(p, '/'); + len = q ? (size_t)(q++ - p) : strlen(p); + if (len > maxlen) + eprintf("%s: includes component longer than %zu bytes\n", + filename, maxlen); + } + + return 0; +} + +static void +usage(void) +{ + eprintf("usage: %s [-pP] filename...\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + int ret = 0; + + ARGBEGIN { + case 'p': + most = 1; + break; + case 'P': + extra = 1; + break; + default: + usage(); + } ARGEND + + if (!argc) + usage(); + + for (; argc--; argv++) + ret |= pathchk(*argv); + + return ret; +} |