Fix coding style

It was about damn time. Consistency is very important in such a
big codebase.
This commit is contained in:
FRIGN 2014-11-13 18:29:30 +01:00 committed by sin
parent e35d9e62a5
commit eee98ed3a4
70 changed files with 622 additions and 557 deletions

View File

@ -24,11 +24,11 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc < 1) if (argc < 1)
usage(); usage();
s = basename(argv[0]); s = basename(argv[0]);
if(argc == 2) { if (argc == 2) {
p = strstr(s, argv[1]); p = strstr(s, argv[1]);
if (p && p[strlen(p)] == '\0') if (p && p[strlen(p)] == '\0')
*p = '\0'; *p = '\0';

29
cal.c
View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "util.h" #include "util.h"
#define MONTHMAX 100 #define MONTHMAX 100
@ -28,11 +29,11 @@ drawcal(int year, int month, int day, int ncols, int nmons, int fday)
int row = 0; int row = 0;
char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", }; char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", };
if(!ncols) if (!ncols)
ncols = nmons; ncols = nmons;
while(nmons > 0) { while (nmons > 0) {
last = MIN(nmons, ncols); last = MIN(nmons, ncols);
for(i = 0; i < last; i++) { for (i = 0; i < last; i++) {
moff = month + ncols * row + i - 1; moff = month + ncols * row + i - 1;
cur = moff % 12; cur = moff % 12;
yoff = year + moff / 12; yoff = year + moff / 12;
@ -43,17 +44,17 @@ drawcal(int year, int month, int day, int ncols, int nmons, int fday)
} }
printf("\n"); printf("\n");
for(i = 0; i < last; i++) { for (i = 0; i < last; i++) {
for(j = fday; j < LEN(days); j++) for (j = fday; j < LEN(days); j++)
printf("%s ", days[j]); printf("%s ", days[j]);
for(j = 0; j < fday; j++) for (j = 0; j < fday; j++)
printf("%s ", days[j]); printf("%s ", days[j]);
printf(" "); printf(" ");
} }
printf("\n"); printf("\n");
for(r = 0; r < 6; r++) { for (r = 0; r < 6; r++) {
for(i = 0; i < last; i++) { for (i = 0; i < last; i++) {
moff = month + ncols * row + i - 1; moff = month + ncols * row + i - 1;
cur = moff % 12; cur = moff % 12;
yoff = year + moff / 12; yoff = year + moff / 12;
@ -61,8 +62,8 @@ drawcal(int year, int month, int day, int ncols, int nmons, int fday)
ndays = mdays[cur] + ((cur == 1) & isleap(yoff)); ndays = mdays[cur] + ((cur == 1) & isleap(yoff));
day1 = dayofweek(year, cur, 1, fday); day1 = dayofweek(year, cur, 1, fday);
for(d = 0; d < 7; d++) { for (d = 0; d < 7; d++) {
if((r || d >= day1) && count[i] <= ndays) if ((r || d >= day1) && count[i] <= ndays)
printf("%2d ", count[i]++); printf("%2d ", count[i]++);
else else
printf(" "); printf(" ");
@ -89,9 +90,9 @@ dayofweek(int year, int month, int day, int fday)
static bool static bool
isleap(int year) isleap(int year)
{ {
if(year % 400 == 0) if (year % 400 == 0)
return true; return true;
if(year % 100 == 0) if (year % 100 == 0)
return false; return false;
return (year % 4 == 0); return (year % 4 == 0);
} }
@ -156,7 +157,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
switch(argc) { switch (argc) {
case 3: case 3:
day = estrtol(argv[0], 0); day = estrtol(argv[0], 0);
argv++; argv++;
@ -172,7 +173,7 @@ main(int argc, char *argv[])
usage(); usage();
} }
if(ncols < 0 || month < 1 || month > 12 || nmons < 1 \ if (ncols < 0 || month < 1 || month > 12 || nmons < 1 \
|| nmons > MONTHMAX || fday < 0 || fday > 6) { || nmons > MONTHMAX || fday < 0 || fday > 6) {
usage(); usage();
} }

4
cat.c
View File

@ -26,13 +26,13 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) { if (argc == 0) {
concat(stdin, "<stdin>", stdout, "<stdout>"); concat(stdin, "<stdin>", stdout, "<stdout>");
} else { } else {
for (; argc; argc--, argv++) { for (; argc; argc--, argv++) {
if (argv[0][0] == '-') if (argv[0][0] == '-')
argv[0] = "/dev/fd/0"; argv[0] = "/dev/fd/0";
if(!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
ret = 1; ret = 1;
continue; continue;

11
chgrp.c
View File

@ -7,6 +7,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"
static int gid; static int gid;
@ -23,7 +24,7 @@ usage(void)
static void static void
chgrp(const char *path) chgrp(const char *path)
{ {
if(chown(path, st.st_uid, gid) == -1) { if (chown(path, st.st_uid, gid) == -1) {
fprintf(stderr, "chgrp: '%s': %s\n", path, strerror(errno)); fprintf(stderr, "chgrp: '%s': %s\n", path, strerror(errno));
failures++; failures++;
} }
@ -44,19 +45,19 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc < 2) if (argc < 2)
usage(); usage();
errno = 0; errno = 0;
gr = getgrnam(argv[0]); gr = getgrnam(argv[0]);
if (errno) if (errno)
eprintf("getgrnam %s:"); eprintf("getgrnam %s:");
else if(!gr) else if (!gr)
eprintf("chgrp: '%s': No such group\n", argv[0]); eprintf("chgrp: '%s': No such group\n", argv[0]);
gid = gr->gr_gid; gid = gr->gr_gid;
while(*++argv) { while (*++argv) {
if(stat(*argv, &st) == -1) { if (stat(*argv, &st) == -1) {
fprintf(stderr, "chgrp: '%s': %s\n", *argv, fprintf(stderr, "chgrp: '%s': %s\n", *argv,
strerror(errno)); strerror(errno));
failures++; failures++;

View File

@ -51,7 +51,7 @@ done:
argv++; argv++;
argc--; argc--;
if(argc < 1) if (argc < 1)
usage(); usage();
for (; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
@ -65,17 +65,17 @@ chmodr(const char *path)
struct stat st; struct stat st;
mode_t m; mode_t m;
if(stat(path, &st) == -1) { if (stat(path, &st) == -1) {
weprintf("stat %s:", path); weprintf("stat %s:", path);
ret = 1; ret = 1;
return; return;
} }
m = parsemode(modestr, st.st_mode, mask); m = parsemode(modestr, st.st_mode, mask);
if(chmod(path, m) == -1) { if (chmod(path, m) == -1) {
weprintf("chmod %s:", path); weprintf("chmod %s:", path);
ret = 1; ret = 1;
} }
if(rflag) if (rflag)
recurse(path, chmodr); recurse(path, chmodr);
} }

27
chown.c
View File

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void chownpwgr(const char *); static void chownpwgr(const char *);
@ -37,42 +38,42 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) if (argc == 0)
usage(); usage();
owner = argv[0]; owner = argv[0];
argv++; argv++;
argc--; argc--;
if((group = strchr(owner, ':'))) if ((group = strchr(owner, ':')))
*group++ = '\0'; *group++ = '\0';
if(owner && *owner) { if (owner && *owner) {
errno = 0; errno = 0;
pw = getpwnam(owner); pw = getpwnam(owner);
if(pw) { if (pw) {
uid = pw->pw_uid; uid = pw->pw_uid;
} else { } else {
if(errno != 0) if (errno != 0)
eprintf("getpwnam %s:", owner); eprintf("getpwnam %s:", owner);
uid = strtoul(owner, &end, 10); uid = strtoul(owner, &end, 10);
if(*end != '\0') if (*end != '\0')
eprintf("getpwnam %s: no such user\n", owner); eprintf("getpwnam %s: no such user\n", owner);
} }
} }
if(group && *group) { if (group && *group) {
errno = 0; errno = 0;
gr = getgrnam(group); gr = getgrnam(group);
if(gr) { if (gr) {
gid = gr->gr_gid; gid = gr->gr_gid;
} else { } else {
if(errno != 0) if (errno != 0)
eprintf("getgrnam %s:", group); eprintf("getgrnam %s:", group);
gid = strtoul(group, &end, 10); gid = strtoul(group, &end, 10);
if(*end != '\0') if (*end != '\0')
eprintf("getgrnam %s: no such group\n", group); eprintf("getgrnam %s: no such group\n", group);
} }
} }
for(; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
chownpwgr(argv[0]); chownpwgr(argv[0]);
return ret; return ret;
@ -81,10 +82,10 @@ main(int argc, char *argv[])
void void
chownpwgr(const char *path) chownpwgr(const char *path)
{ {
if(chown(path, uid, gid) == -1) { if (chown(path, uid, gid) == -1) {
weprintf("chown %s:", path); weprintf("chown %s:", path);
ret = 1; ret = 1;
} }
if(rflag) if (rflag)
recurse(path, chownpwgr); recurse(path, chownpwgr);
} }

View File

@ -2,6 +2,7 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -21,19 +22,19 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc < 1) if (argc < 1)
usage(); usage();
if((aux = getenv("SHELL"))) if ((aux = getenv("SHELL")))
shell[0] = aux; shell[0] = aux;
if(chroot(argv[0]) == -1) if (chroot(argv[0]) == -1)
eprintf("chroot %s:", argv[0]); eprintf("chroot %s:", argv[0]);
if(chdir("/") == -1) if (chdir("/") == -1)
eprintf("chdir:"); eprintf("chdir:");
if(argc == 1) { if (argc == 1) {
p = *shell; p = *shell;
execvp(*shell, shell); execvp(*shell, shell);
} else { } else {

11
cksum.c
View File

@ -4,6 +4,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"
static void cksum(FILE *, const char *); static void cksum(FILE *, const char *);
@ -78,10 +79,10 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) { if (argc == 0) {
cksum(stdin, NULL); cksum(stdin, NULL);
} else { } else {
for(; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if (!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue; continue;
@ -102,18 +103,18 @@ cksum(FILE *fp, const char *s)
size_t i, n; size_t i, n;
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
for(i = 0; i < n; i++) for (i = 0; i < n; i++)
ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]]; ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
len += n; len += n;
} }
if (ferror(fp)) if (ferror(fp))
eprintf("%s: read error:", s ? s : "<stdin>"); eprintf("%s: read error:", s ? s : "<stdin>");
for(i = len; i > 0; i >>= 8) for (i = len; i > 0; i >>= 8)
ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)]; ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)];
printf("%"PRIu32" %lu", ~ck, (unsigned long)len); printf("%"PRIu32" %lu", ~ck, (unsigned long)len);
if(s != NULL) if (s != NULL)
printf(" %s", s); printf(" %s", s);
putchar('\n'); putchar('\n');
} }

23
cmp.c
View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
enum { Same = 0, Diff = 1, Error = 2 }; enum { Same = 0, Diff = 1, Error = 2 };
@ -41,7 +42,7 @@ main(int argc, char *argv[])
argv[0] = "/dev/fd/0"; argv[0] = "/dev/fd/0";
fp[0] = fopen(argv[0], "r"); fp[0] = fopen(argv[0], "r");
if (!fp[0]) { if (!fp[0]) {
if(!sflag) if (!sflag)
weprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
exit(Error); exit(Error);
} }
@ -50,30 +51,30 @@ main(int argc, char *argv[])
argv[1] = "/dev/fd/0"; argv[1] = "/dev/fd/0";
fp[1] = fopen(argv[1], "r"); fp[1] = fopen(argv[1], "r");
if (!fp[1]) { if (!fp[1]) {
if(!sflag) if (!sflag)
weprintf("fopen %s:", argv[1]); weprintf("fopen %s:", argv[1]);
exit(Error); exit(Error);
} }
for(n = 1; ; n++) { for (n = 1; ; n++) {
b[0] = getc(fp[0]); b[0] = getc(fp[0]);
b[1] = getc(fp[1]); b[1] = getc(fp[1]);
if(b[0] == EOF && b[1] == EOF) if (b[0] == EOF && b[1] == EOF)
break; break;
if(b[0] == '\n' && b[1] == '\n') if (b[0] == '\n' && b[1] == '\n')
line++; line++;
if(b[0] == b[1]) if (b[0] == b[1])
continue; continue;
for(i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
if(b[i] == EOF) { if (b[i] == EOF) {
if(!sflag) if (!sflag)
fprintf(stderr, "cmp: EOF on %s\n", fprintf(stderr, "cmp: EOF on %s\n",
!argv[i] ? "<stdin>" : argv[i]); !argv[i] ? "<stdin>" : argv[i]);
exit(Diff); exit(Diff);
} }
} }
if(!lflag) { if (!lflag) {
if(!sflag) if (!sflag)
printf("%s %s differ: char %ld, line %ld\n", printf("%s %s differ: char %ld, line %ld\n",
argv[0], !argv[1] ? "<stdin>" : argv[1], n, line); argv[0], !argv[1] ? "<stdin>" : argv[1], n, line);
exit(Diff); exit(Diff);

27
cols.c
View File

@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -35,7 +36,7 @@ main(int argc, char *argv[])
case 'c': case 'c':
cflag = 1; cflag = 1;
chars = estrtol(EARGF(usage()), 0); chars = estrtol(EARGF(usage()), 0);
if(chars < 3) if (chars < 3)
eprintf("%d: too few character columns"); eprintf("%d: too few character columns");
break; break;
default: default:
@ -49,39 +50,39 @@ main(int argc, char *argv[])
} }
/* XXX librarify this chunk, too? only useful in sponges though */ /* XXX librarify this chunk, too? only useful in sponges though */
if(argc == 0) { if (argc == 0) {
getlines(stdin, &b); getlines(stdin, &b);
} else for(; argc > 0; argc--, argv++) { } else for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) if (!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
getlines(fp, &b); getlines(fp, &b);
fclose(fp); fclose(fp);
} }
for(l = 0; l < b.nlines; ++l) { for (l = 0; l < b.nlines; ++l) {
len = strlen(b.lines[l]); len = strlen(b.lines[l]);
if(len > 0 && b.lines[l][len-1] == '\n') if (len > 0 && b.lines[l][len-1] == '\n')
b.lines[l][--len] = '\0'; b.lines[l][--len] = '\0';
if(len > maxlen) if (len > maxlen)
maxlen = len; maxlen = len;
if(maxlen > (chars - 1) / 2) if (maxlen > (chars - 1) / 2)
break; break;
} }
n_columns = (chars + 1) / (maxlen + 1); n_columns = (chars + 1) / (maxlen + 1);
if(n_columns <= 1) { if (n_columns <= 1) {
for(l = 0; l < b.nlines; ++l) { for (l = 0; l < b.nlines; ++l) {
fputs(b.lines[l], stdout); fputs(b.lines[l], stdout);
} }
return 0; return 0;
} }
n_rows = (b.nlines + (n_columns - 1)) / n_columns; n_rows = (b.nlines + (n_columns - 1)) / n_columns;
for(i = 0; i < n_rows; ++i) { for (i = 0; i < n_rows; ++i) {
for(l = i, col = 1; l < b.nlines; l += n_rows, ++col) { for (l = i, col = 1; l < b.nlines; l += n_rows, ++col) {
len = strlen(b.lines[l]); len = strlen(b.lines[l]);
fputs(b.lines[l], stdout); fputs(b.lines[l], stdout);
if(col < n_columns) if (col < n_columns)
printf("%*s", maxlen + 1 - (int)len, ""); printf("%*s", maxlen + 1 - (int)len, "");
} }
fputs("\n", stdout); fputs("\n", stdout);

29
comm.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include "util.h" #include "util.h"
#define CLAMP(x, l, h) MIN(h, MAX(l, x)) #define CLAMP(x, l, h) MIN(h, MAX(l, x))
@ -37,29 +38,29 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc != 2) if (argc != 2)
usage(); usage();
for(i = 0; i < LEN(fp); i++) { for (i = 0; i < LEN(fp); i++) {
if (argv[i][0] == '-') if (argv[i][0] == '-')
argv[i] = "/dev/fd/0"; argv[i] = "/dev/fd/0";
if(!(fp[i] = fopen(argv[i], "r"))) if (!(fp[i] = fopen(argv[i], "r")))
eprintf("comm: '%s':", argv[i]); eprintf("comm: '%s':", argv[i]);
} }
for(;;) { for (;;) {
if(diff <= 0) { if (diff <= 0) {
lines[0][0] = '\0'; lines[0][0] = '\0';
if(!nextline(lines[0], sizeof(lines[0]), if (!nextline(lines[0], sizeof(lines[0]),
fp[0], argv[0])) { fp[0], argv[0])) {
if (lines[1][0] != '\0') if (lines[1][0] != '\0')
printline(1, lines[1]); printline(1, lines[1]);
finish(1, fp[1], argv[1]); finish(1, fp[1], argv[1]);
} }
} }
if(diff >= 0) { if (diff >= 0) {
lines[1][0] = '\0'; lines[1][0] = '\0';
if(!nextline(lines[1], sizeof(lines[1]), if (!nextline(lines[1], sizeof(lines[1]),
fp[1], argv[1])) { fp[1], argv[1])) {
if (lines[0][0] != '\0') if (lines[0][0] != '\0')
printline(0, lines[0]); printline(0, lines[0]);
@ -79,11 +80,11 @@ printline(int pos, char *line)
{ {
int i; int i;
if(!(show & (0x1 << pos))) if (!(show & (0x1 << pos)))
return; return;
for(i = 0; i < pos; i++) { for (i = 0; i < pos; i++) {
if(show & (0x1 << i)) if (show & (0x1 << i))
putchar('\t'); putchar('\t');
} }
printf("%s", line); printf("%s", line);
@ -93,9 +94,9 @@ static char *
nextline(char *buf, int n, FILE *f, char *name) nextline(char *buf, int n, FILE *f, char *name)
{ {
buf = fgets(buf, n, f); buf = fgets(buf, n, f);
if(!buf && !feof(f)) if (!buf && !feof(f))
eprintf("comm: '%s':", name); eprintf("comm: '%s':", name);
if(buf && !strchr(buf, '\n')) if (buf && !strchr(buf, '\n'))
eprintf("comm: '%s': line too long.\n", name); eprintf("comm: '%s': line too long.\n", name);
return buf; return buf;
} }
@ -105,7 +106,7 @@ finish(int pos, FILE *f, char *name)
{ {
char buf[LINE_MAX+1]; char buf[LINE_MAX+1];
while(nextline(buf, sizeof(buf), f, name)) while (nextline(buf, sizeof(buf), f, name))
printline(pos, buf); printline(pos, buf);
exit(1); exit(1);
} }

3
cp.c
View File

@ -2,6 +2,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "fs.h" #include "fs.h"
#include "util.h" #include "util.h"
@ -46,7 +47,7 @@ main(int argc, char *argv[])
if (argc < 2) if (argc < 2)
usage(); usage();
if(argc > 2 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode))) if (argc > 2 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
eprintf("%s: not a directory\n", argv[argc-1]); eprintf("%s: not a directory\n", argv[argc-1]);
enmasse(argc, argv, cp); enmasse(argc, argv, cp);
return cp_status; return cp_status;

73
cut.c
View File

@ -3,6 +3,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"
@ -30,21 +31,21 @@ insert(Range *r)
{ {
Range *l, *p, *t; Range *l, *p, *t;
for(p = NULL, l = list; l; p = l, l = l->next) { for (p = NULL, l = list; l; p = l, l = l->next) {
if(r->max && r->max + 1 < l->min) { if (r->max && r->max + 1 < l->min) {
r->next = l; r->next = l;
break; break;
} else if(!l->max || r->min < l->max + 2) { } else if (!l->max || r->min < l->max + 2) {
l->min = MIN(r->min, l->min); l->min = MIN(r->min, l->min);
for(p = l, t = l->next; t; p = t, t = t->next) for (p = l, t = l->next; t; p = t, t = t->next)
if(r->max && r->max + 1 < t->min) if (r->max && r->max + 1 < t->min)
break; break;
l->max = (p->max && r->max) ? MAX(p->max, r->max) : 0; l->max = (p->max && r->max) ? MAX(p->max, r->max) : 0;
l->next = t; l->next = t;
return; return;
} }
} }
if(p) if (p)
p->next = r; p->next = r;
else else
list = r; list = r;
@ -57,19 +58,19 @@ parselist(char *str)
size_t n = 1; size_t n = 1;
Range *r; Range *r;
for(s = str; *s; s++) { for (s = str; *s; s++) {
if(*s == ' ') if (*s == ' ')
*s = ','; *s = ',';
if(*s == ',') if (*s == ',')
n++; n++;
} }
if(!(r = malloc(n * sizeof(Range)))) if (!(r = malloc(n * sizeof(Range))))
eprintf("malloc:"); eprintf("malloc:");
for(s = str; n; n--, s++) { for (s = str; n; n--, s++) {
r->min = (*s == '-') ? 1 : strtoul(s, &s, 10); r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min; r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min;
r->next = NULL; r->next = NULL;
if(!r->min || (r->max && r->max < r->min) || (*s && *s != ',')) if (!r->min || (r->max && r->max < r->min) || (*s && *s != ','))
eprintf("cut: bad list value\n"); eprintf("cut: bad list value\n");
insert(r++); insert(r++);
} }
@ -79,7 +80,7 @@ static void
freelist(void) { freelist(void) {
Range *l = list, *next; Range *l = list, *next;
while(l) { while (l) {
next = l->next; next = l->next;
free(l); free(l);
l->next = NULL; l->next = NULL;
@ -93,21 +94,21 @@ seek(const char *s, size_t pos, size_t *prev, size_t count)
const char *t; const char *t;
size_t n = pos - *prev; size_t n = pos - *prev;
if(mode == 'b') { if (mode == 'b') {
if((t = memchr(s, 0, n))) if ((t = memchr(s, 0, n)))
return t - s; return t - s;
if(nflag) if (nflag)
while(n && !UTF8_POINT(s[n])) while (n && !UTF8_POINT(s[n]))
n--; n--;
*prev += n; *prev += n;
return n; return n;
} else if(mode == 'c') { } else if (mode == 'c') {
for(n++, t = s; *t; t++) for (n++, t = s; *t; t++)
if(UTF8_POINT(*t) && !--n) if (UTF8_POINT(*t) && !--n)
break; break;
} else { } else {
for(t = (count < 2) ? s : s + 1; n && *t; t++) for (t = (count < 2) ? s : s + 1; n && *t; t++)
if(*t == delim && !--n && count) if (*t == delim && !--n && count)
break; break;
} }
*prev = pos; *prev = pos;
@ -122,24 +123,24 @@ cut(FILE *fp)
ssize_t len; ssize_t len;
Range *r; Range *r;
while((len = agetline(&buf, &size, fp)) != -1) { while ((len = agetline(&buf, &size, fp)) != -1) {
if(len && buf[len - 1] == '\n') if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0'; buf[len - 1] = '\0';
if(mode == 'f' && !strchr(buf, delim)) { if (mode == 'f' && !strchr(buf, delim)) {
if(!sflag) if (!sflag)
puts(buf); puts(buf);
continue; continue;
} }
for(i = 0, p = 1, s = buf, r = list; r; r = r->next, s += n) { for (i = 0, p = 1, s = buf, r = list; r; r = r->next, s += n) {
s += seek(s, r->min, &p, i++); s += seek(s, r->min, &p, i++);
if(!*s) if (!*s)
break; break;
if(!r->max) { if (!r->max) {
fputs(s, stdout); fputs(s, stdout);
break; break;
} }
n = seek(s, r->max + 1, &p, i++); n = seek(s, r->max + 1, &p, i++);
if(fwrite(s, 1, n, stdout) != n) if (fwrite(s, 1, n, stdout) != n)
eprintf("write error:"); eprintf("write error:");
} }
putchar('\n'); putchar('\n');
@ -172,22 +173,22 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(!mode) if (!mode)
usage(); usage();
if(!argc) if (!argc)
cut(stdin); cut(stdin);
else { else {
for(; argc--; argv++) { for (; argc--; argv++) {
if(strcmp(*argv, "-")) if (strcmp(*argv, "-"))
fp = fopen(*argv, "r"); fp = fopen(*argv, "r");
else else
fp = stdin; fp = stdin;
if(!fp) { if (!fp) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
continue; continue;
} }
cut(fp); cut(fp);
if(fp != stdin) if (fp != stdin)
fclose(fp); fclose(fp);
} }
} }

5
date.c
View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -33,9 +34,9 @@ main(int argc, char *argv[])
default: default:
usage(); usage();
} ARGEND; } ARGEND;
if(argc > 0 && argv[0][0] == '+') if (argc > 0 && argv[0][0] == '+')
fmt = &argv[0][1]; fmt = &argv[0][1];
if(!(now = tztime(&t))) if (!(now = tztime(&t)))
eprintf("%stime failed\n", tz); eprintf("%stime failed\n", tz);
strftime(buf, sizeof buf, fmt, now); strftime(buf, sizeof buf, fmt, now);

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -19,7 +20,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc < 1) if (argc < 1)
usage(); usage();
puts(dirname(argv[0])); puts(dirname(argv[0]));

1
du.c
View File

@ -8,6 +8,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <limits.h> #include <limits.h>
#include "util.h" #include "util.h"
static long blksize = 512; static long blksize = 512;

5
echo.c
View File

@ -2,6 +2,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
static void static void
@ -23,9 +24,9 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
for(; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
putword(argv[0]); putword(argv[0]);
if(!nflag) if (!nflag)
putchar('\n'); putchar('\n');
return 0; return 0;

7
env.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "util.h" #include "util.h"
extern char **environ; extern char **environ;
@ -29,15 +30,15 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
for(; *argv && strchr(*argv, '='); argv++) for (; *argv && strchr(*argv, '='); argv++)
putenv(*argv); putenv(*argv);
if(*argv) { if (*argv) {
execvp(*argv, argv); execvp(*argv, argv);
enprintf(127 - (errno != EEXIST), "env: '%s':", *argv); enprintf(127 - (errno != EEXIST), "env: '%s':", *argv);
} }
while(environ && *environ) while (environ && *environ)
printf("%s\n", *environ++); printf("%s\n", *environ++);
return 0; return 0;

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <wchar.h> #include <wchar.h>
#include "util.h" #include "util.h"
typedef struct { typedef struct {

25
fold.c
View File

@ -4,6 +4,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"
@ -42,11 +43,11 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) { if (argc == 0) {
fold(stdin, width); fold(stdin, width);
} else { } else {
for(; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue; continue;
} }
@ -64,7 +65,7 @@ fold(FILE *fp, long width)
char *buf = NULL; char *buf = NULL;
size_t size = 0; size_t size = 0;
while(agetline(&buf, &size, fp) != -1) while (agetline(&buf, &size, fp) != -1)
foldline(buf, width); foldline(buf, width);
free(buf); free(buf);
} }
@ -79,18 +80,18 @@ foldline(const char *str, long width)
do { do {
space = false; space = false;
for(j = i, col = 0; str[j] && col <= width; j++) { for (j = i, col = 0; str[j] && col <= width; j++) {
c = str[j]; c = str[j];
if(!UTF8_POINT(c) && !bflag) if (!UTF8_POINT(c) && !bflag)
continue; continue;
if(sflag && isspace(c)) { if (sflag && isspace(c)) {
space = true; space = true;
n = j+1; n = j+1;
} }
else if(!space) else if (!space)
n = j; n = j;
if(!bflag && iscntrl(c)) if (!bflag && iscntrl(c))
switch(c) { switch(c) {
case '\b': case '\b':
col--; col--;
@ -105,9 +106,9 @@ foldline(const char *str, long width)
else else
col++; col++;
} }
if(fwrite(&str[i], 1, n-i, stdout) != n-i) if (fwrite(&str[i], 1, n-i, stdout) != n-i)
eprintf("<stdout>: write error:"); eprintf("<stdout>: write error:");
if(str[n]) if (str[n])
putchar('\n'); putchar('\n');
} while(str[i = n] && str[i] != '\n'); } while (str[i = n] && str[i] != '\n');
} }

39
grep.c
View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -62,41 +63,41 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0 && !eflag) if (argc == 0 && !eflag)
usage(); /* no pattern */ usage(); /* no pattern */
/* If -e is not specified treat it as if it were */ /* If -e is not specified treat it as if it were */
if(!eflag) { if (!eflag) {
addpattern(argv[0]); addpattern(argv[0]);
argc--; argc--;
argv++; argv++;
} }
/* Compile regex for all search patterns */ /* Compile regex for all search patterns */
for(pnode = phead; pnode; pnode = pnode->next) { for (pnode = phead; pnode; pnode = pnode->next) {
if((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) { if ((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
regerror(n, &pnode->preg, buf, sizeof buf); regerror(n, &pnode->preg, buf, sizeof buf);
enprintf(Error, "invalid pattern: %s\n", buf); enprintf(Error, "invalid pattern: %s\n", buf);
} }
} }
many = (argc > 1); many = (argc > 1);
if(argc == 0) { if (argc == 0) {
match = grep(stdin, "<stdin>"); match = grep(stdin, "<stdin>");
} else { } else {
for(i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if(!(fp = fopen(argv[i], "r"))) { if (!(fp = fopen(argv[i], "r"))) {
weprintf("fopen %s:", argv[i]); weprintf("fopen %s:", argv[i]);
match = Error; match = Error;
continue; continue;
} }
m = grep(fp, argv[i]); m = grep(fp, argv[i]);
if(m == Error || (match != Error && m == Match)) if (m == Error || (match != Error && m == Match))
match = m; match = m;
fclose(fp); fclose(fp);
} }
} }
pnode = phead; pnode = phead;
while(pnode) { while (pnode) {
tmp = pnode->next; tmp = pnode->next;
regfree(&pnode->preg); regfree(&pnode->preg);
free(pnode->pattern); free(pnode->pattern);
@ -111,9 +112,9 @@ addpattern(const char *pattern)
{ {
struct plist *pnode; struct plist *pnode;
if(!(pnode = malloc(sizeof(*pnode)))) if (!(pnode = malloc(sizeof(*pnode))))
eprintf("malloc:"); eprintf("malloc:");
if(!(pnode->pattern = strdup(pattern))) if (!(pnode->pattern = strdup(pattern)))
eprintf("strdup:"); eprintf("strdup:");
pnode->next = phead; pnode->next = phead;
phead = pnode; phead = pnode;
@ -128,14 +129,14 @@ grep(FILE *fp, const char *str)
struct plist *pnode; struct plist *pnode;
int match = NoMatch; int match = NoMatch;
for(n = 1; (len = agetline(&buf, &size, fp)) != -1; n++) { for (n = 1; (len = agetline(&buf, &size, fp)) != -1; n++) {
/* Remove the trailing newline if one is present. */ /* Remove the trailing newline if one is present. */
if (len && buf[len - 1] == '\n') if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0'; buf[len - 1] = '\0';
for(pnode = phead; pnode; pnode = pnode->next) { for (pnode = phead; pnode; pnode = pnode->next) {
if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag) if (regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
continue; continue;
switch(mode) { switch (mode) {
case 'c': case 'c':
c++; c++;
break; break;
@ -145,9 +146,9 @@ grep(FILE *fp, const char *str)
case 'q': case 'q':
exit(Match); exit(Match);
default: default:
if(many) if (many)
printf("%s:", str); printf("%s:", str);
if(mode == 'n') if (mode == 'n')
printf("%ld:", n); printf("%ld:", n);
puts(buf); puts(buf);
break; break;
@ -155,10 +156,10 @@ grep(FILE *fp, const char *str)
match = Match; match = Match;
} }
} }
if(mode == 'c') if (mode == 'c')
printf("%ld\n", c); printf("%ld\n", c);
end: end:
if(ferror(fp)) { if (ferror(fp)) {
weprintf("%s: read error:", str); weprintf("%s: read error:", str);
match = Error; match = Error;
} }

13
head.c
View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -31,11 +32,11 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) { if (argc == 0) {
head(stdin, "<stdin>", n); head(stdin, "<stdin>", n);
} else { } else {
for(; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue; continue;
} }
@ -55,12 +56,12 @@ head(FILE *fp, const char *str, long n)
ssize_t len; ssize_t len;
unsigned long i = 0; unsigned long i = 0;
while(i < n && ((len = agetline(&buf, &size, fp)) != -1)) { while (i < n && ((len = agetline(&buf, &size, fp)) != -1)) {
fputs(buf, stdout); fputs(buf, stdout);
if(buf[len - 1] == '\n') if (buf[len - 1] == '\n')
i++; i++;
} }
free(buf); free(buf);
if(ferror(fp)) if (ferror(fp))
eprintf("%s: read error:", str); eprintf("%s: read error:", str);
} }

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void

1
kill.c
View File

@ -9,6 +9,7 @@
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
struct { struct {

3
link.c
View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
int int
@ -8,7 +9,7 @@ main(int argc, char *argv[])
{ {
argv0 = argv[0]; argv0 = argv[0];
if(argc != 3) if (argc != 3)
eprintf("usage: %s target linkname\n", argv0); eprintf("usage: %s target linkname\n", argv0);
if (link(argv[1], argv[2]) < 0) if (link(argv[1], argv[2]) < 0)
eprintf("link:"); eprintf("link:");

1
ln.c
View File

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
int int

109
ls.c
View File

@ -10,6 +10,7 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
typedef struct { typedef struct {
@ -89,15 +90,15 @@ main(int argc, char *argv[])
} ARGEND; } ARGEND;
many = (argc > 1); many = (argc > 1);
if(argc == 0) if (argc == 0)
*--argv = ".", argc++; *--argv = ".", argc++;
if(!(ents = malloc(argc * sizeof *ents))) if (!(ents = malloc(argc * sizeof *ents)))
eprintf("malloc:"); eprintf("malloc:");
for(i = 0; i < argc; i++) for (i = 0; i < argc; i++)
mkent(&ents[i], argv[i], true); mkent(&ents[i], argv[i], true);
qsort(ents, argc, sizeof *ents, entcmp); qsort(ents, argc, sizeof *ents, entcmp);
for(i = 0; i < argc; i++) for (i = 0; i < argc; i++)
ls(&ents[rflag ? argc-i-1 : i]); ls(&ents[rflag ? argc-i-1 : i]);
return 0; return 0;
@ -108,7 +109,7 @@ entcmp(const void *va, const void *vb)
{ {
const Entry *a = va, *b = vb; const Entry *a = va, *b = vb;
if(tflag) if (tflag)
return b->mtime - a->mtime; return b->mtime - a->mtime;
else else
return strcmp(a->name, b->name); return strcmp(a->name, b->name);
@ -117,7 +118,7 @@ entcmp(const void *va, const void *vb)
static void static void
ls(Entry *ent) ls(Entry *ent)
{ {
if(S_ISDIR(ent->mode) && !dflag) { if (S_ISDIR(ent->mode) && !dflag) {
lsdir(ent->name); lsdir(ent->name);
} else { } else {
output(ent); output(ent);
@ -135,42 +136,42 @@ lsdir(const char *path)
size_t sz; size_t sz;
cwd = agetcwd(); cwd = agetcwd();
if(!(dp = opendir(path))) if (!(dp = opendir(path)))
eprintf("opendir %s:", path); eprintf("opendir %s:", path);
if(chdir(path) == -1) if (chdir(path) == -1)
eprintf("chdir %s:", path); eprintf("chdir %s:", path);
if(many) { if (many) {
if(!first) if (!first)
putchar('\n'); putchar('\n');
printf("%s:\n", path); printf("%s:\n", path);
first = false; first = false;
} }
while((d = readdir(dp))) { while ((d = readdir(dp))) {
if(d->d_name[0] == '.' && !aflag) if (d->d_name[0] == '.' && !aflag)
continue; continue;
if(Uflag){ if (Uflag){
mkent(&ent, d->d_name, Fflag || lflag || iflag); mkent(&ent, d->d_name, Fflag || lflag || iflag);
output(&ent); output(&ent);
} else { } else {
if(!(ents = realloc(ents, ++n * sizeof *ents))) if (!(ents = realloc(ents, ++n * sizeof *ents)))
eprintf("realloc:"); eprintf("realloc:");
if(!(p = malloc((sz = strlen(d->d_name)+1)))) if (!(p = malloc((sz = strlen(d->d_name)+1))))
eprintf("malloc:"); eprintf("malloc:");
memcpy(p, d->d_name, sz); memcpy(p, d->d_name, sz);
mkent(&ents[n-1], p, tflag || Fflag || lflag || iflag); mkent(&ents[n-1], p, tflag || Fflag || lflag || iflag);
} }
} }
closedir(dp); closedir(dp);
if(!Uflag){ if (!Uflag){
qsort(ents, n, sizeof *ents, entcmp); qsort(ents, n, sizeof *ents, entcmp);
for(i = 0; i < n; i++) { for (i = 0; i < n; i++) {
output(&ents[rflag ? n-i-1 : i]); output(&ents[rflag ? n-i-1 : i]);
free(ents[rflag ? n-i-1 : i].name); free(ents[rflag ? n-i-1 : i].name);
} }
} }
if(chdir(cwd) == -1) if (chdir(cwd) == -1)
eprintf("chdir %s:", cwd); eprintf("chdir %s:", cwd);
free(ents); free(ents);
free(cwd); free(cwd);
@ -182,9 +183,9 @@ mkent(Entry *ent, char *path, bool dostat)
struct stat st; struct stat st;
ent->name = path; ent->name = path;
if(!dostat) if (!dostat)
return; return;
if(lstat(path, &st) == -1) if (lstat(path, &st) == -1)
eprintf("lstat %s:", path); eprintf("lstat %s:", path);
ent->mode = st.st_mode; ent->mode = st.st_mode;
ent->nlink = st.st_nlink; ent->nlink = st.st_nlink;
@ -198,20 +199,20 @@ mkent(Entry *ent, char *path, bool dostat)
static char * static char *
indicator(mode_t mode) indicator(mode_t mode)
{ {
if(!Fflag) if (!Fflag)
return ""; return "";
if(S_ISLNK(mode)) if (S_ISLNK(mode))
return "@"; return "@";
else if(S_ISDIR(mode)) else if (S_ISDIR(mode))
return "/"; return "/";
else if(S_ISFIFO(mode)) else if (S_ISFIFO(mode))
return "|"; return "|";
else if(S_ISSOCK(mode)) else if (S_ISSOCK(mode))
return "="; return "=";
else if(mode & S_IXUSR || else if (mode & S_IXUSR ||
mode & S_IXGRP || mode & S_IXGRP ||
mode & S_IXOTH) mode & S_IXOTH)
return "*"; return "*";
else else
return ""; return "";
@ -231,69 +232,69 @@ output(Entry *ent)
if (iflag) if (iflag)
printf("%lu ", (unsigned long)ent->ino); printf("%lu ", (unsigned long)ent->ino);
if(!lflag) { if (!lflag) {
printf("%s%s\n", ent->name, indicator(ent->mode)); printf("%s%s\n", ent->name, indicator(ent->mode));
return; return;
} }
if(S_ISREG(ent->mode)) if (S_ISREG(ent->mode))
mode[0] = '-'; mode[0] = '-';
else if(S_ISBLK(ent->mode)) else if (S_ISBLK(ent->mode))
mode[0] = 'b'; mode[0] = 'b';
else if(S_ISCHR(ent->mode)) else if (S_ISCHR(ent->mode))
mode[0] = 'c'; mode[0] = 'c';
else if(S_ISDIR(ent->mode)) else if (S_ISDIR(ent->mode))
mode[0] = 'd'; mode[0] = 'd';
else if(S_ISFIFO(ent->mode)) else if (S_ISFIFO(ent->mode))
mode[0] = 'p'; mode[0] = 'p';
else if(S_ISLNK(ent->mode)) else if (S_ISLNK(ent->mode))
mode[0] = 'l'; mode[0] = 'l';
else if(S_ISSOCK(ent->mode)) else if (S_ISSOCK(ent->mode))
mode[0] = 's'; mode[0] = 's';
else else
mode[0] = '?'; mode[0] = '?';
if(ent->mode & S_IRUSR) mode[1] = 'r'; if (ent->mode & S_IRUSR) mode[1] = 'r';
if(ent->mode & S_IWUSR) mode[2] = 'w'; if (ent->mode & S_IWUSR) mode[2] = 'w';
if(ent->mode & S_IXUSR) mode[3] = 'x'; if (ent->mode & S_IXUSR) mode[3] = 'x';
if(ent->mode & S_IRGRP) mode[4] = 'r'; if (ent->mode & S_IRGRP) mode[4] = 'r';
if(ent->mode & S_IWGRP) mode[5] = 'w'; if (ent->mode & S_IWGRP) mode[5] = 'w';
if(ent->mode & S_IXGRP) mode[6] = 'x'; if (ent->mode & S_IXGRP) mode[6] = 'x';
if(ent->mode & S_IROTH) mode[7] = 'r'; if (ent->mode & S_IROTH) mode[7] = 'r';
if(ent->mode & S_IWOTH) mode[8] = 'w'; if (ent->mode & S_IWOTH) mode[8] = 'w';
if(ent->mode & S_IXOTH) mode[9] = 'x'; if (ent->mode & S_IXOTH) mode[9] = 'x';
if(ent->mode & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S'; if (ent->mode & S_ISUID) mode[3] = (mode[3] == 'x') ? 's' : 'S';
if(ent->mode & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S'; if (ent->mode & S_ISGID) mode[6] = (mode[6] == 'x') ? 's' : 'S';
if(ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T'; if (ent->mode & S_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
errno = 0; errno = 0;
pw = getpwuid(ent->uid); pw = getpwuid(ent->uid);
if(errno || !pw) if (errno || !pw)
snprintf(pwname, sizeof(pwname), "%d", ent->uid); snprintf(pwname, sizeof(pwname), "%d", ent->uid);
else else
snprintf(pwname, sizeof(pwname), "%s", pw->pw_name); snprintf(pwname, sizeof(pwname), "%s", pw->pw_name);
errno = 0; errno = 0;
gr = getgrgid(ent->gid); gr = getgrgid(ent->gid);
if(errno || !gr) if (errno || !gr)
snprintf(grname, sizeof(grname), "%d", ent->gid); snprintf(grname, sizeof(grname), "%d", ent->gid);
else else
snprintf(grname, sizeof(grname), "%s", gr->gr_name); snprintf(grname, sizeof(grname), "%s", gr->gr_name);
if(time(NULL) > ent->mtime + (180*24*60*60)) /* 6 months ago? */ if (time(NULL) > ent->mtime + (180*24*60*60)) /* 6 months ago? */
fmt = "%b %d %Y"; fmt = "%b %d %Y";
else else
fmt = "%b %d %H:%M"; fmt = "%b %d %H:%M";
strftime(buf, sizeof buf, fmt, localtime(&ent->mtime)); strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname); printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
if(hflag) if (hflag)
printf("%10s ", humansize((unsigned long)ent->size)); printf("%10s ", humansize((unsigned long)ent->size));
else else
printf("%10lu ", (unsigned long)ent->size); printf("%10lu ", (unsigned long)ent->size);
printf("%s %s%s", buf, ent->name, indicator(ent->mode)); printf("%s %s%s", buf, ent->name, indicator(ent->mode));
if(S_ISLNK(ent->mode)) { if (S_ISLNK(ent->mode)) {
if((len = readlink(ent->name, buf, sizeof buf)) == -1) if ((len = readlink(ent->name, buf, sizeof buf)) == -1)
eprintf("readlink %s:", ent->name); eprintf("readlink %s:", ent->name);
buf[len] = '\0'; buf[len] = '\0';
mkent(&entlnk, buf, Fflag); mkent(&entlnk, buf, Fflag);

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "util.h" #include "util.h"
#include "crypt.h" #include "crypt.h"
#include "md5.h" #include "md5.h"
@ -37,7 +38,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(cflag) if (cflag)
return cryptcheck(checkfile, argc, argv, &md5_ops, md, sizeof(md)); return cryptcheck(checkfile, argc, argv, &md5_ops, md, sizeof(md));
return cryptmain(argc, argv, &md5_ops, md, sizeof(md)); return cryptmain(argc, argv, &md5_ops, md, sizeof(md));
} }

15
mkdir.c
View File

@ -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 "util.h" #include "util.h"
static void mkdirp(char *); static void mkdirp(char *);
@ -39,10 +40,10 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
for(; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if(pflag) { if (pflag) {
mkdirp(argv[0]); mkdirp(argv[0]);
} else if(mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) == -1) { } else if (mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) == -1) {
eprintf("mkdir %s:", argv[0]); eprintf("mkdir %s:", argv[0]);
} }
if (mflag) if (mflag)
@ -59,11 +60,11 @@ mkdirp(char *path)
char *p = path; char *p = path;
do { do {
if(*p && (p = strchr(&p[1], '/'))) if (*p && (p = strchr(&p[1], '/')))
*p = '\0'; *p = '\0';
if(mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) == -1 && errno != EEXIST) if (mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) == -1 && errno != EEXIST)
eprintf("mkdir %s:", path); eprintf("mkdir %s:", path);
if(p) if (p)
*p = '/'; *p = '/';
} while(p); } while (p);
} }

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
static void static void
@ -28,8 +29,8 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
for(; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
if(mkfifo(argv[0], mode) == -1) if (mkfifo(argv[0], mode) == -1)
eprintf("mkfifo %s:", argv[0]); eprintf("mkfifo %s:", argv[0]);
return 0; return 0;
} }

View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void

3
mv.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "fs.h" #include "fs.h"
#include "util.h" #include "util.h"
@ -30,7 +31,7 @@ main(int argc, char *argv[])
if (argc < 2) if (argc < 2)
usage(); usage();
if(argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode))) if (argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
eprintf("%s: not a directory\n", argv[argc-1]); eprintf("%s: not a directory\n", argv[argc-1]);
enmasse(argc, &argv[0], mv); enmasse(argc, &argv[0], mv);

3
nice.c
View File

@ -6,6 +6,7 @@
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -29,7 +30,7 @@ main(int argc, char *argv[])
break; break;
} ARGEND; } ARGEND;
if(argc == 0) if (argc == 0)
usage(); usage();
errno = 0; errno = 0;

21
nl.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -31,9 +32,9 @@ main(int argc, char *argv[])
case 'b': case 'b':
r = EARGF(usage()); r = EARGF(usage());
mode = r[0]; mode = r[0];
if(r[0] == 'p') { if (r[0] == 'p') {
regcomp(&preg, &r[1], REG_NOSUB); regcomp(&preg, &r[1], REG_NOSUB);
} else if(!strchr("ant", mode)) { } else if (!strchr("ant", mode)) {
usage(); usage();
} }
break; break;
@ -47,10 +48,10 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) { if (argc == 0) {
nl(stdin); nl(stdin);
} else for(; argc > 0; argc--, argv++) { } else for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue; continue;
} }
@ -68,11 +69,11 @@ nl(FILE *fp)
long n = 0; long n = 0;
size_t size = 0; size_t size = 0;
while(agetline(&buf, &size, fp) != -1) { while (agetline(&buf, &size, fp) != -1) {
if((mode == 'a') if ((mode == 'a')
|| (mode == 'p' || (mode == 'p'
&& !regexec(&preg, buf, 0, NULL, 0)) && !regexec(&preg, buf, 0, NULL, 0))
|| (mode == 't' && buf[0] != '\n')) { || (mode == 't' && buf[0] != '\n')) {
printf("%6ld%s%s", n += incr, sep, buf); printf("%6ld%s%s", n += incr, sep, buf);
} else { } else {
printf(" %s", buf); printf(" %s", buf);

17
nohup.c
View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
enum { Error = 127, Found = 126 }; enum { Error = 127, Found = 126 };
@ -25,23 +26,23 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) if (argc == 0)
usage(); usage();
if(signal(SIGHUP, SIG_IGN) == SIG_ERR) if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
enprintf(Error, "signal HUP:"); enprintf(Error, "signal HUP:");
if(isatty(STDOUT_FILENO)) { if (isatty(STDOUT_FILENO)) {
if((fd = open("nohup.out", O_APPEND|O_CREAT, if ((fd = open("nohup.out", O_APPEND|O_CREAT,
S_IRUSR|S_IWUSR)) == -1) { S_IRUSR|S_IWUSR)) == -1) {
enprintf(Error, "open nohup.out:"); enprintf(Error, "open nohup.out:");
} }
if(dup2(fd, STDOUT_FILENO) == -1) if (dup2(fd, STDOUT_FILENO) == -1)
enprintf(Error, "dup2:"); enprintf(Error, "dup2:");
close(fd); close(fd);
} }
if(isatty(STDERR_FILENO)) if (isatty(STDERR_FILENO))
if(dup2(STDOUT_FILENO, STDERR_FILENO) == -1) if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
enprintf(Error, "dup2:"); enprintf(Error, "dup2:");
execvp(argv[0], &argv[0]); execvp(argv[0], &argv[0]);

65
paste.c
View File

@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <wchar.h> #include <wchar.h>
#include "util.h" #include "util.h"
typedef struct { typedef struct {
@ -48,48 +49,48 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) if (argc == 0)
usage(); usage();
/* populate delimeters */ /* populate delimeters */
if(!adelim) if (!adelim)
adelim = "\t"; adelim = "\t";
len = mbstowcs(NULL, adelim, 0); len = mbstowcs(NULL, adelim, 0);
if(len == (size_t)-1) if (len == (size_t) - 1)
eprintf("invalid delimiter\n"); eprintf("invalid delimiter\n");
if(!(delim = malloc((len + 1) * sizeof(*delim)))) if (!(delim = malloc((len + 1) * sizeof(*delim))))
eprintf("out of memory\n"); eprintf("out of memory\n");
mbstowcs(delim, adelim, len); mbstowcs(delim, adelim, len);
len = unescape(delim); len = unescape(delim);
if(len == 0) if (len == 0)
eprintf("no delimiters specified\n"); eprintf("no delimiters specified\n");
/* populate file list */ /* populate file list */
if(!(dsc = malloc(argc * sizeof(*dsc)))) if (!(dsc = malloc(argc * sizeof(*dsc))))
eprintf("out of memory\n"); eprintf("out of memory\n");
for(i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if(strcmp(argv[i], "-") == 0) if (strcmp(argv[i], "-") == 0)
dsc[i].fp = stdin; dsc[i].fp = stdin;
else else
dsc[i].fp = fopen(argv[i], "r"); dsc[i].fp = fopen(argv[i], "r");
if(!dsc[i].fp) if (!dsc[i].fp)
eprintf("can't open '%s':", argv[i]); eprintf("can't open '%s':", argv[i]);
dsc[i].name = argv[i]; dsc[i].name = argv[i];
} }
if(seq) if (seq)
sequential(dsc, argc, delim, len); sequential(dsc, argc, delim, len);
else else
parallel(dsc, argc, delim, len); parallel(dsc, argc, delim, len);
for(i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if(dsc[i].fp != stdin) if (dsc[i].fp != stdin)
(void)fclose(dsc[i].fp); (void)fclose(dsc[i].fp);
} }
@ -106,9 +107,9 @@ unescape(wchar_t *delim)
size_t i; size_t i;
size_t len; size_t len;
for(i = 0, len = 0; (c = delim[i++]) != '\0'; len++) { for (i = 0, len = 0; (c = delim[i++]) != '\0'; len++) {
if(c == '\\') { if (c == '\\') {
switch(delim[i++]) { switch (delim[i++]) {
case 'n': case 'n':
delim[len] = '\n'; delim[len] = '\n';
break; break;
@ -138,7 +139,7 @@ in(Fdescr *f)
{ {
wint_t c = fgetwc(f->fp); wint_t c = fgetwc(f->fp);
if(c == WEOF && ferror(f->fp)) if (c == WEOF && ferror(f->fp))
eprintf("'%s' read error:", f->name); eprintf("'%s' read error:", f->name);
return c; return c;
@ -148,7 +149,7 @@ static void
out(wchar_t c) out(wchar_t c)
{ {
putwchar(c); putwchar(c);
if(ferror(stdout)) if (ferror(stdout))
eprintf("write error:"); eprintf("write error:");
} }
@ -159,26 +160,26 @@ sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
size_t d; size_t d;
wint_t c, last; wint_t c, last;
for(i = 0; i < len; i++) { for (i = 0; i < len; i++) {
d = 0; d = 0;
last = WEOF; last = WEOF;
while((c = in(&dsc[i])) != WEOF) { while ((c = in(&dsc[i])) != WEOF) {
if(last == '\n') { if (last == '\n') {
if(delim[d] != '\0') if (delim[d] != '\0')
out(delim[d]); out(delim[d]);
d++; d++;
d %= cnt; d %= cnt;
} }
if(c != '\n') if (c != '\n')
out((wchar_t)c); out((wchar_t)c);
last = c; last = c;
} }
if(last == '\n') if (last == '\n')
out((wchar_t)last); out((wchar_t)last);
} }
} }
@ -192,36 +193,36 @@ parallel(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
do { do {
last = 0; last = 0;
for(i = 0; i < len; i++) { for (i = 0; i < len; i++) {
d = delim[i % cnt]; d = delim[i % cnt];
do { do {
o = in(&dsc[i]); o = in(&dsc[i]);
c = o; c = o;
switch(c) { switch (c) {
case WEOF: case WEOF:
if(last == 0) if (last == 0)
break; break;
o = '\n'; o = '\n';
/* fallthrough */ /* fallthrough */
case '\n': case '\n':
if(i != len - 1) if (i != len - 1)
o = d; o = d;
break; break;
default: default:
break; break;
} }
if(o != WEOF) { if (o != WEOF) {
/* pad with delimiters up to this point */ /* pad with delimiters up to this point */
while(++last < i) { while (++last < i) {
if(d != '\0') if (d != '\0')
out(d); out(d);
} }
out((wchar_t)o); out((wchar_t)o);
} }
} while(c != '\n' && c != WEOF); } while (c != '\n' && c != WEOF);
} }
} while(last > 0); } while (last > 0);
} }

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
extern char **environ; extern char **environ;
@ -21,14 +22,14 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 1) { if (argc == 1) {
while(*environ) while (*environ)
printf("%s\n", *environ++); printf("%s\n", *environ++);
return 0; return 0;
} }
while(*++argv) { while(*++argv) {
if((var = getenv(*argv))) if ((var = getenv(*argv)))
printf("%s\n", var); printf("%s\n", var);
} }

7
pwd.c
View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
static const char *getpwd(const char *cwd); static const char *getpwd(const char *cwd);
@ -40,11 +41,11 @@ getpwd(const char *cwd)
const char *pwd; const char *pwd;
struct stat cst, pst; struct stat cst, pst;
if(!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) == -1) if (!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) == -1)
return cwd; return cwd;
if(stat(cwd, &cst) == -1) if (stat(cwd, &cst) == -1)
eprintf("stat %s:", cwd); eprintf("stat %s:", cwd);
if(pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino) if (pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino)
return pwd; return pwd;
else else
return cwd; return cwd;

View File

@ -6,6 +6,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"
static void static void

View File

@ -7,6 +7,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"
static int strtop(const char *); static int strtop(const char *);
@ -43,31 +44,31 @@ main(int argc, char *argv[])
break; break;
} ARGEND; } ARGEND;
if(argc == 0 || !adj) if (argc == 0 || !adj)
usage(); usage();
val = estrtol(adj, 10); val = estrtol(adj, 10);
for(i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
int who = -1; int who = -1;
if(which == PRIO_USER) { if (which == PRIO_USER) {
const struct passwd *pwd; const struct passwd *pwd;
errno = 0; errno = 0;
do pwd = getpwnam(argv[i]); while(errno == EINTR); do pwd = getpwnam(argv[i]); while (errno == EINTR);
if(pwd) if (pwd)
who = pwd->pw_uid; who = pwd->pw_uid;
else if(errno != 0) { else if (errno != 0) {
perror("can't read passwd"); perror("can't read passwd");
status = 1; status = 1;
continue; continue;
} }
} }
if(who < 0) if (who < 0)
who = strtop(argv[i]); who = strtop(argv[i]);
if(who < 0 || !renice(which, who, val)) if (who < 0 || !renice(which, who, val))
status = 1; status = 1;
} }
@ -82,11 +83,11 @@ strtop(const char *s)
errno = 0; errno = 0;
n = strtol(s, &end, 10); n = strtol(s, &end, 10);
if(*end != '\0') { if (*end != '\0') {
fprintf(stderr, "%s: not an integer\n", s); fprintf(stderr, "%s: not an integer\n", s);
return -1; return -1;
} }
if(errno != 0 || n <= 0 || n > INT_MAX) { if (errno != 0 || n <= 0 || n > INT_MAX) {
fprintf(stderr, "%s: invalid value\n", s); fprintf(stderr, "%s: invalid value\n", s);
return -1; return -1;
} }
@ -99,14 +100,14 @@ renice(int which, int who, long adj)
{ {
errno = 0; errno = 0;
adj += getpriority(which, who); adj += getpriority(which, who);
if(errno != 0) { if (errno != 0) {
fprintf(stderr, "can't get %d nice level: %s\n", fprintf(stderr, "can't get %d nice level: %s\n",
who, strerror(errno)); who, strerror(errno));
return false; return false;
} }
adj = MAX(-NZERO, MIN(adj, NZERO - 1)); adj = MAX(-NZERO, MIN(adj, NZERO - 1));
if(setpriority(which, who, (int)adj) == -1) { if (setpriority(which, who, (int)adj) == -1) {
fprintf(stderr, "can't set %d nice level: %s\n", fprintf(stderr, "can't set %d nice level: %s\n",
who, strerror(errno)); who, strerror(errno));
return false; return false;

3
rm.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "fs.h" #include "fs.h"
#include "util.h" #include "util.h"
@ -35,7 +36,7 @@ main(int argc, char *argv[])
return 0; return 0;
} }
for(; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
rm(argv[0]); rm(argv[0]);
return 0; return 0;

View File

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include "util.h" #include "util.h"
static void static void
@ -23,8 +24,8 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
for(; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
if(rmdir(argv[0]) == -1) if (rmdir(argv[0]) == -1)
fprintf(stderr, "rmdir: '%s': %s\n", fprintf(stderr, "rmdir: '%s': %s\n",
argv[0], strerror(errno)); argv[0], strerror(errno));

27
seq.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static int digitsleft(const char *); static int digitsleft(const char *);
@ -42,7 +43,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
switch(argc) { switch (argc) {
case 3: case 3:
steps = argv[1]; steps = argv[1];
argv[1] = argv[2]; argv[1] = argv[2];
@ -62,15 +63,15 @@ main(int argc, char *argv[])
end = estrtod(ends); end = estrtod(ends);
dir = (step > 0) ? 1.0 : -1.0; dir = (step > 0) ? 1.0 : -1.0;
if(step == 0 || start * dir > end * dir) if (step == 0 || start * dir > end * dir)
return 1; return 1;
if(fmt == ftmp) { if (fmt == ftmp) {
right = MAX(digitsright(starts), right = MAX(digitsright(starts),
MAX(digitsright(ends), MAX(digitsright(ends),
digitsright(steps))); digitsright(steps)));
if(wflag) { if (wflag) {
left = MAX(digitsleft(starts), digitsleft(ends)); left = MAX(digitsleft(starts), digitsleft(ends));
snprintf(ftmp, sizeof ftmp, "%%0%d.%df", snprintf(ftmp, sizeof ftmp, "%%0%d.%df",
@ -78,8 +79,8 @@ main(int argc, char *argv[])
} else } else
snprintf(ftmp, sizeof ftmp, "%%.%df", right); snprintf(ftmp, sizeof ftmp, "%%.%df", right);
} }
for(out = start; out * dir <= end * dir; out += step) { for (out = start; out * dir <= end * dir; out += step) {
if(out != start) if (out != start)
fputs(sep, stdout); fputs(sep, stdout);
printf(fmt, out); printf(fmt, out);
} }
@ -94,7 +95,7 @@ digitsleft(const char *d)
char *exp; char *exp;
int shift; int shift;
if(*d == '+') if (*d == '+')
d++; d++;
exp = strpbrk(d, "eE"); exp = strpbrk(d, "eE");
shift = exp ? estrtol(&exp[1], 10) : 0; shift = exp ? estrtol(&exp[1], 10) : 0;
@ -121,26 +122,26 @@ validfmt(const char *fmt)
int occur = 0; int occur = 0;
literal: literal:
while(*fmt) while (*fmt)
if(*fmt++ == '%') if (*fmt++ == '%')
goto format; goto format;
return occur == 1; return occur == 1;
format: format:
if(*fmt == '%') { if (*fmt == '%') {
fmt++; fmt++;
goto literal; goto literal;
} }
fmt += strspn(fmt, "-+#0 '"); fmt += strspn(fmt, "-+#0 '");
fmt += strspn(fmt, "0123456789"); fmt += strspn(fmt, "0123456789");
if(*fmt == '.') { if (*fmt == '.') {
fmt++; fmt++;
fmt += strspn(fmt, "0123456789"); fmt += strspn(fmt, "0123456789");
} }
if(*fmt == 'L') if (*fmt == 'L')
fmt++; fmt++;
switch(*fmt) { switch (*fmt) {
case 'f': case 'F': case 'f': case 'F':
case 'g': case 'G': case 'g': case 'G':
case 'e': case 'E': case 'e': case 'E':

View File

@ -4,6 +4,7 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "util.h" #include "util.h"
static void static void
@ -25,8 +26,8 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
if(getpgrp() == getpid()) { if (getpgrp() == getpid()) {
switch(fork()){ switch (fork()) {
case -1: case -1:
eprintf("fork:"); eprintf("fork:");
case 0: case 0:
@ -35,7 +36,7 @@ main(int argc, char *argv[])
return 0; return 0;
} }
} }
if(setsid() < 0) if (setsid() < 0)
eprintf("setsid:"); eprintf("setsid:");
execvp(argv[0], argv); execvp(argv[0], argv);
savederrno = errno; savederrno = errno;

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "util.h" #include "util.h"
#include "crypt.h" #include "crypt.h"
#include "sha1.h" #include "sha1.h"
@ -37,7 +38,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(cflag) if (cflag)
return cryptcheck(checkfile, argc, argv, &sha1_ops, md, sizeof(md)); return cryptcheck(checkfile, argc, argv, &sha1_ops, md, sizeof(md));
return cryptmain(argc, argv, &sha1_ops, md, sizeof(md)); return cryptmain(argc, argv, &sha1_ops, md, sizeof(md));
} }

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "util.h" #include "util.h"
#include "crypt.h" #include "crypt.h"
#include "sha256.h" #include "sha256.h"
@ -37,7 +38,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(cflag) if (cflag)
return cryptcheck(checkfile, argc, argv, &sha256_ops, md, sizeof(md)); return cryptcheck(checkfile, argc, argv, &sha256_ops, md, sizeof(md));
return cryptmain(argc, argv, &sha256_ops, md, sizeof(md)); return cryptmain(argc, argv, &sha256_ops, md, sizeof(md));
} }

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "util.h" #include "util.h"
#include "crypt.h" #include "crypt.h"
#include "sha512.h" #include "sha512.h"
@ -37,7 +38,7 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(cflag) if (cflag)
return cryptcheck(checkfile, argc, argv, &sha512_ops, md, sizeof(md)); return cryptcheck(checkfile, argc, argv, &sha512_ops, md, sizeof(md));
return cryptmain(argc, argv, &sha512_ops, md, sizeof(md)); return cryptmain(argc, argv, &sha512_ops, md, sizeof(md));
} }

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -19,11 +20,11 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc < 1) if (argc < 1)
usage(); usage();
seconds = estrtol(argv[0], 0); seconds = estrtol(argv[0], 0);
while((seconds = sleep(seconds)) > 0) while ((seconds = sleep(seconds)) > 0)
; ;
return 0; return 0;
} }

79
sort.c
View File

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -20,7 +21,7 @@ enum {
MOD_N = 1 << 1, MOD_N = 1 << 1,
MOD_STARTB = 1 << 2, MOD_STARTB = 1 << 2,
MOD_ENDB = 1 << 3, MOD_ENDB = 1 << 3,
MOD_R = 1 << 4 MOD_R = 1 << 4,
}; };
struct kdlist { struct kdlist {
@ -72,7 +73,7 @@ main(int argc, char *argv[])
break; break;
case 't': case 't':
fieldsep = EARGF(usage()); fieldsep = EARGF(usage());
if(strlen(fieldsep) != 1) if (strlen(fieldsep) != 1)
usage(); usage();
break; break;
case 'u': case 'u':
@ -82,14 +83,14 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(!head && global_flags) if (!head && global_flags)
addkeydef("1", global_flags); addkeydef("1", global_flags);
addkeydef("1", global_flags & MOD_R); addkeydef("1", global_flags & MOD_R);
if(argc == 0) { if (argc == 0) {
getlines(stdin, &linebuf); getlines(stdin, &linebuf);
} else for(; argc > 0; argc--, argv++) { } else for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
enprintf(2, "fopen %s:", argv[0]); enprintf(2, "fopen %s:", argv[0]);
continue; continue;
} }
@ -99,8 +100,8 @@ main(int argc, char *argv[])
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines,
(int (*)(const void *, const void *))linecmp); (int (*)(const void *, const void *))linecmp);
for(i = 0; i < linebuf.nlines; i++) { for (i = 0; i < linebuf.nlines; i++) {
if(!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i], if (!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i],
(const char **)&linebuf.lines[i-1])) { (const char **)&linebuf.lines[i-1])) {
fputs(linebuf.lines[i], stdout); fputs(linebuf.lines[i], stdout);
} }
@ -116,13 +117,13 @@ addkeydef(char *def, int flags)
struct kdlist *node; struct kdlist *node;
node = malloc(sizeof(*node)); node = malloc(sizeof(*node));
if(!node) if (!node)
enprintf(2, "malloc:"); enprintf(2, "malloc:");
if(!head) if (!head)
head = node; head = node;
if(parse_keydef(&node->keydef, def, flags)) if (parse_keydef(&node->keydef, def, flags))
enprintf(2, "faulty key definition\n"); enprintf(2, "faulty key definition\n");
if(tail) if (tail)
tail->next = node; tail->next = node;
node->next = NULL; node->next = NULL;
tail = node; tail = node;
@ -134,7 +135,7 @@ freelist(void)
struct kdlist *node; struct kdlist *node;
struct kdlist *tmp; struct kdlist *tmp;
for(node = head; node; node = tmp) { for (node = head; node; node = tmp) {
tmp = node->next; tmp = node->next;
free(node); free(node);
} }
@ -147,20 +148,20 @@ linecmp(const char **a, const char **b)
int res = 0; int res = 0;
struct kdlist *node; struct kdlist *node;
for(node = head; node && res == 0; node = node->next) { for (node = head; node && res == 0; node = node->next) {
s1 = columns((char *)*a, &node->keydef); s1 = columns((char *)*a, &node->keydef);
s2 = columns((char *)*b, &node->keydef); s2 = columns((char *)*b, &node->keydef);
/* if -u is given, don't use default key definition /* if -u is given, don't use default key definition
* unless it is the only one */ * unless it is the only one */
if(uflag && node == tail && head != tail) if (uflag && node == tail && head != tail)
res = 0; res = 0;
else if(node->keydef.flags & MOD_N) else if (node->keydef.flags & MOD_N)
res = strtol(s1, 0, 10) - strtol(s2, 0, 10); res = strtol(s1, 0, 10) - strtol(s2, 0, 10);
else else
res = strcmp(s1, s2); res = strcmp(s1, s2);
if(node->keydef.flags & MOD_R) if (node->keydef.flags & MOD_R)
res = -res; res = -res;
free(s1); free(s1);
@ -172,8 +173,8 @@ linecmp(const char **a, const char **b)
static int static int
parse_flags(char **s, int *flags, int bflag) parse_flags(char **s, int *flags, int bflag)
{ {
while(isalpha((int)**s)) while (isalpha((int)**s))
switch(*((*s)++)) { switch (*((*s)++)) {
case 'b': case 'b':
*flags |= bflag; *flags |= bflag;
break; break;
@ -202,27 +203,27 @@ parse_keydef(struct keydef *kd, char *s, int flags)
kd->flags = flags; kd->flags = flags;
kd->start_column = strtol(rest, &rest, 10); kd->start_column = strtol(rest, &rest, 10);
if(kd->start_column < 1) if (kd->start_column < 1)
return -1; return -1;
if(*rest == '.') if (*rest == '.')
kd->start_char = strtol(rest+1, &rest, 10); kd->start_char = strtol(rest+1, &rest, 10);
if(kd->start_char < 1) if (kd->start_char < 1)
return -1; return -1;
if(parse_flags(&rest, &kd->flags, MOD_STARTB) == -1) if (parse_flags(&rest, &kd->flags, MOD_STARTB) == -1)
return -1; return -1;
if(*rest == ',') { if (*rest == ',') {
kd->end_column = strtol(rest+1, &rest, 10); kd->end_column = strtol(rest+1, &rest, 10);
if(kd->end_column && kd->end_column < kd->start_column) if (kd->end_column && kd->end_column < kd->start_column)
return -1; return -1;
if(*rest == '.') { if (*rest == '.') {
kd->end_char = strtol(rest+1, &rest, 10); kd->end_char = strtol(rest+1, &rest, 10);
if(kd->end_char < 1) if (kd->end_char < 1)
return -1; return -1;
} }
if(parse_flags(&rest, &kd->flags, MOD_ENDB) == -1) if (parse_flags(&rest, &kd->flags, MOD_ENDB) == -1)
return -1; return -1;
} }
if(*rest != '\0') if (*rest != '\0')
return -1; return -1;
return 0; return 0;
} }
@ -238,12 +239,12 @@ skipblank(char *s)
static char * static char *
nextcol(char *s) nextcol(char *s)
{ {
if(fieldsep == NULL) { if (fieldsep == NULL) {
s = skipblank(s); s = skipblank(s);
while(*s && !isblank(*s)) while(*s && !isblank(*s))
s++; s++;
} else { } else {
if(strchr(s, *fieldsep) == NULL) if (strchr(s, *fieldsep) == NULL)
s = strchr(s, '\0'); s = strchr(s, '\0');
else else
s = strchr(s, *fieldsep) + 1; s = strchr(s, *fieldsep) + 1;
@ -258,27 +259,27 @@ columns(char *line, const struct keydef *kd)
char *res; char *res;
int i; int i;
for(i = 1, start = line; i < kd->start_column; i++) for (i = 1, start = line; i < kd->start_column; i++)
start = nextcol(start); start = nextcol(start);
if(kd->flags & MOD_STARTB) if (kd->flags & MOD_STARTB)
start = skipblank(start); start = skipblank(start);
start += MIN(kd->start_char, nextcol(start) - start) - 1; start += MIN(kd->start_char, nextcol(start) - start) - 1;
if(kd->end_column) { if (kd->end_column) {
for(i = 1, end = line; i < kd->end_column; i++) for (i = 1, end = line; i < kd->end_column; i++)
end = nextcol(end); end = nextcol(end);
if(kd->flags & MOD_ENDB) if (kd->flags & MOD_ENDB)
end = skipblank(end); end = skipblank(end);
if(kd->end_char) if (kd->end_char)
end += MIN(kd->end_char, nextcol(end) - end); end += MIN(kd->end_char, nextcol(end) - end);
else else
end = nextcol(end); end = nextcol(end);
} else { } else {
if((end = strchr(line, '\n')) == NULL) if ((end = strchr(line, '\n')) == NULL)
end = strchr(line, '\0'); end = strchr(line, '\0');
} }
if((res = strndup(start, end - start)) == NULL) if ((res = strndup(start, end - start)) == NULL)
enprintf(2, "strndup:"); enprintf(2, "strndup:");
return res; return res;
} }

39
split.c
View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h> #include <limits.h>
#include "util.h" #include "util.h"
static int itostr(char *, int, int); static int itostr(char *, int, int);
@ -35,13 +36,13 @@ main(int argc, char *argv[])
case 'b': case 'b':
always = 1; always = 1;
tmp = ARGF(); tmp = ARGF();
if(tmp == NULL) if (tmp == NULL)
break; break;
size = strtoull(tmp, &end, 10); size = strtoull(tmp, &end, 10);
if(*end == '\0') if (*end == '\0')
break; break;
switch(toupper((int)*end)) { switch (toupper((int)*end)) {
case 'K': case 'K':
scale = 1024; scale = 1024;
break; break;
@ -54,14 +55,14 @@ main(int argc, char *argv[])
default: default:
usage(); usage();
} }
if(size > (UINT64_MAX/scale)) if (size > (UINT64_MAX/scale))
eprintf("'%s': out of range\n", tmp); eprintf("'%s': out of range\n", tmp);
size *= scale; size *= scale;
break; break;
case 'l': case 'l':
always = 0; always = 0;
tmp = ARGF(); tmp = ARGF();
if(tmp) if (tmp)
size = estrtol(tmp, 10); size = estrtol(tmp, 10);
break; break;
case 'a': case 'a':
@ -75,31 +76,31 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(*argv) if (*argv)
file = *argv++; file = *argv++;
if(*argv) if (*argv)
prefix = *argv++; prefix = *argv++;
if(*argv) if (*argv)
usage(); usage();
plen = strlen(prefix); plen = strlen(prefix);
if(plen+slen > NAME_MAX) if (plen+slen > NAME_MAX)
eprintf("names cannot exceed %d bytes\n", NAME_MAX); eprintf("names cannot exceed %d bytes\n", NAME_MAX);
strlcpy(name, prefix, sizeof(name)); strlcpy(name, prefix, sizeof(name));
if(file && strcmp(file, "-") != 0) { if (file && strcmp(file, "-") != 0) {
in = fopen(file, "r"); in = fopen(file, "r");
if(!in) if (!in)
eprintf("'%s':", file); eprintf("'%s':", file);
} }
Nextfile: Nextfile:
while((out = nextfile(out, name, plen, slen))) { while ((out = nextfile(out, name, plen, slen))) {
n = 0; n = 0;
while((ch = getc(in)) != EOF) { while ((ch = getc(in)) != EOF) {
putc(ch, out); putc(ch, out);
n += (always || ch == '\n'); n += (always || ch == '\n');
if(n >= size) if (n >= size)
goto Nextfile; goto Nextfile;
} }
fclose(out); fclose(out);
@ -112,11 +113,11 @@ int
itostr(char *str, int x, int n) itostr(char *str, int x, int n)
{ {
str[n] = '\0'; str[n] = '\0';
while(n-- > 0) { while (n-- > 0) {
str[n] = start + (x % base); str[n] = start + (x % base);
x /= base; x /= base;
} }
if(x) if (x)
return -1; return -1;
return 0; return 0;
} }
@ -127,14 +128,14 @@ nextfile(FILE *f, char *buf, int plen, int slen)
static int n = 0; static int n = 0;
int s; int s;
if(f) if (f)
fclose(f); fclose(f);
s = itostr(buf+plen, n++, slen); s = itostr(buf+plen, n++, slen);
if(s == -1) if (s == -1)
return NULL; return NULL;
f = fopen(buf, "w"); f = fopen(buf, "w");
if(!f) if (!f)
eprintf("'%s':", buf); eprintf("'%s':", buf);
return f; return f;
} }

View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -20,16 +21,16 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc != 1) if (argc != 1)
usage(); usage();
if(!(tmpfp = tmpfile())) if (!(tmpfp = tmpfile()))
eprintf("tmpfile:"); eprintf("tmpfile:");
concat(stdin, "<stdin>", tmpfp, "<tmpfile>"); concat(stdin, "<stdin>", tmpfp, "<tmpfile>");
rewind(tmpfp); rewind(tmpfp);
if(!(fp = fopen(argv[0], "w"))) if (!(fp = fopen(argv[0], "w")))
eprintf("sponge: '%s':", argv[0]); eprintf("sponge: '%s':", argv[0]);
concat(tmpfp, "<tmpfile>", fp, argv[0]); concat(tmpfp, "<tmpfile>", fp, argv[0]);

View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include "util.h" #include "util.h"
static void dostrings(FILE *fp, const char *fname); static void dostrings(FILE *fp, const char *fname);

3
sync.c
View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -13,7 +14,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if(argc != 1) if (argc != 1)
usage(); usage();
sync(); sync();

21
tail.c
View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -27,7 +28,7 @@ main(int argc, char *argv[])
case 'n': case 'n':
lines = EARGF(usage()); lines = EARGF(usage());
n = abs(estrtol(lines, 0)); n = abs(estrtol(lines, 0));
if(lines[0] == '+') if (lines[0] == '+')
tail = dropinit; tail = dropinit;
break; break;
ARGNUM: ARGNUM:
@ -36,11 +37,11 @@ main(int argc, char *argv[])
default: default:
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) { if (argc == 0) {
tail(stdin, "<stdin>", n); tail(stdin, "<stdin>", n);
} else { } else {
for (; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[0], "r"))) { if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]); weprintf("fopen %s:", argv[0]);
continue; continue;
} }
@ -60,8 +61,8 @@ dropinit(FILE *fp, const char *str, long n)
ssize_t len; ssize_t len;
unsigned long i = 0; unsigned long i = 0;
while(i < n && ((len = agetline(&buf, &size, fp)) != -1)) while (i < n && ((len = agetline(&buf, &size, fp)) != -1))
if(len && buf[len - 1] == '\n') if (len && buf[len - 1] == '\n')
i++; i++;
free(buf); free(buf);
concat(fp, str, stdout, "<stdout>"); concat(fp, str, stdout, "<stdout>");
@ -74,19 +75,19 @@ taketail(FILE *fp, const char *str, long n)
long i, j; long i, j;
size_t *size = NULL; size_t *size = NULL;
if(!(ring = calloc(n, sizeof *ring)) || !(size = calloc(n, sizeof *size))) if (!(ring = calloc(n, sizeof *ring)) || !(size = calloc(n, sizeof *size)))
eprintf("calloc:"); eprintf("calloc:");
for(i = j = 0; agetline(&ring[i], &size[i], fp) != -1; i = j = (i + 1) % n) for (i = j = 0; agetline(&ring[i], &size[i], fp) != -1; i = j = (i + 1) % n)
; ;
if(ferror(fp)) if (ferror(fp))
eprintf("%s: read error:", str); eprintf("%s: read error:", str);
do { do {
if(ring[j]) { if (ring[j]) {
fputs(ring[j], stdout); fputs(ring[j], stdout);
free(ring[j]); free(ring[j]);
} }
} while((j = (j+1)%n) != i); } while ((j = (j+1)%n) != i);
free(ring); free(ring);
free(size); free(size);
} }

75
tar.c
View File

@ -10,6 +10,7 @@
#include <limits.h> #include <limits.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include "util.h" #include "util.h"
typedef struct Header Header; typedef struct Header Header;
@ -74,7 +75,7 @@ main(int argc, char *argv[])
case 'x': case 'x':
case 'c': case 'c':
case 't': case 't':
if(mode) if (mode)
usage(); usage();
mode = ARGC(); mode = ARGC();
break; break;
@ -91,27 +92,27 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(!mode) { if (!mode) {
if(argc < 1) if (argc < 1)
usage(); usage();
for(ap = argv[0]; *ap; ap++) { for (ap = argv[0]; *ap; ap++) {
switch(*ap) { switch (*ap) {
case 'x': case 'x':
case 'c': case 'c':
case 't': case 't':
if(mode) if (mode)
usage(); usage();
mode = *ap; mode = *ap;
break; break;
case 'f': case 'f':
if(argc < 2) if (argc < 2)
usage(); usage();
argc--, argv++; argc--, argv++;
file = argv[0]; file = argv[0];
break; break;
case 'C': case 'C':
if(argc < 2) if (argc < 2)
usage(); usage();
argc--, argv++; argc--, argv++;
dir = argv[0]; dir = argv[0];
@ -126,12 +127,12 @@ main(int argc, char *argv[])
argc--, argv++; argc--, argv++;
} }
if(!mode || argc != (mode == 'c')) if (!mode || argc != (mode == 'c'))
usage(); usage();
if(file) { if (file) {
tarfile = fopen(file, (mode == 'c') ? "wb" : "rb"); tarfile = fopen(file, (mode == 'c') ? "wb" : "rb");
if(!tarfile) if (!tarfile)
eprintf("tar: open '%s':", file); eprintf("tar: open '%s':", file);
if (lstat(file, &st) < 0) if (lstat(file, &st) < 0)
eprintf("tar: stat '%s':", file); eprintf("tar: stat '%s':", file);
@ -143,7 +144,7 @@ main(int argc, char *argv[])
chdir(dir); chdir(dir);
if(mode == 'c') { if (mode == 'c') {
c(argv[0]); c(argv[0]);
} else { } else {
xt((mode == 'x') ? unarchive : print); xt((mode == 'x') ? unarchive : print);
@ -192,16 +193,16 @@ archive(const char* path)
snprintf(h->gname, sizeof h->gname, "%s", gr ? gr->gr_name : ""); snprintf(h->gname, sizeof h->gname, "%s", gr ? gr->gr_name : "");
mode = st.st_mode; mode = st.st_mode;
if(S_ISREG(mode)) { if (S_ISREG(mode)) {
h->type = REG; h->type = REG;
putoctal(h->size, (unsigned)st.st_size, sizeof h->size); putoctal(h->size, (unsigned)st.st_size, sizeof h->size);
f = fopen(path, "r"); f = fopen(path, "r");
} else if(S_ISDIR(mode)) { } else if (S_ISDIR(mode)) {
h->type = DIRECTORY; h->type = DIRECTORY;
} else if(S_ISLNK(mode)) { } else if (S_ISLNK(mode)) {
h->type = SYMLINK; h->type = SYMLINK;
readlink(path, h->link, (sizeof h->link)-1); readlink(path, h->link, (sizeof h->link)-1);
} else if(S_ISCHR(mode) || S_ISBLK(mode)) { } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
h->type = S_ISCHR(mode) ? CHARDEV : BLOCKDEV; h->type = S_ISCHR(mode) ? CHARDEV : BLOCKDEV;
#if defined(major) && defined(minor) #if defined(major) && defined(minor)
putoctal(h->major, (unsigned)major(st.st_dev), sizeof h->major); putoctal(h->major, (unsigned)major(st.st_dev), sizeof h->major);
@ -209,20 +210,20 @@ archive(const char* path)
#else #else
return 0; return 0;
#endif #endif
} else if(S_ISFIFO(mode)) { } else if (S_ISFIFO(mode)) {
h->type = FIFO; h->type = FIFO;
} }
memset(h->chksum, ' ', sizeof h->chksum); memset(h->chksum, ' ', sizeof h->chksum);
for(x = 0, chksum = 0; x < sizeof *h; x++) for (x = 0, chksum = 0; x < sizeof *h; x++)
chksum += b[x]; chksum += b[x];
putoctal(h->chksum, chksum, sizeof h->chksum); putoctal(h->chksum, chksum, sizeof h->chksum);
fwrite(b, Blksiz, 1, tarfile); fwrite(b, Blksiz, 1, tarfile);
if(!f) if (!f)
return 0; return 0;
while((l = fread(b, 1, Blksiz, f)) > 0) { while ((l = fread(b, 1, Blksiz, f)) > 0) {
if(l < Blksiz) if (l < Blksiz)
memset(b+l, 0, Blksiz-l); memset(b+l, 0, Blksiz-l);
fwrite(b, Blksiz, 1, tarfile); fwrite(b, Blksiz, 1, tarfile);
} }
@ -239,25 +240,25 @@ unarchive(char *fname, int l, char b[Blksiz])
struct timeval times[2]; struct timeval times[2];
Header *h = (void*)b; Header *h = (void*)b;
if(!mflag) if (!mflag)
mtime = strtoul(h->mtime, 0, 8); mtime = strtoul(h->mtime, 0, 8);
unlink(fname); unlink(fname);
switch(h->type) { switch (h->type) {
case REG: case REG:
case AREG: case AREG:
mode = strtoul(h->mode, 0, 8); mode = strtoul(h->mode, 0, 8);
if(!(f = fopen(fname, "w")) || chmod(fname, mode)) if (!(f = fopen(fname, "w")) || chmod(fname, mode))
perror(fname); perror(fname);
break; break;
case HARDLINK: case HARDLINK:
case SYMLINK: case SYMLINK:
snprintf(lname, sizeof lname, "%s", h->link); snprintf(lname, sizeof lname, "%s", h->link);
if(!((h->type == HARDLINK) ? link : symlink)(lname, fname)) if (!((h->type == HARDLINK) ? link : symlink)(lname, fname))
perror(fname); perror(fname);
break; break;
case DIRECTORY: case DIRECTORY:
mode = strtoul(h->mode, 0, 8); mode = strtoul(h->mode, 0, 8);
if(mkdir(fname, (mode_t)mode)) if (mkdir(fname, (mode_t)mode))
perror(fname); perror(fname);
break; break;
case CHARDEV: case CHARDEV:
@ -267,34 +268,34 @@ unarchive(char *fname, int l, char b[Blksiz])
major = strtoul(h->major, 0, 8); major = strtoul(h->major, 0, 8);
minor = strtoul(h->mode, 0, 8); minor = strtoul(h->mode, 0, 8);
type = (h->type == CHARDEV) ? S_IFCHR : S_IFBLK; type = (h->type == CHARDEV) ? S_IFCHR : S_IFBLK;
if(mknod(fname, type | mode, makedev(major, minor))) if (mknod(fname, type | mode, makedev(major, minor)))
perror(fname); perror(fname);
#endif #endif
break; break;
case FIFO: case FIFO:
mode = strtoul(h->mode, 0, 8); mode = strtoul(h->mode, 0, 8);
if(mknod(fname, S_IFIFO | mode, 0)) if (mknod(fname, S_IFIFO | mode, 0))
perror(fname); perror(fname);
break; break;
default: default:
fprintf(stderr, "usupported tarfiletype %c\n", h->type); fprintf(stderr, "usupported tarfiletype %c\n", h->type);
} }
if(getuid() == 0 && chown(fname, strtoul(h->uid, 0, 8), if (getuid() == 0 && chown(fname, strtoul(h->uid, 0, 8),
strtoul(h->gid, 0, 8))) strtoul(h->gid, 0, 8)))
perror(fname); perror(fname);
for(; l > 0; l -= Blksiz) { for (; l > 0; l -= Blksiz) {
fread(b, Blksiz, 1, tarfile); fread(b, Blksiz, 1, tarfile);
if(f) if (f)
fwrite(b, MIN(l, 512), 1, f); fwrite(b, MIN(l, 512), 1, f);
} }
if(f) if (f)
fclose(f); fclose(f);
if(!mflag) { if (!mflag) {
times[0].tv_sec = times[1].tv_sec = mtime; times[0].tv_sec = times[1].tv_sec = mtime;
times[0].tv_usec = times[1].tv_usec = 0; times[0].tv_usec = times[1].tv_usec = 0;
if(utimes(fname, times)) if (utimes(fname, times))
perror(fname); perror(fname);
} }
return 0; return 0;
@ -304,7 +305,7 @@ static int
print(char * fname, int l, char b[Blksiz]) print(char * fname, int l, char b[Blksiz])
{ {
puts(fname); puts(fname);
for(; l > 0; l -= Blksiz) for (; l > 0; l -= Blksiz)
fread(b, Blksiz, 1, tarfile); fread(b, Blksiz, 1, tarfile);
return 0; return 0;
} }
@ -322,7 +323,7 @@ xt(int (*fn)(char*, int, char[Blksiz]))
char b[Blksiz], fname[257], *s; char b[Blksiz], fname[257], *s;
Header *h = (void*)b; Header *h = (void*)b;
while(fread(b, Blksiz, 1, tarfile) && h->name[0] != '\0') { while (fread(b, Blksiz, 1, tarfile) && h->name[0] != '\0') {
s = fname; s = fname;
if (h->prefix[0] != '\0') if (h->prefix[0] != '\0')
s += sprintf(s, "%.*s/", (int)sizeof h->prefix, h->prefix); s += sprintf(s, "%.*s/", (int)sizeof h->prefix, h->prefix);

15
tee.c
View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -29,21 +30,21 @@ main(int argc, char *argv[])
} ARGEND; } ARGEND;
nfps = argc + 1; nfps = argc + 1;
if(!(fps = calloc(nfps, sizeof *fps))) if (!(fps = calloc(nfps, sizeof *fps)))
eprintf("calloc:"); eprintf("calloc:");
for(i = 0; argc > 0; argc--, argv++, i++) for (i = 0; argc > 0; argc--, argv++, i++)
if(!(fps[i] = fopen(*argv, aflag ? "a" : "w"))) if (!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
eprintf("fopen %s:", *argv); eprintf("fopen %s:", *argv);
fps[i] = stdout; fps[i] = stdout;
while((n = fread(buf, 1, sizeof buf, stdin)) > 0) { while ((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
for(i = 0; i < nfps; i++) { for (i = 0; i < nfps; i++) {
if(fwrite(buf, 1, n, fps[i]) != n) if (fwrite(buf, 1, n, fps[i]) != n)
eprintf("%s: write error:", buf); eprintf("%s: write error:", buf);
} }
} }
if(ferror(stdin)) if (ferror(stdin))
eprintf("<stdin>: read error:"); eprintf("<stdin>: read error:");
free(fps); free(fps);

43
test.c
View File

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void static void
@ -14,20 +15,20 @@ stoi(char *s, int *a)
char *p; char *p;
errno = 0; errno = 0;
*a = strtol(s, &p, 0); *a = strtol(s, &p, 0);
if(errno || !*s || *p) if (errno || !*s || *p)
enprintf(2, "bad integer %s\n", s); enprintf(2, "bad integer %s\n", s);
} }
static bool unary_b(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISBLK (buf.st_mode); } static bool unary_b(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISBLK (buf.st_mode); }
static bool unary_c(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISCHR (buf.st_mode); } static bool unary_c(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISCHR (buf.st_mode); }
static bool unary_d(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISDIR (buf.st_mode); } static bool unary_d(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISDIR (buf.st_mode); }
static bool unary_f(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISREG (buf.st_mode); } static bool unary_f(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISREG (buf.st_mode); }
static bool unary_g(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISGID & buf.st_mode ; } static bool unary_g(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISGID & buf.st_mode ; }
static bool unary_h(char *s) { struct stat buf; if(lstat(s, &buf)) return 0; return S_ISLNK (buf.st_mode); } static bool unary_h(char *s) { struct stat buf; if (lstat(s, &buf)) return 0; return S_ISLNK (buf.st_mode); }
static bool unary_p(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISFIFO (buf.st_mode); } static bool unary_p(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISFIFO (buf.st_mode); }
static bool unary_S(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISSOCK (buf.st_mode); } static bool unary_S(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISSOCK (buf.st_mode); }
static bool unary_s(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return buf.st_size ; } static bool unary_s(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return buf.st_size ; }
static bool unary_u(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISUID & buf.st_mode ; } static bool unary_u(char *s) { struct stat buf; if ( stat(s, &buf)) return 0; return S_ISUID & buf.st_mode ; }
static bool unary_n(char *s) { return strlen(s); } static bool unary_n(char *s) { return strlen(s); }
static bool unary_z(char *s) { return !strlen(s); } static bool unary_z(char *s) { return !strlen(s); }
@ -95,8 +96,8 @@ find_test(Test *tests, char *name)
{ {
Test *t; Test *t;
for(t = tests; t->name; ++t) for (t = tests; t->name; ++t)
if(strcmp(t->name, name) == 0) if (strcmp(t->name, name) == 0)
return t; return t;
return NULL; return NULL;
} }
@ -118,10 +119,10 @@ twoarg(char **argv)
{ {
Test *t = find_test(unary, *argv); Test *t = find_test(unary, *argv);
if(strcmp(argv[0], "!") == 0) if (strcmp(argv[0], "!") == 0)
return !onearg(argv + 1); return !onearg(argv + 1);
if(t) if (t)
return t->func(argv[1]); return t->func(argv[1]);
return enprintf(2, "bad unary test %s\n", argv[0]), 0; return enprintf(2, "bad unary test %s\n", argv[0]), 0;
@ -132,10 +133,10 @@ threearg(char **argv)
{ {
Test *t = find_test(binary, argv[1]); Test *t = find_test(binary, argv[1]);
if(t) if (t)
return t->func(argv[0], argv[2]); return t->func(argv[0], argv[2]);
if(strcmp(argv[0], "!") == 0) if (strcmp(argv[0], "!") == 0)
return !twoarg(argv + 1); return !twoarg(argv + 1);
return enprintf(2, "bad binary test %s\n", argv[1]), 0; return enprintf(2, "bad binary test %s\n", argv[1]), 0;
@ -144,7 +145,7 @@ threearg(char **argv)
static bool static bool
fourarg(char **argv) fourarg(char **argv)
{ {
if(strcmp(argv[0], "!") == 0) if (strcmp(argv[0], "!") == 0)
return !threearg(argv + 1); return !threearg(argv + 1);
return enprintf(2, "too many arguments\n"), 0; return enprintf(2, "too many arguments\n"), 0;
@ -156,13 +157,13 @@ main(int argc, char **argv)
bool (*narg[])(char**) = { noarg, onearg, twoarg, threearg, fourarg }; bool (*narg[])(char**) = { noarg, onearg, twoarg, threearg, fourarg };
int len = strlen(argv[0]); int len = strlen(argv[0]);
if(len && argv[0][len - 1] == '[') if (len && argv[0][len - 1] == '[')
if(strcmp(argv[--argc], "]") != 0) if (strcmp(argv[--argc], "]") != 0)
enprintf(2, "no matching ]\n"); enprintf(2, "no matching ]\n");
--argc; ++argv; --argc; ++argv;
if(argc > 4) if (argc > 4)
enprintf(2, "too many arguments\n"); enprintf(2, "too many arguments\n");
return !narg[argc](argv); return !narg[argc](argv);

15
touch.c
View File

@ -7,6 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include <utime.h> #include <utime.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
static void touch(const char *); static void touch(const char *);
@ -39,7 +40,7 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
for(; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
touch(argv[0]); touch(argv[0]);
return 0; return 0;
@ -52,19 +53,19 @@ touch(const char *str)
struct stat st; struct stat st;
struct utimbuf ut; struct utimbuf ut;
if(stat(str, &st) == 0) { if (stat(str, &st) == 0) {
ut.actime = st.st_atime; ut.actime = st.st_atime;
ut.modtime = t; ut.modtime = t;
if(utime(str, &ut) == -1) if (utime(str, &ut) == -1)
eprintf("utime %s:", str); eprintf("utime %s:", str);
return; return;
} }
else if(errno != ENOENT) else if (errno != ENOENT)
eprintf("stat %s:", str); eprintf("stat %s:", str);
else if(cflag) else if (cflag)
return; return;
if((fd = open(str, O_CREAT|O_EXCL, if ((fd = open(str, O_CREAT|O_EXCL,
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1) S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1)
eprintf("open %s:", str); eprintf("open %s:", str);
close(fd); close(fd);
touch(str); touch(str);

93
tr.c
View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <locale.h> #include <locale.h>
#include <wchar.h> #include <wchar.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -54,7 +55,7 @@ resolve_escape(char *s)
int i; int i;
unsigned char c; unsigned char c;
switch(*s) { switch (*s) {
case 'n': case 'n':
*s = '\n'; *s = '\n';
return 0; return 0;
@ -84,13 +85,13 @@ resolve_escape(char *s)
default: ; default: ;
} }
if(*s<'0' || *s>'7') if(*s < '0' || *s > '7')
eprintf("invalid character after '\\':"); eprintf("invalid character after '\\':");
for(i=0, c=0; s[i]>='0' && s[i]<='7' && i<3; i++) { for(i = 0, c = 0; s[i] >= '0' && s[i] <= '7' && i < 3; i++) {
c <<= 3; c <<= 3;
c += s[i]-'0'; c += s[i]-'0';
} }
if(*s>'3' && i==3) if(*s > '3' && i == 3)
eprintf("octal byte cannot be bigger than 377:"); eprintf("octal byte cannot be bigger than 377:");
*s = c; *s = c;
return i; return i;
@ -112,8 +113,8 @@ xmbtowc(wchar_t *unicodep, const char *s)
static int static int
has_octal_escapes(const char *s) has_octal_escapes(const char *s)
{ {
while(*s) while (*s)
if(*s++ == '\\' && *s >= '0' && *s <= '7') if (*s++ == '\\' && *s >= '0' && *s <= '7')
return 1; return 1;
return 0; return 0;
} }
@ -125,26 +126,26 @@ get_next_char(struct set_state *s)
int nchars; int nchars;
start: start:
if(s->rfirst <= s->rlast) { if (s->rfirst <= s->rlast) {
c = s->rfirst; c = s->rfirst;
s->rfirst++; s->rfirst++;
return c; return c;
} }
if(*s->s == '-' && !s->prev_was_octal) { if (*s->s == '-' && !s->prev_was_octal) {
s->s++; s->s++;
if(!*s->s) if (!*s->s)
return '-'; return '-';
if(*s->s == '\\' && (nchars = resolve_escape(++(s->s)))) if (*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
goto char_is_octal; goto char_is_octal;
s->rlast = *(s->s)++; s->rlast = *(s->s)++;
if(!s->rlast) if (!s->rlast)
return '\0'; return '\0';
s->prev_was_octal = 1; s->prev_was_octal = 1;
s->rfirst = ++(s->prev); s->rfirst = ++(s->prev);
goto start; goto start;
} }
if(*s->s == '\\' && (nchars = resolve_escape(++(s->s)))) if (*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
goto char_is_octal; goto char_is_octal;
s->prev_was_octal = 0; s->prev_was_octal = 0;
@ -163,27 +164,27 @@ static wchar_t
get_next_wchar(struct wset_state *s) get_next_wchar(struct wset_state *s)
{ {
start: start:
if(s->rfirst <= s->rlast) { if (s->rfirst <= s->rlast) {
s->prev = s->rfirst; s->prev = s->rfirst;
s->rfirst++; s->rfirst++;
return s->prev; return s->prev;
} }
if(*s->s == '-' && !s->prev_was_range) { if (*s->s == '-' && !s->prev_was_range) {
s->s++; s->s++;
if(!*s->s) if (!*s->s)
return '-'; return '-';
if(*s->s == '\\') if (*s->s == '\\')
resolve_escape(++(s->s)); resolve_escape(++(s->s));
s->s += xmbtowc(&s->rlast, s->s); s->s += xmbtowc(&s->rlast, s->s);
if(!s->rlast) if (!s->rlast)
return '\0'; return '\0';
s->rfirst = ++(s->prev); s->rfirst = ++(s->prev);
s->prev_was_range = 1; s->prev_was_range = 1;
goto start; goto start;
} }
if(*s->s == '\\') if (*s->s == '\\')
resolve_escape(++(s->s)); resolve_escape(++(s->s));
s->s += xmbtowc(&s->prev, s->s); s->s += xmbtowc(&s->prev, s->s);
s->prev_was_range = 0; s->prev_was_range = 0;
@ -197,39 +198,39 @@ is_mapping_wide(const char *set1, const char *set2)
struct wset_state wss1, wss2; struct wset_state wss1, wss2;
wchar_t wc1, wc2, last_wc2; wchar_t wc1, wc2, last_wc2;
if(has_octal_escapes(set1)) { if (has_octal_escapes(set1)) {
set_state_defaults(&ss1); set_state_defaults(&ss1);
ss1.s = (char *) set1; ss1.s = (char *) set1;
if(set2) { if (set2) {
set_state_defaults(&ss2); set_state_defaults(&ss2);
ss2.s = (char *) set2; ss2.s = (char *) set2;
/* if the character returned is from an octal triplet, it might be null /* if the character returned is from an octal triplet, it might be null
* and still need to continue */ * and still need to continue */
while((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal ) { while ((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal ) {
if(!(wc2 = (unsigned char) get_next_char(&ss2))) if (!(wc2 = (unsigned char) get_next_char(&ss2)))
wc2 = last_wc2; wc2 = last_wc2;
mappings[wc1] = wc2; mappings[wc1] = wc2;
last_wc2 = wc2; last_wc2 = wc2;
} }
} else { } else {
while((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal) while ((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal)
mappings[wc1] = 1; mappings[wc1] = 1;
} }
return 0; return 0;
} else { } else {
wset_state_defaults(&wss1); wset_state_defaults(&wss1);
wss1.s = (char *) set1; wss1.s = (char *) set1;
if(set2) { if (set2) {
wset_state_defaults(&wss2); wset_state_defaults(&wss2);
wss2.s = (char *) set2; wss2.s = (char *) set2;
while((wc1 = get_next_wchar(&wss1))) { while ((wc1 = get_next_wchar(&wss1))) {
if(!(wc2 = get_next_wchar(&wss2))) if (!(wc2 = get_next_wchar(&wss2)))
wc2 = last_wc2; wc2 = last_wc2;
mappings[wc1] = wc2; mappings[wc1] = wc2;
last_wc2 = wc2; last_wc2 = wc2;
} }
} else { } else {
while((wc1 = get_next_wchar(&wss1))) while ((wc1 = get_next_wchar(&wss1)))
mappings[wc1] = 1; mappings[wc1] = 1;
} }
return 1; return 1;
@ -245,13 +246,13 @@ wmap_null(char *in, ssize_t nbytes)
int parsed_bytes = 0; int parsed_bytes = 0;
s = in; s = in;
while(nbytes) { while (nbytes) {
parsed_bytes = embtowc(&rune, s); parsed_bytes = embtowc(&rune, s);
if(parsed_bytes < 0) { if (parsed_bytes < 0) {
rune = *s; rune = *s;
parsed_bytes = 1; parsed_bytes = 1;
} }
if(((!mappings[rune])&1) ^ cflag) if (((!mappings[rune])&1) ^ cflag)
putwchar(rune); putwchar(rune);
s += parsed_bytes; s += parsed_bytes;
nbytes -= parsed_bytes; nbytes -= parsed_bytes;
@ -266,13 +267,13 @@ wmap_set(char *in, ssize_t nbytes)
int parsed_bytes = 0; int parsed_bytes = 0;
s = in; s = in;
while(nbytes) { while (nbytes) {
parsed_bytes = embtowc(&rune, s); parsed_bytes = embtowc(&rune, s);
if(parsed_bytes < 0) { if (parsed_bytes < 0) {
rune = *s; rune = *s;
parsed_bytes = 1; parsed_bytes = 1;
} }
if(!mappings[rune]) if (!mappings[rune])
putwchar(rune); putwchar(rune);
else else
putwchar(mappings[rune]); putwchar(mappings[rune]);
@ -286,8 +287,8 @@ map_null(char *in, ssize_t nbytes)
{ {
char *s; char *s;
for(s=in; nbytes; s++, nbytes--) for (s = in; nbytes; s++, nbytes--)
if(((!mappings[(unsigned char)*s])&1) ^ cflag) if (((!mappings[(unsigned char)*s])&1) ^ cflag)
putchar(*s); putchar(*s);
} }
@ -296,8 +297,8 @@ map_set(char *in, ssize_t nbytes)
{ {
char *s; char *s;
for(s=in; nbytes; s++, nbytes--) for (s = in; nbytes; s++, nbytes--)
if(!mappings[(unsigned char)*s]) if (!mappings[(unsigned char)*s])
putchar(*s); putchar(*s);
else else
putchar(mappings[(unsigned char)*s]); putchar(mappings[(unsigned char)*s]);
@ -325,20 +326,20 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) if (argc == 0)
usage(); usage();
if(dflag) { if (dflag) {
if(argc != 1) if (argc != 1)
usage(); usage();
if(is_mapping_wide(argv[0], NULL)) if (is_mapping_wide(argv[0], NULL))
mapfunc = wmap_null; mapfunc = wmap_null;
else else
mapfunc = map_null; mapfunc = map_null;
} else if(cflag) { } else if (cflag) {
usage(); usage();
} else if(argc == 2) { } else if (argc == 2) {
if(is_mapping_wide(argv[0], argv[1])) if (is_mapping_wide(argv[0], argv[1]))
mapfunc = wmap_set; mapfunc = wmap_set;
else else
mapfunc = map_set; mapfunc = map_set;
@ -346,10 +347,10 @@ main(int argc, char *argv[])
usage(); usage();
} }
while((nbytes = agetline(&buf, &size, stdin)) != -1) while ((nbytes = agetline(&buf, &size, stdin)) != -1)
mapfunc(buf, nbytes); mapfunc(buf, nbytes);
free(buf); free(buf);
if(ferror(stdin)) if (ferror(stdin))
eprintf("<stdin>: read error:"); eprintf("<stdin>: read error:");
return 0; return 0;

1
tty.c
View File

@ -2,6 +2,7 @@
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
static void static void

13
uname.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include "util.h" #include "util.h"
static void static void
@ -44,18 +45,18 @@ main(int argc, char *argv[])
default: default:
usage(); usage();
} ARGEND; } ARGEND;
if(uname(&u) == -1) if (uname(&u) == -1)
eprintf("uname:"); eprintf("uname:");
if(sflag || !(nflag || rflag || vflag || mflag)) if (sflag || !(nflag || rflag || vflag || mflag))
putword(u.sysname); putword(u.sysname);
if(nflag) if (nflag)
putword(u.nodename); putword(u.nodename);
if(rflag) if (rflag)
putword(u.release); putword(u.release);
if(vflag) if (vflag)
putword(u.version); putword(u.version);
if(mflag) if (mflag)
putword(u.machine); putword(u.machine);
putchar('\n'); putchar('\n');

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <wchar.h> #include <wchar.h>
#include "util.h" #include "util.h"
typedef struct { typedef struct {
@ -30,7 +31,7 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
case 't': case 't':
tabsize = estrtol(EARGF(usage()), 0); tabsize = estrtol(EARGF(usage()), 0);
if(tabsize <= 0) if (tabsize <= 0)
eprintf("unexpand: invalid tabsize\n", argv[0]); eprintf("unexpand: invalid tabsize\n", argv[0]);
/* Fallthrough: -t implies -a */ /* Fallthrough: -t implies -a */
case 'a': case 'a':
@ -84,12 +85,12 @@ unexpandspan(unsigned int n, unsigned int col)
{ {
unsigned int off = (col-n) % tabsize; unsigned int off = (col-n) % tabsize;
if(n + off >= tabsize && n > 1) if (n + off >= tabsize && n > 1)
n += off; n += off;
for(; n >= tabsize; n -= tabsize) for (; n >= tabsize; n -= tabsize)
out('\t'); out('\t');
while(n--) while (n--)
out(' '); out(' ');
} }

18
uniq.c
View File

@ -46,10 +46,10 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if(argc == 0) { if (argc == 0) {
uniq(stdin, "<stdin>"); uniq(stdin, "<stdin>");
} else if(argc == 1) { } else if (argc == 1) {
if(!(fp = fopen(argv[0], "r"))) if (!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
uniq(fp, argv[0]); uniq(fp, argv[0]);
fclose(fp); fclose(fp);
@ -67,14 +67,14 @@ uniqline(char *l)
? l == prevline ? l == prevline
: !strcmp(l, prevline); : !strcmp(l, prevline);
if(linesequel) { if (linesequel) {
++prevlinecount; ++prevlinecount;
return; return;
} }
if(prevline != NULL) { if (prevline != NULL) {
if((prevlinecount == 1 && !dflag) || if ((prevlinecount == 1 && !dflag) ||
(prevlinecount != 1 && !uflag)) { (prevlinecount != 1 && !uflag)) {
printf(countfmt, prevlinecount); printf(countfmt, prevlinecount);
fputs(prevline, stdout); fputs(prevline, stdout);
} }
@ -82,7 +82,7 @@ uniqline(char *l)
prevline = NULL; prevline = NULL;
} }
if(l && !(prevline = strdup(l))) if (l && !(prevline = strdup(l)))
eprintf("strdup:"); eprintf("strdup:");
prevlinecount = 1; prevlinecount = 1;
} }
@ -93,7 +93,7 @@ uniq(FILE *fp, const char *str)
char *buf = NULL; char *buf = NULL;
size_t size = 0; size_t size = 0;
while(agetline(&buf, &size, fp) != -1) while (agetline(&buf, &size, fp) != -1)
uniqline(buf); uniqline(buf);
} }

View File

@ -12,10 +12,10 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if(argc != 2) if (argc != 2)
usage(); usage();
if(unlink(argv[1]) == -1) if (unlink(argv[1]) == -1)
eprintf("unlink: '%s':", argv[1]); eprintf("unlink: '%s':", argv[1]);
return 0; return 0;

View File

@ -7,6 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include "util.h" #include "util.h"
#include "text.h" #include "text.h"
@ -127,10 +128,10 @@ uudecode(FILE *fp, FILE *outfp)
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
#define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]", (c), 1 + ' ', 077 + ' ' + 1) #define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]", (c), 1 + ' ', 077 + ' ' + 1)
while((len = agetline(&bufb, &n, fp)) != -1) { while ((len = agetline(&bufb, &n, fp)) != -1) {
p = bufb; p = bufb;
/* trim newlines */ /* trim newlines */
if(len && bufb[len - 1] != '\n') if (len && bufb[len - 1] != '\n')
bufb[len - 1] = '\0'; bufb[len - 1] = '\0';
else else
eprintf("no newline found, aborting\n"); eprintf("no newline found, aborting\n");
@ -172,7 +173,7 @@ uudecode(FILE *fp, FILE *outfp)
} }
/* check for end or fail */ /* check for end or fail */
len = agetline(&bufb, &n, fp); len = agetline(&bufb, &n, fp);
if(len < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n') if (len < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
eprintf("invalid uudecode footer \"end\" not found\n"); eprintf("invalid uudecode footer \"end\" not found\n");
free(bufb); free(bufb);
} }

View File

@ -4,6 +4,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void uuencode(FILE *, const char *, const char *); static void uuencode(FILE *, const char *, const char *);

21
wc.c
View File

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void output(const char *, long, long, long); static void output(const char *, long, long, long);
@ -47,7 +48,7 @@ main(int argc, char *argv[])
wc(stdin, NULL); wc(stdin, NULL);
} else { } else {
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if(!(fp = fopen(argv[i], "r"))) { if (!(fp = fopen(argv[i], "r"))) {
weprintf("fopen %s:", argv[i]); weprintf("fopen %s:", argv[i]);
continue; continue;
} }
@ -65,13 +66,13 @@ output(const char *str, long nc, long nl, long nw)
{ {
bool noflags = !cmode && !lflag && !wflag; bool noflags = !cmode && !lflag && !wflag;
if(lflag || noflags) if (lflag || noflags)
printf(" %5ld", nl); printf(" %5ld", nl);
if(wflag || noflags) if (wflag || noflags)
printf(" %5ld", nw); printf(" %5ld", nw);
if(cmode || noflags) if (cmode || noflags)
printf(" %5ld", nc); printf(" %5ld", nc);
if(str) if (str)
printf(" %s", str); printf(" %s", str);
putchar('\n'); putchar('\n');
} }
@ -83,14 +84,14 @@ wc(FILE *fp, const char *str)
int c; int c;
long nc = 0, nl = 0, nw = 0; long nc = 0, nl = 0, nw = 0;
while((c = getc(fp)) != EOF) { while ((c = getc(fp)) != EOF) {
if(cmode != 'm' || UTF8_POINT(c)) if (cmode != 'm' || UTF8_POINT(c))
nc++; nc++;
if(c == '\n') if (c == '\n')
nl++; nl++;
if(!isspace(c)) if (!isspace(c))
word = true; word = true;
else if(word) { else if (word) {
word = false; word = false;
nw++; nw++;
} }

View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
@ -48,7 +49,7 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
case 'n': case 'n':
nflag = 1; nflag = 1;
if((maxargs = strtol(EARGF(usage()), NULL, 10)) <= 0) if ((maxargs = strtol(EARGF(usage()), NULL, 10)) <= 0)
eprintf("%s: value for -n option should be >= 1\n", argv0); eprintf("%s: value for -n option should be >= 1\n", argv0);
break; break;
case 'r': case 'r':
@ -92,13 +93,13 @@ main(int argc, char *argv[])
i++; i++;
a++; a++;
leftover = 0; leftover = 0;
if(nflag == 1 && a >= maxargs) if (nflag == 1 && a >= maxargs)
break; break;
} }
cmd[i] = NULL; cmd[i] = NULL;
if(a >= maxargs && nflag == 1) if (a >= maxargs && nflag == 1)
spawn(); spawn();
else if(!a || (i == 1 && rflag == 1)) else if (!a || (i == 1 && rflag == 1))
; ;
else else
spawn(); spawn();

1
yes.c
View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
static void static void