Add -0 option.

This commit is contained in:
christos 2003-08-20 23:37:51 +00:00
parent 109461480a
commit 0d30aed428
2 changed files with 20 additions and 6 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: shuffle.1,v 1.3 2002/03/05 15:06:16 wiz Exp $
.\" $NetBSD: shuffle.1,v 1.4 2003/08/20 23:37:51 christos Exp $
.\"
.\" Copyright (c) 1998
.\" Perry E. Metzger. All rights reserved.
@ -38,6 +38,7 @@
.Nd print a random permutation of the command line arguments
.Sh SYNOPSIS
.Nm
.Op Fl 0
.Op Fl f Ar filename ...
.Op Fl n Ar number
.Op Fl p Ar number
@ -69,6 +70,15 @@ If the
.Fl p
option is given, its argument is treated as a number, and the program
prints that number of randomly selected lines or arguments in a random order.
.Pp
The
.Fl 0
option, changes the field separator char from a \en to \e0, so that it
is suitable to be sent to
.Xr xargs 1
with
.Fl 0
to handle filenames with whitespace in them.
.Sh EXAMPLES
.Bd -literal -offset indent
$ shuffle a b c d

View File

@ -1,4 +1,4 @@
/* $NetBSD: shuffle.c,v 1.12 2003/04/18 03:21:02 lukem Exp $ */
/* $NetBSD: shuffle.c,v 1.13 2003/08/20 23:37:51 christos Exp $ */
/*
* Copyright (c) 1998
@ -33,7 +33,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: shuffle.c,v 1.12 2003/04/18 03:21:02 lukem Exp $");
__RCSID("$NetBSD: shuffle.c,v 1.13 2003/08/20 23:37:51 christos Exp $");
#endif /* not lint */
#include <sys/time.h>
@ -132,7 +132,7 @@ usage(void)
{
(void) fprintf(stderr,
"Usage: %s [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
"Usage: %s [-0] [-f <filename>] [-n <number>] [-p <number>] [<arg> ...]\n",
getprogname());
exit(1);
}
@ -205,9 +205,13 @@ main(int argc, char *argv[])
struct timeval tv;
char **lines = NULL;
size_t nlines = 0, pick = 0;
char sep = '\n';
while ((ch = getopt(argc, argv, "f:n:p:")) != -1) {
switch(ch) {
case '0':
sep = '\0';
break;
case 'f':
fname = optarg;
break;
@ -250,9 +254,9 @@ main(int argc, char *argv[])
for (i = 0; i < nlines; i++) {
if (nflag)
printf("%ld\n", (long)shuffle[i]);
printf("%ld%c", (long)shuffle[i], sep);
else
printf("%s\n", lines[shuffle[i]]);
printf("%s%c", lines[shuffle[i]], sep);
}
return 0;