[hackers] [sbase] Adding who, chroot, env and split. || Christoph Lohmann

From: <git_AT_suckless.org>
Date: Fri, 14 Jun 2013 18:57:05 +0200

commit 9df408f8c6cfb323a1dad871241361e2692e1e57
Author: Christoph Lohmann <20h_AT_r-36.net>
Date: Fri Jun 14 18:55:25 2013 +0200

    Adding who, chroot, env and split.
    
    Thanks "Galos, David" <galosd83_AT_students.rowan.edu>!

diff --git a/Makefile b/Makefile
index d90d690..05c12dd 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -25,6 +25,7 @@ SRC = \
         chgrp.c \
         chmod.c \
         chown.c \
+ chroot.c \
         chvt.c \
         cksum.c \
         cmp.c \
_AT_@ -32,6 +33,7 @@ SRC = \
         date.c \
         dirname.c \
         echo.c \
+ env.c \
         false.c \
         fold.c \
         grep.c \
_AT_@ -53,6 +55,7 @@ SRC = \
         rmdir.c \
         sleep.c \
         sort.c \
+ split.c \
         sync.c \
         tail.c \
         tee.c \
_AT_@ -65,6 +68,7 @@ SRC = \
         unlink.c \
         seq.c \
         wc.c \
+ who.c \
         yes.c
 
 OBJ = $(SRC:.c=.o) $(LIB)
diff --git a/TODO b/TODO
index 217829f..00d3839 100644
--- a/TODO
+++ b/TODO
_AT_@ -10,8 +10,6 @@ diff [-ru] file1 file2
 
 du [-hdi] [path]
 
-env [-u] [name=value...] [command]
-
 expand [-i] [-t N] [file...]
 
 expr [expression]
_AT_@ -32,8 +30,6 @@ seq [-s string] [N [N]] N
 
 sha1sum [-c] [file...]
 
-split [-a N] [-b N] [-l N] [input [prefix]]
-
 test [expression...]
 
 tr string1 [string2]
