sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit b0898c605de3d015862f242c3ba7ec6f4325ec23
parent 2c162042b14ed36f58b705fa885c05f36d7bf712
Author: Christoph Lohmann <20h@r-36.net>
Date:   Sun,  9 Jun 2013 15:20:55 +0200

Adding the new C files too.

Diffstat:
Achgrp.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Achvt.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Anice.c | 38++++++++++++++++++++++++++++++++++++++
Aprintenv.c | 24++++++++++++++++++++++++
Armdir.c | 28++++++++++++++++++++++++++++
Async.c | 20++++++++++++++++++++
Aunlink.c | 21+++++++++++++++++++++
7 files changed, 264 insertions(+), 0 deletions(-)

diff --git a/chgrp.c b/chgrp.c @@ -0,0 +1,72 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <ftw.h> +#include <errno.h> +#include <grp.h> +#include <sys/types.h> +#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 @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/ioctl.h> +#include "util.h" + +enum { + /* from <linux/vt.h> */ + VT_ACTIVATE = 0x5606, + VT_WAITACTIVE = 0x5607, + /* from <linux/kd.h> */ + 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 @@ -0,0 +1,38 @@ +/* See LICENSE file for copyright and license details. */ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#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 @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> + +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 @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#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 @@ -0,0 +1,20 @@ +#include <stdio.h> +#include <unistd.h> +#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 @@ -0,0 +1,21 @@ +#include <unistd.h> +#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; +} +