diff --git a/df.c b/df.c index f3a3049..3b71e53 100644 --- 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 index 91c2923..2537fe5 100644 --- 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 index 4d0efdf..cef5c4e 100644 --- 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 index 25b4ded..d63a72a 100644 --- 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 index 1a45814..e03284c 100644 --- a/id.c +++ b/id.c @@ -17,49 +17,8 @@ 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[]) -{ - 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; - - 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(); - } - - return 0; -} - static void groupid(struct passwd *pw) { @@ -78,6 +37,32 @@ groupid(struct passwd *pw) putchar('\n'); } +static void +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("%s%u", !i ? " groups=" : ",", gid); + if (!(gr = getgrgid(gid))) + eprintf("getgrgid:"); + printf("(%s)", gr->gr_name); + } + putchar('\n'); +} + static void usernam(const char *nam) { @@ -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; - - 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("%s%u", !i ? " groups=" : ",", gid); - if (!(gr = getgrgid(gid))) - eprintf("getgrgid:"); - printf("(%s)", gr->gr_name); - } - putchar('\n'); + eprintf("usage: %s [-g] [-u] [-G] [user | uid]\n", argv0); +} + +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; + + 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(); + } + + return 0; } diff --git a/killall5.c b/killall5.c index 55e5862..e841841 100644 --- 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 index 31b7a26..25a59e4 100644 --- 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 index 28355c1..8de35c7 100644 --- 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 index 9a13c57..6061758 100644 --- 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 index ffcda61..c5916be 100644 --- 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 index 314c17f..1df0a54 100644 --- 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 index d179fb9..eca6196 100644 --- 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 index 0516912..77670f5 100644 --- 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 index 9d9a04c..220a659 100644 --- 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 index 45867fb..329238f 100644 --- 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 index 21d9d26..7f9e174 100644 --- 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