logo

utils-std

Collection of commonly available Unix tools
commit: 48f6992741d06ac8701d39ccbf44cd0553271041
parent 5ebd5d747ab67436708a92e87cbe1b2b42411c6f
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 12 Mar 2024 13:01:41 +0100

lib/symbolize_mode.c: new

Diffstat:

MMakefile5++++-
Mlib/mode.h4++++
Alib/symbolize_mode.c64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest-lib/Kyuafile1+
Atest-lib/symbolize_mode.c37+++++++++++++++++++++++++++++++++++++
5 files changed, 110 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -13,7 +13,7 @@ all: $(EXE) $(MAN1SO) rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) -c -o $@ $< -TEST_LIBS = test-lib/mode test-lib/strtodur +TEST_LIBS = test-lib/mode test-lib/strtodur test-lib/symbolize_mode .PHONY: check check: all $(TEST_LIBS) MALLOC_CHECK_=3 POSIX_ME_HARDER=1 POSIXLY_CORRECT=1 LDSTATIC=$(LDSTATIC) kyua test || (kyua report --verbose --results-filter=broken,failed; false) @@ -73,6 +73,9 @@ test-lib/mode: test-lib/mode.c lib/mode.c Makefile test-lib/strtodur: test-lib/strtodur.c lib/strtodur.c Makefile $(CC) -std=c99 $(CFLAGS) $(ATF_CFLAGS) -o $@ test-lib/strtodur.c lib/strtodur.c $(LDFLAGS) $(ATF_LIBS) +test-lib/symbolize_mode: test-lib/symbolize_mode.c lib/symbolize_mode.c Makefile + $(CC) -std=c99 $(CFLAGS) $(ATF_CFLAGS) -o $@ test-lib/symbolize_mode.c lib/symbolize_mode.c $(LDFLAGS) $(ATF_LIBS) + cmd/df: cmd/df.c lib/humanize.c Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) -o $@ cmd/df.c lib/humanize.c $(LDFLAGS) $(LDSTATIC) diff --git a/lib/mode.h b/lib/mode.h @@ -6,3 +6,7 @@ // Returns 0 on error and sets errstr mode_t new_mode(const char *mode, mode_t old, const char **errstr); + +// Octal file mode in `mode`, symbolic version in `str` +// Example: mode=040755 ; str="drwxr-xr-x" +void symbolize_mode(mode_t mode, char str[11]); diff --git a/lib/symbolize_mode.c b/lib/symbolize_mode.c @@ -0,0 +1,64 @@ +// Collection of Unix tools, comparable to coreutils +// SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: MPL-2.0 + +#define _POSIX_C_SOURCE 200809L +#include "bitmasks.h" +#include "mode.h" + +#include <sys/stat.h> + +void +symbolize_mode(mode_t mode, char str[11]) +{ + switch(mode & S_IFMT) + { + case S_IFDIR: + str[0] = 'd'; + break; + case S_IFCHR: + str[0] = 'c'; + break; + case S_IFBLK: + str[0] = 'b'; + break; + case S_IFREG: + str[0] = '-'; + break; + case S_IFIFO: + str[0] = 'f'; + break; + case S_IFLNK: + str[0] = 'l'; + break; + case S_IFSOCK: + str[0] = 's'; + break; + default: + str[0] = '?'; + break; + } + + str[1] = FIELD_MATCH(mode, S_IRUSR) ? 'r' : '-'; + str[2] = FIELD_MATCH(mode, S_IWUSR) ? 'w' : '-'; + if(FIELD_MATCH(mode, S_IXUSR)) + str[3] = FIELD_MATCH(mode, S_ISUID) ? 's' : 'x'; + else + str[3] = '-'; + + str[4] = FIELD_MATCH(mode, S_IRGRP) ? 'r' : '-'; + str[5] = FIELD_MATCH(mode, S_IWGRP) ? 'w' : '-'; + if(FIELD_MATCH(mode, S_IXGRP)) + str[6] = FIELD_MATCH(mode, S_ISGID) ? 's' : 'x'; + else + str[6] = '-'; + + str[7] = FIELD_MATCH(mode, S_IROTH) ? 'r' : '-'; + str[8] = FIELD_MATCH(mode, S_IWOTH) ? 'w' : '-'; + if(FIELD_MATCH(mode, S_IXOTH)) + str[9] = FIELD_MATCH(mode, S_ISVTX) ? 't' : 'x'; + else + str[9] = '-'; + + str[10] = 0; +} diff --git a/test-lib/Kyuafile b/test-lib/Kyuafile @@ -7,3 +7,4 @@ test_suite("utils-std libs") -- 7,$|LC_ALL=C.UTF-8 sort atf_test_program{name="mode"} atf_test_program{name="strtodur"} +atf_test_program{name="symbolize_mode"} diff --git a/test-lib/symbolize_mode.c b/test-lib/symbolize_mode.c @@ -0,0 +1,37 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: MPL-2.0 + +#define _POSIX_C_SOURCE 200809L +#include <atf-c.h> +#include <assert.h> +#include <signal.h> // SIGABRT +#include <sys/stat.h> // umask + +#include "../lib/mode.h" + +ATF_TC(examples); +ATF_TC_HEAD(examples, tc) +{ + atf_tc_set_md_var(tc, "descr", "some usual examples"); +} +ATF_TC_BODY(examples, tc) +{ + char str[11] = ""; + + symbolize_mode(0040000, str); + ATF_CHECK_STREQ("d---------", str); + + symbolize_mode(0040755, str); + ATF_CHECK_STREQ("drwxr-xr-x", str); + + symbolize_mode(0020644, str); + ATF_CHECK_STREQ("crw-r--r--", str); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, examples); + + return atf_no_error(); +}