commit: 3bf1114d04bf5f715b1d3ca44fdddca13afea62f
parent bcfb37963fbf1abe03220e8a32bcd5fbb27e06ab
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 4 Jan 2026 12:07:52 +0100
libutils/filetype: new
Diffstat:
2 files changed, 49 insertions(+), 0 deletions(-)
diff --git a/libutils/filetype.c b/libutils/filetype.c
@@ -0,0 +1,46 @@
+// Collection of Unix tools, comparable to coreutils
+// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+
+#define _POSIX_C_SOURCE 200809L
+#define _XOPEN_SOURCE 700 // S_ISVTX, S_IFMT, S_IFDIR, …
+#include "./mode.h"
+
+#include <sys/stat.h>
+
+const char *
+filetype(struct stat *st)
+{
+#ifdef S_TYPEISMQ
+ if(S_TYPEISMQ(st)) return "message queue";
+#endif
+#ifdef S_TYPEISSEM
+ if(S_TYPEISSEM(st)) return "semaphore";
+#endif
+#ifdef S_TYPEISSHM
+ if(S_TYPEISSHM(st)) return "shared memory object";
+#endif
+#ifdef S_TYPEISTMO
+ if(S_TYPEISTMO(st)) return "typed memory object";
+#endif
+
+ switch(st->st_mode & S_IFMT)
+ {
+ case S_IFBLK:
+ return "block special";
+ case S_IFCHR:
+ return "character special";
+ case S_IFIFO:
+ return "FIFO special";
+ case S_IFREG:
+ return "regular file";
+ case S_IFLNK:
+ return "symbolic link";
+ case S_IFDIR:
+ return "directory";
+ case S_IFSOCK:
+ return "socket";
+ }
+
+ return "unknown filetype";
+}
diff --git a/libutils/mode.h b/libutils/mode.h
@@ -10,3 +10,6 @@ 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]);
+
+// Return full type name based on the mode
+const char *filetype(struct stat *st);