aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit e57de22d9c3d20df7061a041e7770e1d380c76cf
parent 008f34d708d4a63ff6bb13f8c314c15aea9e7112
Author: xplshn <anto@xplshn.com.ar>
Date:   Tue, 24 Nov 2026 14:37:27 -0300

filesystem: major and minor behind the backend

Diffstat:
Mcmd/extra/file.c | 13+++++--------
Mcmd/posix/ls.c | 7++-----
Mcmd/posix/pax.c | 18+++++++++---------
Mcmd/pseudo/stat.c | 12++++++------
Mcmd/pseudo/tar.c | 8++++----
Mcmd/xsi/mknod.c | 7++-----
Mshared/filesystem.h | 6++++++
Mshared/libutil/filesystem_linux.c | 19+++++++++++++++++++
Mshared/libutil/filesystem_other.c | 33+++++++++++++++++++++++++++++++++
Rshared/libutil/tty.c -> shared/libutil/tty_linux.c | 0
10 files changed, 86 insertions(+), 37 deletions(-)

diff --git a/cmd/extra/file.c b/cmd/extra/file.c @@ -1,14 +1,11 @@ /* see LICENSE file for copyright and license details */ #include "../../shared/libmagic/magic.h" #include "arg.h" +#include "filesystem.h" #include "util.h" #include <sys/stat.h> #include <sys/types.h> -#ifndef major -#include <sys/sysmacros.h> -#endif - #include <errno.h> #include <fcntl.h> #include <limits.h> @@ -216,8 +213,8 @@ file(const char *name) buf, sizeof(buf), "character special (%ld/%ld)", - (long)major(st.st_rdev), - (long)minor(st.st_rdev) + (long)fs_devmajor(st.st_rdev), + (long)fs_devminor(st.st_rdev) ); put_fs(name, prefix, buf, "inode/chardevice", 1); return 0; @@ -226,8 +223,8 @@ file(const char *name) buf, sizeof(buf), "block special (%ld/%ld)", - (long)major(st.st_rdev), - (long)minor(st.st_rdev) + (long)fs_devmajor(st.st_rdev), + (long)fs_devminor(st.st_rdev) ); put_fs(name, prefix, buf, "inode/blockdevice", 1); return 0; diff --git a/cmd/posix/ls.c b/cmd/posix/ls.c @@ -7,10 +7,6 @@ #endif #include <sys/stat.h> #include <sys/types.h> -#ifndef major -#include <sys/sysmacros.h> -#endif - #include <dirent.h> #include <grp.h> #include <pwd.h> @@ -20,6 +16,7 @@ #include <time.h> #include <unistd.h> +#include "filesystem.h" #include "utf.h" #include "util.h" @@ -396,7 +393,7 @@ output(const struct entry *ent) printf("%-8.8s ", grname); if (S_ISBLK(ent->mode) || S_ISCHR(ent->mode)) - printf("%4u, %4u ", major(ent->rdev), minor(ent->rdev)); + printf("%4u, %4u ", fs_devmajor(ent->rdev), fs_devminor(ent->rdev)); else if (hflag) printf("%10s ", humansize(ent->size)); else diff --git a/cmd/posix/pax.c b/cmd/posix/pax.c @@ -6,6 +6,7 @@ #endif #include "arg.h" +#include "filesystem.h" #include <assert.h> #include <cpio.h> @@ -25,7 +26,6 @@ #include <stdlib.h> #include <string.h> #include <sys/stat.h> -#include <sys/sysmacros.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/wait.h> @@ -677,7 +677,7 @@ readustar(struct bufio *f, struct header *h) major = octnum(buf + 329, 8); minor = octnum(buf + 337, 8); - h->rdev = makedev(major, minor); + h->rdev = fs_makedev(major, minor); } } return 1; @@ -1225,13 +1225,13 @@ writeustar(FILE *f, struct header *h) if (strlen(h->gname) > 31) fatal("group name is too long: %s\n", h->gname); strncpy(buf + 297, h->gname, 32); - if (major(h->rdev) > 07777777) - fatal("device major is too large: %ju\n", (uintmax_t)major(h->rdev)); - snprintf(tmp, sizeof(tmp), "%.7lo", (unsigned long)major(h->rdev) & 07777777); + if (fs_devmajor(h->rdev) > 07777777) + fatal("device major is too large: %ju\n", (uintmax_t)fs_devmajor(h->rdev)); + snprintf(tmp, sizeof(tmp), "%.7lo", (unsigned long)fs_devmajor(h->rdev) & 07777777); memcpy(buf + 329, tmp, 8); - if (minor(h->rdev) > 07777777) - fatal("device minor is too large: %ju\n", (uintmax_t)minor(h->rdev)); - snprintf(tmp, sizeof(tmp), "%.7lo", (unsigned long)minor(h->rdev) & 07777777); + if (fs_devminor(h->rdev) > 07777777) + fatal("device minor is too large: %ju\n", (uintmax_t)fs_devminor(h->rdev)); + snprintf(tmp, sizeof(tmp), "%.7lo", (unsigned long)fs_devminor(h->rdev) & 07777777); memcpy(buf + 337, tmp, 8); memset(buf + 500, 0, 12); sum = 0; @@ -1920,7 +1920,7 @@ listhdr(FILE *f, struct header *h) fatal("localtime:"); strftime(time, sizeof time, timefmt, tm); if (h->type == CHRTYPE || h->type == BLKTYPE) - snprintf(info, sizeof info, "%u, %u", major(h->rdev), minor(h->rdev)); + snprintf(info, sizeof info, "%u, %u", fs_devmajor(h->rdev), fs_devminor(h->rdev)); else snprintf(info, sizeof info, "%ju", (uintmax_t)h->size); printf( diff --git a/cmd/pseudo/stat.c b/cmd/pseudo/stat.c @@ -5,6 +5,7 @@ #endif #include "config.h" +#include "filesystem.h" #include "util.h" #include <grp.h> @@ -13,7 +14,6 @@ #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> -#include <sys/sysmacros.h> #include <sys/types.h> #include <sys/vfs.h> #include <time.h> @@ -188,12 +188,12 @@ print_custom(const char *file, struct stat *st, const char *fmt) case 't': fmt_spec[fmt_len++] = 'x'; fmt_spec[fmt_len] = '\0'; - printf(fmt_spec, major(st->st_rdev)); + printf(fmt_spec, fs_devmajor(st->st_rdev)); break; case 'T': fmt_spec[fmt_len++] = 'x'; fmt_spec[fmt_len] = '\0'; - printf(fmt_spec, minor(st->st_rdev)); + printf(fmt_spec, fs_devminor(st->st_rdev)); break; case 'u': fmt_spec[fmt_len++] = 'u'; @@ -466,7 +466,7 @@ show_stat_terse(const char *file, struct stat *st) printf("%04o %u %u ", st->st_mode & 0777, st->st_uid, st->st_gid); printf("%llx ", (unsigned long long)st->st_dev); printf("%lu %lu ", (unsigned long)st->st_ino, (unsigned long)st->st_nlink); - printf("%d %d ", major(st->st_rdev), minor(st->st_rdev)); + printf("%d %d ", fs_devmajor(st->st_rdev), fs_devminor(st->st_rdev)); printf("%ld %ld %ld ", st->st_atime, st->st_mtime, st->st_ctime); printf("%lu\n", (unsigned long)st->st_blksize); } @@ -485,8 +485,8 @@ show_stat(const char *file, struct stat *st) ); printf( "Device: %xh/%ud\tInode: %lu\tLinks %lu\n", - major(st->st_dev), - minor(st->st_dev), + fs_devmajor(st->st_dev), + fs_devminor(st->st_dev), (unsigned long)st->st_ino, (unsigned long)st->st_nlink ); diff --git a/cmd/pseudo/tar.c b/cmd/pseudo/tar.c @@ -7,6 +7,7 @@ #endif #include "config.h" +#include "filesystem.h" #include "fs.h" #include "utf.h" #include "util.h" @@ -23,7 +24,6 @@ #include <stdlib.h> #include <string.h> #include <sys/stat.h> -#include <sys/sysmacros.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> @@ -443,8 +443,8 @@ archive(const char *path) h->linkname[r] = '\0'; } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { h->type = S_ISCHR(st.st_mode) ? CHARDEV : BLOCKDEV; - putoctal(h->major, (unsigned)major(st.st_dev), sizeof(h->major)); - putoctal(h->minor, (unsigned)minor(st.st_dev), sizeof(h->minor)); + putoctal(h->major, (unsigned)fs_devmajor(st.st_dev), sizeof(h->major)); + putoctal(h->minor, (unsigned)fs_devminor(st.st_dev), sizeof(h->minor)); } else if (S_ISFIFO(st.st_mode)) { h->type = FIFO; } @@ -548,7 +548,7 @@ unarchive(char *fname, ssize_t l, char b[BLKSIZ]) if ((minor = strtol(h->minor, &p, 8)) < 0 || *p != '\0') eprintf("strtol %s: invalid minor device\n", h->minor); type = (h->type == CHARDEV) ? S_IFCHR : S_IFBLK; - if (mknod(fname, type | mode, makedev(major, minor)) < 0) + if (mknod(fname, type | mode, fs_makedev(major, minor)) < 0) eprintf("mknod %s:", fname); #else eprintf("tar: device extraction not supported\n"); diff --git a/cmd/xsi/mknod.c b/cmd/xsi/mknod.c @@ -13,12 +13,9 @@ #include <string.h> #include <unistd.h> +#include "filesystem.h" #include "util.h" -#ifndef makedev -#include <sys/sysmacros.h> -#endif - static void usage(void) { @@ -78,7 +75,7 @@ main(int argc, char *argv[]) } else { if (argc != 4) usage(); - dev = makedev(estrtonum(argv[2], 0, LLONG_MAX), estrtonum(argv[3], 0, LLONG_MAX)); + dev = fs_makedev(estrtonum(argv[2], 0, LLONG_MAX), estrtonum(argv[3], 0, LLONG_MAX)); } if (mknod(argv[0], mode, dev) == -1) diff --git a/shared/filesystem.h b/shared/filesystem.h @@ -2,6 +2,7 @@ #define FILESYSTEM_H #include <stddef.h> +#include <sys/types.h> struct FilesystemStatus { unsigned long long block_size; @@ -19,4 +20,9 @@ int fs_get_status(const char *, struct FilesystemStatus *); int fs_get_mounts(struct FilesystemMount **, size_t *); void fs_free_mounts(struct FilesystemMount *, size_t); +/* each kernel packs the two numbers of a device its own way */ +unsigned int fs_devmajor(dev_t); +unsigned int fs_devminor(dev_t); +dev_t fs_makedev(unsigned int, unsigned int); + #endif diff --git a/shared/libutil/filesystem_linux.c b/shared/libutil/filesystem_linux.c @@ -11,6 +11,7 @@ #include <stdlib.h> #include <string.h> #include <sys/statvfs.h> +#include <sys/sysmacros.h> int fs_get_status(const char *path, struct FilesystemStatus *status) @@ -66,4 +67,22 @@ fs_free_mounts(struct FilesystemMount *mounts, size_t count) } free(mounts); } + +unsigned int +fs_devmajor(dev_t dev) +{ + return major(dev); +} + +unsigned int +fs_devminor(dev_t dev) +{ + return minor(dev); +} + +dev_t +fs_makedev(unsigned int ma, unsigned int mi) +{ + return makedev(ma, mi); +} #endif diff --git a/shared/libutil/filesystem_other.c b/shared/libutil/filesystem_other.c @@ -2,6 +2,7 @@ #include <errno.h> #include <string.h> +#include <sys/types.h> #if !defined(__linux__) || defined(ARUU_FORCE_OTHER) int @@ -28,4 +29,36 @@ fs_free_mounts(struct FilesystemMount *mounts, size_t count) (void)mounts; (void)count; } + +/* sys/types.h has them on most systems. without them, the split of + * the first unixes: 8 bits each */ +unsigned int +fs_devmajor(dev_t dev) +{ +#if defined(major) + return major(dev); +#else + return ((unsigned int)dev >> 8) & 0xff; +#endif +} + +unsigned int +fs_devminor(dev_t dev) +{ +#if defined(minor) + return minor(dev); +#else + return (unsigned int)dev & 0xff; +#endif +} + +dev_t +fs_makedev(unsigned int ma, unsigned int mi) +{ +#if defined(makedev) + return makedev(ma, mi); +#else + return (dev_t)((ma & 0xff) << 8 | (mi & 0xff)); +#endif +} #endif diff --git a/shared/libutil/tty.c b/shared/libutil/tty_linux.c