diff --git a/chroot.1 b/chroot.1
new file mode 100644
index 0000000..48cbe19
--- /dev/null
+++ b/chroot.1
_AT_@ -0,0 +1,26 @@
+.TH CHROOT 1 sbase\-VERSION
+.SH NAME
+chroot \- invoke a command with a different root directory
+.SH SYNOPSIS
+.B chroot
+.IR dir
+.RI [ command
+.RI [ arg ...]]
+
+.SH DESCRIPTION
+.B chroot
+runs
+.IR command
+after changing the root directory to
+.IR dir
+with the
+.B chroot
+system call, and changing the working directory to the new root.
+
+If
+.IR command
+is not specified, an interactive shell is started in the new root.
+
+.SH SEE ALSO
+.IR chroot (2)
+.IR chdir (2)
diff --git a/chroot.c b/chroot.c
new file mode 100644
index 0000000..3abab0f
--- /dev/null
+++ b/chroot.c
_AT_@ -0,0 +1,38 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include "util.h"
+
+static void usage(void);
+
+int
+main(int argc, char **argv)
+{
+ char *shell[] = {"/bin/sh", "-i", NULL};
+
+ if(getenv("SHELL"))
+ shell[0] = getenv("SHELL");
+
+ if(argc < 2)
+ usage();
+
+ if(chroot(argv[1]) == -1)
+ eprintf("chroot: '%s':", argv[1]);
+
+ if(chdir("/") == -1)
+ eprintf("chroot:");
+
+ if(argc == 2) {
+ execvp(*shell, shell);
+ } else {
+ execvp(argv[2], argv+2);
+ }
+
+ eprintf("chroot: '%s':", argv[2]);
+ return 1;
+}
+
+void
+usage(void)
+{
+ eprintf("usage: chroot dir [command [arg...]]
");
+}
diff --git a/env.1 b/env.1
new file mode 100644
index 0000000..b77fb2f
--- /dev/null
+++ b/env.1
_AT_@ -0,0 +1,41 @@
+.TH ENV 1 sbase\-VERSION
+.SH NAME
+env \- modify the environment, then print it or run a command.
+.SH SYNOPSIS
+.B env
+.RB [ \-i ]
+.RB [ \-u
+.IR name ]...
+.RI [ name=value ]...
+.RI [ cmd
+.RI [ arg ...]]
+
+.SH DESCRIPTION
+.B env
+removes part of the environment according to the flags, then adds or
+sets each variable specified by
+.IR name
+to equal
+.IR value .
+
+If
+.IR cmd
+is given, it is executed in this new environment; otherwise, the
+modified environment is printed to standard out.
+
+.SH OPTIONS
+.TP
+.B \-i
+Comptetely ignore the existing environment; start fresh.
+
+.TP
+.B \-u name
+Unsets
+.IR name
+from the environment.
+
+.SH SEE ALSO
+.IR printenv (1)
+.IR putenv (3)
+.IR environ (7)
+
diff --git a/env.c b/env.c
new file mode 100644
index 0000000..718b13e
--- /dev/null
+++ b/env.c
_AT_@ -0,0 +1,44 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "util.h"
+
+extern char **environ;
+
+static void usage(void);
+
+int
+main(int argc, char **argv)
+{
+ ARGBEGIN {
+ case 'i':
+ clearenv();
+ break;
+ case 'u':
+ unsetenv(EARGF(usage()));
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ for(; *argv && strchr(*argv, '='); argv++)
+ putenv(*argv);
+
+ if(*argv) {
+ execvp(*argv, argv);
+ enprintf(127-(errno!=EEXIST), "env: '%s':", *argv);
+ }
+
+ while(environ && *environ)
+ printf("%s
", *environ++);
+
+ return 0;
+}
+
+void
+usage(void)
+{
+ eprintf("usage: env [-i] [-u name]... [name=value]... [cmd [arg...]]
");
+}
diff --git a/split.1 b/split.1
new file mode 100644
index 0000000..e615168
--- /dev/null
+++ b/split.1
_AT_@ -0,0 +1,58 @@
+.TH SPLIT 1 sbase\-VERSION
+.SH NAME
+split \- split up a file
+.SH SYNOPSIS
+.B split
+.RB [ \-d ]
+.RB [ \-a
+.IR len ]
+.RB [ \-b
+.RI [ bytes [k|m|g]]]
+.RB [ \-l
+.RI [ lines ]]
+.RI [ input
+.RI [ prefix ]]
+
+.SH DESCRIPTION
+.B split
+Reads a file, splitting it into smaller files, every
+.IR bytes
+bytes
+or
+.IR lines
+lines. If
+.B split
+runs out of filenames before all the data can be written, it stops at the
+last valid filename, leaving all the written data on the disk.
+
+The
+.IR b
+and
+.IR l
+flags are mutually exclusive. Only the last one specified will be obeyed.
+
+.SH OPTIONS
+.TP
+.B \-d
+Use decimal suffixes rather than alphabetical.
+
+.TP
+.B \-a "len"
+Set the suffix length to
+.IR len
+characters long.
+
+.TP
+.B \-b [bytes[k|m|g]]
+Start a new file every
+.IR bytes
+bytes. The units k, m, and g are case insensitive, and powers of 2, not 10.
+
+.TP
+.B \-l [lines]
+Start a new file every
+.IR lines
+lines.
+
+.SH SEE ALSO
+.IR cat (1)
diff --git a/split.c b/split.c
new file mode 100644
index 0000000..87e1099
--- /dev/null
+++ b/split.c
_AT_@ -0,0 +1,138 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <ctype.h>
+#include <limits.h>
+#include "util.h"
+
+static int itostr(char *, int, int);
+static FILE *nextfile(FILE *, char *, int, int);
+static void usage(void);
+
+static int base = 26, start = 'a';
+
+int
+main(int argc, char **argv)
+{
+ int plen, slen = 2;
+ int ch;
+ char name[NAME_MAX+1];
+ char *prefix = "x";
+ char *file = NULL;
+ char *tmp, *end;
+ uint64_t sizes['M'+1];
+ uint64_t size = 1000, scale, n;
+ int always = 0;
+ FILE *in=stdin, *out=NULL;
+
+ sizes['K'] = 1024;
+ sizes['M'] = 1024L*1024L;
+ sizes['G'] = 1024L*1024L*1024L;
+
+ ARGBEGIN {
+ case 'b':
+ always = 1;
+ tmp = ARGF();
+ if(tmp == NULL)
+ break;
+
+ size = strtoull(tmp, &end, 10);
+ if(*end == '
Received on Fri Jun 14 2013 - 18:57:05 CEST

This archive was generated by hypermail 2.3.0 : Fri Jun 14 2013 - 19:00:12 CEST