From b0898c605de3d015862f242c3ba7ec6f4325ec23 Mon Sep 17 00:00:00 2001 From: Christoph Lohmann <20h@r-36.net> Date: Sun, 9 Jun 2013 15:20:55 +0200 Subject: [PATCH] Adding the new C files too. --- chgrp.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ chvt.c | 61 +++++++++++++++++++++++++++++++++++++++++++++ nice.c | 38 ++++++++++++++++++++++++++++ printenv.c | 24 ++++++++++++++++++ rmdir.c | 28 +++++++++++++++++++++ sync.c | 20 +++++++++++++++ unlink.c | 21 ++++++++++++++++ 7 files changed, 264 insertions(+) create mode 100644 chgrp.c create mode 100755 chvt.c create mode 100644 nice.c create mode 100644 printenv.c create mode 100644 rmdir.c create mode 100644 sync.c create mode 100644 unlink.c diff --git a/chgrp.c b/chgrp.c new file mode 100644 index 0000000..f769374 --- /dev/null +++ b/chgrp.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include "util.h" + +int gid; +int failures = 0; + +static void +usage(void) +{ + eprintf("usage: chgrp [-R] groupname file...\n"); +} + +static int +chgrp(const char *path, const struct stat *st, int f) +{ + (void)f; + + if(chown(path, st->st_uid, gid) == -1) { + fprintf(stderr, "chgrp: '%s': %s\n", path, strerror(errno)); + failures++; + } + + return 0; +} + +int +main(int argc, char **argv) +{ + int rflag = 0; + struct group *gr; + struct stat st; + + ARGBEGIN { + case 'R': + rflag = 1; + break; + default: + usage(); + } ARGEND; + if(argc<2) + usage(); + + gr = getgrnam(argv[0]); + if(!gr) + eprintf("chgrp: '%s': No such group\n", argv[0]); + gid = gr->gr_gid; + + if(rflag) { + while(*++argv) + ftw(*argv, chgrp, FOPEN_MAX); + + return 0; + } + while(*++argv) { + if(stat(*argv, &st) == -1) { + fprintf(stderr, "chgrp: '%s': %s\n", *argv, + strerror(errno)); + failures++; + continue; + } + chgrp(*argv, &st, 0); + } + + return failures; +} + diff --git a/chvt.c b/chvt.c new file mode 100755 index 0000000..fd76cb1 --- /dev/null +++ b/chvt.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include "util.h" + +enum { + /* from */ + VT_ACTIVATE = 0x5606, + VT_WAITACTIVE = 0x5607, + /* from */ + KDGKBTYPE = 0x4B33 +}; + +char *vts[] = { + "/proc/self/fd/0", + "/dev/console", + "/dev/tty", + "/dev/tty0", +}; + +static void +usage(void) +{ + eprintf("usage: chvt N\n"); +} + +int +main(int argc, char **argv) +{ + int n, i, fd; + char c; + + if(argc!=2 || strspn(argv[1], "1234567890") != strlen(argv[1])) + usage(); + + n = atoi(argv[1]); + for(i = 0; i < LEN(vts); i++) { + fd = open(vts[i], O_RDONLY); + if(fd < 1) + continue; + c = 0; + if(ioctl(fd, KDGKBTYPE, &c) == 0) + goto VTfound; + close(fd); + } + + eprintf("chvt: couldn't find a console.\n"); +VTfound: + if(ioctl(fd, VT_ACTIVATE, n) == -1) + eprintf("chvt: VT_ACTIVATE '%d':", n); + if(ioctl(fd, VT_WAITACTIVE, n) == -1) + eprintf("chvt: VT_WAITACTIVE '%d':", n); + close(fd); + + return 0; +} + diff --git a/nice.c b/nice.c new file mode 100644 index 0000000..dcbb05f --- /dev/null +++ b/nice.c @@ -0,0 +1,38 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: nice [-n inc] command [options ...]\n"); +} + +int +main(int argc, char **argv) +{ + int inc = 10; + + ARGBEGIN { + case 'n': + inc = atoi(EARGF(usage())); + break; + default: + usage(); + } ARGEND; + + nice(inc); /* POSIX specifies the nice failure still invokes the command. */ + + if(!*argv) + usage(); + + execvp(*argv, argv); + eprintf("nice: '%s': %s\n", *argv, strerror(errno)); + + return EXIT_FAILURE; +} + diff --git a/printenv.c b/printenv.c new file mode 100644 index 0000000..14fcaf7 --- /dev/null +++ b/printenv.c @@ -0,0 +1,24 @@ +#include +#include + +extern char **environ; + +int +main(int argc, char **argv) +{ + char *var; + + if(argc == 1) { + while(*environ) + printf("%s\n", *environ++); + + return 0; + } + while(*++argv) { + if((var = getenv(*argv))) + printf("%s\n", var); + } + + return 0; +} + diff --git a/rmdir.c b/rmdir.c new file mode 100644 index 0000000..7773777 --- /dev/null +++ b/rmdir.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: rmdir dir...\n"); +} + +int +main(int argc, char **argv) +{ + argv++;; + if(!*argv) + usage(); + + while(*argv) { + if(rmdir(*argv++) == -1) + fprintf(stderr, "rmdir: '%s': %s\n", + argv[-1], strerror(errno)); + } + + return 0; +} + diff --git a/sync.c b/sync.c new file mode 100644 index 0000000..db21d8a --- /dev/null +++ b/sync.c @@ -0,0 +1,20 @@ +#include +#include +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: sync\n"); +} + +int +main(int argc, char **argv) +{ + if(argc != 1) + usage(); + sync(); + + return 0; +} + diff --git a/unlink.c b/unlink.c new file mode 100644 index 0000000..c365680 --- /dev/null +++ b/unlink.c @@ -0,0 +1,21 @@ +#include +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: unlink file\n"); +} + +int +main(int argc, char **argv) +{ + if(argc != 2) + usage(); + + if(unlink(argv[1]) == -1) + eprintf("unlink: '%s':", argv[1]); + + return 0; +} +