ubase

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

commit 062c7fcbb9719090a520eb33022d672bb4834297
parent 8f3dabf349b599a257772a8c7dcf0d3307095b7c
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri Feb  6 15:19:33 +0100

code-style: cleanup

- move main() at the bottom.
- put global variables at the top.
- improve usage() lines (consistent with man page).

Diffstat:
df.c | 113+++++++++++++++++++++++++++++++++++++++----------------------------------------
free.c | 12++++++------
getty.c | 6+++---
hwclock.c | 111++++++++++++++++++++++++++++++++++++++-----------------------------------------
id.c | 118++++++++++++++++++++++++++++++++++++++++----------------------------------------
killall5.c | 18+++++++++---------
login.c | 52+++++++++++++++++++++++++---------------------------
mknod.c | 2+-
mount.c | 14+++++++-------
passwd.c | 12++++++------
pidof.c | 12++++++------
ps.c | 4++--
respawn.c | 2+-
stat.c | 70++++++++++++++++++++++++++++++++++------------------------------------
su.c | 53++++++++++++++++++++++++++---------------------------
umount.c | 63+++++++++++++++++++++++++++++++--------------------------------
16 files changed, 325 insertions(+), 337 deletions(-)
diff --git a/df.c b/df.c @@ -13,63 +13,6 @@ static int aflag = 0; static int hflag = 0; static int kflag = 0; -static int mnt_show(const char *fsname, const char *dir); - -static void -usage(void) -{ - eprintf("usage: %s [-a]\n", argv0); -} - -int -main(int argc, char *argv[]) -{ - struct mntent *me = NULL; - FILE *fp; - int ret = 0; - - ARGBEGIN { - case 'a': - aflag = 1; - break; - case 'h': - hflag = 1; - kflag = 0; - break; - case 'k': - kflag = 1; - hflag = 0; - blksize = 1024; - break; - case 's': - case 'i': - eprintf("not implemented\n"); - default: - usage(); - } ARGEND; - - if (hflag) - printf("Filesystem Size Used " - "Avail Capacity Mounted on\n"); - else - printf("Filesystem %ld-blocks Used " - "Avail Capacity Mounted on\n", blksize); - - fp = setmntent("/proc/mounts", "r"); - if (!fp) - eprintf("setmntent %s:", "/proc/mounts"); - while ((me = getmntent(fp)) != NULL) { - if (aflag == 0) - if (strcmp(me->mnt_type, "rootfs") == 0) - continue; - if (mnt_show(me->mnt_fsname, me->mnt_dir) < 0) - ret = 1; - } - endmntent(fp); - - return ret; -} - #define CALC_POWER(n, power, base, i) do { \ while (n > power) { \ power = power * base; \ @@ -141,3 +84,58 @@ mnt_show(const char *fsname, const char *dir) return 0; } + +static void +usage(void) +{ + eprintf("usage: %s [-a]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + struct mntent *me = NULL; + FILE *fp; + int ret = 0; + + ARGBEGIN { + case 'a': + aflag = 1; + break; + case 'h': + hflag = 1; + kflag = 0; + break; + case 'k': + kflag = 1; + hflag = 0; + blksize = 1024; + break; + case 's': + case 'i': + eprintf("not implemented\n"); + default: + usage(); + } ARGEND; + + if (hflag) + printf("Filesystem Size Used " + "Avail Capacity Mounted on\n"); + else + printf("Filesystem %ld-blocks Used " + "Avail Capacity Mounted on\n", blksize); + + fp = setmntent("/proc/mounts", "r"); + if (!fp) + eprintf("setmntent %s:", "/proc/mounts"); + while ((me = getmntent(fp)) != NULL) { + if (aflag == 0) + if (strcmp(me->mnt_type, "rootfs") == 0) + continue; + if (mnt_show(me->mnt_fsname, me->mnt_dir) < 0) + ret = 1; + } + endmntent(fp); + + return ret; +}+ \ No newline at end of file diff --git a/free.c b/free.c @@ -6,12 +6,6 @@ #include "util.h" -static void -usage(void) -{ - eprintf("usage: %s [-bkmg]\n", argv0); -} - static unsigned int mem_unit = 1; static unsigned int unit_shift; @@ -21,6 +15,12 @@ scale(unsigned long long v) return (v * mem_unit) >> unit_shift; } +static void +usage(void) +{ + eprintf("usage: %s [-bkmg]\n", argv0); +} + int main(int argc, char *argv[]) { diff --git a/getty.c b/getty.c @@ -15,15 +15,15 @@ #include "config.h" #include "util.h" +static char *tty = "/dev/tty1"; +static char *defaultterm = "linux"; + static void usage(void) { eprintf("usage: %s [tty] [term] [cmd] [args...]\n", argv0); } -static char *tty = "/dev/tty1"; -static char *defaultterm = "linux"; - int main(int argc, char *argv[]) { diff --git a/hwclock.c b/hwclock.c @@ -14,64 +14,6 @@ #include "rtc.h" #include "util.h" -static void readrtctm(struct tm *, int); -static void writertctm(struct tm *, int); -static void show(char *); -static void hctosys(char *); -static void systohc(char *); - -static void -usage(void) -{ - eprintf("usage: %s [-rsw] [-u] [dev]\n", argv0); -} - -int -main(int argc, char *argv[]) -{ - char *dev = "/dev/rtc"; - int rflag = 0; - int sflag = 0; - int wflag = 0; - - ARGBEGIN { - case 'r': - rflag = 1; - break; - case 's': - sflag = 1; - break; - case 'w': - wflag = 1; - break; - case 'u': - break; - default: - usage(); - } ARGEND; - - if (argc > 1) - usage(); - else if (argc == 1) - dev = argv[0]; - - if ((rflag ^ sflag ^ wflag) == 0) - eprintf("missing or incompatible function\n"); - - /* Only UTC support at the moment */ - setenv("TZ", "UTC0", 1); - tzset(); - - if (rflag == 1) - show(dev); - else if (sflag == 1) - hctosys(dev); - else if (wflag == 1) - systohc(dev); - - return 0; -} - static void readrtctm(struct tm *tm, int fd) { @@ -163,3 +105,55 @@ systohc(char *dev) writertctm(tm, fd); close(fd); } + +static void +usage(void) +{ + eprintf("usage: %s [-rsw] [-u] [dev]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + char *dev = "/dev/rtc"; + int rflag = 0; + int sflag = 0; + int wflag = 0; + + ARGBEGIN { + case 'r': + rflag = 1; + break; + case 's': + sflag = 1; + break; + case 'w': + wflag = 1; + break; + case 'u': + break; + default: + usage(); + } ARGEND; + + if (argc > 1) + usage(); + else if (argc == 1) + dev = argv[0]; + + if ((rflag ^ sflag ^ wflag) == 0) + eprintf("missing or incompatible function\n"); + + /* Only UTC support at the moment */ + setenv("TZ", "UTC0", 1); + tzset(); + + if (rflag == 1) + show(dev); + else if (sflag == 1) + hctosys(dev); + else if (wflag == 1) + systohc(dev); + + return 0; +}+ \ No newline at end of file diff --git a/id.c b/id.c @@ -17,63 +17,48 @@ static void user(struct passwd *pw); static void userid(uid_t id); static void usernam(const char *nam); -static void -usage(void) -{ - eprintf("usage: %s [-g] [-u] [-G] [user | uid]\n", argv0); -} - static int Gflag = 0; -int -main(int argc, char *argv[]) +static void +groupid(struct passwd *pw) { - ARGBEGIN { - case 'g': - printf("%d\n", getegid()); - return 0; - case 'u': - printf("%d\n", geteuid()); - return 0; - case 'G': - Gflag = 1; - break; - default: - usage(); - } ARGEND; + gid_t gid, groups[NGROUPS_MAX]; + int ngroups; + int i; - switch (argc) { - case 0: - userid(getuid()); - break; - case 1: - /* user names can't begin [0-9] */ - if (isdigit(argv[0][0])) - userid(estrtol(argv[0], 0)); - else - usernam(argv[0]); - break; - default: - usage(); + ngroups = NGROUPS_MAX; + getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); + for (i = 0; i < ngroups; i++) { + gid = groups[i]; + printf("%u", gid); + if (i < ngroups - 1) + putchar(' '); } - - return 0; + putchar('\n'); } static void -groupid(struct passwd *pw) +user(struct passwd *pw) { + struct group *gr; gid_t gid, groups[NGROUPS_MAX]; int ngroups; int i; + printf("uid=%u(%s)", pw->pw_uid, pw->pw_name); + printf(" gid=%u", pw->pw_gid); + if (!(gr = getgrgid(pw->pw_gid))) + eprintf("getgrgid:"); + printf("(%s)", gr->gr_name); + ngroups = NGROUPS_MAX; getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); for (i = 0; i < ngroups; i++) { gid = groups[i]; - printf("%u", gid); - if (i < ngroups - 1) - putchar(' '); + printf("%s%u", !i ? " groups=" : ",", gid); + if (!(gr = getgrgid(gid))) + eprintf("getgrgid:"); + printf("(%s)", gr->gr_name); } putchar('\n'); } @@ -117,27 +102,42 @@ userid(uid_t id) } static void -user(struct passwd *pw) +usage(void) { - struct group *gr; - gid_t gid, groups[NGROUPS_MAX]; - int ngroups; - int i; + eprintf("usage: %s [-g] [-u] [-G] [user | uid]\n", argv0); +} - printf("uid=%u(%s)", pw->pw_uid, pw->pw_name); - printf(" gid=%u", pw->pw_gid); - if (!(gr = getgrgid(pw->pw_gid))) - eprintf("getgrgid:"); - printf("(%s)", gr->gr_name); +int +main(int argc, char *argv[]) +{ + ARGBEGIN { + case 'g': + printf("%d\n", getegid()); + return 0; + case 'u': + printf("%d\n", geteuid()); + return 0; + case 'G': + Gflag = 1; + break; + default: + usage(); + } ARGEND; - ngroups = NGROUPS_MAX; - getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); - for (i = 0; i < ngroups; i++) { - gid = groups[i]; - printf("%s%u", !i ? " groups=" : ",", gid); - if (!(gr = getgrgid(gid))) - eprintf("getgrgid:"); - printf("(%s)", gr->gr_name); + switch (argc) { + case 0: + userid(getuid()); + break; + case 1: + /* user names can't begin [0-9] */ + if (isdigit(argv[0][0])) + userid(estrtol(argv[0], 0)); + else + usernam(argv[0]); + break; + default: + usage(); } - putchar('\n'); + + return 0; } diff --git a/killall5.c b/killall5.c @@ -22,12 +22,6 @@ struct { #undef SIG }; -static void -usage(void) -{ - eprintf("usage: %s [-o pid1,pid2,..,pidN] [-s signal]\n", argv0); -} - struct pidentry { pid_t pid; TAILQ_ENTRY(pidentry) entry; @@ -35,15 +29,21 @@ struct pidentry { static TAILQ_HEAD(omitpid_head, pidentry) omitpid_head; +static void +usage(void) +{ + eprintf("usage: %s [-o pid1,pid2,..,pidN] [-s signal]\n", argv0); +} + int main(int argc, char *argv[]) { struct pidentry *pe, *tmp; - int oflag = 0; - char *p, *arg = NULL; - DIR *dp; struct dirent *entry; + DIR *dp; + char *p, *arg = NULL; char *end, *v; + int oflag = 0; int sig = SIGTERM; pid_t pid; size_t i; diff --git a/login.c b/login.c @@ -16,14 +16,6 @@ #include "passwd.h" #include "util.h" -static int dologin(struct passwd *, int); - -static void -usage(void) -{ - eprintf("usage: %s [-p] username\n", argv0); -} - /* Write utmp entry */ static void writeutmp(const char *user, const char *tty) @@ -50,6 +42,31 @@ writeutmp(const char *user, const char *tty) } } +static int +dologin(struct passwd *pw, int preserve) +{ + char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell; + + if (preserve == 0) + clearenv(); + setenv("HOME", pw->pw_dir, 1); + setenv("SHELL", shell, 1); + setenv("USER", pw->pw_name, 1); + setenv("LOGNAME", pw->pw_name, 1); + setenv("PATH", ENV_PATH, 1); + if (chdir(pw->pw_dir) < 0) + eprintf("chdir %s:", pw->pw_dir); + execlp(shell, shell, "-l", NULL); + weprintf("execlp %s:", shell); + return (errno == ENOENT) ? 127 : 126; +} + +static void +usage(void) +{ + eprintf("usage: %s [-p] username\n", argv0); +} + int main(int argc, char *argv[]) { @@ -111,22 +128,3 @@ main(int argc, char *argv[]) return dologin(pw, pflag); } - -static int -dologin(struct passwd *pw, int preserve) -{ - char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell; - - if (preserve == 0) - clearenv(); - setenv("HOME", pw->pw_dir, 1); - setenv("SHELL", shell, 1); - setenv("USER", pw->pw_name, 1); - setenv("LOGNAME", pw->pw_name, 1); - setenv("PATH", ENV_PATH, 1); - if (chdir(pw->pw_dir) < 0) - eprintf("chdir %s:", pw->pw_dir); - execlp(shell, shell, "-l", NULL); - weprintf("execlp %s:", shell); - return (errno == ENOENT) ? 127 : 126; -} diff --git a/mknod.c b/mknod.c @@ -13,7 +13,7 @@ static void usage(void) { - eprintf("usage: mknod [-m mode] name type major minor\n"); + eprintf("usage: %s [-m mode] name type major minor\n", argv0); } int diff --git a/mount.c b/mount.c @@ -97,13 +97,6 @@ mounted(const char *dir) return 0; } -static void -usage(void) -{ - eprintf("usage: %s [-BMRan] [-t fstype] [-o options] [source] [target]\n", - argv0); -} - static int catfile(FILE *in, FILE *out) { @@ -119,6 +112,13 @@ catfile(FILE *in, FILE *out) return 1; } +static void +usage(void) +{ + eprintf("usage: %s [-BMRan] [-t fstype] [-o options] [source] [target]\n", + argv0); +} + int main(int argc, char *argv[]) { diff --git a/passwd.c b/passwd.c @@ -18,12 +18,6 @@ #include "text.h" #include "util.h" -static void -usage(void) -{ - eprintf("usage: %s [username]\n", argv0); -} - static FILE * spw_get_file(const char *user) { @@ -133,6 +127,12 @@ cleanup: return r; } +static void +usage(void) +{ + eprintf("usage: %s [username]\n", argv0); +} + int main(int argc, char *argv[]) { diff --git a/pidof.c b/pidof.c @@ -13,12 +13,6 @@ #include "queue.h" #include "util.h" -static void -usage(void) -{ - eprintf("usage: %s [-o pid1,pid2,...pidN] [-s] [program...]\n", argv0); -} - struct pidentry { pid_t pid; TAILQ_ENTRY(pidentry) entry; @@ -26,6 +20,12 @@ struct pidentry { static TAILQ_HEAD(omitpid_head, pidentry) omitpid_head; +static void +usage(void) +{ + eprintf("usage: %s [-o pid1,pid2,...pidN] [-s] [program...]\n", argv0); +} + int main(int argc, char *argv[]) { diff --git a/ps.c b/ps.c @@ -25,14 +25,14 @@ enum { PS_fflag = 1 << 3 }; +static int flags; + static void usage(void) { eprintf("usage: [-aAdef] %s\n", argv0); } -static int flags; - int main(int argc, char *argv[]) { diff --git a/respawn.c b/respawn.c @@ -26,7 +26,7 @@ sigterm(int sig) static void usage(void) { - eprintf("usage: respawn [-l fifo] [-d N] cmd [args...]\n"); + eprintf("usage: %s [-l fifo] [-d N] cmd [args...]\n", argv0); } int diff --git a/stat.c b/stat.c @@ -10,8 +10,38 @@ #include "util.h" -static void show_stat(const char *file, struct stat *st); -static void show_stat_terse(const char *file, struct stat *st); +static void +show_stat_terse(const char *file, struct stat *st) +{ + printf("%s ", file); + printf("%lu %lu ", (unsigned long)st->st_size, + (unsigned long)st->st_blocks); + 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("%ld %ld %ld ", st->st_atime, st->st_mtime, st->st_ctime); + printf("%lu\n", (unsigned long)st->st_blksize); +} + +static void +show_stat(const char *file, struct stat *st) +{ + char buf[100]; + + printf(" File: ā€˜%sā€™\n", file); + printf(" Size: %lu\tBlocks: %lu\tIO Block: %lu\n", (unsigned long)st->st_size, + (unsigned long)st->st_blocks, (unsigned long)st->st_blksize); + printf("Device: %xh/%ud\tInode: %lu\tLinks %lu\n", major(st->st_dev), + minor(st->st_dev), (unsigned long)st->st_ino, (unsigned long)st->st_nlink); + printf("Access: %04o\tUid: %u\tGid: %u\n", st->st_mode & 0777, st->st_uid, st->st_gid); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_atime)); + printf("Access: %s\n", buf); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_mtime)); + printf("Modify: %s\n", buf); + strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_ctime)); + printf("Change: %s\n", buf); +} static void usage(void) @@ -56,37 +86,4 @@ main(int argc, char *argv[]) } return ret; -} - -static void -show_stat_terse(const char *file, struct stat *st) -{ - printf("%s ", file); - printf("%lu %lu ", (unsigned long)st->st_size, - (unsigned long)st->st_blocks); - 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("%ld %ld %ld ", st->st_atime, st->st_mtime, st->st_ctime); - printf("%lu\n", (unsigned long)st->st_blksize); -} - -static void -show_stat(const char *file, struct stat *st) -{ - char buf[100]; - - printf(" File: ā€˜%sā€™\n", file); - printf(" Size: %lu\tBlocks: %lu\tIO Block: %lu\n", (unsigned long)st->st_size, - (unsigned long)st->st_blocks, (unsigned long)st->st_blksize); - printf("Device: %xh/%ud\tInode: %lu\tLinks %lu\n", major(st->st_dev), - minor(st->st_dev), (unsigned long)st->st_ino, (unsigned long)st->st_nlink); - printf("Access: %04o\tUid: %u\tGid: %u\n", st->st_mode & 0777, st->st_uid, st->st_gid); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_atime)); - printf("Access: %s\n", buf); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_mtime)); - printf("Modify: %s\n", buf); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&st->st_ctime)); - printf("Change: %s\n", buf); -} +}+ \ No newline at end of file diff --git a/su.c b/su.c @@ -15,7 +15,30 @@ extern char **environ; -static int dologin(struct passwd *); +static int lflag = 0; +static int pflag = 0; + +static int +dologin(struct passwd *pw) +{ + char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell; + char *term = getenv("TERM"); + clearenv(); + setenv("HOME", pw->pw_dir, 1); + setenv("SHELL", shell, 1); + setenv("USER", pw->pw_name, 1); + setenv("LOGNAME", pw->pw_name, 1); + setenv("TERM", term ? term : "linux", 1); + if (strcmp(pw->pw_name, "root") == 0) + setenv("PATH", ENV_SUPATH, 1); + else + setenv("PATH", ENV_PATH, 1); + if (chdir(pw->pw_dir) < 0) + eprintf("chdir %s:", pw->pw_dir); + execlp(shell, shell, "-l", NULL); + weprintf("execlp %s:", shell); + return (errno == ENOENT) ? 127 : 126; +} static void usage(void) @@ -23,9 +46,6 @@ usage(void) eprintf("usage: %s [-lp] [username]\n", argv0); } -static int lflag = 0; -static int pflag = 0; - int main(int argc, char *argv[]) { @@ -102,26 +122,4 @@ main(int argc, char *argv[]) return (errno == ENOENT) ? 127 : 126; } return 0; -} - -static int -dologin(struct passwd *pw) -{ - char *shell = pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell; - char *term = getenv("TERM"); - clearenv(); - setenv("HOME", pw->pw_dir, 1); - setenv("SHELL", shell, 1); - setenv("USER", pw->pw_name, 1); - setenv("LOGNAME", pw->pw_name, 1); - setenv("TERM", term ? term : "linux", 1); - if (strcmp(pw->pw_name, "root") == 0) - setenv("PATH", ENV_SUPATH, 1); - else - setenv("PATH", ENV_PATH, 1); - if (chdir(pw->pw_dir) < 0) - eprintf("chdir %s:", pw->pw_dir); - execlp(shell, shell, "-l", NULL); - weprintf("execlp %s:", shell); - return (errno == ENOENT) ? 127 : 126; -} +}+ \ No newline at end of file diff --git a/umount.c b/umount.c @@ -8,7 +8,35 @@ #include "util.h" -static int umountall(int); +static int +umountall(int flags) +{ + FILE *fp; + struct mntent *me; + int ret; + char **mntdirs = NULL; + int len = 0; + + fp = setmntent("/proc/mounts", "r"); + if (!fp) + eprintf("setmntent %s:", "/proc/mounts"); + while ((me = getmntent(fp))) { + if (strcmp(me->mnt_type, "proc") == 0) + continue; + mntdirs = erealloc(mntdirs, ++len * sizeof(*mntdirs)); + mntdirs[len - 1] = estrdup(me->mnt_dir); + } + endmntent(fp); + while (--len >= 0) { + if (umount2(mntdirs[len], flags) < 0) { + weprintf("umount2 %s:", mntdirs[len]); + ret = 1; + } + free(mntdirs[len]); + } + free(mntdirs); + return ret; +} static void usage(void) @@ -55,34 +83,4 @@ main(int argc, char *argv[]) } } return ret; -} - -static int -umountall(int flags) -{ - FILE *fp; - struct mntent *me; - int ret; - char **mntdirs = NULL; - int len = 0; - - fp = setmntent("/proc/mounts", "r"); - if (!fp) - eprintf("setmntent %s:", "/proc/mounts"); - while ((me = getmntent(fp))) { - if (strcmp(me->mnt_type, "proc") == 0) - continue; - mntdirs = erealloc(mntdirs, ++len * sizeof(*mntdirs)); - mntdirs[len - 1] = estrdup(me->mnt_dir); - } - endmntent(fp); - while (--len >= 0) { - if (umount2(mntdirs[len], flags) < 0) { - weprintf("umount2 %s:", mntdirs[len]); - ret = 1; - } - free(mntdirs[len]); - } - free(mntdirs); - return ret; -} +}+ \ No newline at end of file