Fix coding style
It was about damn time. Consistency is very important in such a big codebase.master
parent
e35d9e62a5
commit
eee98ed3a4
|
@ -24,11 +24,11 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc < 1)
|
||||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
s = basename(argv[0]);
|
||||
if(argc == 2) {
|
||||
if (argc == 2) {
|
||||
p = strstr(s, argv[1]);
|
||||
if (p && p[strlen(p)] == '\0')
|
||||
*p = '\0';
|
||||
|
|
29
cal.c
29
cal.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
#define MONTHMAX 100
|
||||
|
@ -28,11 +29,11 @@ drawcal(int year, int month, int day, int ncols, int nmons, int fday)
|
|||
int row = 0;
|
||||
char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", };
|
||||
|
||||
if(!ncols)
|
||||
if (!ncols)
|
||||
ncols = nmons;
|
||||
while(nmons > 0) {
|
||||
while (nmons > 0) {
|
||||
last = MIN(nmons, ncols);
|
||||
for(i = 0; i < last; i++) {
|
||||
for (i = 0; i < last; i++) {
|
||||
moff = month + ncols * row + i - 1;
|
||||
cur = 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");
|
||||
|
||||
for(i = 0; i < last; i++) {
|
||||
for(j = fday; j < LEN(days); j++)
|
||||
for (i = 0; i < last; i++) {
|
||||
for (j = fday; j < LEN(days); j++)
|
||||
printf("%s ", days[j]);
|
||||
for(j = 0; j < fday; j++)
|
||||
for (j = 0; j < fday; j++)
|
||||
printf("%s ", days[j]);
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
for(r = 0; r < 6; r++) {
|
||||
for(i = 0; i < last; i++) {
|
||||
for (r = 0; r < 6; r++) {
|
||||
for (i = 0; i < last; i++) {
|
||||
moff = month + ncols * row + i - 1;
|
||||
cur = 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));
|
||||
day1 = dayofweek(year, cur, 1, fday);
|
||||
|
||||
for(d = 0; d < 7; d++) {
|
||||
if((r || d >= day1) && count[i] <= ndays)
|
||||
for (d = 0; d < 7; d++) {
|
||||
if ((r || d >= day1) && count[i] <= ndays)
|
||||
printf("%2d ", count[i]++);
|
||||
else
|
||||
printf(" ");
|
||||
|
@ -89,9 +90,9 @@ dayofweek(int year, int month, int day, int fday)
|
|||
static bool
|
||||
isleap(int year)
|
||||
{
|
||||
if(year % 400 == 0)
|
||||
if (year % 400 == 0)
|
||||
return true;
|
||||
if(year % 100 == 0)
|
||||
if (year % 100 == 0)
|
||||
return false;
|
||||
return (year % 4 == 0);
|
||||
}
|
||||
|
@ -156,7 +157,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
switch(argc) {
|
||||
switch (argc) {
|
||||
case 3:
|
||||
day = estrtol(argv[0], 0);
|
||||
argv++;
|
||||
|
@ -172,7 +173,7 @@ main(int argc, char *argv[])
|
|||
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) {
|
||||
usage();
|
||||
}
|
||||
|
|
4
cat.c
4
cat.c
|
@ -26,13 +26,13 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
concat(stdin, "<stdin>", stdout, "<stdout>");
|
||||
} else {
|
||||
for (; argc; argc--, argv++) {
|
||||
if (argv[0][0] == '-')
|
||||
argv[0] = "/dev/fd/0";
|
||||
if(!(fp = fopen(argv[0], "r"))) {
|
||||
if (!(fp = fopen(argv[0], "r"))) {
|
||||
weprintf("fopen %s:", argv[0]);
|
||||
ret = 1;
|
||||
continue;
|
||||
|
|
11
chgrp.c
11
chgrp.c
|
@ -7,6 +7,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static int gid;
|
||||
|
@ -23,7 +24,7 @@ usage(void)
|
|||
static void
|
||||
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));
|
||||
failures++;
|
||||
}
|
||||
|
@ -44,19 +45,19 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc < 2)
|
||||
if (argc < 2)
|
||||
usage();
|
||||
|
||||
errno = 0;
|
||||
gr = getgrnam(argv[0]);
|
||||
if (errno)
|
||||
eprintf("getgrnam %s:");
|
||||
else if(!gr)
|
||||
else if (!gr)
|
||||
eprintf("chgrp: '%s': No such group\n", argv[0]);
|
||||
gid = gr->gr_gid;
|
||||
|
||||
while(*++argv) {
|
||||
if(stat(*argv, &st) == -1) {
|
||||
while (*++argv) {
|
||||
if (stat(*argv, &st) == -1) {
|
||||
fprintf(stderr, "chgrp: '%s': %s\n", *argv,
|
||||
strerror(errno));
|
||||
failures++;
|
||||
|
|
8
chmod.c
8
chmod.c
|
@ -51,7 +51,7 @@ done:
|
|||
argv++;
|
||||
argc--;
|
||||
|
||||
if(argc < 1)
|
||||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
for (; argc > 0; argc--, argv++)
|
||||
|
@ -65,17 +65,17 @@ chmodr(const char *path)
|
|||
struct stat st;
|
||||
mode_t m;
|
||||
|
||||
if(stat(path, &st) == -1) {
|
||||
if (stat(path, &st) == -1) {
|
||||
weprintf("stat %s:", path);
|
||||
ret = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
m = parsemode(modestr, st.st_mode, mask);
|
||||
if(chmod(path, m) == -1) {
|
||||
if (chmod(path, m) == -1) {
|
||||
weprintf("chmod %s:", path);
|
||||
ret = 1;
|
||||
}
|
||||
if(rflag)
|
||||
if (rflag)
|
||||
recurse(path, chmodr);
|
||||
}
|
||||
|
|
27
chown.c
27
chown.c
|
@ -6,6 +6,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void chownpwgr(const char *);
|
||||
|
@ -37,42 +38,42 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0)
|
||||
if (argc == 0)
|
||||
usage();
|
||||
|
||||
owner = argv[0];
|
||||
argv++;
|
||||
argc--;
|
||||
if((group = strchr(owner, ':')))
|
||||
if ((group = strchr(owner, ':')))
|
||||
*group++ = '\0';
|
||||
|
||||
if(owner && *owner) {
|
||||
if (owner && *owner) {
|
||||
errno = 0;
|
||||
pw = getpwnam(owner);
|
||||
if(pw) {
|
||||
if (pw) {
|
||||
uid = pw->pw_uid;
|
||||
} else {
|
||||
if(errno != 0)
|
||||
if (errno != 0)
|
||||
eprintf("getpwnam %s:", owner);
|
||||
uid = strtoul(owner, &end, 10);
|
||||
if(*end != '\0')
|
||||
if (*end != '\0')
|
||||
eprintf("getpwnam %s: no such user\n", owner);
|
||||
}
|
||||
}
|
||||
if(group && *group) {
|
||||
if (group && *group) {
|
||||
errno = 0;
|
||||
gr = getgrnam(group);
|
||||
if(gr) {
|
||||
if (gr) {
|
||||
gid = gr->gr_gid;
|
||||
} else {
|
||||
if(errno != 0)
|
||||
if (errno != 0)
|
||||
eprintf("getgrnam %s:", group);
|
||||
gid = strtoul(group, &end, 10);
|
||||
if(*end != '\0')
|
||||
if (*end != '\0')
|
||||
eprintf("getgrnam %s: no such group\n", group);
|
||||
}
|
||||
}
|
||||
for(; argc > 0; argc--, argv++)
|
||||
for (; argc > 0; argc--, argv++)
|
||||
chownpwgr(argv[0]);
|
||||
|
||||
return ret;
|
||||
|
@ -81,10 +82,10 @@ main(int argc, char *argv[])
|
|||
void
|
||||
chownpwgr(const char *path)
|
||||
{
|
||||
if(chown(path, uid, gid) == -1) {
|
||||
if (chown(path, uid, gid) == -1) {
|
||||
weprintf("chown %s:", path);
|
||||
ret = 1;
|
||||
}
|
||||
if(rflag)
|
||||
if (rflag)
|
||||
recurse(path, chownpwgr);
|
||||
}
|
||||
|
|
11
chroot.c
11
chroot.c
|
@ -2,6 +2,7 @@
|
|||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -21,19 +22,19 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc < 1)
|
||||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
if((aux = getenv("SHELL")))
|
||||
if ((aux = getenv("SHELL")))
|
||||
shell[0] = aux;
|
||||
|
||||
if(chroot(argv[0]) == -1)
|
||||
if (chroot(argv[0]) == -1)
|
||||
eprintf("chroot %s:", argv[0]);
|
||||
|
||||
if(chdir("/") == -1)
|
||||
if (chdir("/") == -1)
|
||||
eprintf("chdir:");
|
||||
|
||||
if(argc == 1) {
|
||||
if (argc == 1) {
|
||||
p = *shell;
|
||||
execvp(*shell, shell);
|
||||
} else {
|
||||
|
|
11
cksum.c
11
cksum.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void cksum(FILE *, const char *);
|
||||
|
@ -78,10 +79,10 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
cksum(stdin, NULL);
|
||||
} else {
|
||||
for(; argc > 0; argc--, argv++) {
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
if (!(fp = fopen(argv[0], "r"))) {
|
||||
weprintf("fopen %s:", argv[0]);
|
||||
continue;
|
||||
|
@ -102,18 +103,18 @@ cksum(FILE *fp, const char *s)
|
|||
size_t i, n;
|
||||
|
||||
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]];
|
||||
len += n;
|
||||
}
|
||||
if (ferror(fp))
|
||||
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)];
|
||||
|
||||
printf("%"PRIu32" %lu", ~ck, (unsigned long)len);
|
||||
if(s != NULL)
|
||||
if (s != NULL)
|
||||
printf(" %s", s);
|
||||
putchar('\n');
|
||||
}
|
||||
|
|
23
cmp.c
23
cmp.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
enum { Same = 0, Diff = 1, Error = 2 };
|
||||
|
@ -41,7 +42,7 @@ main(int argc, char *argv[])
|
|||
argv[0] = "/dev/fd/0";
|
||||
fp[0] = fopen(argv[0], "r");
|
||||
if (!fp[0]) {
|
||||
if(!sflag)
|
||||
if (!sflag)
|
||||
weprintf("fopen %s:", argv[0]);
|
||||
exit(Error);
|
||||
}
|
||||
|
@ -50,30 +51,30 @@ main(int argc, char *argv[])
|
|||
argv[1] = "/dev/fd/0";
|
||||
fp[1] = fopen(argv[1], "r");
|
||||
if (!fp[1]) {
|
||||
if(!sflag)
|
||||
if (!sflag)
|
||||
weprintf("fopen %s:", argv[1]);
|
||||
exit(Error);
|
||||
}
|
||||
|
||||
for(n = 1; ; n++) {
|
||||
for (n = 1; ; n++) {
|
||||
b[0] = getc(fp[0]);
|
||||
b[1] = getc(fp[1]);
|
||||
if(b[0] == EOF && b[1] == EOF)
|
||||
if (b[0] == EOF && b[1] == EOF)
|
||||
break;
|
||||
if(b[0] == '\n' && b[1] == '\n')
|
||||
if (b[0] == '\n' && b[1] == '\n')
|
||||
line++;
|
||||
if(b[0] == b[1])
|
||||
if (b[0] == b[1])
|
||||
continue;
|
||||
for(i = 0; i < 2; i++) {
|
||||
if(b[i] == EOF) {
|
||||
if(!sflag)
|
||||
for (i = 0; i < 2; i++) {
|
||||
if (b[i] == EOF) {
|
||||
if (!sflag)
|
||||
fprintf(stderr, "cmp: EOF on %s\n",
|
||||
!argv[i] ? "<stdin>" : argv[i]);
|
||||
exit(Diff);
|
||||
}
|
||||
}
|
||||
if(!lflag) {
|
||||
if(!sflag)
|
||||
if (!lflag) {
|
||||
if (!sflag)
|
||||
printf("%s %s differ: char %ld, line %ld\n",
|
||||
argv[0], !argv[1] ? "<stdin>" : argv[1], n, line);
|
||||
exit(Diff);
|
||||
|
|
27
cols.c
27
cols.c
|
@ -6,6 +6,7 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -35,7 +36,7 @@ main(int argc, char *argv[])
|
|||
case 'c':
|
||||
cflag = 1;
|
||||
chars = estrtol(EARGF(usage()), 0);
|
||||
if(chars < 3)
|
||||
if (chars < 3)
|
||||
eprintf("%d: too few character columns");
|
||||
break;
|
||||
default:
|
||||
|
@ -49,39 +50,39 @@ main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
/* XXX librarify this chunk, too? only useful in sponges though */
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
getlines(stdin, &b);
|
||||
} else for(; argc > 0; argc--, argv++) {
|
||||
if(!(fp = fopen(argv[0], "r")))
|
||||
} else for (; argc > 0; argc--, argv++) {
|
||||
if (!(fp = fopen(argv[0], "r")))
|
||||
eprintf("fopen %s:", argv[0]);
|
||||
getlines(fp, &b);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
for(l = 0; l < b.nlines; ++l) {
|
||||
for (l = 0; l < b.nlines; ++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';
|
||||
if(len > maxlen)
|
||||
if (len > maxlen)
|
||||
maxlen = len;
|
||||
if(maxlen > (chars - 1) / 2)
|
||||
if (maxlen > (chars - 1) / 2)
|
||||
break;
|
||||
}
|
||||
|
||||
n_columns = (chars + 1) / (maxlen + 1);
|
||||
if(n_columns <= 1) {
|
||||
for(l = 0; l < b.nlines; ++l) {
|
||||
if (n_columns <= 1) {
|
||||
for (l = 0; l < b.nlines; ++l) {
|
||||
fputs(b.lines[l], stdout);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
n_rows = (b.nlines + (n_columns - 1)) / n_columns;
|
||||
for(i = 0; i < n_rows; ++i) {
|
||||
for(l = i, col = 1; l < b.nlines; l += n_rows, ++col) {
|
||||
for (i = 0; i < n_rows; ++i) {
|
||||
for (l = i, col = 1; l < b.nlines; l += n_rows, ++col) {
|
||||
len = strlen(b.lines[l]);
|
||||
fputs(b.lines[l], stdout);
|
||||
if(col < n_columns)
|
||||
if (col < n_columns)
|
||||
printf("%*s", maxlen + 1 - (int)len, "");
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
|
|
29
comm.c
29
comm.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
#define CLAMP(x, l, h) MIN(h, MAX(l, x))
|
||||
|
@ -37,29 +38,29 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc != 2)
|
||||
if (argc != 2)
|
||||
usage();
|
||||
|
||||
for(i = 0; i < LEN(fp); i++) {
|
||||
for (i = 0; i < LEN(fp); i++) {
|
||||
if (argv[i][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]);
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
if(diff <= 0) {
|
||||
for (;;) {
|
||||
if (diff <= 0) {
|
||||
lines[0][0] = '\0';
|
||||
if(!nextline(lines[0], sizeof(lines[0]),
|
||||
if (!nextline(lines[0], sizeof(lines[0]),
|
||||
fp[0], argv[0])) {
|
||||
if (lines[1][0] != '\0')
|
||||
printline(1, lines[1]);
|
||||
finish(1, fp[1], argv[1]);
|
||||
}
|
||||
}
|
||||
if(diff >= 0) {
|
||||
if (diff >= 0) {
|
||||
lines[1][0] = '\0';
|
||||
if(!nextline(lines[1], sizeof(lines[1]),
|
||||
if (!nextline(lines[1], sizeof(lines[1]),
|
||||
fp[1], argv[1])) {
|
||||
if (lines[0][0] != '\0')
|
||||
printline(0, lines[0]);
|
||||
|
@ -79,11 +80,11 @@ printline(int pos, char *line)
|
|||
{
|
||||
int i;
|
||||
|
||||
if(!(show & (0x1 << pos)))
|
||||
if (!(show & (0x1 << pos)))
|
||||
return;
|
||||
|
||||
for(i = 0; i < pos; i++) {
|
||||
if(show & (0x1 << i))
|
||||
for (i = 0; i < pos; i++) {
|
||||
if (show & (0x1 << i))
|
||||
putchar('\t');
|
||||
}
|
||||
printf("%s", line);
|
||||
|
@ -93,9 +94,9 @@ static char *
|
|||
nextline(char *buf, int n, FILE *f, char *name)
|
||||
{
|
||||
buf = fgets(buf, n, f);
|
||||
if(!buf && !feof(f))
|
||||
if (!buf && !feof(f))
|
||||
eprintf("comm: '%s':", name);
|
||||
if(buf && !strchr(buf, '\n'))
|
||||
if (buf && !strchr(buf, '\n'))
|
||||
eprintf("comm: '%s': line too long.\n", name);
|
||||
return buf;
|
||||
}
|
||||
|
@ -105,7 +106,7 @@ finish(int pos, FILE *f, char *name)
|
|||
{
|
||||
char buf[LINE_MAX+1];
|
||||
|
||||
while(nextline(buf, sizeof(buf), f, name))
|
||||
while (nextline(buf, sizeof(buf), f, name))
|
||||
printline(pos, buf);
|
||||
exit(1);
|
||||
}
|
||||
|
|
3
cp.c
3
cp.c
|
@ -2,6 +2,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "fs.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -46,7 +47,7 @@ main(int argc, char *argv[])
|
|||
if (argc < 2)
|
||||
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]);
|
||||
enmasse(argc, argv, cp);
|
||||
return cp_status;
|
||||
|
|
73
cut.c
73
cut.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -30,21 +31,21 @@ insert(Range *r)
|
|||
{
|
||||
Range *l, *p, *t;
|
||||
|
||||
for(p = NULL, l = list; l; p = l, l = l->next) {
|
||||
if(r->max && r->max + 1 < l->min) {
|
||||
for (p = NULL, l = list; l; p = l, l = l->next) {
|
||||
if (r->max && r->max + 1 < l->min) {
|
||||
r->next = l;
|
||||
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);
|
||||
for(p = l, t = l->next; t; p = t, t = t->next)
|
||||
if(r->max && r->max + 1 < t->min)
|
||||
for (p = l, t = l->next; t; p = t, t = t->next)
|
||||
if (r->max && r->max + 1 < t->min)
|
||||
break;
|
||||
l->max = (p->max && r->max) ? MAX(p->max, r->max) : 0;
|
||||
l->next = t;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(p)
|
||||
if (p)
|
||||
p->next = r;
|
||||
else
|
||||
list = r;
|
||||
|
@ -57,19 +58,19 @@ parselist(char *str)
|
|||
size_t n = 1;
|
||||
Range *r;
|
||||
|
||||
for(s = str; *s; s++) {
|
||||
if(*s == ' ')
|
||||
for (s = str; *s; s++) {
|
||||
if (*s == ' ')
|
||||
*s = ',';
|
||||
if(*s == ',')
|
||||
if (*s == ',')
|
||||
n++;
|
||||
}
|
||||
if(!(r = malloc(n * sizeof(Range))))
|
||||
if (!(r = malloc(n * sizeof(Range))))
|
||||
eprintf("malloc:");
|
||||
for(s = str; n; n--, s++) {
|
||||
for (s = str; n; n--, s++) {
|
||||
r->min = (*s == '-') ? 1 : strtoul(s, &s, 10);
|
||||
r->max = (*s == '-') ? strtoul(s + 1, &s, 10) : r->min;
|
||||
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");
|
||||
insert(r++);
|
||||
}
|
||||
|
@ -79,7 +80,7 @@ static void
|
|||
freelist(void) {
|
||||
Range *l = list, *next;
|
||||
|
||||
while(l) {
|
||||
while (l) {
|
||||
next = l->next;
|
||||
free(l);
|
||||
l->next = NULL;
|
||||
|
@ -93,21 +94,21 @@ seek(const char *s, size_t pos, size_t *prev, size_t count)
|
|||
const char *t;
|
||||
size_t n = pos - *prev;
|
||||
|
||||
if(mode == 'b') {
|
||||
if((t = memchr(s, 0, n)))
|
||||
if (mode == 'b') {
|
||||
if ((t = memchr(s, 0, n)))
|
||||
return t - s;
|
||||
if(nflag)
|
||||
while(n && !UTF8_POINT(s[n]))
|
||||
if (nflag)
|
||||
while (n && !UTF8_POINT(s[n]))
|
||||
n--;
|
||||
*prev += n;
|
||||
return n;
|
||||
} else if(mode == 'c') {
|
||||
for(n++, t = s; *t; t++)
|
||||
if(UTF8_POINT(*t) && !--n)
|
||||
} else if (mode == 'c') {
|
||||
for (n++, t = s; *t; t++)
|
||||
if (UTF8_POINT(*t) && !--n)
|
||||
break;
|
||||
} else {
|
||||
for(t = (count < 2) ? s : s + 1; n && *t; t++)
|
||||
if(*t == delim && !--n && count)
|
||||
for (t = (count < 2) ? s : s + 1; n && *t; t++)
|
||||
if (*t == delim && !--n && count)
|
||||
break;
|
||||
}
|
||||
*prev = pos;
|
||||
|
@ -122,24 +123,24 @@ cut(FILE *fp)
|
|||
ssize_t len;
|
||||
Range *r;
|
||||
|
||||
while((len = agetline(&buf, &size, fp)) != -1) {
|
||||
if(len && buf[len - 1] == '\n')
|
||||
while ((len = agetline(&buf, &size, fp)) != -1) {
|
||||
if (len && buf[len - 1] == '\n')
|
||||
buf[len - 1] = '\0';
|
||||
if(mode == 'f' && !strchr(buf, delim)) {
|
||||
if(!sflag)
|
||||
if (mode == 'f' && !strchr(buf, delim)) {
|
||||
if (!sflag)
|
||||
puts(buf);
|
||||
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++);
|
||||
if(!*s)
|
||||
if (!*s)
|
||||
break;
|
||||
if(!r->max) {
|
||||
if (!r->max) {
|
||||
fputs(s, stdout);
|
||||
break;
|
||||
}
|
||||
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:");
|
||||
}
|
||||
putchar('\n');
|
||||
|
@ -172,22 +173,22 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(!mode)
|
||||
if (!mode)
|
||||
usage();
|
||||
if(!argc)
|
||||
if (!argc)
|
||||
cut(stdin);
|
||||
else {
|
||||
for(; argc--; argv++) {
|
||||
if(strcmp(*argv, "-"))
|
||||
for (; argc--; argv++) {
|
||||
if (strcmp(*argv, "-"))
|
||||
fp = fopen(*argv, "r");
|
||||
else
|
||||
fp = stdin;
|
||||
if(!fp) {
|
||||
if (!fp) {
|
||||
weprintf("fopen %s:", *argv);
|
||||
continue;
|
||||
}
|
||||
cut(fp);
|
||||
if(fp != stdin)
|
||||
if (fp != stdin)
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
|
5
date.c
5
date.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -33,9 +34,9 @@ main(int argc, char *argv[])
|
|||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
if(argc > 0 && argv[0][0] == '+')
|
||||
if (argc > 0 && argv[0][0] == '+')
|
||||
fmt = &argv[0][1];
|
||||
if(!(now = tztime(&t)))
|
||||
if (!(now = tztime(&t)))
|
||||
eprintf("%stime failed\n", tz);
|
||||
|
||||
strftime(buf, sizeof buf, fmt, now);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -19,7 +20,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc < 1)
|
||||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
puts(dirname(argv[0]));
|
||||
|
|
1
du.c
1
du.c
|
@ -8,6 +8,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static long blksize = 512;
|
||||
|
|
5
echo.c
5
echo.c
|
@ -2,6 +2,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -23,9 +24,9 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
for(; argc > 0; argc--, argv++)
|
||||
for (; argc > 0; argc--, argv++)
|
||||
putword(argv[0]);
|
||||
if(!nflag)
|
||||
if (!nflag)
|
||||
putchar('\n');
|
||||
|
||||
return 0;
|
||||
|
|
7
env.c
7
env.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
extern char **environ;
|
||||
|
@ -29,15 +30,15 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
for(; *argv && strchr(*argv, '='); argv++)
|
||||
for (; *argv && strchr(*argv, '='); argv++)
|
||||
putenv(*argv);
|
||||
|
||||
if(*argv) {
|
||||
if (*argv) {
|
||||
execvp(*argv, argv);
|
||||
enprintf(127 - (errno != EEXIST), "env: '%s':", *argv);
|
||||
}
|
||||
|
||||
while(environ && *environ)
|
||||
while (environ && *environ)
|
||||
printf("%s\n", *environ++);
|
||||
|
||||
return 0;
|
||||
|
|
1
expand.c
1
expand.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
typedef struct {
|
||||
|
|
25
fold.c
25
fold.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -42,11 +43,11 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
fold(stdin, width);
|
||||
} else {
|
||||
for(; argc > 0; argc--, argv++) {
|
||||
if(!(fp = fopen(argv[0], "r"))) {
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
if (!(fp = fopen(argv[0], "r"))) {
|
||||
weprintf("fopen %s:", argv[0]);
|
||||
continue;
|
||||
}
|
||||
|
@ -64,7 +65,7 @@ fold(FILE *fp, long width)
|
|||
char *buf = NULL;
|
||||
size_t size = 0;
|
||||
|
||||
while(agetline(&buf, &size, fp) != -1)
|
||||
while (agetline(&buf, &size, fp) != -1)
|
||||
foldline(buf, width);
|
||||
free(buf);
|
||||
}
|
||||
|
@ -79,18 +80,18 @@ foldline(const char *str, long width)
|
|||
|
||||
do {
|
||||
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];
|
||||
if(!UTF8_POINT(c) && !bflag)
|
||||
if (!UTF8_POINT(c) && !bflag)
|
||||
continue;
|
||||
if(sflag && isspace(c)) {
|
||||
if (sflag && isspace(c)) {
|
||||
space = true;
|
||||
n = j+1;
|
||||
}
|
||||
else if(!space)
|
||||
else if (!space)
|
||||
n = j;
|
||||
|
||||
if(!bflag && iscntrl(c))
|
||||
if (!bflag && iscntrl(c))
|
||||
switch(c) {
|
||||
case '\b':
|
||||
col--;
|
||||
|
@ -105,9 +106,9 @@ foldline(const char *str, long width)
|
|||
else
|
||||
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:");
|
||||
if(str[n])
|
||||
if (str[n])
|
||||
putchar('\n');
|
||||
} while(str[i = n] && str[i] != '\n');
|
||||
} while (str[i = n] && str[i] != '\n');
|
||||
}
|
||||
|
|
39
grep.c
39
grep.c
|
@ -5,6 +5,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -62,41 +63,41 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0 && !eflag)
|
||||
if (argc == 0 && !eflag)
|
||||
usage(); /* no pattern */
|
||||
|
||||
/* If -e is not specified treat it as if it were */
|
||||
if(!eflag) {
|
||||
if (!eflag) {
|
||||
addpattern(argv[0]);
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
/* Compile regex for all search patterns */
|
||||
for(pnode = phead; pnode; pnode = pnode->next) {
|
||||
if((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
|
||||
for (pnode = phead; pnode; pnode = pnode->next) {
|
||||
if ((n = regcomp(&pnode->preg, pnode->pattern, flags)) != 0) {
|
||||
regerror(n, &pnode->preg, buf, sizeof buf);
|
||||
enprintf(Error, "invalid pattern: %s\n", buf);
|
||||
}
|
||||
}
|
||||
many = (argc > 1);
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
match = grep(stdin, "<stdin>");
|
||||
} else {
|
||||
for(i = 0; i < argc; i++) {
|
||||
if(!(fp = fopen(argv[i], "r"))) {
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (!(fp = fopen(argv[i], "r"))) {
|
||||
weprintf("fopen %s:", argv[i]);
|
||||
match = Error;
|
||||
continue;
|
||||
}
|
||||
m = grep(fp, argv[i]);
|
||||
if(m == Error || (match != Error && m == Match))
|
||||
if (m == Error || (match != Error && m == Match))
|
||||
match = m;
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
pnode = phead;
|
||||
while(pnode) {
|
||||
while (pnode) {
|
||||
tmp = pnode->next;
|
||||
regfree(&pnode->preg);
|
||||
free(pnode->pattern);
|
||||
|
@ -111,9 +112,9 @@ addpattern(const char *pattern)
|
|||
{
|
||||
struct plist *pnode;
|
||||
|
||||
if(!(pnode = malloc(sizeof(*pnode))))
|
||||
if (!(pnode = malloc(sizeof(*pnode))))
|
||||
eprintf("malloc:");
|
||||
if(!(pnode->pattern = strdup(pattern)))
|
||||
if (!(pnode->pattern = strdup(pattern)))
|
||||
eprintf("strdup:");
|
||||
pnode->next = phead;
|
||||
phead = pnode;
|
||||
|
@ -128,14 +129,14 @@ grep(FILE *fp, const char *str)
|
|||
struct plist *pnode;
|
||||
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. */
|
||||
if (len && buf[len - 1] == '\n')
|
||||
buf[len - 1] = '\0';
|
||||
for(pnode = phead; pnode; pnode = pnode->next) {
|
||||
if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
|
||||
for (pnode = phead; pnode; pnode = pnode->next) {
|
||||
if (regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
|
||||
continue;
|
||||
switch(mode) {
|
||||
switch (mode) {
|
||||
case 'c':
|
||||
c++;
|
||||
break;
|
||||
|
@ -145,9 +146,9 @@ grep(FILE *fp, const char *str)
|
|||
case 'q':
|
||||
exit(Match);
|
||||
default:
|
||||
if(many)
|
||||
if (many)
|
||||
printf("%s:", str);
|
||||
if(mode == 'n')
|
||||
if (mode == 'n')
|
||||
printf("%ld:", n);
|
||||
puts(buf);
|
||||
break;
|
||||
|
@ -155,10 +156,10 @@ grep(FILE *fp, const char *str)
|
|||
match = Match;
|
||||
}
|
||||
}
|
||||
if(mode == 'c')
|
||||
if (mode == 'c')
|
||||
printf("%ld\n", c);
|
||||
end:
|
||||
if(ferror(fp)) {
|
||||
if (ferror(fp)) {
|
||||
weprintf("%s: read error:", str);
|
||||
match = Error;
|
||||
}
|
||||
|
|
13
head.c
13
head.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -31,11 +32,11 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
head(stdin, "<stdin>", n);
|
||||
} else {
|
||||
for(; argc > 0; argc--, argv++) {
|
||||
if(!(fp = fopen(argv[0], "r"))) {
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
if (!(fp = fopen(argv[0], "r"))) {
|
||||
weprintf("fopen %s:", argv[0]);
|
||||
continue;
|
||||
}
|
||||
|
@ -55,12 +56,12 @@ head(FILE *fp, const char *str, long n)
|
|||
ssize_t len;
|
||||
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);
|
||||
if(buf[len - 1] == '\n')
|
||||
if (buf[len - 1] == '\n')
|
||||
i++;
|
||||
}
|
||||
free(buf);
|
||||
if(ferror(fp))
|
||||
if (ferror(fp))
|
||||
eprintf("%s: read error:", str);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
|
1
kill.c
1
kill.c
|
@ -9,6 +9,7 @@
|
|||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
struct {
|
||||
|
|
3
link.c
3
link.c
|
@ -1,6 +1,7 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
int
|
||||
|
@ -8,7 +9,7 @@ main(int argc, char *argv[])
|
|||
{
|
||||
argv0 = argv[0];
|
||||
|
||||
if(argc != 3)
|
||||
if (argc != 3)
|
||||
eprintf("usage: %s target linkname\n", argv0);
|
||||
if (link(argv[1], argv[2]) < 0)
|
||||
eprintf("link:");
|
||||
|
|
1
ln.c
1
ln.c
|
@ -6,6 +6,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
int
|
||||
|
|
109
ls.c
109
ls.c
|
@ -10,6 +10,7 @@
|
|||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
typedef struct {
|
||||
|
@ -89,15 +90,15 @@ main(int argc, char *argv[])
|
|||
} ARGEND;
|
||||
|
||||
many = (argc > 1);
|
||||
if(argc == 0)
|
||||
if (argc == 0)
|
||||
*--argv = ".", argc++;
|
||||
|
||||
if(!(ents = malloc(argc * sizeof *ents)))
|
||||
if (!(ents = malloc(argc * sizeof *ents)))
|
||||
eprintf("malloc:");
|
||||
for(i = 0; i < argc; i++)
|
||||
for (i = 0; i < argc; i++)
|
||||
mkent(&ents[i], argv[i], true);
|
||||
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]);
|
||||
|
||||
return 0;
|
||||
|
@ -108,7 +109,7 @@ entcmp(const void *va, const void *vb)
|
|||
{
|
||||