From ca156092365bf00bc14a3a147334ef354fdc9246 Mon Sep 17 00:00:00 2001 From: Philippe Charnier Date: Tue, 1 Jul 1997 06:34:31 +0000 Subject: [PATCH] Add usage(), rcsid. Use getopt(). Obtained from: OpenBSD. --- usr.bin/expand/expand.c | 56 ++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/usr.bin/expand/expand.c b/usr.bin/expand/expand.c index c3a00e7d01cb..9d92f41cfdad 100644 --- a/usr.bin/expand/expand.c +++ b/usr.bin/expand/expand.c @@ -38,16 +38,29 @@ static char copyright[] = #endif /* not lint */ #ifndef lint +#if 0 static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93"; +#else +static const char rcsid[] = + "$Id$"; +#endif #endif /* not lint */ +#include #include +#include +#include + /* * expand - expand tabs to equivalent spaces */ int nstops; int tabstops[100]; +static void getstops __P((char *)); +static void usage __P((void)); + +int main(argc, argv) int argc; char *argv[]; @@ -55,12 +68,27 @@ main(argc, argv) register int c, column; register int n; - argc--, argv++; - do { - while (argc > 0 && argv[0][0] == '-') { - getstops(argv[0]); - argc--, argv++; + /* handle obsolete syntax */ + while (argc > 1 && argv[1][0] && isdigit(argv[1][1])) { + getstops(&argv[1][1]); + argc--; argv++; + } + + while ((c = getopt (argc, argv, "t:")) != -1) { + switch (c) { + case 't': + getstops(optarg); + break; + case '?': + default: + usage(); + /* NOTREACHED */ } + } + argc -= optind; + argv += optind; + + do { if (argc > 0) { if (freopen(argv[0], "r", stdin) == NULL) { perror(argv[0]); @@ -69,12 +97,8 @@ main(argc, argv) argc--, argv++; } column = 0; - for (;;) { - c = getc(stdin); - if (c == -1) - break; + while ((c = getchar()) != EOF) { switch (c) { - case '\t': if (nstops == 0) { do { @@ -125,13 +149,13 @@ main(argc, argv) exit(0); } +static void getstops(cp) register char *cp; { register int i; nstops = 0; - cp++; for (;;) { i = 0; while (*cp >= '0' && *cp <= '9') @@ -146,7 +170,15 @@ getstops(cp) tabstops[nstops++] = i; if (*cp == 0) break; - if (*cp++ != ',') + if (*cp != ',' && *cp != ' ') goto bad; + cp++; } } + +static void +usage() +{ + (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n"); + exit(1); +}