Remove handrolled strcmp()'s
Favor readability over bare-metal.
This commit is contained in:
parent
1797df01e7
commit
9a074144c9
3
cat.c
3
cat.c
|
@ -1,5 +1,6 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
|
@ -29,7 +30,7 @@ main(int argc, char *argv[])
|
||||||
concat(stdin, "<stdin>", stdout, "<stdout>");
|
concat(stdin, "<stdin>", stdout, "<stdout>");
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
3
cksum.c
3
cksum.c
|
@ -1,6 +1,7 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -96,7 +97,7 @@ main(int argc, char *argv[])
|
||||||
cksum(stdin, NULL);
|
cksum(stdin, NULL);
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
3
cmp.c
3
cmp.c
|
@ -1,6 +1,7 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
for (n = 0; n < 2; n++) {
|
for (n = 0; n < 2; n++) {
|
||||||
if (argv[n][0] == '-' && !argv[n][1]) {
|
if (!strcmp(argv[n], "-")) {
|
||||||
argv[n] = "<stdin>";
|
argv[n] = "<stdin>";
|
||||||
fp[n] = stdin;
|
fp[n] = stdin;
|
||||||
} else {
|
} else {
|
||||||
|
|
2
cols.c
2
cols.c
|
@ -48,7 +48,7 @@ main(int argc, char *argv[])
|
||||||
getlines(stdin, &b);
|
getlines(stdin, &b);
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
2
comm.c
2
comm.c
|
@ -50,7 +50,7 @@ main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
for (i = 0; i < 2; i++) {
|
for (i = 0; i < 2; i++) {
|
||||||
if (argv[i][0] == '-' && !argv[i][1]) {
|
if (!strcmp(argv[i], "-")) {
|
||||||
argv[i] = "<stdin>";
|
argv[i] = "<stdin>";
|
||||||
fp[i] = stdin;
|
fp[i] = stdin;
|
||||||
} else if (!(fp[i] = fopen(argv[i], "r"))) {
|
} else if (!(fp[i] = fopen(argv[i], "r"))) {
|
||||||
|
|
2
cut.c
2
cut.c
|
@ -187,7 +187,7 @@ main(int argc, char *argv[])
|
||||||
cut(stdin, "<stdin>");
|
cut(stdin, "<stdin>");
|
||||||
else {
|
else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
3
expand.c
3
expand.c
|
@ -1,6 +1,7 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "utf.h"
|
#include "utf.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -110,7 +111,7 @@ main(int argc, char *argv[])
|
||||||
expand("<stdin>", stdin);
|
expand("<stdin>", stdin);
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
2
fold.c
2
fold.c
|
@ -95,7 +95,7 @@ main(int argc, char *argv[])
|
||||||
fold(stdin, "<stdin>");
|
fold(stdin, "<stdin>");
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
2
grep.c
2
grep.c
|
@ -260,7 +260,7 @@ main(int argc, char *argv[])
|
||||||
match = grep(stdin, "<stdin>");
|
match = grep(stdin, "<stdin>");
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
3
head.c
3
head.c
|
@ -2,6 +2,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -50,7 +51,7 @@ main(int argc, char *argv[])
|
||||||
} else {
|
} else {
|
||||||
many = argc > 1;
|
many = argc > 1;
|
||||||
for (newline = 0; *argv; argc--, argv++) {
|
for (newline = 0; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
2
join.c
2
join.c
|
@ -528,7 +528,7 @@ main(int argc, char *argv[])
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
for (n = 0; n < 2; n++) {
|
for (n = 0; n < 2; n++) {
|
||||||
if (argv[n][0] == '-' && !argv[n][1]) {
|
if (!strcmp(argv[n], "-")) {
|
||||||
argv[n] = "<stdin>";
|
argv[n] = "<stdin>";
|
||||||
fp[n] = stdin;
|
fp[n] = stdin;
|
||||||
} else if (!(fp[n] = fopen(argv[n], "r"))) {
|
} else if (!(fp[n] = fopen(argv[n], "r"))) {
|
||||||
|
|
2
nl.c
2
nl.c
|
@ -190,7 +190,7 @@ main(int argc, char *argv[])
|
||||||
if (!argc) {
|
if (!argc) {
|
||||||
nl("<stdin>", stdin);
|
nl("<stdin>", stdin);
|
||||||
} else {
|
} else {
|
||||||
if (argv[0][0] == '-' && !argv[0][1]) {
|
if (!strcmp(argv[0], "-")) {
|
||||||
argv[0] = "<stdin>";
|
argv[0] = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(argv[0], "r"))) {
|
} else if (!(fp = fopen(argv[0], "r"))) {
|
||||||
|
|
2
paste.c
2
paste.c
|
@ -116,7 +116,7 @@ main(int argc, char *argv[])
|
||||||
dsc = ereallocarray(NULL, argc, sizeof(*dsc));
|
dsc = ereallocarray(NULL, argc, sizeof(*dsc));
|
||||||
|
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
if (argv[i][0] == '-' && !argv[i][1]) {
|
if (!strcmp(argv[i], "-")) {
|
||||||
argv[i] = "<stdin>";
|
argv[i] = "<stdin>";
|
||||||
dsc[i].fp = stdin;
|
dsc[i].fp = stdin;
|
||||||
} else if (!(dsc[i].fp = fopen(argv[i], "r"))) {
|
} else if (!(dsc[i].fp = fopen(argv[i], "r"))) {
|
||||||
|
|
2
sed.c
2
sed.c
|
@ -1114,7 +1114,7 @@ next_file(void)
|
||||||
if (first) /* given no files, default to stdin */
|
if (first) /* given no files, default to stdin */
|
||||||
file = stdin;
|
file = stdin;
|
||||||
/* else we've used all our files, leave file = NULL */
|
/* else we've used all our files, leave file = NULL */
|
||||||
} else if ((*files)[0] == '-' && !(*files)[1]) {
|
} else if (!strcmp(*files, "-")) {
|
||||||
file = stdin;
|
file = stdin;
|
||||||
files++;
|
files++;
|
||||||
} else if (!(file = fopen(*files++, "r"))) {
|
} else if (!(file = fopen(*files++, "r"))) {
|
||||||
|
|
2
sort.c
2
sort.c
|
@ -301,7 +301,7 @@ main(int argc, char *argv[])
|
||||||
getlines(stdin, &linebuf);
|
getlines(stdin, &linebuf);
|
||||||
}
|
}
|
||||||
} else for (; *argv; argc--, argv++) {
|
} else for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "utf.h"
|
#include "utf.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -79,7 +80,7 @@ main(int argc, char *argv[])
|
||||||
strings(stdin, "<stdin>", len);
|
strings(stdin, "<stdin>", len);
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
2
tail.c
2
tail.c
|
@ -115,7 +115,7 @@ main(int argc, char *argv[])
|
||||||
if ((many = argc > 1) && fflag)
|
if ((many = argc > 1) && fflag)
|
||||||
usage();
|
usage();
|
||||||
for (newline = 0; *argv; argc--, argv++) {
|
for (newline = 0; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "utf.h"
|
#include "utf.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -153,7 +154,7 @@ main(int argc, char *argv[])
|
||||||
unexpand("<stdin>", stdin);
|
unexpand("<stdin>", stdin);
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
|
@ -246,7 +246,7 @@ main(int argc, char *argv[])
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
if (!argc || (argv[0][0] == '-' && !argv[0][1])) {
|
if (!argc || !strcmp(argv[0], "-")) {
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
ifname = "<stdin>";
|
ifname = "<stdin>";
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -114,7 +115,7 @@ main(int argc, char *argv[])
|
||||||
if (!argc || argc > 2)
|
if (!argc || argc > 2)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
if (argc == 1 || (argv[0][0] == '-' && !argv[0][1])) {
|
if (argc == 1 || !strcmp(argv[0], "-")) {
|
||||||
if (mflag)
|
if (mflag)
|
||||||
uuencodeb64(stdin, argv[0], "<stdin>");
|
uuencodeb64(stdin, argv[0], "<stdin>");
|
||||||
else
|
else
|
||||||
|
|
4
wc.c
4
wc.c
|
@ -1,4 +1,6 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "utf.h"
|
#include "utf.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
@ -84,7 +86,7 @@ main(int argc, char *argv[])
|
||||||
wc(stdin, NULL);
|
wc(stdin, NULL);
|
||||||
} else {
|
} else {
|
||||||
for (many = (argc > 1); *argv; argc--, argv++) {
|
for (many = (argc > 1); *argv; argc--, argv++) {
|
||||||
if ((*argv)[0] == '-' && !(*argv)[1]) {
|
if (!strcmp(*argv, "-")) {
|
||||||
*argv = "<stdin>";
|
*argv = "<stdin>";
|
||||||
fp = stdin;
|
fp = stdin;
|
||||||
} else if (!(fp = fopen(*argv, "r"))) {
|
} else if (!(fp = fopen(*argv, "r"))) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user