Fix coding style
It was about damn time. Consistency is very important in such a big codebase.
This commit is contained in:
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)
|
|||
{
|
||||
const Entry *a = va, *b = vb;
|
||||
|
||||
if(tflag)
|
||||
if (tflag)
|
||||
return b->mtime - a->mtime;
|
||||
else
|
||||
return strcmp(a->name, b->name);
|
||||
|
@ -117,7 +118,7 @@ entcmp(const void *va, const void *vb)
|
|||
static void
|
||||
ls(Entry *ent)
|
||||
{
|
||||
if(S_ISDIR(ent->mode) && !dflag) {
|
||||
if (S_ISDIR(ent->mode) && !dflag) {
|
||||
lsdir(ent->name);
|
||||
} else {
|
||||
output(ent);
|
||||
|
@ -135,42 +136,42 @@ lsdir(const char *path)
|
|||
size_t sz;
|
||||
|
||||
cwd = agetcwd();
|
||||
if(!(dp = opendir(path)))
|
||||
if (!(dp = opendir(path)))
|
||||
eprintf("opendir %s:", path);
|
||||
if(chdir(path) == -1)
|
||||
if (chdir(path) == -1)
|
||||
eprintf("chdir %s:", path);
|
||||
|
||||
if(many) {
|
||||
if(!first)
|
||||
if (many) {
|
||||
if (!first)
|
||||
putchar('\n');
|
||||
printf("%s:\n", path);
|
||||
first = false;
|
||||
}
|
||||
|
||||
while((d = readdir(dp))) {
|
||||
if(d->d_name[0] == '.' && !aflag)
|
||||
while ((d = readdir(dp))) {
|
||||
if (d->d_name[0] == '.' && !aflag)
|
||||
continue;
|
||||
if(Uflag){
|
||||
if (Uflag){
|
||||
mkent(&ent, d->d_name, Fflag || lflag || iflag);
|
||||
output(&ent);
|
||||
} else {
|
||||
if(!(ents = realloc(ents, ++n * sizeof *ents)))
|
||||
if (!(ents = realloc(ents, ++n * sizeof *ents)))
|
||||
eprintf("realloc:");
|
||||
if(!(p = malloc((sz = strlen(d->d_name)+1))))
|
||||
if (!(p = malloc((sz = strlen(d->d_name)+1))))
|
||||
eprintf("malloc:");
|
||||
memcpy(p, d->d_name, sz);
|
||||
mkent(&ents[n-1], p, tflag || Fflag || lflag || iflag);
|
||||
}
|
||||
}
|
||||
closedir(dp);
|
||||
if(!Uflag){
|
||||
if (!Uflag){
|
||||
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]);
|
||||
free(ents[rflag ? n-i-1 : i].name);
|
||||
}
|
||||
}
|
||||
if(chdir(cwd) == -1)
|
||||
if (chdir(cwd) == -1)
|
||||
eprintf("chdir %s:", cwd);
|
||||
free(ents);
|
||||
free(cwd);
|
||||
|
@ -182,9 +183,9 @@ mkent(Entry *ent, char *path, bool dostat)
|
|||
struct stat st;
|
||||
|
||||
ent->name = path;
|
||||
if(!dostat)
|
||||
if (!dostat)
|
||||
return;
|
||||
if(lstat(path, &st) == -1)
|
||||
if (lstat(path, &st) == -1)
|
||||
eprintf("lstat %s:", path);
|
||||
ent->mode = st.st_mode;
|
||||
ent->nlink = st.st_nlink;
|
||||
|
@ -198,20 +199,20 @@ mkent(Entry *ent, char *path, bool dostat)
|
|||
static char *
|
||||
indicator(mode_t mode)
|
||||
{
|
||||
if(!Fflag)
|
||||
if (!Fflag)
|
||||
return "";
|
||||
|
||||
if(S_ISLNK(mode))
|
||||
if (S_ISLNK(mode))
|
||||
return "@";
|
||||
else if(S_ISDIR(mode))
|
||||
else if (S_ISDIR(mode))
|
||||
return "/";
|
||||
else if(S_ISFIFO(mode))
|
||||
else if (S_ISFIFO(mode))
|
||||
return "|";
|
||||
else if(S_ISSOCK(mode))
|
||||
else if (S_ISSOCK(mode))
|
||||
return "=";
|
||||
else if(mode & S_IXUSR ||
|
||||
mode & S_IXGRP ||
|
||||
mode & S_IXOTH)
|
||||
else if (mode & S_IXUSR ||
|
||||
mode & S_IXGRP ||
|
||||
mode & S_IXOTH)
|
||||
return "*";
|
||||
else
|
||||
return "";
|
||||
|
@ -231,69 +232,69 @@ output(Entry *ent)
|
|||
|
||||
if (iflag)
|
||||
printf("%lu ", (unsigned long)ent->ino);
|
||||
if(!lflag) {
|
||||
if (!lflag) {
|
||||
printf("%s%s\n", ent->name, indicator(ent->mode));
|
||||
return;
|
||||
}
|
||||
if(S_ISREG(ent->mode))
|
||||
if (S_ISREG(ent->mode))
|
||||
mode[0] = '-';
|
||||
else if(S_ISBLK(ent->mode))
|
||||
else if (S_ISBLK(ent->mode))
|
||||
mode[0] = 'b';
|
||||
else if(S_ISCHR(ent->mode))
|
||||
else if (S_ISCHR(ent->mode))
|
||||
mode[0] = 'c';
|
||||
else if(S_ISDIR(ent->mode))
|
||||
else if (S_ISDIR(ent->mode))
|
||||
mode[0] = 'd';
|
||||
else if(S_ISFIFO(ent->mode))
|
||||
else if (S_ISFIFO(ent->mode))
|
||||
mode[0] = 'p';
|
||||
else if(S_ISLNK(ent->mode))
|
||||
else if (S_ISLNK(ent->mode))
|
||||
mode[0] = 'l';
|
||||
else if(S_ISSOCK(ent->mode))
|
||||
else if (S_ISSOCK(ent->mode))
|
||||
mode[0] = 's';
|
||||
else
|
||||
mode[0] = '?';
|
||||
|
||||
if(ent->mode & S_IRUSR) mode[1] = 'r';
|
||||
if(ent->mode & S_IWUSR) mode[2] = 'w';
|
||||
if(ent->mode & S_IXUSR) mode[3] = 'x';
|
||||
if(ent->mode & S_IRGRP) mode[4] = 'r';
|
||||
if(ent->mode & S_IWGRP) mode[5] = 'w';
|
||||
if(ent->mode & S_IXGRP) mode[6] = 'x';
|
||||
if(ent->mode & S_IROTH) mode[7] = 'r';
|
||||
if(ent->mode & S_IWOTH) mode[8] = 'w';
|
||||
if(ent->mode & S_IXOTH) mode[9] = 'x';
|
||||
if (ent->mode & S_IRUSR) mode[1] = 'r';
|
||||
if (ent->mode & S_IWUSR) mode[2] = 'w';
|
||||
if (ent->mode & S_IXUSR) mode[3] = 'x';
|
||||
if (ent->mode & S_IRGRP) mode[4] = 'r';
|
||||
if (ent->mode & S_IWGRP) mode[5] = 'w';
|
||||
if (ent->mode & S_IXGRP) mode[6] = 'x';
|
||||
if (ent->mode & S_IROTH) mode[7] = 'r';
|
||||
if (ent->mode & S_IWOTH) mode[8] = 'w';
|
||||
if (ent->mode & S_IXOTH) mode[9] = 'x';
|
||||
|
||||
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_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
|
||||
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_ISVTX) mode[9] = (mode[9] == 'x') ? 't' : 'T';
|
||||
|
||||
errno = 0;
|
||||
pw = getpwuid(ent->uid);
|
||||
if(errno || !pw)
|
||||
if (errno || !pw)
|
||||
snprintf(pwname, sizeof(pwname), "%d", ent->uid);
|
||||
else
|
||||
snprintf(pwname, sizeof(pwname), "%s", pw->pw_name);
|
||||
|
||||
errno = 0;
|
||||
gr = getgrgid(ent->gid);
|
||||
if(errno || !gr)
|
||||
if (errno || !gr)
|
||||
snprintf(grname, sizeof(grname), "%d", ent->gid);
|
||||
else
|
||||
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";
|
||||
else
|
||||
fmt = "%b %d %H:%M";
|
||||
|
||||
strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
|
||||
printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
|
||||
if(hflag)
|
||||
if (hflag)
|
||||
printf("%10s ", humansize((unsigned long)ent->size));
|
||||
else
|
||||
printf("%10lu ", (unsigned long)ent->size);
|
||||
printf("%s %s%s", buf, ent->name, indicator(ent->mode));
|
||||
if(S_ISLNK(ent->mode)) {
|
||||
if((len = readlink(ent->name, buf, sizeof buf)) == -1)
|
||||
if (S_ISLNK(ent->mode)) {
|
||||
if ((len = readlink(ent->name, buf, sizeof buf)) == -1)
|
||||
eprintf("readlink %s:", ent->name);
|
||||
buf[len] = '\0';
|
||||
mkent(&entlnk, buf, Fflag);
|
||||
|
|
3
md5sum.c
3
md5sum.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "crypt.h"
|
||||
#include "md5.h"
|
||||
|
@ -37,7 +38,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(cflag)
|
||||
if (cflag)
|
||||
return cryptcheck(checkfile, argc, argv, &md5_ops, md, sizeof(md));
|
||||
return cryptmain(argc, argv, &md5_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
15
mkdir.c
15
mkdir.c
|
@ -7,6 +7,7 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void mkdirp(char *);
|
||||
|
@ -39,10 +40,10 @@ main(int argc, char *argv[])
|
|||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
for(; argc > 0; argc--, argv++) {
|
||||
if(pflag) {
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
if (pflag) {
|
||||
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]);
|
||||
}
|
||||
if (mflag)
|
||||
|
@ -59,11 +60,11 @@ mkdirp(char *path)
|
|||
char *p = path;
|
||||
|
||||
do {
|
||||
if(*p && (p = strchr(&p[1], '/')))
|
||||
if (*p && (p = strchr(&p[1], '/')))
|
||||
*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);
|
||||
if(p)
|
||||
if (p)
|
||||
*p = '/';
|
||||
} while(p);
|
||||
} while (p);
|
||||
}
|
||||
|
|
5
mkfifo.c
5
mkfifo.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -28,8 +29,8 @@ main(int argc, char *argv[])
|
|||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
for(; argc > 0; argc--, argv++)
|
||||
if(mkfifo(argv[0], mode) == -1)
|
||||
for (; argc > 0; argc--, argv++)
|
||||
if (mkfifo(argv[0], mode) == -1)
|
||||
eprintf("mkfifo %s:", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
|
1
mktemp.c
1
mktemp.c
|
@ -5,6 +5,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
|
3
mv.c
3
mv.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "fs.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -30,7 +31,7 @@ main(int argc, char *argv[])
|
|||
if (argc < 2)
|
||||
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]);
|
||||
enmasse(argc, &argv[0], mv);
|
||||
|
||||
|
|
3
nice.c
3
nice.c
|
@ -6,6 +6,7 @@
|
|||
#include <sys/resource.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -29,7 +30,7 @@ main(int argc, char *argv[])
|
|||
break;
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0)
|
||||
if (argc == 0)
|
||||
usage();
|
||||
|
||||
errno = 0;
|
||||
|
|
21
nl.c
21
nl.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -31,9 +32,9 @@ main(int argc, char *argv[])
|
|||
case 'b':
|
||||
r = EARGF(usage());
|
||||
mode = r[0];
|
||||
if(r[0] == 'p') {
|
||||
if (r[0] == 'p') {
|
||||
regcomp(&preg, &r[1], REG_NOSUB);
|
||||
} else if(!strchr("ant", mode)) {
|
||||
} else if (!strchr("ant", mode)) {
|
||||
usage();
|
||||
}
|
||||
break;
|
||||
|
@ -47,10 +48,10 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
nl(stdin);
|
||||
} else for(; argc > 0; argc--, argv++) {
|
||||
if(!(fp = fopen(argv[0], "r"))) {
|
||||
} else for (; argc > 0; argc--, argv++) {
|
||||
if (!(fp = fopen(argv[0], "r"))) {
|
||||
weprintf("fopen %s:", argv[0]);
|
||||
continue;
|
||||
}
|
||||
|
@ -68,11 +69,11 @@ nl(FILE *fp)
|
|||
long n = 0;
|
||||
size_t size = 0;
|
||||
|
||||
while(agetline(&buf, &size, fp) != -1) {
|
||||
if((mode == 'a')
|
||||
|| (mode == 'p'
|
||||
&& !regexec(&preg, buf, 0, NULL, 0))
|
||||
|| (mode == 't' && buf[0] != '\n')) {
|
||||
while (agetline(&buf, &size, fp) != -1) {
|
||||
if ((mode == 'a')
|
||||
|| (mode == 'p'
|
||||
&& !regexec(&preg, buf, 0, NULL, 0))
|
||||
|| (mode == 't' && buf[0] != '\n')) {
|
||||
printf("%6ld%s%s", n += incr, sep, buf);
|
||||
} else {
|
||||
printf(" %s", buf);
|
||||
|
|
17
nohup.c
17
nohup.c
|
@ -5,6 +5,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
enum { Error = 127, Found = 126 };
|
||||
|
@ -25,23 +26,23 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0)
|
||||
if (argc == 0)
|
||||
usage();
|
||||
|
||||
if(signal(SIGHUP, SIG_IGN) == SIG_ERR)
|
||||
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
|
||||
enprintf(Error, "signal HUP:");
|
||||
|
||||
if(isatty(STDOUT_FILENO)) {
|
||||
if((fd = open("nohup.out", O_APPEND|O_CREAT,
|
||||
S_IRUSR|S_IWUSR)) == -1) {
|
||||
if (isatty(STDOUT_FILENO)) {
|
||||
if ((fd = open("nohup.out", O_APPEND|O_CREAT,
|
||||
S_IRUSR|S_IWUSR)) == -1) {
|
||||
enprintf(Error, "open nohup.out:");
|
||||
}
|
||||
if(dup2(fd, STDOUT_FILENO) == -1)
|
||||
if (dup2(fd, STDOUT_FILENO) == -1)
|
||||
enprintf(Error, "dup2:");
|
||||
close(fd);
|
||||
}
|
||||
if(isatty(STDERR_FILENO))
|
||||
if(dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
|
||||
if (isatty(STDERR_FILENO))
|
||||
if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
|
||||
enprintf(Error, "dup2:");
|
||||
|
||||
execvp(argv[0], &argv[0]);
|
||||
|
|
65
paste.c
65
paste.c
|
@ -6,6 +6,7 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
typedef struct {
|
||||
|
@ -48,48 +49,48 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0)
|
||||
if (argc == 0)
|
||||
usage();
|
||||
|
||||
/* populate delimeters */
|
||||
if(!adelim)
|
||||
if (!adelim)
|
||||
adelim = "\t";
|
||||
|
||||
len = mbstowcs(NULL, adelim, 0);
|
||||
if(len == (size_t)-1)
|
||||
if (len == (size_t) - 1)
|
||||
eprintf("invalid delimiter\n");
|
||||
|
||||
if(!(delim = malloc((len + 1) * sizeof(*delim))))
|
||||
if (!(delim = malloc((len + 1) * sizeof(*delim))))
|
||||
eprintf("out of memory\n");
|
||||
|
||||
mbstowcs(delim, adelim, len);
|
||||
len = unescape(delim);
|
||||
if(len == 0)
|
||||
if (len == 0)
|
||||
eprintf("no delimiters specified\n");
|
||||
|
||||
/* populate file list */
|
||||
if(!(dsc = malloc(argc * sizeof(*dsc))))
|
||||
if (!(dsc = malloc(argc * sizeof(*dsc))))
|
||||
eprintf("out of memory\n");
|
||||
|
||||
for(i = 0; i < argc; i++) {
|
||||
if(strcmp(argv[i], "-") == 0)
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-") == 0)
|
||||
dsc[i].fp = stdin;
|
||||
else
|
||||
dsc[i].fp = fopen(argv[i], "r");
|
||||
|
||||
if(!dsc[i].fp)
|
||||
if (!dsc[i].fp)
|
||||
eprintf("can't open '%s':", argv[i]);
|
||||
|
||||
dsc[i].name = argv[i];
|
||||
}
|
||||
|
||||
if(seq)
|
||||
if (seq)
|
||||
sequential(dsc, argc, delim, len);
|
||||
else
|
||||
parallel(dsc, argc, delim, len);
|
||||
|
||||
for(i = 0; i < argc; i++) {
|
||||
if(dsc[i].fp != stdin)
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (dsc[i].fp != stdin)
|
||||
(void)fclose(dsc[i].fp);
|
||||
}
|
||||
|
||||
|
@ -106,9 +107,9 @@ unescape(wchar_t *delim)
|
|||
size_t i;
|
||||
size_t len;
|
||||
|
||||
for(i = 0, len = 0; (c = delim[i++]) != '\0'; len++) {
|
||||
if(c == '\\') {
|
||||
switch(delim[i++]) {
|
||||
for (i = 0, len = 0; (c = delim[i++]) != '\0'; len++) {
|
||||
if (c == '\\') {
|
||||
switch (delim[i++]) {
|
||||
case 'n':
|
||||
delim[len] = '\n';
|
||||
break;
|
||||
|
@ -138,7 +139,7 @@ in(Fdescr *f)
|
|||
{
|
||||
wint_t c = fgetwc(f->fp);
|
||||
|
||||
if(c == WEOF && ferror(f->fp))
|
||||
if (c == WEOF && ferror(f->fp))
|
||||
eprintf("'%s' read error:", f->name);
|
||||
|
||||
return c;
|
||||
|
@ -148,7 +149,7 @@ static void
|
|||
out(wchar_t c)
|
||||
{
|
||||
putwchar(c);
|
||||
if(ferror(stdout))
|
||||
if (ferror(stdout))
|
||||
eprintf("write error:");
|
||||
}
|
||||
|
||||
|
@ -159,26 +160,26 @@ sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
|
|||
size_t d;
|
||||
wint_t c, last;
|
||||
|
||||
for(i = 0; i < len; i++) {
|
||||
for (i = 0; i < len; i++) {
|
||||
d = 0;
|
||||
last = WEOF;
|
||||
|
||||
while((c = in(&dsc[i])) != WEOF) {
|
||||
if(last == '\n') {
|
||||
if(delim[d] != '\0')
|
||||
while ((c = in(&dsc[i])) != WEOF) {
|
||||
if (last == '\n') {
|
||||
if (delim[d] != '\0')
|
||||
out(delim[d]);
|
||||
|
||||
d++;
|
||||
d %= cnt;
|
||||
}
|
||||
|
||||
if(c != '\n')
|
||||
if (c != '\n')
|
||||
out((wchar_t)c);
|
||||
|
||||
last = c;
|
||||
}
|
||||
|
||||
if(last == '\n')
|
||||
if (last == '\n')
|
||||
out((wchar_t)last);
|
||||
}
|
||||
}
|
||||
|
@ -192,36 +193,36 @@ parallel(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
|
|||
|
||||
do {
|
||||
last = 0;
|
||||
for(i = 0; i < len; i++) {
|
||||
for (i = 0; i < len; i++) {
|
||||
d = delim[i % cnt];
|
||||
|
||||
do {
|
||||
o = in(&dsc[i]);
|
||||
c = o;
|
||||
switch(c) {
|
||||
switch (c) {
|
||||
case WEOF:
|
||||
if(last == 0)
|
||||
if (last == 0)
|
||||
break;
|
||||
|
||||
o = '\n';
|
||||
/* fallthrough */
|
||||
case '\n':
|
||||
if(i != len - 1)
|
||||
if (i != len - 1)
|
||||
o = d;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(o != WEOF) {
|
||||
if (o != WEOF) {
|
||||
/* pad with delimiters up to this point */
|
||||
while(++last < i) {
|
||||
if(d != '\0')
|
||||
while (++last < i) {
|
||||
if (d != '\0')
|
||||
out(d);
|
||||
}
|
||||
out((wchar_t)o);
|
||||
}
|
||||
} while(c != '\n' && c != WEOF);
|
||||
} while (c != '\n' && c != WEOF);
|
||||
}
|
||||
} while(last > 0);
|
||||
} while (last > 0);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
extern char **environ;
|
||||
|
@ -21,14 +22,14 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 1) {
|
||||
while(*environ)
|
||||
if (argc == 1) {
|
||||
while (*environ)
|
||||
printf("%s\n", *environ++);
|
||||
|
||||
return 0;
|
||||
}
|
||||
while(*++argv) {
|
||||
if((var = getenv(*argv)))
|
||||
if ((var = getenv(*argv)))
|
||||
printf("%s\n", var);
|
||||
}
|
||||
|
||||
|
|
7
pwd.c
7
pwd.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static const char *getpwd(const char *cwd);
|
||||
|
@ -40,11 +41,11 @@ getpwd(const char *cwd)
|
|||
const char *pwd;
|
||||
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;
|
||||
if(stat(cwd, &cst) == -1)
|
||||
if (stat(cwd, &cst) == -1)
|
||||
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;
|
||||
else
|
||||
return cwd;
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
|
25
renice.c
25
renice.c
|
@ -7,6 +7,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static int strtop(const char *);
|
||||
|
@ -43,31 +44,31 @@ main(int argc, char *argv[])
|
|||
break;
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0 || !adj)
|
||||
if (argc == 0 || !adj)
|
||||
usage();
|
||||
|
||||
val = estrtol(adj, 10);
|
||||
for(i = 0; i < argc; i++) {
|
||||
for (i = 0; i < argc; i++) {
|
||||
int who = -1;
|
||||
|
||||
if(which == PRIO_USER) {
|
||||
if (which == PRIO_USER) {
|
||||
const struct passwd *pwd;
|
||||
|
||||
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;
|
||||
else if(errno != 0) {
|
||||
else if (errno != 0) {
|
||||
perror("can't read passwd");
|
||||
status = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if(who < 0)
|
||||
if (who < 0)
|
||||
who = strtop(argv[i]);
|
||||
|
||||
if(who < 0 || !renice(which, who, val))
|
||||
if (who < 0 || !renice(which, who, val))
|
||||
status = 1;
|
||||
}
|
||||
|
||||
|
@ -82,11 +83,11 @@ strtop(const char *s)
|
|||
|
||||
errno = 0;
|
||||
n = strtol(s, &end, 10);
|
||||
if(*end != '\0') {
|
||||
if (*end != '\0') {
|
||||
fprintf(stderr, "%s: not an integer\n", s);
|
||||
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);
|
||||
return -1;
|
||||
}
|
||||
|
@ -99,14 +100,14 @@ renice(int which, int who, long adj)
|
|||
{
|
||||
errno = 0;
|
||||
adj += getpriority(which, who);
|
||||
if(errno != 0) {
|
||||
if (errno != 0) {
|
||||
fprintf(stderr, "can't get %d nice level: %s\n",
|
||||
who, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
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",
|
||||
who, strerror(errno));
|
||||
return false;
|
||||
|
|
3
rm.c
3
rm.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "fs.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -35,7 +36,7 @@ main(int argc, char *argv[])
|
|||
return 0;
|
||||
}
|
||||
|
||||
for(; argc > 0; argc--, argv++)
|
||||
for (; argc > 0; argc--, argv++)
|
||||
rm(argv[0]);
|
||||
|
||||
return 0;
|
||||
|
|
5
rmdir.c
5
rmdir.c
|
@ -4,6 +4,7 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -23,8 +24,8 @@ main(int argc, char *argv[])
|
|||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
for(; argc > 0; argc--, argv++)
|
||||
if(rmdir(argv[0]) == -1)
|
||||
for (; argc > 0; argc--, argv++)
|
||||
if (rmdir(argv[0]) == -1)
|
||||
fprintf(stderr, "rmdir: '%s': %s\n",
|
||||
argv[0], strerror(errno));
|
||||
|
||||
|
|
27
seq.c
27
seq.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static int digitsleft(const char *);
|
||||
|
@ -42,7 +43,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
switch(argc) {
|
||||
switch (argc) {
|
||||
case 3:
|
||||
steps = argv[1];
|
||||
argv[1] = argv[2];
|
||||
|
@ -62,15 +63,15 @@ main(int argc, char *argv[])
|
|||
end = estrtod(ends);
|
||||
|
||||
dir = (step > 0) ? 1.0 : -1.0;
|
||||
if(step == 0 || start * dir > end * dir)
|
||||
if (step == 0 || start * dir > end * dir)
|
||||
return 1;
|
||||
|
||||
if(fmt == ftmp) {
|
||||
if (fmt == ftmp) {
|
||||
right = MAX(digitsright(starts),
|
||||
MAX(digitsright(ends),
|
||||
digitsright(steps)));
|
||||
|
||||
if(wflag) {
|
||||
if (wflag) {
|
||||
left = MAX(digitsleft(starts), digitsleft(ends));
|
||||
|
||||
snprintf(ftmp, sizeof ftmp, "%%0%d.%df",
|
||||
|
@ -78,8 +79,8 @@ main(int argc, char *argv[])
|
|||
} else
|
||||
snprintf(ftmp, sizeof ftmp, "%%.%df", right);
|
||||
}
|
||||
for(out = start; out * dir <= end * dir; out += step) {
|
||||
if(out != start)
|
||||
for (out = start; out * dir <= end * dir; out += step) {
|
||||
if (out != start)
|
||||
fputs(sep, stdout);
|
||||
printf(fmt, out);
|
||||
}
|
||||
|
@ -94,7 +95,7 @@ digitsleft(const char *d)
|
|||
char *exp;
|
||||
int shift;
|
||||
|
||||
if(*d == '+')
|
||||
if (*d == '+')
|
||||
d++;
|
||||
exp = strpbrk(d, "eE");
|
||||
shift = exp ? estrtol(&exp[1], 10) : 0;
|
||||
|
@ -121,26 +122,26 @@ validfmt(const char *fmt)
|
|||
int occur = 0;
|
||||
|
||||
literal:
|
||||
while(*fmt)
|
||||
if(*fmt++ == '%')
|
||||
while (*fmt)
|
||||
if (*fmt++ == '%')
|
||||
goto format;
|
||||
return occur == 1;
|
||||
|
||||
format:
|
||||
if(*fmt == '%') {
|
||||
if (*fmt == '%') {
|
||||
fmt++;
|
||||
goto literal;
|
||||
}
|
||||
fmt += strspn(fmt, "-+#0 '");
|
||||
fmt += strspn(fmt, "0123456789");
|
||||
if(*fmt == '.') {
|
||||
if (*fmt == '.') {
|
||||
fmt++;
|
||||
fmt += strspn(fmt, "0123456789");
|
||||
}
|
||||
if(*fmt == 'L')
|
||||
if (*fmt == 'L')
|
||||
fmt++;
|
||||
|
||||
switch(*fmt) {
|
||||
switch (*fmt) {
|
||||
case 'f': case 'F':
|
||||
case 'g': case 'G':
|
||||
case 'e': case 'E':
|
||||
|
|
7
setsid.c
7
setsid.c
|
@ -4,6 +4,7 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -25,8 +26,8 @@ main(int argc, char *argv[])
|
|||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
if(getpgrp() == getpid()) {
|
||||
switch(fork()){
|
||||
if (getpgrp() == getpid()) {
|
||||
switch (fork()) {
|
||||
case -1:
|
||||
eprintf("fork:");
|
||||
case 0:
|
||||
|
@ -35,7 +36,7 @@ main(int argc, char *argv[])
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
if(setsid() < 0)
|
||||
if (setsid() < 0)
|
||||
eprintf("setsid:");
|
||||
execvp(argv[0], argv);
|
||||
savederrno = errno;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "crypt.h"
|
||||
#include "sha1.h"
|
||||
|
@ -37,7 +38,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(cflag)
|
||||
if (cflag)
|
||||
return cryptcheck(checkfile, argc, argv, &sha1_ops, md, sizeof(md));
|
||||
return cryptmain(argc, argv, &sha1_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "crypt.h"
|
||||
#include "sha256.h"
|
||||
|
@ -37,7 +38,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(cflag)
|
||||
if (cflag)
|
||||
return cryptcheck(checkfile, argc, argv, &sha256_ops, md, sizeof(md));
|
||||
return cryptmain(argc, argv, &sha256_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "crypt.h"
|
||||
#include "sha512.h"
|
||||
|
@ -37,7 +38,7 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(cflag)
|
||||
if (cflag)
|
||||
return cryptcheck(checkfile, argc, argv, &sha512_ops, md, sizeof(md));
|
||||
return cryptmain(argc, argv, &sha512_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
5
sleep.c
5
sleep.c
|
@ -1,6 +1,7 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -19,11 +20,11 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc < 1)
|
||||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
seconds = estrtol(argv[0], 0);
|
||||
while((seconds = sleep(seconds)) > 0)
|
||||
while ((seconds = sleep(seconds)) > 0)
|
||||
;
|
||||
return 0;
|
||||
}
|
||||
|
|
79
sort.c
79
sort.c
|
@ -5,6 +5,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -20,7 +21,7 @@ enum {
|
|||
MOD_N = 1 << 1,
|
||||
MOD_STARTB = 1 << 2,
|
||||
MOD_ENDB = 1 << 3,
|
||||
MOD_R = 1 << 4
|
||||
MOD_R = 1 << 4,
|
||||
};
|
||||
|
||||
struct kdlist {
|
||||
|
@ -72,7 +73,7 @@ main(int argc, char *argv[])
|
|||
break;
|
||||
case 't':
|
||||
fieldsep = EARGF(usage());
|
||||
if(strlen(fieldsep) != 1)
|
||||
if (strlen(fieldsep) != 1)
|
||||
usage();
|
||||
break;
|
||||
case 'u':
|
||||
|
@ -82,14 +83,14 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(!head && global_flags)
|
||||
if (!head && global_flags)
|
||||
addkeydef("1", global_flags);
|
||||
addkeydef("1", global_flags & MOD_R);
|
||||
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
getlines(stdin, &linebuf);
|
||||
} else for(; argc > 0; argc--, argv++) {
|
||||
if(!(fp = fopen(argv[0], "r"))) {
|
||||
} else for (; argc > 0; argc--, argv++) {
|
||||
if (!(fp = fopen(argv[0], "r"))) {
|
||||
enprintf(2, "fopen %s:", argv[0]);
|
||||
continue;
|
||||
}
|
||||
|
@ -99,8 +100,8 @@ main(int argc, char *argv[])
|
|||
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines,
|
||||
(int (*)(const void *, const void *))linecmp);
|
||||
|
||||
for(i = 0; i < linebuf.nlines; i++) {
|
||||
if(!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i],
|
||||
for (i = 0; i < linebuf.nlines; i++) {
|
||||
if (!uflag || i == 0 || linecmp((const char **)&linebuf.lines[i],
|
||||
(const char **)&linebuf.lines[i-1])) {
|
||||
fputs(linebuf.lines[i], stdout);
|
||||
}
|
||||
|
@ -116,13 +117,13 @@ addkeydef(char *def, int flags)
|
|||
struct kdlist *node;
|
||||
|
||||
node = malloc(sizeof(*node));
|
||||
if(!node)
|
||||
if (!node)
|
||||
enprintf(2, "malloc:");
|
||||
if(!head)
|
||||
if (!head)
|
||||
head = node;
|
||||
if(parse_keydef(&node->keydef, def, flags))
|
||||
if (parse_keydef(&node->keydef, def, flags))
|
||||
enprintf(2, "faulty key definition\n");
|
||||
if(tail)
|
||||
if (tail)
|
||||
tail->next = node;
|
||||
node->next = NULL;
|
||||
tail = node;
|
||||
|
@ -134,7 +135,7 @@ freelist(void)
|
|||
struct kdlist *node;
|
||||
struct kdlist *tmp;
|
||||
|
||||
for(node = head; node; node = tmp) {
|
||||
for (node = head; node; node = tmp) {
|
||||
tmp = node->next;
|
||||
free(node);
|
||||
}
|
||||
|
@ -147,20 +148,20 @@ linecmp(const char **a, const char **b)
|
|||
int res = 0;
|
||||
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);
|
||||
s2 = columns((char *)*b, &node->keydef);
|
||||
|
||||
/* if -u is given, don't use default key definition
|
||||
* unless it is the only one */
|
||||
if(uflag && node == tail && head != tail)
|
||||
if (uflag && node == tail && head != tail)
|
||||
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);
|
||||
else
|
||||
res = strcmp(s1, s2);
|
||||
|
||||
if(node->keydef.flags & MOD_R)
|
||||
if (node->keydef.flags & MOD_R)
|
||||
res = -res;
|
||||
|
||||
free(s1);
|
||||
|
@ -172,8 +173,8 @@ linecmp(const char **a, const char **b)
|
|||
static int
|
||||
parse_flags(char **s, int *flags, int bflag)
|
||||
{
|
||||
while(isalpha((int)**s))
|
||||
switch(*((*s)++)) {
|
||||
while (isalpha((int)**s))
|
||||
switch (*((*s)++)) {
|
||||
case 'b':
|
||||
*flags |= bflag;
|
||||
break;
|
||||
|
@ -202,27 +203,27 @@ parse_keydef(struct keydef *kd, char *s, int flags)
|
|||
kd->flags = flags;
|
||||
|
||||
kd->start_column = strtol(rest, &rest, 10);
|
||||
if(kd->start_column < 1)
|
||||
if (kd->start_column < 1)
|
||||
return -1;
|
||||
if(*rest == '.')
|
||||
if (*rest == '.')
|
||||
kd->start_char = strtol(rest+1, &rest, 10);
|
||||
if(kd->start_char < 1)
|
||||
if (kd->start_char < 1)
|
||||
return -1;
|
||||
if(parse_flags(&rest, &kd->flags, MOD_STARTB) == -1)
|
||||
if (parse_flags(&rest, &kd->flags, MOD_STARTB) == -1)
|
||||
return -1;
|
||||
if(*rest == ',') {
|
||||
if (*rest == ',') {
|
||||
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;
|
||||
if(*rest == '.') {
|
||||
if (*rest == '.') {
|
||||
kd->end_char = strtol(rest+1, &rest, 10);
|
||||
if(kd->end_char < 1)
|
||||
if (kd->end_char < 1)
|
||||
return -1;
|
||||
}
|
||||
if(parse_flags(&rest, &kd->flags, MOD_ENDB) == -1)
|
||||
if (parse_flags(&rest, &kd->flags, MOD_ENDB) == -1)
|
||||
return -1;
|
||||
}
|
||||
if(*rest != '\0')
|
||||
if (*rest != '\0')
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -238,12 +239,12 @@ skipblank(char *s)
|
|||
static char *
|
||||
nextcol(char *s)
|
||||
{
|
||||
if(fieldsep == NULL) {
|
||||
if (fieldsep == NULL) {
|
||||
s = skipblank(s);
|
||||
while(*s && !isblank(*s))
|
||||
s++;
|
||||
} else {
|
||||
if(strchr(s, *fieldsep) == NULL)
|
||||
if (strchr(s, *fieldsep) == NULL)
|
||||
s = strchr(s, '\0');
|
||||
else
|
||||
s = strchr(s, *fieldsep) + 1;
|
||||
|
@ -258,27 +259,27 @@ columns(char *line, const struct keydef *kd)
|
|||
char *res;
|
||||
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);
|
||||
if(kd->flags & MOD_STARTB)
|
||||
if (kd->flags & MOD_STARTB)
|
||||
start = skipblank(start);
|
||||
start += MIN(kd->start_char, nextcol(start) - start) - 1;
|
||||
|
||||
if(kd->end_column) {
|
||||
for(i = 1, end = line; i < kd->end_column; i++)
|
||||
if (kd->end_column) {
|
||||
for (i = 1, end = line; i < kd->end_column; i++)
|
||||
end = nextcol(end);
|
||||
if(kd->flags & MOD_ENDB)
|
||||
if (kd->flags & MOD_ENDB)
|
||||
end = skipblank(end);
|
||||
if(kd->end_char)
|
||||
if (kd->end_char)
|
||||
end += MIN(kd->end_char, nextcol(end) - end);
|
||||
else
|
||||
end = nextcol(end);
|
||||
} else {
|
||||
if((end = strchr(line, '\n')) == NULL)
|
||||
if ((end = strchr(line, '\n')) == NULL)
|
||||
end = strchr(line, '\0');
|
||||
}
|
||||
|
||||
if((res = strndup(start, end - start)) == NULL)
|
||||
if ((res = strndup(start, end - start)) == NULL)
|
||||
enprintf(2, "strndup:");
|
||||
return res;
|
||||
}
|
||||
|
|
39
split.c
39
split.c
|
@ -5,6 +5,7 @@
|
|||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static int itostr(char *, int, int);
|
||||
|
@ -35,13 +36,13 @@ main(int argc, char *argv[])
|
|||
case 'b':
|
||||
always = 1;
|
||||
tmp = ARGF();
|
||||
if(tmp == NULL)
|
||||
if (tmp == NULL)
|
||||
break;
|
||||
|
||||
size = strtoull(tmp, &end, 10);
|
||||
if(*end == '\0')
|
||||
if (*end == '\0')
|
||||
break;
|
||||
switch(toupper((int)*end)) {
|
||||
switch (toupper((int)*end)) {
|
||||
case 'K':
|
||||
scale = 1024;
|
||||
break;
|
||||
|
@ -54,14 +55,14 @@ main(int argc, char *argv[])
|
|||
default:
|
||||
usage();
|
||||
}
|
||||
if(size > (UINT64_MAX/scale))
|
||||
if (size > (UINT64_MAX/scale))
|
||||
eprintf("'%s': out of range\n", tmp);
|
||||
size *= scale;
|
||||
break;
|
||||
case 'l':
|
||||
always = 0;
|
||||
tmp = ARGF();
|
||||
if(tmp)
|
||||
if (tmp)
|
||||
size = estrtol(tmp, 10);
|
||||
break;
|
||||
case 'a':
|
||||
|
@ -75,31 +76,31 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(*argv)
|
||||
if (*argv)
|
||||
file = *argv++;
|
||||
if(*argv)
|
||||
if (*argv)
|
||||
prefix = *argv++;
|
||||
if(*argv)
|
||||
if (*argv)
|
||||
usage();
|
||||
|
||||
plen = strlen(prefix);
|
||||
if(plen+slen > NAME_MAX)
|
||||
if (plen+slen > NAME_MAX)
|
||||
eprintf("names cannot exceed %d bytes\n", NAME_MAX);
|
||||
strlcpy(name, prefix, sizeof(name));
|
||||
|
||||
if(file && strcmp(file, "-") != 0) {
|
||||
if (file && strcmp(file, "-") != 0) {
|
||||
in = fopen(file, "r");
|
||||
if(!in)
|
||||
if (!in)
|
||||
eprintf("'%s':", file);
|
||||
}
|
||||
|
||||
Nextfile:
|
||||
while((out = nextfile(out, name, plen, slen))) {
|
||||
while ((out = nextfile(out, name, plen, slen))) {
|
||||
n = 0;
|
||||
while((ch = getc(in)) != EOF) {
|
||||
while ((ch = getc(in)) != EOF) {
|
||||
putc(ch, out);
|
||||
n += (always || ch == '\n');
|
||||
if(n >= size)
|
||||
if (n >= size)
|
||||
goto Nextfile;
|
||||
}
|
||||
fclose(out);
|
||||
|
@ -112,11 +113,11 @@ int
|
|||
itostr(char *str, int x, int n)
|
||||
{
|
||||
str[n] = '\0';
|
||||
while(n-- > 0) {
|
||||
while (n-- > 0) {
|
||||
str[n] = start + (x % base);
|
||||
x /= base;
|
||||
}
|
||||
if(x)
|
||||
if (x)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -127,14 +128,14 @@ nextfile(FILE *f, char *buf, int plen, int slen)
|
|||
static int n = 0;
|
||||
int s;
|
||||
|
||||
if(f)
|
||||
if (f)
|
||||
fclose(f);
|
||||
s = itostr(buf+plen, n++, slen);
|
||||
if(s == -1)
|
||||
if (s == -1)
|
||||
return NULL;
|
||||
|
||||
f = fopen(buf, "w");
|
||||
if(!f)
|
||||
if (!f)
|
||||
eprintf("'%s':", buf);
|
||||
return f;
|
||||
}
|
||||
|
|
7
sponge.c
7
sponge.c
|
@ -1,6 +1,7 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -20,16 +21,16 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc != 1)
|
||||
if (argc != 1)
|
||||
usage();
|
||||
|
||||
if(!(tmpfp = tmpfile()))
|
||||
if (!(tmpfp = tmpfile()))
|
||||
eprintf("tmpfile:");
|
||||
|
||||
concat(stdin, "<stdin>", tmpfp, "<tmpfile>");
|
||||
rewind(tmpfp);
|
||||
|
||||
if(!(fp = fopen(argv[0], "w")))
|
||||
if (!(fp = fopen(argv[0], "w")))
|
||||
eprintf("sponge: '%s':", argv[0]);
|
||||
concat(tmpfp, "<tmpfile>", fp, argv[0]);
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void dostrings(FILE *fp, const char *fname);
|
||||
|
|
3
sync.c
3
sync.c
|
@ -2,6 +2,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -13,7 +14,7 @@ usage(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if(argc != 1)
|
||||
if (argc != 1)
|
||||
usage();
|
||||
sync();
|
||||
|
||||
|
|
21
tail.c
21
tail.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -27,7 +28,7 @@ main(int argc, char *argv[])
|
|||
case 'n':
|
||||
lines = EARGF(usage());
|
||||
n = abs(estrtol(lines, 0));
|
||||
if(lines[0] == '+')
|
||||
if (lines[0] == '+')
|
||||
tail = dropinit;
|
||||
break;
|
||||
ARGNUM:
|
||||
|
@ -36,11 +37,11 @@ main(int argc, char *argv[])
|
|||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
tail(stdin, "<stdin>", n);
|
||||
} else {
|
||||
for (; argc > 0; argc--, argv++) {
|
||||
if(!(fp = fopen(argv[0], "r"))) {
|
||||
if (!(fp = fopen(argv[0], "r"))) {
|
||||
weprintf("fopen %s:", argv[0]);
|
||||
continue;
|
||||
}
|
||||
|
@ -60,8 +61,8 @@ dropinit(FILE *fp, const char *str, long n)
|
|||
ssize_t len;
|
||||
unsigned long i = 0;
|
||||
|
||||
while(i < n && ((len = agetline(&buf, &size, fp)) != -1))
|
||||
if(len && buf[len - 1] == '\n')
|
||||
while (i < n && ((len = agetline(&buf, &size, fp)) != -1))
|
||||
if (len && buf[len - 1] == '\n')
|
||||
i++;
|
||||
free(buf);
|
||||
concat(fp, str, stdout, "<stdout>");
|
||||
|
@ -74,19 +75,19 @@ taketail(FILE *fp, const char *str, long n)
|
|||
long i, j;
|
||||
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:");
|
||||
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);
|
||||
|
||||
do {
|
||||
if(ring[j]) {
|
||||
if (ring[j]) {
|
||||
fputs(ring[j], stdout);
|
||||
free(ring[j]);
|
||||
}
|
||||
} while((j = (j+1)%n) != i);
|
||||
} while ((j = (j+1)%n) != i);
|
||||
free(ring);
|
||||
free(size);
|
||||
}
|
||||
|
|
75
tar.c
75
tar.c
|
@ -10,6 +10,7 @@
|
|||
#include <limits.h>
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
typedef struct Header Header;
|
||||
|
@ -74,7 +75,7 @@ main(int argc, char *argv[])
|
|||
case 'x':
|
||||
case 'c':
|
||||
case 't':
|
||||
if(mode)
|
||||
if (mode)
|
||||
usage();
|
||||
mode = ARGC();
|
||||
break;
|
||||
|
@ -91,27 +92,27 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(!mode) {
|
||||
if(argc < 1)
|
||||
if (!mode) {
|
||||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
for(ap = argv[0]; *ap; ap++) {
|
||||
switch(*ap) {
|
||||
for (ap = argv[0]; *ap; ap++) {
|
||||
switch (*ap) {
|
||||
case 'x':
|
||||
case 'c':
|
||||
case 't':
|
||||
if(mode)
|
||||
if (mode)
|
||||
usage();
|
||||
mode = *ap;
|
||||
break;
|
||||
case 'f':
|
||||
if(argc < 2)
|
||||
if (argc < 2)
|
||||
usage();
|
||||
argc--, argv++;
|
||||
file = argv[0];
|
||||
break;
|
||||
case 'C':
|
||||
if(argc < 2)
|
||||
if (argc < 2)
|
||||
usage();
|
||||
argc--, argv++;
|
||||
dir = argv[0];
|
||||
|
@ -126,12 +127,12 @@ main(int argc, char *argv[])
|
|||
argc--, argv++;
|
||||
}
|
||||
|
||||
if(!mode || argc != (mode == 'c'))
|
||||
if (!mode || argc != (mode == 'c'))
|
||||
usage();
|
||||
|
||||
if(file) {
|
||||
if (file) {
|
||||
tarfile = fopen(file, (mode == 'c') ? "wb" : "rb");
|
||||
if(!tarfile)
|
||||
if (!tarfile)
|
||||
eprintf("tar: open '%s':", file);
|
||||
if (lstat(file, &st) < 0)
|
||||
eprintf("tar: stat '%s':", file);
|
||||
|
@ -143,7 +144,7 @@ main(int argc, char *argv[])
|
|||
|
||||
chdir(dir);
|
||||
|
||||
if(mode == 'c') {
|
||||
if (mode == 'c') {
|
||||
c(argv[0]);
|
||||
} else {
|
||||
xt((mode == 'x') ? unarchive : print);
|
||||
|
@ -192,16 +193,16 @@ archive(const char* path)
|
|||
snprintf(h->gname, sizeof h->gname, "%s", gr ? gr->gr_name : "");
|
||||
|
||||
mode = st.st_mode;
|
||||
if(S_ISREG(mode)) {
|
||||
if (S_ISREG(mode)) {
|
||||
h->type = REG;
|
||||
putoctal(h->size, (unsigned)st.st_size, sizeof h->size);
|
||||
f = fopen(path, "r");
|
||||
} else if(S_ISDIR(mode)) {
|
||||
} else if (S_ISDIR(mode)) {
|
||||
h->type = DIRECTORY;
|
||||
} else if(S_ISLNK(mode)) {
|
||||
} else if (S_ISLNK(mode)) {
|
||||
h->type = SYMLINK;
|
||||
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;
|
||||
#if defined(major) && defined(minor)
|
||||
putoctal(h->major, (unsigned)major(st.st_dev), sizeof h->major);
|
||||
|
@ -209,20 +210,20 @@ archive(const char* path)
|
|||
#else
|
||||
return 0;
|
||||
#endif
|
||||
} else if(S_ISFIFO(mode)) {
|
||||
} else if (S_ISFIFO(mode)) {
|
||||
h->type = FIFO;
|
||||
}
|
||||
|
||||
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];
|
||||
putoctal(h->chksum, chksum, sizeof h->chksum);
|
||||
|
||||
fwrite(b, Blksiz, 1, tarfile);
|
||||
if(!f)
|
||||
if (!f)
|
||||
return 0;
|
||||
while((l = fread(b, 1, Blksiz, f)) > 0) {
|
||||
if(l < Blksiz)
|
||||
while ((l = fread(b, 1, Blksiz, f)) > 0) {
|
||||
if (l < Blksiz)
|
||||
memset(b+l, 0, Blksiz-l);
|
||||
fwrite(b, Blksiz, 1, tarfile);
|
||||
}
|
||||
|
@ -239,25 +240,25 @@ unarchive(char *fname, int l, char b[Blksiz])
|
|||
struct timeval times[2];
|
||||
Header *h = (void*)b;
|
||||
|
||||
if(!mflag)
|
||||
if (!mflag)
|
||||
mtime = strtoul(h->mtime, 0, 8);
|
||||
unlink(fname);
|
||||
switch(h->type) {
|
||||
switch (h->type) {
|
||||
case REG:
|
||||
case AREG:
|
||||
mode = strtoul(h->mode, 0, 8);
|
||||
if(!(f = fopen(fname, "w")) || chmod(fname, mode))
|
||||
if (!(f = fopen(fname, "w")) || chmod(fname, mode))
|
||||
perror(fname);
|
||||
break;
|
||||
case HARDLINK:
|
||||
case SYMLINK:
|
||||
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);
|
||||
break;
|
||||
case DIRECTORY:
|
||||
mode = strtoul(h->mode, 0, 8);
|
||||
if(mkdir(fname, (mode_t)mode))
|
||||
if (mkdir(fname, (mode_t)mode))
|
||||
perror(fname);
|
||||
break;
|
||||
case CHARDEV:
|
||||
|
@ -267,34 +268,34 @@ unarchive(char *fname, int l, char b[Blksiz])
|
|||
major = strtoul(h->major, 0, 8);
|
||||
minor = strtoul(h->mode, 0, 8);
|
||||
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);
|
||||
#endif
|
||||
break;
|
||||
case FIFO:
|
||||
mode = strtoul(h->mode, 0, 8);
|
||||
if(mknod(fname, S_IFIFO | mode, 0))
|
||||
if (mknod(fname, S_IFIFO | mode, 0))
|
||||
perror(fname);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "usupported tarfiletype %c\n", h->type);
|
||||
}
|
||||
if(getuid() == 0 && chown(fname, strtoul(h->uid, 0, 8),
|
||||
strtoul(h->gid, 0, 8)))
|
||||
if (getuid() == 0 && chown(fname, strtoul(h->uid, 0, 8),
|
||||
strtoul(h->gid, 0, 8)))
|
||||
perror(fname);
|
||||
|
||||
for(; l > 0; l -= Blksiz) {
|
||||
for (; l > 0; l -= Blksiz) {
|
||||
fread(b, Blksiz, 1, tarfile);
|
||||
if(f)
|
||||
if (f)
|
||||
fwrite(b, MIN(l, 512), 1, f);
|
||||
}
|
||||
if(f)
|
||||
if (f)
|
||||
fclose(f);
|
||||
|
||||
if(!mflag) {
|
||||
if (!mflag) {
|
||||
times[0].tv_sec = times[1].tv_sec = mtime;
|
||||
times[0].tv_usec = times[1].tv_usec = 0;
|
||||
if(utimes(fname, times))
|
||||
if (utimes(fname, times))
|
||||
perror(fname);
|
||||
}
|
||||
return 0;
|
||||
|
@ -304,7 +305,7 @@ static int
|
|||
print(char * fname, int l, char b[Blksiz])
|
||||
{
|
||||
puts(fname);
|
||||
for(; l > 0; l -= Blksiz)
|
||||
for (; l > 0; l -= Blksiz)
|
||||
fread(b, Blksiz, 1, tarfile);
|
||||
return 0;
|
||||
}
|
||||
|
@ -322,7 +323,7 @@ xt(int (*fn)(char*, int, char[Blksiz]))
|
|||
char b[Blksiz], fname[257], *s;
|
||||
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;
|
||||
if (h->prefix[0] != '\0')
|
||||
s += sprintf(s, "%.*s/", (int)sizeof h->prefix, h->prefix);
|
||||
|
|
15
tee.c
15
tee.c
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -29,21 +30,21 @@ main(int argc, char *argv[])
|
|||
} ARGEND;
|
||||
|
||||
nfps = argc + 1;
|
||||
if(!(fps = calloc(nfps, sizeof *fps)))
|
||||
if (!(fps = calloc(nfps, sizeof *fps)))
|
||||
eprintf("calloc:");
|
||||
|
||||
for(i = 0; argc > 0; argc--, argv++, i++)
|
||||
if(!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
|
||||
for (i = 0; argc > 0; argc--, argv++, i++)
|
||||
if (!(fps[i] = fopen(*argv, aflag ? "a" : "w")))
|
||||
eprintf("fopen %s:", *argv);
|
||||
fps[i] = stdout;
|
||||
|
||||
while((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
|
||||
for(i = 0; i < nfps; i++) {
|
||||
if(fwrite(buf, 1, n, fps[i]) != n)
|
||||
while ((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
|
||||
for (i = 0; i < nfps; i++) {
|
||||
if (fwrite(buf, 1, n, fps[i]) != n)
|
||||
eprintf("%s: write error:", buf);
|
||||
}
|
||||
}
|
||||
if(ferror(stdin))
|
||||
if (ferror(stdin))
|
||||
eprintf("<stdin>: read error:");
|
||||
free(fps);
|
||||
|
||||
|
|
43
test.c
43
test.c
|
@ -6,6 +6,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -14,20 +15,20 @@ stoi(char *s, int *a)
|
|||
char *p;
|
||||
errno = 0;
|
||||
*a = strtol(s, &p, 0);
|
||||
if(errno || !*s || *p)
|
||||
if (errno || !*s || *p)
|
||||
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_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_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_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_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_u(char *s) { struct stat buf; if( stat(s, &buf)) return 0; return S_ISUID & 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_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_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_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 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_n(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;
|
||||
|
||||
for(t = tests; t->name; ++t)
|
||||
if(strcmp(t->name, name) == 0)
|
||||
for (t = tests; t->name; ++t)
|
||||
if (strcmp(t->name, name) == 0)
|
||||
return t;
|
||||
return NULL;
|
||||
}
|
||||
|
@ -118,10 +119,10 @@ twoarg(char **argv)
|
|||
{
|
||||
Test *t = find_test(unary, *argv);
|
||||
|
||||
if(strcmp(argv[0], "!") == 0)
|
||||
if (strcmp(argv[0], "!") == 0)
|
||||
return !onearg(argv + 1);
|
||||
|
||||
if(t)
|
||||
if (t)
|
||||
return t->func(argv[1]);
|
||||
|
||||
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]);
|
||||
|
||||
if(t)
|
||||
if (t)
|
||||
return t->func(argv[0], argv[2]);
|
||||
|
||||
if(strcmp(argv[0], "!") == 0)
|
||||
if (strcmp(argv[0], "!") == 0)
|
||||
return !twoarg(argv + 1);
|
||||
|
||||
return enprintf(2, "bad binary test %s\n", argv[1]), 0;
|
||||
|
@ -144,7 +145,7 @@ threearg(char **argv)
|
|||
static bool
|
||||
fourarg(char **argv)
|
||||
{
|
||||
if(strcmp(argv[0], "!") == 0)
|
||||
if (strcmp(argv[0], "!") == 0)
|
||||
return !threearg(argv + 1);
|
||||
|
||||
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 };
|
||||
int len = strlen(argv[0]);
|
||||
|
||||
if(len && argv[0][len - 1] == '[')
|
||||
if(strcmp(argv[--argc], "]") != 0)
|
||||
if (len && argv[0][len - 1] == '[')
|
||||
if (strcmp(argv[--argc], "]") != 0)
|
||||
enprintf(2, "no matching ]\n");
|
||||
|
||||
--argc; ++argv;
|
||||
|
||||
if(argc > 4)
|
||||
if (argc > 4)
|
||||
enprintf(2, "too many arguments\n");
|
||||
|
||||
return !narg[argc](argv);
|
||||
|
|
15
touch.c
15
touch.c
|
@ -7,6 +7,7 @@
|
|||
#include <unistd.h>
|
||||
#include <utime.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void touch(const char *);
|
||||
|
@ -39,7 +40,7 @@ main(int argc, char *argv[])
|
|||
if (argc < 1)
|
||||
usage();
|
||||
|
||||
for(; argc > 0; argc--, argv++)
|
||||
for (; argc > 0; argc--, argv++)
|
||||
touch(argv[0]);
|
||||
|
||||
return 0;
|
||||
|
@ -52,19 +53,19 @@ touch(const char *str)
|
|||
struct stat st;
|
||||
struct utimbuf ut;
|
||||
|
||||
if(stat(str, &st) == 0) {
|
||||
if (stat(str, &st) == 0) {
|
||||
ut.actime = st.st_atime;
|
||||
ut.modtime = t;
|
||||
if(utime(str, &ut) == -1)
|
||||
if (utime(str, &ut) == -1)
|
||||
eprintf("utime %s:", str);
|
||||
return;
|
||||
}
|
||||
else if(errno != ENOENT)
|
||||
else if (errno != ENOENT)
|
||||
eprintf("stat %s:", str);
|
||||
else if(cflag)
|
||||
else if (cflag)
|
||||
return;
|
||||
if((fd = open(str, O_CREAT|O_EXCL,
|
||||
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1)
|
||||
if ((fd = open(str, O_CREAT|O_EXCL,
|
||||
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1)
|
||||
eprintf("open %s:", str);
|
||||
close(fd);
|
||||
touch(str);
|
||||
|
|
93
tr.c
93
tr.c
|
@ -5,6 +5,7 @@
|
|||
#include <string.h>
|
||||
#include <locale.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -54,7 +55,7 @@ resolve_escape(char *s)
|
|||
int i;
|
||||
unsigned char c;
|
||||
|
||||
switch(*s) {
|
||||
switch (*s) {
|
||||
case 'n':
|
||||
*s = '\n';
|
||||
return 0;
|
||||
|
@ -84,13 +85,13 @@ resolve_escape(char *s)
|
|||
default: ;
|
||||
}
|
||||
|
||||
if(*s<'0' || *s>'7')
|
||||
if(*s < '0' || *s > '7')
|
||||
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 += s[i]-'0';
|
||||
}
|
||||
if(*s>'3' && i==3)
|
||||
if(*s > '3' && i == 3)
|
||||
eprintf("octal byte cannot be bigger than 377:");
|
||||
*s = c;
|
||||
return i;
|
||||
|
@ -112,8 +113,8 @@ xmbtowc(wchar_t *unicodep, const char *s)
|
|||
static int
|
||||
has_octal_escapes(const char *s)
|
||||
{
|
||||
while(*s)
|
||||
if(*s++ == '\\' && *s >= '0' && *s <= '7')
|
||||
while (*s)
|
||||
if (*s++ == '\\' && *s >= '0' && *s <= '7')
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -125,26 +126,26 @@ get_next_char(struct set_state *s)
|
|||
int nchars;
|
||||
|
||||
start:
|
||||
if(s->rfirst <= s->rlast) {
|
||||
if (s->rfirst <= s->rlast) {
|
||||
c = s->rfirst;
|
||||
s->rfirst++;
|
||||
return c;
|
||||
}
|
||||
|
||||
if(*s->s == '-' && !s->prev_was_octal) {
|
||||
if (*s->s == '-' && !s->prev_was_octal) {
|
||||
s->s++;
|
||||
if(!*s->s)
|
||||
if (!*s->s)
|
||||
return '-';
|
||||
if(*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
|
||||
if (*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
|
||||
goto char_is_octal;
|
||||
s->rlast = *(s->s)++;
|
||||
if(!s->rlast)
|
||||
if (!s->rlast)
|
||||
return '\0';
|
||||
s->prev_was_octal = 1;
|
||||
s->rfirst = ++(s->prev);
|
||||
goto start;
|
||||
}
|
||||
if(*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
|
||||
if (*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
|
||||
goto char_is_octal;
|
||||
|
||||
s->prev_was_octal = 0;
|
||||
|
@ -163,27 +164,27 @@ static wchar_t
|
|||
get_next_wchar(struct wset_state *s)
|
||||
{
|
||||
start:
|
||||
if(s->rfirst <= s->rlast) {
|
||||
if (s->rfirst <= s->rlast) {
|
||||
s->prev = s->rfirst;
|
||||
s->rfirst++;
|
||||
return s->prev;
|
||||
}
|
||||
|
||||
if(*s->s == '-' && !s->prev_was_range) {
|
||||
if (*s->s == '-' && !s->prev_was_range) {
|
||||
s->s++;
|
||||
if(!*s->s)
|
||||
if (!*s->s)
|
||||
return '-';
|
||||
if(*s->s == '\\')
|
||||
if (*s->s == '\\')
|
||||
resolve_escape(++(s->s));
|
||||
s->s += xmbtowc(&s->rlast, s->s);
|
||||
if(!s->rlast)
|
||||
if (!s->rlast)
|
||||
return '\0';
|
||||
s->rfirst = ++(s->prev);
|
||||
s->prev_was_range = 1;
|
||||
goto start;
|
||||
}
|
||||
|
||||
if(*s->s == '\\')
|
||||
if (*s->s == '\\')
|
||||
resolve_escape(++(s->s));
|
||||
s->s += xmbtowc(&s->prev, s->s);
|
||||
s->prev_was_range = 0;
|
||||
|
@ -197,39 +198,39 @@ is_mapping_wide(const char *set1, const char *set2)
|
|||
struct wset_state wss1, wss2;
|
||||
wchar_t wc1, wc2, last_wc2;
|
||||
|
||||
if(has_octal_escapes(set1)) {
|
||||
if (has_octal_escapes(set1)) {
|
||||
set_state_defaults(&ss1);
|
||||
ss1.s = (char *) set1;
|
||||
if(set2) {
|
||||
if (set2) {
|
||||
set_state_defaults(&ss2);
|
||||
ss2.s = (char *) set2;
|
||||
/* if the character returned is from an octal triplet, it might be null
|
||||
* and still need to continue */
|
||||
while((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal ) {
|
||||
if(!(wc2 = (unsigned char) get_next_char(&ss2)))
|
||||
while ((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal ) {
|
||||
if (!(wc2 = (unsigned char) get_next_char(&ss2)))
|
||||
wc2 = last_wc2;
|
||||
mappings[wc1] = wc2;
|
||||
last_wc2 = wc2;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
wset_state_defaults(&wss1);
|
||||
wss1.s = (char *) set1;
|
||||
if(set2) {
|
||||
if (set2) {
|
||||
wset_state_defaults(&wss2);
|
||||
wss2.s = (char *) set2;
|
||||
while((wc1 = get_next_wchar(&wss1))) {
|
||||
if(!(wc2 = get_next_wchar(&wss2)))
|
||||
while ((wc1 = get_next_wchar(&wss1))) {
|
||||
if (!(wc2 = get_next_wchar(&wss2)))
|
||||
wc2 = last_wc2;
|
||||
mappings[wc1] = wc2;
|
||||
last_wc2 = wc2;
|
||||
}
|
||||
} else {
|
||||
while((wc1 = get_next_wchar(&wss1)))
|
||||
while ((wc1 = get_next_wchar(&wss1)))
|
||||
mappings[wc1] = 1;
|
||||
}
|
||||
return 1;
|
||||
|
@ -245,13 +246,13 @@ wmap_null(char *in, ssize_t nbytes)
|
|||
int parsed_bytes = 0;
|
||||
|
||||
s = in;
|
||||
while(nbytes) {
|
||||
while (nbytes) {
|
||||
parsed_bytes = embtowc(&rune, s);
|
||||
if(parsed_bytes < 0) {
|
||||
if (parsed_bytes < 0) {
|
||||
rune = *s;
|
||||
parsed_bytes = 1;
|
||||
}
|
||||
if(((!mappings[rune])&1) ^ cflag)
|
||||
if (((!mappings[rune])&1) ^ cflag)
|
||||
putwchar(rune);
|
||||
s += parsed_bytes;
|
||||
nbytes -= parsed_bytes;
|
||||
|
@ -266,13 +267,13 @@ wmap_set(char *in, ssize_t nbytes)
|
|||
int parsed_bytes = 0;
|
||||
|
||||
s = in;
|
||||
while(nbytes) {
|
||||
while (nbytes) {
|
||||
parsed_bytes = embtowc(&rune, s);
|
||||
if(parsed_bytes < 0) {
|
||||
if (parsed_bytes < 0) {
|
||||
rune = *s;
|
||||
parsed_bytes = 1;
|
||||
}
|
||||
if(!mappings[rune])
|
||||
if (!mappings[rune])
|
||||
putwchar(rune);
|
||||
else
|
||||
putwchar(mappings[rune]);
|
||||
|
@ -286,8 +287,8 @@ map_null(char *in, ssize_t nbytes)
|
|||
{
|
||||
char *s;
|
||||
|
||||
for(s=in; nbytes; s++, nbytes--)
|
||||
if(((!mappings[(unsigned char)*s])&1) ^ cflag)
|
||||
for (s = in; nbytes; s++, nbytes--)
|
||||
if (((!mappings[(unsigned char)*s])&1) ^ cflag)
|
||||
putchar(*s);
|
||||
}
|
||||
|
||||
|
@ -296,8 +297,8 @@ map_set(char *in, ssize_t nbytes)
|
|||
{
|
||||
char *s;
|
||||
|
||||
for(s=in; nbytes; s++, nbytes--)
|
||||
if(!mappings[(unsigned char)*s])
|
||||
for (s = in; nbytes; s++, nbytes--)
|
||||
if (!mappings[(unsigned char)*s])
|
||||
putchar(*s);
|
||||
else
|
||||
putchar(mappings[(unsigned char)*s]);
|
||||
|
@ -325,20 +326,20 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0)
|
||||
if (argc == 0)
|
||||
usage();
|
||||
|
||||
if(dflag) {
|
||||
if(argc != 1)
|
||||
if (dflag) {
|
||||
if (argc != 1)
|
||||
usage();
|
||||
if(is_mapping_wide(argv[0], NULL))
|
||||
if (is_mapping_wide(argv[0], NULL))
|
||||
mapfunc = wmap_null;
|
||||
else
|
||||
mapfunc = map_null;
|
||||
} else if(cflag) {
|
||||
} else if (cflag) {
|
||||
usage();
|
||||
} else if(argc == 2) {
|
||||
if(is_mapping_wide(argv[0], argv[1]))
|
||||
} else if (argc == 2) {
|
||||
if (is_mapping_wide(argv[0], argv[1]))
|
||||
mapfunc = wmap_set;
|
||||
else
|
||||
mapfunc = map_set;
|
||||
|
@ -346,10 +347,10 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
}
|
||||
|
||||
while((nbytes = agetline(&buf, &size, stdin)) != -1)
|
||||
while ((nbytes = agetline(&buf, &size, stdin)) != -1)
|
||||
mapfunc(buf, nbytes);
|
||||
free(buf);
|
||||
if(ferror(stdin))
|
||||
if (ferror(stdin))
|
||||
eprintf("<stdin>: read error:");
|
||||
|
||||
return 0;
|
||||
|
|
1
tty.c
1
tty.c
|
@ -2,6 +2,7 @@
|
|||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
|
13
uname.c
13
uname.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void
|
||||
|
@ -44,18 +45,18 @@ main(int argc, char *argv[])
|
|||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
if(uname(&u) == -1)
|
||||
if (uname(&u) == -1)
|
||||
eprintf("uname:");
|
||||
|
||||
if(sflag || !(nflag || rflag || vflag || mflag))
|
||||
if (sflag || !(nflag || rflag || vflag || mflag))
|
||||
putword(u.sysname);
|
||||
if(nflag)
|
||||
if (nflag)
|
||||
putword(u.nodename);
|
||||
if(rflag)
|
||||
if (rflag)
|
||||
putword(u.release);
|
||||
if(vflag)
|
||||
if (vflag)
|
||||
putword(u.version);
|
||||
if(mflag)
|
||||
if (mflag)
|
||||
putword(u.machine);
|
||||
putchar('\n');
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
typedef struct {
|
||||
|
@ -30,7 +31,7 @@ main(int argc, char *argv[])
|
|||
ARGBEGIN {
|
||||
case 't':
|
||||
tabsize = estrtol(EARGF(usage()), 0);
|
||||
if(tabsize <= 0)
|
||||
if (tabsize <= 0)
|
||||
eprintf("unexpand: invalid tabsize\n", argv[0]);
|
||||
/* Fallthrough: -t implies -a */
|
||||
case 'a':
|
||||
|
@ -84,12 +85,12 @@ unexpandspan(unsigned int n, unsigned int col)
|
|||
{
|
||||
unsigned int off = (col-n) % tabsize;
|
||||
|
||||
if(n + off >= tabsize && n > 1)
|
||||
if (n + off >= tabsize && n > 1)
|
||||
n += off;
|
||||
|
||||
for(; n >= tabsize; n -= tabsize)
|
||||
for (; n >= tabsize; n -= tabsize)
|
||||
out('\t');
|
||||
while(n--)
|
||||
while (n--)
|
||||
out(' ');
|
||||
}
|
||||
|
||||
|
|
18
uniq.c
18
uniq.c
|
@ -46,10 +46,10 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if(argc == 0) {
|
||||
if (argc == 0) {
|
||||
uniq(stdin, "<stdin>");
|
||||
} else if(argc == 1) {
|
||||
if(!(fp = fopen(argv[0], "r")))
|
||||
} else if (argc == 1) {
|
||||
if (!(fp = fopen(argv[0], "r")))
|
||||
eprintf("fopen %s:", argv[0]);
|
||||
uniq(fp, argv[0]);
|
||||
fclose(fp);
|
||||
|
@ -67,14 +67,14 @@ uniqline(char *l)
|
|||
? l == prevline
|
||||
: !strcmp(l, prevline);
|
||||
|
||||
if(linesequel) {
|
||||
if (linesequel) {
|
||||
++prevlinecount;
|
||||
return;
|
||||
}
|
||||
|
||||
if(prevline != NULL) {
|
||||
if((prevlinecount == 1 && !dflag) ||
|
||||
(prevlinecount != 1 && !uflag)) {
|
||||
if (prevline != NULL) {
|
||||
if ((prevlinecount == 1 && !dflag) ||
|
||||
(prevlinecount != 1 && !uflag)) {
|
||||
printf(countfmt, prevlinecount);
|
||||
fputs(prevline, stdout);
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ uniqline(char *l)
|
|||
prevline = NULL;
|
||||
}
|
||||
|
||||
if(l && !(prevline = strdup(l)))
|
||||
if (l && !(prevline = strdup(l)))
|
||||
eprintf("strdup:");
|
||||
prevlinecount = 1;
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ uniq(FILE *fp, const char *str)
|
|||
char *buf = NULL;
|
||||
size_t size = 0;
|
||||
|
||||
while(agetline(&buf, &size, fp) != -1)
|
||||
while (agetline(&buf, &size, fp) != -1)
|
||||
uniqline(buf);
|
||||
}
|
||||
|
||||
|
|
4
unlink.c
4
unlink.c
|
@ -12,10 +12,10 @@ usage(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if(argc != 2)
|
||||
if (argc != 2)
|
||||
usage();
|
||||
|
||||
if(unlink(argv[1]) == -1)
|
||||
if (unlink(argv[1]) == -1)
|
||||
eprintf("unlink: '%s':", argv[1]);
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "text.h"
|
||||
|
||||
|
@ -127,10 +128,10 @@ uudecode(FILE *fp, FILE *outfp)
|
|||
#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)
|
||||
|
||||
while((len = agetline(&bufb, &n, fp)) != -1) {
|
||||
while ((len = agetline(&bufb, &n, fp)) != -1) {
|
||||
p = bufb;
|
||||
/* trim newlines */
|
||||
if(len && bufb[len - 1] != '\n')
|
||||
if (len && bufb[len - 1] != '\n')
|
||||
bufb[len - 1] = '\0';
|
||||
else
|
||||
eprintf("no newline found, aborting\n");
|
||||
|
@ -172,7 +173,7 @@ uudecode(FILE *fp, FILE *outfp)
|
|||
}
|
||||
/* check for end or fail */
|
||||
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");
|
||||
free(bufb);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void uuencode(FILE *, const char *, const char *);
|
||||
|
|
21
wc.c
21
wc.c
|
@ -4,6 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
static void output(const char *, long, long, long);
|
||||
|
@ -47,7 +48,7 @@ main(int argc, char *argv[])
|
|||
wc(stdin, NULL);
|
||||
} else {
|
||||
for (i = 0; i < argc; i++) {
|
||||
if(!(fp = fopen(argv[i], "r"))) {
|
||||
if (!(fp = fopen(argv[i], "r"))) {
|
||||
weprintf("fopen %s:", argv[i]);
|
||||
continue;
|
||||
}
|
||||
|
@ -65,13 +66,13 @@ output(const char *str, long nc, long nl, long nw)
|
|||
{
|
||||
bool noflags = !cmode && !lflag && !wflag;
|
||||
|
||||
if(lflag || noflags)
|
||||
if (lflag || noflags)
|
||||
printf(" %5ld", nl);
|
||||
if(wflag || noflags)
|
||||
if (wflag || noflags)
|
||||
printf(" %5ld", nw);
|
||||
if(cmode || noflags)
|
||||
if (cmode || noflags)
|
||||
printf(" %5ld", nc);
|
||||
if(str)
|
||||
if (str)
|
||||
printf(" %s", str);
|
||||
putchar('\n');
|
||||
}
|
||||
|
@ -83,14 +84,14 @@ wc(FILE *fp, const char *str)
|
|||
int c;
|
||||
long nc = 0, nl = 0, nw = 0;
|
||||
|
||||
while((c = getc(fp)) != EOF) {
|
||||
if(cmode != 'm' || UTF8_POINT(c))
|
||||
while ((c = getc(fp)) != EOF) {
|
||||
if (cmode != 'm' || UTF8_POINT(c))
|
||||
nc++;
|
||||
if(c == '\n')
|
||||
if (c == '\n')
|
||||
nl++;
|
||||
if(!isspace(c))
|
||||
if (!isspace(c))
|
||||
word = true;
|
||||
else if(word) {
|
||||
else if (word) {
|
||||
word = false;
|
||||
nw++;
|
||||
}
|
||||
|
|
9
xargs.c
9
xargs.c
|
@ -5,6 +5,7 @@
|
|||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
|
@ -48,7 +49,7 @@ main(int argc, char *argv[])
|
|||
ARGBEGIN {
|
||||
case 'n':
|
||||
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);
|
||||
break;
|
||||
case 'r':
|
||||
|
@ -92,13 +93,13 @@ main(int argc, char *argv[])
|
|||
i++;
|
||||
a++;
|
||||
leftover = 0;
|
||||
if(nflag == 1 && a >= maxargs)
|
||||
if (nflag == 1 && a >= maxargs)
|
||||
break;
|
||||
}
|
||||
cmd[i] = NULL;
|
||||
if(a >= maxargs && nflag == 1)
|
||||
if (a >= maxargs && nflag == 1)
|
||||
spawn();
|
||||
else if(!a || (i == 1 && rflag == 1))
|
||||
else if (!a || (i == 1 && rflag == 1))
|
||||
;
|
||||
else
|
||||
spawn();
|
||||
|
|
Loading…
Reference in New Issue
Block a user