Style inquistion for util and some tools.

This commit is contained in:
Christoph Lohmann 2013-03-05 21:46:48 +01:00
parent 52d39e35c2
commit f8dc6883a3
15 changed files with 100 additions and 32 deletions

9
cat.c
View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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:");
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -2,6 +2,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "../util.h"
long
@ -13,10 +14,13 @@ estrtol(const char *s, int base)
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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}