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