ubase

suckless linux base utils
git clone git://git.2f30.org/ubase.git
Log | Files | Refs | README | LICENSE

commit 422511ab60d1047eba033086db9dfd73019fb9c5
parent 5f82a7df1cb26e79a904867990c416a7d294a99e
Author: sin <sin@2f30.org>
Date:   Sat Nov 29 21:44:07 +0000

Rename util/ to libutil/

Diffstat:
Makefile | 46+++++++++++++++++++++++-----------------------
libutil/agetcwd.c | 18++++++++++++++++++
libutil/agetline.c | 13+++++++++++++
libutil/apathmax.c | 22++++++++++++++++++++++
libutil/concat.c | 20++++++++++++++++++++
libutil/ealloc.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
libutil/eprintf.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
libutil/estrtol.c | 27+++++++++++++++++++++++++++
libutil/estrtoul.c | 26++++++++++++++++++++++++++
libutil/explicit_bzero.c | 12++++++++++++
libutil/passwd.c | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
libutil/proc.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
libutil/putword.c | 16++++++++++++++++
libutil/recurse.c | 42++++++++++++++++++++++++++++++++++++++++++
libutil/strlcat.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
libutil/strlcpy.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
libutil/tty.c | 39+++++++++++++++++++++++++++++++++++++++
util/agetcwd.c | 18------------------
util/agetline.c | 13-------------
util/apathmax.c | 22----------------------
util/concat.c | 20--------------------
util/ealloc.c | 47-----------------------------------------------
util/eprintf.c | 67-------------------------------------------------------------------
util/estrtol.c | 27---------------------------
util/estrtoul.c | 26--------------------------
util/explicit_bzero.c | 12------------
util/passwd.c | 77-----------------------------------------------------------------------------
util/proc.c | 117-------------------------------------------------------------------------------
util/putword.c | 16----------------
util/recurse.c | 42------------------------------------------
util/strlcat.c | 52----------------------------------------------------
util/strlcpy.c | 48------------------------------------------------
util/tty.c | 39---------------------------------------
33 files changed, 666 insertions(+), 666 deletions(-)
diff --git a/Makefile b/Makefile @@ -15,22 +15,22 @@ HDR = \ util.h LIB = \ - util/agetcwd.o \ - util/agetline.o \ - util/apathmax.o \ - util/concat.o \ - util/ealloc.o \ - util/eprintf.o \ - util/estrtol.o \ - util/estrtoul.o \ - util/explicit_bzero.o \ - util/passwd.o \ - util/proc.o \ - util/putword.o \ - util/recurse.o \ - util/strlcat.o \ - util/strlcpy.o \ - util/tty.o + libutil/agetcwd.o \ + libutil/agetline.o \ + libutil/apathmax.o \ + libutil/concat.o \ + libutil/ealloc.o \ + libutil/eprintf.o \ + libutil/estrtol.o \ + libutil/estrtoul.o \ + libutil/explicit_bzero.o \ + libutil/passwd.o \ + libutil/proc.o \ + libutil/putword.o \ + libutil/recurse.o \ + libutil/strlcat.o \ + libutil/strlcpy.o \ + libutil/tty.o SRC = \ last.c \ @@ -144,7 +144,7 @@ options: @echo "LDFLAGS = $(LDFLAGS)" @echo "CC = $(CC)" -binlib: util.a +binlib: libutil.a $(MAKE) bin bin: $(BIN) @@ -157,13 +157,13 @@ config.h: .o: @echo LD $@ - @$(LD) -o $@ $< util.a $(LDFLAGS) + @$(LD) -o $@ $< libutil.a $(LDFLAGS) .c.o: @echo CC $< @$(CC) -c -o $@ $< $(CFLAGS) -util.a: $(LIB) +libutil.a: $(LIB) @echo AR $@ @$(AR) -r -c $@ $? @ranlib $@ @@ -194,12 +194,12 @@ uninstall: dist: clean @echo creating dist tarball @mkdir -p ubase-$(VERSION) - @cp -r LICENSE Makefile config.mk TODO $(SRC) $(MAN) util $(HDR) ubase-$(VERSION) + @cp -r LICENSE Makefile config.mk TODO $(SRC) $(MAN) libutil $(HDR) ubase-$(VERSION) @tar -cf ubase-$(VERSION).tar ubase-$(VERSION) @gzip ubase-$(VERSION).tar @rm -rf ubase-$(VERSION) -ubase-box: config.h $(SRC) util.a +ubase-box: config.h $(SRC) libutil.a @echo creating box binary @mkdir -p build @cp $(HDR) build @@ -217,9 +217,9 @@ ubase-box: config.h $(SRC) util.a @for f in $(SRC); do echo "printf(\"`basename $$f .c`\"); putchar(' ');" >> build/$@.c; done @echo "putchar(0xa); }; return 0; }" >> build/$@.c @echo LD $@ - @$(LD) -o $@ build/*.c util.a $(CFLAGS) $(LDFLAGS) + @$(LD) -o $@ build/*.c libutil.a $(CFLAGS) $(LDFLAGS) @rm -r build clean: @echo cleaning - @rm -f $(BIN) $(OBJ) $(LIB) util.a ubase-box + @rm -f $(BIN) $(OBJ) $(LIB) libutil.a ubase-box diff --git a/libutil/agetcwd.c b/libutil/agetcwd.c @@ -0,0 +1,18 @@ +/* See LICENSE file for copyright and license details. */ +#include <unistd.h> + +#include "../util.h" + +char * +agetcwd(void) +{ + char *buf; + long size; + + apathmax(&buf, &size); + if (!getcwd(buf, size)) + eprintf("getcwd:"); + + return buf; +} + diff --git a/libutil/agetline.c b/libutil/agetline.c @@ -0,0 +1,13 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../text.h" +#include "../util.h" + +ssize_t +agetline(char **p, size_t *size, FILE *fp) +{ + return getline(p, size, fp); +} diff --git a/libutil/apathmax.c b/libutil/apathmax.c @@ -0,0 +1,22 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "../util.h" + +void +apathmax(char **p, long *size) +{ + errno = 0; + + if ((*size = pathconf("/", _PC_PATH_MAX)) == -1) { + if (errno == 0) { + *size = BUFSIZ; + } else { + eprintf("pathconf:"); + } + } + *p = emalloc(*size); +} diff --git a/libutil/concat.c b/libutil/concat.c @@ -0,0 +1,20 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <unistd.h> + +#include "../text.h" +#include "../util.h" + +void +concat(FILE *fp1, const char *s1, FILE *fp2, const char *s2) +{ + char buf[BUFSIZ]; + ssize_t n; + + while ((n = read(fileno(fp1), buf, sizeof buf)) > 0) { + if (write(fileno(fp2), buf, n) != n) + eprintf("%s: write error:", s2); + } + if (n < 0) + eprintf("%s: read error:", s1); +} diff --git a/libutil/ealloc.c b/libutil/ealloc.c @@ -0,0 +1,47 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdlib.h> +#include <string.h> + +#include "../util.h" + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + p = calloc(nmemb, size); + if (!p) + eprintf("calloc: out of memory\n"); + return p; +} + +void * +emalloc(size_t size) +{ + void *p; + + p = malloc(size); + if (!p) + eprintf("malloc: out of memory\n"); + return p; +} + +void * +erealloc(void *p, size_t size) +{ + p = realloc(p, size); + if (!p) + eprintf("realloc: out of memory\n"); + return p; +} + +char * +estrdup(const char *s) +{ + char *p; + + p = strdup(s); + if (!p) + eprintf("strdup: out of memory\n"); + return p; +} diff --git a/libutil/eprintf.c b/libutil/eprintf.c @@ -0,0 +1,67 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../util.h" + +char *argv0; + +static void venprintf(int, const char *, va_list); + +void +eprintf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(1, fmt, ap); + va_end(ap); +} + +void +enprintf(int status, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(status, fmt, ap); + va_end(ap); +} + +void +venprintf(int status, const char *fmt, va_list ap) +{ +#ifdef DEBUG + fprintf(stderr, "%s: ", argv0); +#endif + + vfprintf(stderr, fmt, ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } + + exit(status); +} + +void +weprintf(const char *fmt, ...) +{ + va_list ap; + +#ifdef DEBUG + fprintf(stderr, "%s: ", argv0); +#endif + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } +} diff --git a/libutil/estrtol.c b/libutil/estrtol.c @@ -0,0 +1,27 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + +#include "../util.h" + +long +estrtol(const char *s, int base) +{ + char *end; + long n; + + errno = 0; + n = strtol(s, &end, base); + if (*end != '\0') { + if (base == 0) + eprintf("%s: not an integer\n", s); + else + eprintf("%s: not a base %d integer\n", s, base); + } + if (errno != 0) + eprintf("%s:", s); + + return n; +} + diff --git a/libutil/estrtoul.c b/libutil/estrtoul.c @@ -0,0 +1,26 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + +#include "../util.h" + +unsigned long +estrtoul(const char *s, int base) +{ + char *end; + unsigned long n; + + errno = 0; + n = strtoul(s, &end, base); + if (*end != '\0') { + if (base == 0) + eprintf("%s: not an integer\n", s); + else + eprintf("%s: not a base %d integer\n", s, base); + } + if (errno != 0) + eprintf("%s:", s); + + return n; +} diff --git a/libutil/explicit_bzero.c b/libutil/explicit_bzero.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ +#include <string.h> + +#include "../util.h" + +static void *(*volatile explicit_memset)(void *, int, size_t) = memset; + +void +explicit_bzero(void *b, size_t len) +{ + (*explicit_memset)(b, 0, len); +} diff --git a/libutil/passwd.c b/libutil/passwd.c @@ -0,0 +1,77 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/resource.h> +#include <sys/time.h> + +#include <errno.h> +#include <pwd.h> +#include <shadow.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "../passwd.h" +#include "../text.h" +#include "../util.h" + +/* Returns -1 on error, 0 for incorrect password + * and 1 if all went OK */ +int +pw_check(const struct passwd *pw, const char *pass) +{ + char *cryptpass, *p; + struct spwd *spw; + + p = pw->pw_passwd; + if (p[0] == '!' || p[0] == '*') { + weprintf("denied\n"); + return -1; + } + + if (pw->pw_passwd[0] == '\0') { + if (pass[0] == '\0') + return 1; + weprintf("incorrect password\n"); + return 0; + } + + if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') { + errno = 0; + spw = getspnam(pw->pw_name); + if (!spw) { + if (errno) + weprintf("getspnam: %s:", pw->pw_name); + else + weprintf("who are you?\n"); + return -1; + } + p = spw->sp_pwdp; + if (p[0] == '!' || p[0] == '*') { + weprintf("denied\n"); + return -1; + } + } + + cryptpass = crypt(pass, p); + if (!cryptpass) { + weprintf("crypt:"); + return -1; + } + if (strcmp(cryptpass, p) != 0) { + weprintf("incorrect password\n"); + return 0; + } + return 1; +} + +int +pw_init(void) +{ + struct rlimit rlim; + + rlim.rlim_cur = 0; + rlim.rlim_max = 0; + if (setrlimit(RLIMIT_CORE, &rlim) < 0) + eprintf("setrlimit:"); + return 0; +} diff --git a/libutil/proc.c b/libutil/proc.c @@ -0,0 +1,117 @@ +/* See LICENSE file for copyright and license details. */ +#include <sys/stat.h> + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "../proc.h" +#include "../util.h" + +int +parsecmdline(pid_t pid, char *buf, size_t siz) +{ + int fd; + char path[PATH_MAX]; + ssize_t n, i; + + snprintf(path, sizeof(path), "/proc/%ld/cmdline", (long)pid); + fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + n = read(fd, buf, siz > 0 ? siz - 1 : 0); + if (n < 0) { + weprintf("read %s:", path); + close(fd); + return -1; + } + if (!n) { + close(fd); + return -1; + } + buf[n] = '\0'; + for (i = 0; i < n; i++) + if (buf[i] == '\0') + buf[i] = ' '; + close(fd); + return 0; +} + +int +parsestat(pid_t pid, struct procstat *ps) +{ + char path[PATH_MAX]; + FILE *fp; + size_t len; + + snprintf(path, sizeof(path), "/proc/%d/stat", pid); + if (!(fp = fopen(path, "r"))) + return -1; + fscanf(fp, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", + &ps->pid, ps->comm, + &ps->state, &ps->ppid, &ps->pgrp, + &ps->sid, &ps->tty_nr, &ps->tpgid, &ps->flags, + &ps->minflt, &ps->cminflt, &ps->majflt, &ps->cmajflt, + &ps->utime, &ps->stime); + fscanf(fp, "%ld %ld %ld %ld %ld %ld %llu %lu %ld %ld", + &ps->cutime, &ps->cstime, &ps->priority, &ps->nice, + &ps->num_threads, &ps->itrealvalue, &ps->starttime, + &ps->vsize, &ps->rss, &ps->rsslim); + /* Filter out '(' and ')' from comm */ + if ((len = strlen(ps->comm)) > 0) + len--; + ps->comm[len] = '\0'; + memmove(ps->comm, ps->comm + 1, len); + fclose(fp); + return 0; +} + +int +parsestatus(pid_t pid, struct procstatus *pstatus) +{ + char path[PATH_MAX]; + char buf[BUFSIZ], *off; + int fd; + ssize_t n; + + snprintf(path, sizeof(path), "/proc/%d/status", pid); + fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + n = read(fd, buf, sizeof(buf) - 1); + if (n < 0) + eprintf("%s: read error:", path); + if (!n) { + close(fd); + return -1; + } + buf[n] = '\0'; + close(fd); + off = strstr(buf, "Uid:"); + if (!off) + return -1; + sscanf(off, "Uid: %u %u", &pstatus->uid, &pstatus->euid); + off = strstr(buf, "Gid:"); + if (!off) + return -1; + sscanf(off, "Gid: %u %u", &pstatus->gid, &pstatus->egid); + return 0; +} + +int +pidfile(const char *file) +{ + char *end; + + errno = 0; + strtol(file, &end, 10); + if (*end != '\0') + return 0; + if (errno != 0) + return 0; + return 1; +} diff --git a/libutil/putword.c b/libutil/putword.c @@ -0,0 +1,16 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> + +#include "../util.h" + +void +putword(const char *s) +{ + static int first = 1; + + if (!first) + putchar(' '); + + fputs(s, stdout); + first = 0; +} diff --git a/libutil/recurse.c b/libutil/recurse.c @@ -0,0 +1,42 @@ +/* See LICENSE file for copyright and license details. */ +#include <dirent.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "../util.h" + +void +recurse(const char *path, void (*fn)(const char *)) +{ + char buf[PATH_MAX]; + struct dirent *d; + struct stat st; + DIR *dp; + + if (lstat(path, &st) == -1 || S_ISDIR(st.st_mode) == 0) + return; + + if (!(dp = opendir(path))) + eprintf("opendir %s:", path); + + while ((d = readdir(dp))) { + if (strcmp(d->d_name, ".") == 0 || + strcmp(d->d_name, "..") == 0) + continue; + if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) + eprintf("path too long\n"); + if (buf[strlen(buf) - 1] != '/') + if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf)) + eprintf("path too long\n"); + if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf)) + eprintf("path too long\n"); + fn(buf); + } + + closedir(dp); +} diff --git a/libutil/strlcat.c b/libutil/strlcat.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <string.h> +#include <sys/types.h> + +#include "../util.h" + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + return(dlen + (s - src)); /* count does not include NUL */ +} diff --git a/libutil/strlcpy.c b/libutil/strlcpy.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <string.h> +#include <sys/types.h> + +#include "../util.h" + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + return(s - src - 1); /* count does not include NUL */ +} diff --git a/libutil/tty.c b/libutil/tty.c @@ -0,0 +1,39 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../util.h" + +void +devtotty(int dev, int *tty_maj, int *tty_min) +{ + *tty_maj = (dev >> 8) & 0xfff; + *tty_min = (dev & 0xff) | ((dev >> 12) & 0xfff00); +} + +char * +ttytostr(int tty_maj, int tty_min) +{ + const char *pts = "pts/"; + const char *tty = "tty"; + char *ttystr; + size_t len; + + /* Up to 10k ttys */ + len = strlen(pts) + 4 + 1; + ttystr = emalloc(len); + switch (tty_maj) { + case 136: + snprintf(ttystr, len, "%s%d", pts, tty_min); + break; + case 4: + snprintf(ttystr, len, "%s%d", tty, tty_min); + break; + default: + ttystr[0] = '?'; + ttystr[1] = '\0'; + break; + } + return ttystr; +} diff --git a/util/agetcwd.c b/util/agetcwd.c @@ -1,18 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <unistd.h> - -#include "../util.h" - -char * -agetcwd(void) -{ - char *buf; - long size; - - apathmax(&buf, &size); - if (!getcwd(buf, size)) - eprintf("getcwd:"); - - return buf; -} - diff --git a/util/agetline.c b/util/agetline.c @@ -1,13 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "../text.h" -#include "../util.h" - -ssize_t -agetline(char **p, size_t *size, FILE *fp) -{ - return getline(p, size, fp); -} diff --git a/util/apathmax.c b/util/apathmax.c @@ -1,22 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> - -#include "../util.h" - -void -apathmax(char **p, long *size) -{ - errno = 0; - - if ((*size = pathconf("/", _PC_PATH_MAX)) == -1) { - if (errno == 0) { - *size = BUFSIZ; - } else { - eprintf("pathconf:"); - } - } - *p = emalloc(*size); -} diff --git a/util/concat.c b/util/concat.c @@ -1,20 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdio.h> -#include <unistd.h> - -#include "../text.h" -#include "../util.h" - -void -concat(FILE *fp1, const char *s1, FILE *fp2, const char *s2) -{ - char buf[BUFSIZ]; - ssize_t n; - - while ((n = read(fileno(fp1), buf, sizeof buf)) > 0) { - if (write(fileno(fp2), buf, n) != n) - eprintf("%s: write error:", s2); - } - if (n < 0) - eprintf("%s: read error:", s1); -} diff --git a/util/ealloc.c b/util/ealloc.c @@ -1,47 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdlib.h> -#include <string.h> - -#include "../util.h" - -void * -ecalloc(size_t nmemb, size_t size) -{ - void *p; - - p = calloc(nmemb, size); - if (!p) - eprintf("calloc: out of memory\n"); - return p; -} - -void * -emalloc(size_t size) -{ - void *p; - - p = malloc(size); - if (!p) - eprintf("malloc: out of memory\n"); - return p; -} - -void * -erealloc(void *p, size_t size) -{ - p = realloc(p, size); - if (!p) - eprintf("realloc: out of memory\n"); - return p; -} - -char * -estrdup(const char *s) -{ - char *p; - - p = strdup(s); - if (!p) - eprintf("strdup: out of memory\n"); - return p; -} diff --git a/util/eprintf.c b/util/eprintf.c @@ -1,67 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "../util.h" - -char *argv0; - -static void venprintf(int, const char *, va_list); - -void -eprintf(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - venprintf(1, fmt, ap); - va_end(ap); -} - -void -enprintf(int status, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - venprintf(status, fmt, ap); - va_end(ap); -} - -void -venprintf(int status, const char *fmt, va_list ap) -{ -#ifdef DEBUG - fprintf(stderr, "%s: ", argv0); -#endif - - vfprintf(stderr, fmt, ap); - - if (fmt[0] && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } - - exit(status); -} - -void -weprintf(const char *fmt, ...) -{ - va_list ap; - -#ifdef DEBUG - fprintf(stderr, "%s: ", argv0); -#endif - - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - - if (fmt[0] && fmt[strlen(fmt)-1] == ':') { - fputc(' ', stderr); - perror(NULL); - } -} diff --git a/util/estrtol.c b/util/estrtol.c @@ -1,27 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> - -#include "../util.h" - -long -estrtol(const char *s, int base) -{ - char *end; - long n; - - errno = 0; - n = strtol(s, &end, base); - if (*end != '\0') { - if (base == 0) - eprintf("%s: not an integer\n", s); - else - eprintf("%s: not a base %d integer\n", s, base); - } - if (errno != 0) - eprintf("%s:", s); - - return n; -} - diff --git a/util/estrtoul.c b/util/estrtoul.c @@ -1,26 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> - -#include "../util.h" - -unsigned long -estrtoul(const char *s, int base) -{ - char *end; - unsigned long n; - - errno = 0; - n = strtoul(s, &end, base); - if (*end != '\0') { - if (base == 0) - eprintf("%s: not an integer\n", s); - else - eprintf("%s: not a base %d integer\n", s, base); - } - if (errno != 0) - eprintf("%s:", s); - - return n; -} diff --git a/util/explicit_bzero.c b/util/explicit_bzero.c @@ -1,12 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <string.h> - -#include "../util.h" - -static void *(*volatile explicit_memset)(void *, int, size_t) = memset; - -void -explicit_bzero(void *b, size_t len) -{ - (*explicit_memset)(b, 0, len); -} diff --git a/util/passwd.c b/util/passwd.c @@ -1,77 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <sys/resource.h> -#include <sys/time.h> - -#include <errno.h> -#include <pwd.h> -#include <shadow.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "../passwd.h" -#include "../text.h" -#include "../util.h" - -/* Returns -1 on error, 0 for incorrect password - * and 1 if all went OK */ -int -pw_check(const struct passwd *pw, const char *pass) -{ - char *cryptpass, *p; - struct spwd *spw; - - p = pw->pw_passwd; - if (p[0] == '!' || p[0] == '*') { - weprintf("denied\n"); - return -1; - } - - if (pw->pw_passwd[0] == '\0') { - if (pass[0] == '\0') - return 1; - weprintf("incorrect password\n"); - return 0; - } - - if (pw->pw_passwd[0] == 'x' && pw->pw_passwd[1] == '\0') { - errno = 0; - spw = getspnam(pw->pw_name); - if (!spw) { - if (errno) - weprintf("getspnam: %s:", pw->pw_name); - else - weprintf("who are you?\n"); - return -1; - } - p = spw->sp_pwdp; - if (p[0] == '!' || p[0] == '*') { - weprintf("denied\n"); - return -1; - } - } - - cryptpass = crypt(pass, p); - if (!cryptpass) { - weprintf("crypt:"); - return -1; - } - if (strcmp(cryptpass, p) != 0) { - weprintf("incorrect password\n"); - return 0; - } - return 1; -} - -int -pw_init(void) -{ - struct rlimit rlim; - - rlim.rlim_cur = 0; - rlim.rlim_max = 0; - if (setrlimit(RLIMIT_CORE, &rlim) < 0) - eprintf("setrlimit:"); - return 0; -} diff --git a/util/proc.c b/util/proc.c @@ -1,117 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <sys/stat.h> - -#include <errno.h> -#include <fcntl.h> -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "../proc.h" -#include "../util.h" - -int -parsecmdline(pid_t pid, char *buf, size_t siz) -{ - int fd; - char path[PATH_MAX]; - ssize_t n, i; - - snprintf(path, sizeof(path), "/proc/%ld/cmdline", (long)pid); - fd = open(path, O_RDONLY); - if (fd < 0) - return -1; - n = read(fd, buf, siz > 0 ? siz - 1 : 0); - if (n < 0) { - weprintf("read %s:", path); - close(fd); - return -1; - } - if (!n) { - close(fd); - return -1; - } - buf[n] = '\0'; - for (i = 0; i < n; i++) - if (buf[i] == '\0') - buf[i] = ' '; - close(fd); - return 0; -} - -int -parsestat(pid_t pid, struct procstat *ps) -{ - char path[PATH_MAX]; - FILE *fp; - size_t len; - - snprintf(path, sizeof(path), "/proc/%d/stat", pid); - if (!(fp = fopen(path, "r"))) - return -1; - fscanf(fp, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", - &ps->pid, ps->comm, - &ps->state, &ps->ppid, &ps->pgrp, - &ps->sid, &ps->tty_nr, &ps->tpgid, &ps->flags, - &ps->minflt, &ps->cminflt, &ps->majflt, &ps->cmajflt, - &ps->utime, &ps->stime); - fscanf(fp, "%ld %ld %ld %ld %ld %ld %llu %lu %ld %ld", - &ps->cutime, &ps->cstime, &ps->priority, &ps->nice, - &ps->num_threads, &ps->itrealvalue, &ps->starttime, - &ps->vsize, &ps->rss, &ps->rsslim); - /* Filter out '(' and ')' from comm */ - if ((len = strlen(ps->comm)) > 0) - len--; - ps->comm[len] = '\0'; - memmove(ps->comm, ps->comm + 1, len); - fclose(fp); - return 0; -} - -int -parsestatus(pid_t pid, struct procstatus *pstatus) -{ - char path[PATH_MAX]; - char buf[BUFSIZ], *off; - int fd; - ssize_t n; - - snprintf(path, sizeof(path), "/proc/%d/status", pid); - fd = open(path, O_RDONLY); - if (fd < 0) - return -1; - n = read(fd, buf, sizeof(buf) - 1); - if (n < 0) - eprintf("%s: read error:", path); - if (!n) { - close(fd); - return -1; - } - buf[n] = '\0'; - close(fd); - off = strstr(buf, "Uid:"); - if (!off) - return -1; - sscanf(off, "Uid: %u %u", &pstatus->uid, &pstatus->euid); - off = strstr(buf, "Gid:"); - if (!off) - return -1; - sscanf(off, "Gid: %u %u", &pstatus->gid, &pstatus->egid); - return 0; -} - -int -pidfile(const char *file) -{ - char *end; - - errno = 0; - strtol(file, &end, 10); - if (*end != '\0') - return 0; - if (errno != 0) - return 0; - return 1; -} diff --git a/util/putword.c b/util/putword.c @@ -1,16 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdio.h> - -#include "../util.h" - -void -putword(const char *s) -{ - static int first = 1; - - if (!first) - putchar(' '); - - fputs(s, stdout); - first = 0; -} diff --git a/util/recurse.c b/util/recurse.c @@ -1,42 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <dirent.h> -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - -#include "../util.h" - -void -recurse(const char *path, void (*fn)(const char *)) -{ - char buf[PATH_MAX]; - struct dirent *d; - struct stat st; - DIR *dp; - - if (lstat(path, &st) == -1 || S_ISDIR(st.st_mode) == 0) - return; - - if (!(dp = opendir(path))) - eprintf("opendir %s:", path); - - while ((d = readdir(dp))) { - if (strcmp(d->d_name, ".") == 0 || - strcmp(d->d_name, "..") == 0) - continue; - if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) - eprintf("path too long\n"); - if (buf[strlen(buf) - 1] != '/') - if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf)) - eprintf("path too long\n"); - if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf)) - eprintf("path too long\n"); - fn(buf); - } - - closedir(dp); -} diff --git a/util/strlcat.c b/util/strlcat.c @@ -1,52 +0,0 @@ -/* - * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <string.h> -#include <sys/types.h> - -#include "../util.h" - -/* - * Appends src to string dst of size siz (unlike strncat, siz is the - * full size of dst, not space left). At most siz-1 characters - * will be copied. Always NUL terminates (unless siz <= strlen(dst)). - * Returns strlen(src) + MIN(siz, strlen(initial dst)). - * If retval >= siz, truncation occurred. - */ -size_t -strlcat(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - size_t dlen; - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = siz - dlen; - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = '\0'; - return(dlen + (s - src)); /* count does not include NUL */ -} diff --git a/util/strlcpy.c b/util/strlcpy.c @@ -1,48 +0,0 @@ -/* - * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <string.h> -#include <sys/types.h> - -#include "../util.h" - -/* - * Copy src to string dst of size siz. At most siz-1 characters - * will be copied. Always NUL terminates (unless siz == 0). - * Returns strlen(src); if retval >= siz, truncation occurred. - */ -size_t -strlcpy(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - /* Copy as many bytes as will fit */ - if (n != 0) { - while (--n != 0) { - if ((*d++ = *s++) == '\0') - break; - } - } - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - return(s - src - 1); /* count does not include NUL */ -} diff --git a/util/tty.c b/util/tty.c @@ -1,39 +0,0 @@ -/* See LICENSE file for copyright and license details. */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "../util.h" - -void -devtotty(int dev, int *tty_maj, int *tty_min) -{ - *tty_maj = (dev >> 8) & 0xfff; - *tty_min = (dev & 0xff) | ((dev >> 12) & 0xfff00); -} - -char * -ttytostr(int tty_maj, int tty_min) -{ - const char *pts = "pts/"; - const char *tty = "tty"; - char *ttystr; - size_t len; - - /* Up to 10k ttys */ - len = strlen(pts) + 4 + 1; - ttystr = emalloc(len); - switch (tty_maj) { - case 136: - snprintf(ttystr, len, "%s%d", pts, tty_min); - break; - case 4: - snprintf(ttystr, len, "%s%d", tty, tty_min); - break; - default: - ttystr[0] = '?'; - ttystr[1] = '\0'; - break; - } - return ttystr; -}