ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 97cbad7eefeb1cf2b2b1f52bc42b18ea017c52b9
parent 92c339065773106da4c779818fdb1bf14c827d4b
Author: sin <sin@2f30.org>
Date:   Wed, 14 Aug 2013 14:19:05 +0100

Remove OpenBSD support

This is becoming a nightmare.  Just support Linux.

Diffstat:
MMakefile | 17+++++------------
Mconfig.mk | 4----
Mdf.c | 2+-
Mdmesg.c | 46+++++++++++++++++++++++++++++++++++++++++-----
Agrabmntinfo.h | 8++++++++
Dlinux/dmesg.c | 49-------------------------------------------------
Dlinux/grabmntinfo.c | 31-------------------------------
Dlinux/umount.c | 17-----------------
Mmount.c | 2+-
Dopenbsd/dmesg.c | 47-----------------------------------------------
Dopenbsd/grabmntinfo.c | 29-----------------------------
Dopenbsd/umount.c | 21---------------------
Dubase.h | 22----------------------
Mumount.c | 10+++++-----
Autil/grabmntinfo.c | 31+++++++++++++++++++++++++++++++
15 files changed, 92 insertions(+), 244 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,22 +3,15 @@ include config.mk .POSIX: .SUFFIXES: .c .o -HDR = util.h arg.h ubase.h +HDR = arg.h ubase.h util.h LIB = \ - $(OS)/dmesg.o \ - $(OS)/grabmntinfo.o \ - $(OS)/umount.o \ util/eprintf.o \ - util/estrtol.o + util/estrtol.o \ + util/grabmntinfo.o SRC = \ df.c \ dmesg.c \ - umount.c \ - stat.c - -ifeq ($(OS),linux) -SRC += \ halt.c \ insmod.c \ lsmod.c \ @@ -26,10 +19,11 @@ SRC += \ mount.c \ reboot.c \ rmmod.c \ + stat.c \ swapoff.c \ swapon.c \ + umount.c \ unshare.c -endif OBJ = $(SRC:.c=.o) $(LIB) BIN = $(SRC:.c=) @@ -39,7 +33,6 @@ all: options binlib options: @echo ubase build options: - @echo "OS = $(OS)" @echo "CFLAGS = $(CFLAGS)" @echo "LDFLAGS = $(LDFLAGS)" @echo "CC = $(CC)" diff --git a/config.mk b/config.mk @@ -5,10 +5,6 @@ VERSION = 0.0 PREFIX = /usr/local MANPREFIX = $(PREFIX)/share/man -# OS to build against -OS = linux -#OS = openbsd - #CC = gcc #CC = musl-gcc LD = $(CC) diff --git a/df.c b/df.c @@ -2,7 +2,7 @@ #include <sys/statvfs.h> #include <stdio.h> #include <stdlib.h> -#include "ubase.h" +#include "grabmntinfo.h" #include "util.h" static void mnt_show(const char *fsname, const char *dir); diff --git a/dmesg.c b/dmesg.c @@ -1,10 +1,19 @@ /* See LICENSE file for copyright and license details. */ +#include <sys/klog.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> -#include "ubase.h" +#include <string.h> +#include "grabmntinfo.h" #include "util.h" +static int dmesg_show(int fd, const void *buf, size_t n); + +enum { + SYSLOG_ACTION_READ_ALL = 3, + SYSLOG_ACTION_SIZE_BUFFER = 10 +}; + static void usage(void) { @@ -22,17 +31,17 @@ main(int argc, char *argv[]) usage(); } ARGEND; - n = dmesg_size(); + n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0); if (n < 0) - eprintf("dmesg_size:"); + eprintf("klogctl:"); buf = malloc(n); if (!buf) eprintf("malloc:"); - n = dmesg_read(buf, n); + n = klogctl(SYSLOG_ACTION_READ_ALL, buf, n); if (n < 0) - eprintf("dmesg_read:"); + eprintf("klogctl:"); n = dmesg_show(STDOUT_FILENO, buf, n); if (n < 0) @@ -41,3 +50,30 @@ main(int argc, char *argv[]) free(buf); return 0; } + +int +dmesg_show(int fd, const void *buf, size_t n) +{ + int last = '\n'; + char newbuf[n], *q = newbuf; + const char *p = buf; + size_t i; + + memset(newbuf, 0, n); + for (i = 0; i < n; ) { + if (last == '\n' && p[i] == '<') { + i += 2; + if (i + 1 < n && p[i + 1] == '>') + i++; + } else { + *q++ = p[i]; + } + last = p[i++]; + } + if (write(fd, newbuf, n) != n) + return -1; + if (last != '\n') + if (write(fd, "\n", 1) != 1) + return -1; + return 0; +} diff --git a/grabmntinfo.h b/grabmntinfo.h @@ -0,0 +1,8 @@ +/* See LICENSE file for copyright and license details. */ + +struct mntinfo { + const char *fsname; + const char *mntdir; +}; + +int grabmntinfo(struct mntinfo **minfo); diff --git a/linux/dmesg.c b/linux/dmesg.c @@ -1,49 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <sys/klog.h> -#include <unistd.h> -#include <stdio.h> -#include <string.h> - -enum { - SYSLOG_ACTION_READ_ALL = 3, - SYSLOG_ACTION_SIZE_BUFFER = 10 -}; - -int -dmesg_size(void) -{ - return klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0); -} - -int -dmesg_read(void *buf, size_t n) -{ - return klogctl(SYSLOG_ACTION_READ_ALL, buf, n); -} - -int -dmesg_show(int fd, const void *buf, size_t n) -{ - int last = '\n'; - char newbuf[n], *q = newbuf; - const char *p = buf; - size_t i; - - memset(newbuf, 0, n); - for (i = 0; i < n; ) { - if (last == '\n' && p[i] == '<') { - i += 2; - if (i + 1 < n && p[i + 1] == '>') - i++; - } else { - *q++ = p[i]; - } - last = p[i++]; - } - if (write(fd, newbuf, n) != n) - return -1; - if (last != '\n') - if (write(fd, "\n", 1) != 1) - return -1; - return 0; -} diff --git a/linux/grabmntinfo.c b/linux/grabmntinfo.c @@ -1,31 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <mntent.h> -#include "../ubase.h" -#include "../util.h" - -int -grabmntinfo(struct mntinfo **minfo) -{ - struct mntent *me; - struct mntinfo *mi = NULL; - int siz = 0; - FILE *fp; - - fp = setmntent("/proc/mounts", "r"); - if (!fp) - eprintf("setmntent:"); - while ((me = getmntent(fp))) { - mi = realloc(mi, (siz + 1) * sizeof(*mi)); - if (!mi) - eprintf("realloc:"); - mi[siz].fsname = strdup(me->mnt_fsname); - mi[siz].mntdir = strdup(me->mnt_dir); - siz++; - } - endmntent(fp); - *minfo = mi; - return siz; -} diff --git a/linux/umount.c b/linux/umount.c @@ -1,17 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <sys/mount.h> -#include <stdio.h> -#include "../ubase.h" -#include "../util.h" - -int -do_umount(const char *target, int opts) -{ - int flags = 0; - - if (opts & UBASE_MNT_FORCE) - flags |= MNT_FORCE; - if (opts & UBASE_MNT_DETACH) - flags |= MNT_DETACH; - return umount2(target, flags); -} diff --git a/mount.c b/mount.c @@ -4,7 +4,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "ubase.h" +#include "grabmntinfo.h" #include "util.h" struct { diff --git a/openbsd/dmesg.c b/openbsd/dmesg.c @@ -1,47 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <sys/param.h> -#include <sys/sysctl.h> -#include <sys/msgbuf.h> -#include <unistd.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -int -dmesg_size(void) -{ - int mib[2], msgbufsize; - size_t len; - int ret; - - mib[0] = CTL_KERN; - mib[1] = KERN_MSGBUFSIZE; - len = sizeof(msgbufsize); - ret = sysctl(mib, 2, &msgbufsize, &len, NULL, 0); - if (ret < 0) - return ret; - msgbufsize += sizeof(struct msgbuf) - 1; - return msgbufsize; -} - -int -dmesg_read(void *buf, size_t n) -{ - int mib[2]; - int ret; - - memset(buf, 0, n); - mib[0] = CTL_KERN; - mib[1] = KERN_MSGBUF; - ret = sysctl(mib, 2, buf, &n, NULL, 0); - if (ret < 0) - return ret; - memmove(buf, ((struct msgbuf *)buf)->msg_bufc, n); - return n; -} - -int -dmesg_show(int fd, const void *buf, size_t n) -{ - return write(fd, buf, n); -} diff --git a/openbsd/grabmntinfo.c b/openbsd/grabmntinfo.c @@ -1,29 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <sys/param.h> -#include <sys/mount.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "../ubase.h" -#include "../util.h" - -int -grabmntinfo(struct mntinfo **minfo) -{ - int siz, i; - struct statfs *mntbuf; - struct mntinfo *mi; - - siz = getmntinfo(&mntbuf, MNT_WAIT); - if (!siz) - eprintf("getmntinfo:"); - mi = malloc(siz * sizeof(*mi)); - if (!mi) - eprintf("malloc:"); - for (i = 0; i < siz; i++) { - mi[i].fsname = strdup(mntbuf[i].f_mntfromname); - mi[i].mntdir = strdup(mntbuf[i].f_mntonname); - } - *minfo = mi; - return siz; -} diff --git a/openbsd/umount.c b/openbsd/umount.c @@ -1,21 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <sys/param.h> -#include <sys/mount.h> -#include <errno.h> -#include <stdio.h> -#include "../ubase.h" -#include "../util.h" - -int -do_umount(const char *target, int opts) -{ - int flags = 0; - - if (opts & UBASE_MNT_FORCE) - flags |= MNT_FORCE; - if (opts & UBASE_MNT_DETACH) { - errno = ENOTSUP; - return -1; - } - return unmount(target, flags); -} diff --git a/ubase.h b/ubase.h @@ -1,22 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -/* grabmntinfo.c */ -struct mntinfo { - const char *fsname; - const char *mntdir; -}; - -int grabmntinfo(struct mntinfo **minfo); - -/* dmesg.c */ -int dmesg_size(void); -int dmesg_read(void *buf, size_t n); -int dmesg_show(int fd, const void *buf, size_t n); - -/* umount.c */ -enum { - UBASE_MNT_FORCE = 1 << 0, - UBASE_MNT_DETACH = 1 << 1 -}; - -int do_umount(const char *target, int opts); diff --git a/umount.c b/umount.c @@ -1,6 +1,6 @@ /* See LICENSE file for copyright and license details. */ +#include <sys/mount.h> #include <stdio.h> -#include "ubase.h" #include "util.h" static void @@ -17,10 +17,10 @@ main(int argc, char *argv[]) { ARGBEGIN { case 'f': - flags |= UBASE_MNT_FORCE; + flags |= MNT_FORCE; break; case 'l': - flags |= UBASE_MNT_DETACH; + flags |= MNT_DETACH; break; default: usage(); @@ -30,8 +30,8 @@ main(int argc, char *argv[]) { usage(); for (i = 0; i < argc; i++) { - if (do_umount(argv[i], flags) < 0) - eprintf("do_umount:"); + if (umount2(argv[i], flags) < 0) + eprintf("umount2:"); ret = 1; } return ret; diff --git a/util/grabmntinfo.c b/util/grabmntinfo.c @@ -0,0 +1,31 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <mntent.h> +#include "../grabmntinfo.h" +#include "../util.h" + +int +grabmntinfo(struct mntinfo **minfo) +{ + struct mntent *me; + struct mntinfo *mi = NULL; + int siz = 0; + FILE *fp; + + fp = setmntent("/proc/mounts", "r"); + if (!fp) + eprintf("setmntent:"); + while ((me = getmntent(fp))) { + mi = realloc(mi, (siz + 1) * sizeof(*mi)); + if (!mi) + eprintf("realloc:"); + mi[siz].fsname = strdup(me->mnt_fsname); + mi[siz].mntdir = strdup(me->mnt_dir); + siz++; + } + endmntent(fp); + *minfo = mi; + return siz; +}