sbase

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

commit f8dc6883a313911170c759bf0d1240c8a0ac2fb5
parent 52d39e35c2468510c356335fb78b604c7f3d3bba
Author: Christoph Lohmann <20h@r-36.net>
Date:   Tue Mar  5 21:46:48 +0100

Style inquistion for util and some tools.

Diffstat:
cat.c | 9++++++---
config.mk | 2+-
util/afgets.c | 3+++
util/agetcwd.c | 3+++
util/apathmax.c | 9+++++++--
util/concat.c | 6+++++-
util/cp.c | 31+++++++++++++++++++++++--------
util/enmasse.c | 13+++++++++----
util/eprintf.c | 3+++
util/estrtol.c | 10+++++++---
util/fnck.c | 10++++++++--
util/getlines.c | 13++++++++++---
util/putword.c | 3+++
util/recurse.c | 12+++++++++---
util/rm.c | 5+++--
15 files changed, 100 insertions(+), 32 deletions(-)
diff --git a/cat.c b/cat.c @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> + #include "text.h" #include "util.h" @@ -16,13 +17,15 @@ main(int argc, char *argv[]) eprintf("usage: %s [files...]\n", argv0); } ARGEND; - if(argc == 0) + if(argc == 0) { concat(stdin, "<stdin>", stdout, "<stdout>"); - else for(i = 0; i < argc; i++) { + } else for(i = 0; i < argc; i++) { if(!(fp = fopen(argv[i], "r"))) eprintf("fopen %s:", argv[i]); + concat(fp, argv[i], stdout, "<stdout>"); fclose(fp); } - return EXIT_SUCCESS; + + return 0; } diff --git a/config.mk b/config.mk @@ -8,7 +8,7 @@ MANPREFIX = $(PREFIX)/share/man #CC = gcc #CC = musl-gcc LD = $(CC) -CPPFLAGS = -D_POSIX_C_SOURCE=200112L +CPPFLAGS = -D_BSD_SOURCE -D_GNU_SOURCE CFLAGS = -g -ansi -Wall -pedantic $(CPPFLAGS) LDFLAGS = -g diff --git a/util/afgets.c b/util/afgets.c @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + #include "../text.h" #include "../util.h" @@ -15,10 +16,12 @@ afgets(char **p, size_t *size, FILE *fp) len += (n = strlen(buf)); if(len+1 > *size && !(*p = realloc(*p, len+1))) eprintf("realloc:"); + strcpy(&(*p)[len-n], buf); if(buf[n-1] == '\n' || feof(fp)) break; } + return (len > 0) ? *p : NULL; } diff --git a/util/agetcwd.c b/util/agetcwd.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <unistd.h> + #include "../util.h" char * @@ -11,5 +12,7 @@ agetcwd(void) apathmax(&buf, &size); if(!getcwd(buf, size)) eprintf("getcwd:"); + return buf; } + diff --git a/util/apathmax.c b/util/apathmax.c @@ -3,18 +3,23 @@ #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) + if(errno == 0) { *size = BUFSIZ; - else + } else { eprintf("pathconf:"); + } } + if(!(*p = malloc(*size))) eprintf("malloc:"); } + diff --git a/util/concat.c b/util/concat.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <stdio.h> + #include "../text.h" #include "../util.h" @@ -9,9 +10,12 @@ concat(FILE *fp1, const char *s1, FILE *fp2, const char *s2) char buf[BUFSIZ]; size_t n; - while((n = fread(buf, 1, sizeof buf, fp1)) > 0) + while((n = fread(buf, 1, sizeof buf, fp1)) > 0) { if(fwrite(buf, 1, n, fp2) != n) eprintf("%s: write error:", s2); + } + if(ferror(fp1)) eprintf("%s: read error:", s1); } + diff --git a/util/cp.c b/util/cp.c @@ -7,6 +7,7 @@ #include <string.h> #include <unistd.h> #include <sys/stat.h> + #include "../fs.h" #include "../text.h" #include "../util.h" @@ -26,22 +27,32 @@ cp(const char *s1, const char *s2) if (stat(s1, &st) == 0 && S_ISDIR(st.st_mode)) { if (!cp_rflag) { eprintf("%s: is a directory\n", s1); - } - else { + } else { if(!(dp = opendir(s1))) eprintf("opendir %s:", s1); + if (mkdir(s2, st.st_mode) == -1 && errno != EEXIST) eprintf("mkdir %s:", s2); + apathmax(&ns1, &size1); apathmax(&ns2, &size2); - while((d = readdir(dp))) - if(strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { - if(snprintf(ns1, size1, "%s/%s", s1, d->d_name) > size1) - eprintf("%s/%s: filename too long\n", s1, d->d_name); - if(snprintf(ns2, size2, "%s/%s", s2, d->d_name) > size2) - eprintf("%s/%s: filename too long\n", s2, d->d_name); + while((d = readdir(dp))) { + if(strcmp(d->d_name, ".") + && strcmp(d->d_name, "..")) { + if(snprintf(ns1, size1, "%s/%s", s1, + d->d_name) > size1) { + eprintf("%s/%s: filename too long\n", + s1, d->d_name); + } + + if(snprintf(ns2, size2, "%s/%s", s2, + d->d_name) > size2) { + eprintf("%s/%s: filename too long\n", + s2, d->d_name); + } fnck(ns1, ns2, cp); } + } closedir(dp); free(ns1); free(ns2); @@ -50,10 +61,14 @@ cp(const char *s1, const char *s2) } if(!(f1 = fopen(s1, "r"))) eprintf("fopen %s:", s1); + if(!(f2 = fopen(s2, "w"))) eprintf("fopen %s:", s2); + concat(f1, s1, f2, s2); fclose(f2); fclose(f1); + return 0; } + diff --git a/util/enmasse.c b/util/enmasse.c @@ -5,6 +5,7 @@ #include <string.h> #include <unistd.h> #include <sys/stat.h> + #include "../util.h" void @@ -17,16 +18,20 @@ enmasse(int argc, char **argv, int (*fn)(const char *, const char *)) if(argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) { fnck(argv[0], argv[1], fn); + return; - } - else + } else { dir = (argc == 1) ? "." : argv[--argc]; + } apathmax(&buf, &size); for(i = 0; i < argc; i++) { - if(snprintf(buf, size, "%s/%s", dir, basename(argv[i])) > size) - eprintf("%s/%s: filename too long\n", dir, basename(argv[i])); + if(snprintf(buf, size, "%s/%s", dir, basename(argv[i])) > size) { + eprintf("%s/%s: filename too long\n", dir, + basename(argv[i])); + } fnck(argv[i], buf, fn); } free(buf); } + diff --git a/util/eprintf.c b/util/eprintf.c @@ -3,6 +3,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + #include "../util.h" char *argv0; @@ -40,5 +41,7 @@ venprintf(int status, const char *fmt, va_list ap) fputc(' ', stderr); perror(NULL); } + exit(status); } + diff --git a/util/estrtol.c b/util/estrtol.c @@ -2,6 +2,7 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> + #include "../util.h" long @@ -9,14 +10,17 @@ estrtol(const char *s, int base) { char *end; long n; - + errno = 0; n = strtol(s, &end, base); if(*end != '\0' || errno != 0) { - if(base == 0) + if(base == 0) { eprintf("%s: not an integer\n", s); - else + } else { eprintf("%s: not a base %d integer\n", s, base); + } } + return n; } + diff --git a/util/fnck.c b/util/fnck.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <sys/stat.h> + #include "../util.h" void @@ -7,9 +8,14 @@ fnck(const char *a, const char *b, int (*fn)(const char *, const char *)) { struct stat sta, stb; - if(stat(a, &sta) == 0 && stat(b, &stb) == 0 - && sta.st_dev == stb.st_dev && sta.st_ino == stb.st_ino) + if(stat(a, &sta) == 0 + && stat(b, &stb) == 0 + && sta.st_dev == stb.st_dev + && sta.st_ino == stb.st_ino) { eprintf("%s -> %s: same file\n", a, b); + } + if(fn(a, b) == -1) eprintf("%s -> %s:", a, b); } + diff --git a/util/getlines.c b/util/getlines.c @@ -2,21 +2,28 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + #include "../text.h" #include "../util.h" void getlines(FILE *fp, struct linebuf *b) { - char *line = NULL; + char *line = NULL, **nline; size_t size = 0; while(afgets(&line, &size, fp)) { - if(++b->nlines > b->capacity && !(b->lines = realloc(b->lines, (b->capacity+=512) * sizeof *b->lines))) - eprintf("realloc:"); + if(++b->nlines > b->capacity) { + b->capacity += 512; + nline = realloc(b->lines, b->capacity * sizeof(*b->lines)); + if(nline == NULL) + eprintf("realloc:"); + b->lines = nline; + } if(!(b->lines[b->nlines-1] = malloc(strlen(line)+1))) eprintf("malloc:"); strcpy(b->lines[b->nlines-1], line); } free(line); } + diff --git a/util/putword.c b/util/putword.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include <stdbool.h> #include <stdio.h> + #include "../util.h" void @@ -10,6 +11,8 @@ putword(const char *s) if(!first) putchar(' '); + fputs(s, stdout); first = false; } + diff --git a/util/recurse.c b/util/recurse.c @@ -5,6 +5,7 @@ #include <string.h> #include <unistd.h> #include <sys/stat.h> + #include "../util.h" void @@ -15,20 +16,25 @@ recurse(const char *path, void (*fn)(const char *)) struct stat st; DIR *dp; - if(lstat(path, &st) == -1 || !S_ISDIR(st.st_mode)) + if(lstat(path, &st) == -1 || !S_ISDIR(st.st_mode)) { return; - else if(!(dp = opendir(path))) + } else if(!(dp = opendir(path))) { eprintf("opendir %s:", path); + } cwd = agetcwd(); if(chdir(path) == -1) eprintf("chdir %s:", path); - while((d = readdir(dp))) + + while((d = readdir(dp))) { if(strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) fn(d->d_name); + } closedir(dp); if(chdir(cwd) == -1) eprintf("chdir %s:", cwd); + free(cwd); } + diff --git a/util/rm.c b/util/rm.c @@ -1,11 +1,11 @@ /* See LICENSE file for copyright and license details. */ #include <stdbool.h> #include <stdio.h> + #include "../fs.h" #include "../util.h" -bool rm_fflag = false; -bool rm_rflag = false; +bool rm_fflag = false, rm_rflag = false; void rm(const char *path) @@ -15,3 +15,4 @@ rm(const char *path) if(remove(path) == -1 && !rm_fflag) eprintf("remove %s:", path); } +