Skip to content

Commit 0703456

Browse files
committed
shuf-2.9
Save a line; only check -e and -i flags together once. Improve error messages. Shorten test output without losing test coverage.
1 parent 1b3ce0f commit 0703456

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Makefile
12
shuf
23
*.o
34
*.core

configure

+6-6
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,16 @@ test:
345345
@echo BSD shuf test suite
346346
@echo ====================================
347347
@echo Test 1: File input
348-
@echo shuf shuf.c
349-
@./shuf shuf.c || (echo Test 1 failed; exit 1)
348+
@echo shuf LICENSE
349+
@./shuf LICENSE || (echo Test 1 failed; exit 1)
350350
@echo ====================================
351351
@echo Test 2: -e flag, -n flag, -r flag
352-
@echo shuf -e -n 10 -r Heads Tails
353-
@./shuf -e -n 10 -r Heads Tails || (echo Test 2 failed; exit 1)
352+
@echo shuf -e -n 5 -r Heads Tails
353+
@./shuf -e -n 5 -r Heads Tails || (echo Test 2 failed; exit 1)
354354
@echo ====================================
355355
@echo Test 3: -i flag
356-
@echo shuf -i 1-20 -n 10
357-
@./shuf -i 1-20 -n 10 || (echo Test 3 failed; exit 1)
356+
@echo shuf -i 1-10 -n 5
357+
@./shuf -i 1-10 -n 5 || (echo Test 3 failed; exit 1)
358358
@echo ====================================
359359
@echo All tests passed
360360
@echo ====================================

shuf.c

+15-16
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ shuffile(const char *input, int argn, size_t inputlen)
7777
size_t len;
7878

7979
if ((args = reallocarray(args, argn, sizeof(char *))) == NULL)
80-
err(1, "shuffile");
80+
err(1, "could not create shuffile buffer");
8181

8282
while (i < argn) {
8383
for (s = input; *s != delimiter; s++) {
@@ -87,7 +87,7 @@ shuffile(const char *input, int argn, size_t inputlen)
8787
len = s - input;
8888

8989
if ((args[i] = malloc(len + 1)) == NULL)
90-
err(1, "shuffile");
90+
err(1, "could not create shuffile item buffer");
9191
argt = args[i++];
9292

9393
while (len-- > 0)
@@ -160,7 +160,7 @@ repledge(int oflag)
160160
new = "stdio";
161161

162162
if (pledge(new, NULL) == -1)
163-
errx(1, "pledge");
163+
err(1, "repledge");
164164
#endif
165165
}
166166

@@ -183,7 +183,7 @@ static void __dead
183183
version(void)
184184
{
185185

186-
fputs("shuf 2.8\n"
186+
fputs("shuf 2.9\n"
187187
"Copyright (c) 2017-2019 Brian Callahan <[email protected]>\n"
188188
"\nPermission to use, copy, modify, and distribute this software"
189189
" for any\npurpose with or without fee is hereby granted, "
@@ -213,26 +213,22 @@ main(int argc, char *argv[])
213213

214214
#ifdef HAVE_PLEDGE
215215
if (pledge("stdio rpath wpath cpath", NULL) == -1)
216-
errx(1, "pledge");
216+
err(1, "pledge");
217217
#endif
218218

219219
while ((ch = getopt(argc, argv, "ehi:n:o:rvz")) != -1) {
220220
switch (ch) {
221221
case 'e':
222-
if (iflag)
223-
errx(1, "cannot combine -e with -i");
224222
eflag = 1;
225223
break;
226224
case 'h':
227225
usage();
228226
case 'i':
229-
if (eflag)
230-
errx(1, "cannot combine -i with -e");
231227
if (iflag++)
232-
errx(1, "cannot have multiple -i");
228+
errx(1, "cannot have multiple -i options");
233229

234230
if ((argp = strchr(optarg, '-')) == NULL)
235-
errx(1, "must provide lo and hi for -i");
231+
errx(1, "must provide lo and hi for -i option");
236232
*argp = '\0';
237233

238234
lo = strtonum(optarg, 0, INT_MAX, &errstr);
@@ -246,7 +242,7 @@ main(int argc, char *argv[])
246242
if (lo >= hi)
247243
errx(1, "lo is greater than or equal to hi");
248244
if (hi == INT_MAX && lo == 0)
249-
errx(1, "lo-hi range too large");
245+
errx(1, "lo-hi range too large for -i option");
250246
break;
251247
case 'n':
252248
most = strtonum(optarg, 0, INT_MAX, &errstr);
@@ -258,10 +254,10 @@ main(int argc, char *argv[])
258254
break;
259255
case 'o':
260256
if (oflag++)
261-
errx(1, "cannot have multiple -o");
257+
errx(1, "cannot have multiple -o options");
262258

263259
if ((ofile = fopen(optarg, "w")) == NULL)
264-
err(1, "couldn't open output file %s", optarg);
260+
err(1, "could not open output file %s", optarg);
265261

266262
break;
267263
case 'r':
@@ -279,6 +275,9 @@ main(int argc, char *argv[])
279275
argc -= optind;
280276
argv += optind;
281277

278+
if (eflag && iflag)
279+
errx(1, "cannot combine -e and -i options");
280+
282281
if (oflag == 0)
283282
ofile = stdout;
284283

@@ -312,7 +311,7 @@ main(int argc, char *argv[])
312311
repledge(oflag);
313312

314313
if ((buf = malloc(bufsize)) == NULL)
315-
err(1, "malloc failed");
314+
err(1, "could not create initial shuffle buffer");
316315

317316
while ((ch = fgetc(ifile)) != EOF) {
318317
buf[buflen++] = ch;
@@ -322,7 +321,7 @@ main(int argc, char *argv[])
322321
if ((nbuf = realloc(buf, nbufsize)) == NULL) {
323322
free(buf);
324323
buf = NULL;
325-
err(1, "main");
324+
err(1, "could not resize shuffle buffer");
326325
}
327326
buf = nbuf;
328327
bufsize = nbufsize;

0 commit comments

Comments
 (0)