Skip to content

Commit e8eddca

Browse files
committed
shuf 2.3
-e can have 0 options; handle incorrect -i input better.
1 parent 02a1d31 commit e8eddca

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

shuf.c

+13-14
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ shuffile(const char *input, int argn, size_t inputlen)
8888
size_t len;
8989

9090
if ((args = reallocarray(args, argn, sizeof(char *))) == NULL)
91-
errx(1, "shuffile");
91+
err(1, "shuffile");
9292

9393
while (i < argn) {
9494
for (s = input; *s != delimiter; s++) {
@@ -98,18 +98,15 @@ shuffile(const char *input, int argn, size_t inputlen)
9898
len = s - input;
9999

100100
if ((args[i] = malloc(len + 1)) == NULL)
101-
err(1, "malloc");
101+
err(1, "shuffile");
102102
argt = args[i++];
103103

104104
while (len-- > 0)
105105
*argt++ = *input++;
106106
*argt = '\0';
107107

108108
input++;
109-
if (inputlen > 0) {
110-
if (--inputlen == 0)
111-
break;
112-
}
109+
--inputlen;
113110
}
114111

115112
most = (most < argn ? most : argn);
@@ -134,12 +131,13 @@ shufintegers(int range, int lo)
134131
int *args = NULL, argt, i, j;
135132

136133
if ((args = reallocarray(args, range, sizeof(int))) == NULL)
137-
errx(1, "range size will exhaust memory");
134+
err(1, "range size will exhaust memory");
138135

139136
if (rflag) {
140-
while ((most == -1 ? 1 : most-- > 0))
137+
while ((most == -1 ? 1 : most-- > 0)) {
141138
fprintf(ofile, "%u%c",
142139
arc4random_uniform(range) + lo, delimiter);
140+
}
143141
} else {
144142
for (i = 0; i < range; i++)
145143
args[i] = lo + i;
@@ -196,7 +194,7 @@ static void
196194
version(void)
197195
{
198196

199-
fputs("shuf 2.2\n"
197+
fputs("shuf 2.3\n"
200198
"Copyright (c) 2017-2019 Brian Callahan <[email protected]>\n"
201199
"\nPermission to use, copy, modify, and distribute this software"
202200
" for any\npurpose with or without fee is hereby granted, "
@@ -263,10 +261,11 @@ main(int argc, char *argv[])
263261
break;
264262
case 'n':
265263
most = strtonum(optarg, 0, INT_MAX, &errstr);
266-
if (errstr != NULL)
264+
if (errstr != NULL) {
267265
errx(1,
268266
"-n count must be from 0 to %d, not %s",
269267
INT_MAX, optarg);
268+
}
270269
break;
271270
case 'o':
272271
if (oflag++)
@@ -294,12 +293,12 @@ main(int argc, char *argv[])
294293
if (oflag == 0)
295294
ofile = stdout;
296295

297-
if (eflag == 0 && argc > 1)
298-
errx(1, "extra operand '%s'", *++argv);
296+
if (eflag == 0 && ((iflag == 1 && argc != 0) || argc > 1))
297+
errx(1, "extra operand '%s'", iflag == 1 ? *argv : *++argv);
299298

300299
if (eflag) {
301-
if (argc < 1)
302-
errx(1, "must provide at least one argument with -e");
300+
if (argc == 0)
301+
goto out;
303302
repledge(oflag);
304303
shufecho(argc, argv);
305304
goto out;

0 commit comments

Comments
 (0)