[hackers] [sbase] Adding the new C files too. || Christoph Lohmann

From: <git_AT_suckless.org>
Date: Sun, 09 Jun 2013 15:22:31 +0200

commit b0898c605de3d015862f242c3ba7ec6f4325ec23
Author: Christoph Lohmann <20h_AT_r-36.net>
Date: Sun Jun 9 15:20:55 2013 +0200

    Adding the new C files too.

diff --git a/chgrp.c b/chgrp.c
new file mode 100644
index 0000000..f769374
--- /dev/null
+++ b/chgrp.c
_AT_@ -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...
");
+}
+
+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
", 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
", 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
", *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
_AT_@ -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
");
+}
+
+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.
");
+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
_AT_@ -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 ...]
");
+}
+
+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
", *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
_AT_@ -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
", *environ++);
+
+ return 0;
+ }
+ while(*++argv) {
+ if((var = getenv(*argv)))
+ printf("%s
", var);
+ }
+
+ return 0;
+}
+
diff --git a/rmdir.c b/rmdir.c
new file mode 100644
index 0000000..7773777
--- /dev/null
+++ b/rmdir.c
_AT_@ -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...
");
+}
+
+int
+main(int argc, char **argv)
+{
+ argv++;;
+ if(!*argv)
+ usage();
+
+ while(*argv) {
+ if(rmdir(*argv++) == -1)
+ fprintf(stderr, "rmdir: '%s': %s
",
+ 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
_AT_@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <unistd.h>
+#include "util.h"
+
+static void
+usage(void)
+{
+ eprintf("usage: sync
");
+}
+
+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
_AT_@ -0,0 +1,21 @@
+#include <unistd.h>
+#include "util.h"
+
+static void
+usage(void)
+{
+ eprintf("usage: unlink file
");
+}
+
+int
+main(int argc, char **argv)
+{
+ if(argc != 2)
+ usage();
+
+ if(unlink(argv[1]) == -1)
+ eprintf("unlink: '%s':", argv[1]);
+
+ return 0;
+}
+
Received on Sun Jun 09 2013 - 15:22:31 CEST

This archive was generated by hypermail 2.3.0 : Sun Jun 09 2013 - 15:24:08 CEST