xref: /illumos-gate/usr/src/cmd/nl/nl.c (revision 6a7605d4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*6a7605d4SToomas Soome /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <locale.h>
327c478bd9Sstevel@tonic-gate #include <regexpr.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <wchar.h>
387c478bd9Sstevel@tonic-gate #include <wctype.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	EXPSIZ		512
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifdef XPG4
447c478bd9Sstevel@tonic-gate #define	USAGE "usage: nl [-p] [-b type] [-d delim] [ -f type] " \
457c478bd9Sstevel@tonic-gate 	"[-h type] [-i incr] [-l num] [-n format]\n" \
467c478bd9Sstevel@tonic-gate 	"[-s sep] [-v startnum] [-w width] [file]\n"
477c478bd9Sstevel@tonic-gate #else
487c478bd9Sstevel@tonic-gate #define	USAGE "usage: nl [-p] [-btype] [-ddelim] [ -ftype] " \
497c478bd9Sstevel@tonic-gate 	"[-htype] [-iincr] [-lnum] [-nformat] [-ssep] " \
507c478bd9Sstevel@tonic-gate 	"[-vstartnum] [-wwidth] [file]\n"
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #ifdef u370
547c478bd9Sstevel@tonic-gate 	int nbra, sed;	/* u370 - not used in nl.c, but extern in regexp.h */
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate static int width = 6;	/* Declare default width of number */
577c478bd9Sstevel@tonic-gate static char nbuf[100];	/* Declare bufsize used in convert/pad/cnt routines */
587c478bd9Sstevel@tonic-gate static char *bexpbuf;	/* Declare the regexp buf */
597c478bd9Sstevel@tonic-gate static char *hexpbuf;	/* Declare the regexp buf */
607c478bd9Sstevel@tonic-gate static char *fexpbuf;	/* Declare the regexp buf */
617c478bd9Sstevel@tonic-gate static char delim1 = '\\';
627c478bd9Sstevel@tonic-gate static char delim2 = ':';	/* Default delimiters. */
637c478bd9Sstevel@tonic-gate static char pad = ' ';	/* Declare the default pad for numbers */
647c478bd9Sstevel@tonic-gate static char *s;	/* Declare the temp array for args */
657c478bd9Sstevel@tonic-gate static char s1[EXPSIZ];	/* Declare the conversion array */
667c478bd9Sstevel@tonic-gate static char format = 'n'; /* Declare the format of numbers to be rt just */
677c478bd9Sstevel@tonic-gate static int q = 2;	/* Initialize arg pointer to drop 1st 2 chars */
687c478bd9Sstevel@tonic-gate static int k;	/* Declare var for return of convert */
697c478bd9Sstevel@tonic-gate static int r;	/* Declare the arg array ptr for string args */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifdef XPG4
727c478bd9Sstevel@tonic-gate static int convert(int, char *);
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate static int convert(char *);
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate static void num(int, int);
777c478bd9Sstevel@tonic-gate static void npad(int, char *);
787c478bd9Sstevel@tonic-gate #ifdef XPG4
797c478bd9Sstevel@tonic-gate static void optmsg(int, char *);
807c478bd9Sstevel@tonic-gate #else
817c478bd9Sstevel@tonic-gate static void optmsg(char *);
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate static void pnum(int, char *);
847c478bd9Sstevel@tonic-gate static void regerr(int);
857c478bd9Sstevel@tonic-gate static void usage();
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate extern char *optarg;	/* getopt support */
887c478bd9Sstevel@tonic-gate extern int optind;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])9156849bc6SToomas Soome main(int argc, char *argv[])
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	register int j;
947c478bd9Sstevel@tonic-gate 	register int i = 0;
957c478bd9Sstevel@tonic-gate 	register char *p;
967c478bd9Sstevel@tonic-gate 	register char header = 'n';
977c478bd9Sstevel@tonic-gate 	register char body = 't';
987c478bd9Sstevel@tonic-gate 	register char footer = 'n';
997c478bd9Sstevel@tonic-gate 	char line[LINE_MAX];
1007c478bd9Sstevel@tonic-gate 	char tempchr;	/* Temporary holding variable. */
1017c478bd9Sstevel@tonic-gate 	char swtch = 'n';
1027c478bd9Sstevel@tonic-gate 	char cntck = 'n';
1037c478bd9Sstevel@tonic-gate 	char type;
1047c478bd9Sstevel@tonic-gate 	int cnt;	/* line counter */
1057c478bd9Sstevel@tonic-gate 	int pass1 = 1;	/* First pass flag. 1=pass1, 0=additional passes. */
1067c478bd9Sstevel@tonic-gate 	char sep[EXPSIZ];
1077c478bd9Sstevel@tonic-gate 	char pat[EXPSIZ];
1087c478bd9Sstevel@tonic-gate 	int startcnt = 1;
1097c478bd9Sstevel@tonic-gate 	int increment = 1;
1107c478bd9Sstevel@tonic-gate 	int blank = 1;
1117c478bd9Sstevel@tonic-gate 	int blankctr = 0;
1127c478bd9Sstevel@tonic-gate 	int c;
1137c478bd9Sstevel@tonic-gate 	int lnt;
1147c478bd9Sstevel@tonic-gate 	char last;
1157c478bd9Sstevel@tonic-gate 	FILE *iptr = stdin;
1167c478bd9Sstevel@tonic-gate 	FILE *optr = stdout;
1177c478bd9Sstevel@tonic-gate #ifndef XPG4
1187c478bd9Sstevel@tonic-gate 	int option_end = 0;
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	sep[0] = '\t';
1227c478bd9Sstevel@tonic-gate 	sep[1] = '\0';
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1257c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1267c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #ifdef XPG4
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * XPG4:  Allow either a space or no space between the
1337c478bd9Sstevel@tonic-gate 	 *	  options and their required arguments.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	while (argc > 0) {
1377c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv,
13856849bc6SToomas Soome 		    "pb:d:f:h:i:l:n:s:v:w:")) != EOF) {
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 			switch (c) {
1417c478bd9Sstevel@tonic-gate 			case 'h':
1427c478bd9Sstevel@tonic-gate 				switch (*optarg) {
1437c478bd9Sstevel@tonic-gate 				case 'n':
1447c478bd9Sstevel@tonic-gate 					header = 'n';
1457c478bd9Sstevel@tonic-gate 					break;
1467c478bd9Sstevel@tonic-gate 				case 't':
1477c478bd9Sstevel@tonic-gate 					header = 't';
1487c478bd9Sstevel@tonic-gate 					break;
1497c478bd9Sstevel@tonic-gate 				case 'a':
1507c478bd9Sstevel@tonic-gate 					header = 'a';
1517c478bd9Sstevel@tonic-gate 					break;
1527c478bd9Sstevel@tonic-gate 				case 'p':
1537c478bd9Sstevel@tonic-gate 					(void) strcpy(pat, optarg+1);
1547c478bd9Sstevel@tonic-gate 					header = 'h';
1557c478bd9Sstevel@tonic-gate 					hexpbuf =
15656849bc6SToomas Soome 					    compile(pat, NULL,  NULL);
1577c478bd9Sstevel@tonic-gate 					if (regerrno)
1587c478bd9Sstevel@tonic-gate 						regerr(regerrno);
1597c478bd9Sstevel@tonic-gate 					break;
1607c478bd9Sstevel@tonic-gate 				case '\0':
1617c478bd9Sstevel@tonic-gate 					header = 'n';
1627c478bd9Sstevel@tonic-gate 					break;
1637c478bd9Sstevel@tonic-gate 				default:
1647c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
1657c478bd9Sstevel@tonic-gate 				}
1667c478bd9Sstevel@tonic-gate 				break;
1677c478bd9Sstevel@tonic-gate 			case 'b':
1687c478bd9Sstevel@tonic-gate 				switch (*optarg) {
1697c478bd9Sstevel@tonic-gate 				case 't':
1707c478bd9Sstevel@tonic-gate 					body = 't';
1717c478bd9Sstevel@tonic-gate 					break;
1727c478bd9Sstevel@tonic-gate 				case 'a':
1737c478bd9Sstevel@tonic-gate 					body = 'a';
1747c478bd9Sstevel@tonic-gate 					break;
1757c478bd9Sstevel@tonic-gate 				case 'n':
1767c478bd9Sstevel@tonic-gate 					body = 'n';
1777c478bd9Sstevel@tonic-gate 					break;
1787c478bd9Sstevel@tonic-gate 				case 'p':
1797c478bd9Sstevel@tonic-gate 					(void) strcpy(pat, optarg+1);
1807c478bd9Sstevel@tonic-gate 					body = 'b';
1817c478bd9Sstevel@tonic-gate 					bexpbuf =
18256849bc6SToomas Soome 					    compile(pat, NULL, NULL);
1837c478bd9Sstevel@tonic-gate 					if (regerrno)
1847c478bd9Sstevel@tonic-gate 						regerr(regerrno);
1857c478bd9Sstevel@tonic-gate 					break;
1867c478bd9Sstevel@tonic-gate 				case '\0':
1877c478bd9Sstevel@tonic-gate 					body = 't';
1887c478bd9Sstevel@tonic-gate 					break;
1897c478bd9Sstevel@tonic-gate 				default:
1907c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
1917c478bd9Sstevel@tonic-gate 				}
1927c478bd9Sstevel@tonic-gate 				break;
1937c478bd9Sstevel@tonic-gate 			case 'f':
1947c478bd9Sstevel@tonic-gate 				switch (*optarg) {
1957c478bd9Sstevel@tonic-gate 				case 'n':
1967c478bd9Sstevel@tonic-gate 					footer = 'n';
1977c478bd9Sstevel@tonic-gate 					break;
1987c478bd9Sstevel@tonic-gate 				case 't':
1997c478bd9Sstevel@tonic-gate 					footer = 't';
2007c478bd9Sstevel@tonic-gate 					break;
2017c478bd9Sstevel@tonic-gate 				case 'a':
2027c478bd9Sstevel@tonic-gate 					footer = 'a';
2037c478bd9Sstevel@tonic-gate 					break;
2047c478bd9Sstevel@tonic-gate 				case 'p':
2057c478bd9Sstevel@tonic-gate 					(void) strcpy(pat, optarg+1);
2067c478bd9Sstevel@tonic-gate 					footer = 'f';
2077c478bd9Sstevel@tonic-gate 					fexpbuf =
20856849bc6SToomas Soome 					    compile(pat, NULL, NULL);
2097c478bd9Sstevel@tonic-gate 					if (regerrno)
2107c478bd9Sstevel@tonic-gate 						regerr(regerrno);
2117c478bd9Sstevel@tonic-gate 					break;
2127c478bd9Sstevel@tonic-gate 				case '\0':
2137c478bd9Sstevel@tonic-gate 					footer = 'n';
2147c478bd9Sstevel@tonic-gate 					break;
2157c478bd9Sstevel@tonic-gate 				default:
2167c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
2177c478bd9Sstevel@tonic-gate 				}
2187c478bd9Sstevel@tonic-gate 				break;
2197c478bd9Sstevel@tonic-gate 			case 'p':
2207c478bd9Sstevel@tonic-gate 				if (optarg == (char *)NULL)
2217c478bd9Sstevel@tonic-gate 					cntck = 'y';
2227c478bd9Sstevel@tonic-gate 				else
2237c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
2247c478bd9Sstevel@tonic-gate 				break;
2257c478bd9Sstevel@tonic-gate 			case 'v':
2267c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
2277c478bd9Sstevel@tonic-gate 					startcnt = 1;
2287c478bd9Sstevel@tonic-gate 				else
2297c478bd9Sstevel@tonic-gate 					startcnt = convert(c, optarg);
2307c478bd9Sstevel@tonic-gate 				break;
2317c478bd9Sstevel@tonic-gate 			case 'i':
2327c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
2337c478bd9Sstevel@tonic-gate 					increment = 1;
2347c478bd9Sstevel@tonic-gate 				else
2357c478bd9Sstevel@tonic-gate 					increment = convert(c, optarg);
2367c478bd9Sstevel@tonic-gate 				break;
2377c478bd9Sstevel@tonic-gate 			case 'w':
2387c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
2397c478bd9Sstevel@tonic-gate 					width = 6;
2407c478bd9Sstevel@tonic-gate 				else
2417c478bd9Sstevel@tonic-gate 					width = convert(c, optarg);
2427c478bd9Sstevel@tonic-gate 				break;
2437c478bd9Sstevel@tonic-gate 			case 'l':
2447c478bd9Sstevel@tonic-gate 				if (*optarg == '\0')
2457c478bd9Sstevel@tonic-gate 					blank = 1;
2467c478bd9Sstevel@tonic-gate 				else
2477c478bd9Sstevel@tonic-gate 					blank = convert(c, optarg);
2487c478bd9Sstevel@tonic-gate 				break;
2497c478bd9Sstevel@tonic-gate 			case 'n':
2507c478bd9Sstevel@tonic-gate 				switch (*optarg) {
2517c478bd9Sstevel@tonic-gate 				case 'l':
2527c478bd9Sstevel@tonic-gate 					if (*(optarg+1) == 'n')
2537c478bd9Sstevel@tonic-gate 						format = 'l';
2547c478bd9Sstevel@tonic-gate 					else
2557c478bd9Sstevel@tonic-gate 						optmsg(c, optarg);
2567c478bd9Sstevel@tonic-gate 					break;
2577c478bd9Sstevel@tonic-gate 				case 'r':
2587c478bd9Sstevel@tonic-gate 					if ((*(optarg+1) == 'n') ||
2597c478bd9Sstevel@tonic-gate 					    (*(optarg+1) == 'z'))
2607c478bd9Sstevel@tonic-gate 						format = *(optarg+1);
2617c478bd9Sstevel@tonic-gate 					else
2627c478bd9Sstevel@tonic-gate 						optmsg(c, optarg);
2637c478bd9Sstevel@tonic-gate 					break;
2647c478bd9Sstevel@tonic-gate 				case '\0':
2657c478bd9Sstevel@tonic-gate 					format = 'n';
2667c478bd9Sstevel@tonic-gate 					break;
2677c478bd9Sstevel@tonic-gate 				default:
2687c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
2697c478bd9Sstevel@tonic-gate 					break;
2707c478bd9Sstevel@tonic-gate 				}
2717c478bd9Sstevel@tonic-gate 				break;
2727c478bd9Sstevel@tonic-gate 			case 's':
2737c478bd9Sstevel@tonic-gate 				(void) strcpy(sep, optarg);
2747c478bd9Sstevel@tonic-gate 				break;
2757c478bd9Sstevel@tonic-gate 			case 'd':
2767c478bd9Sstevel@tonic-gate 				delim1 = *optarg;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 				if (*(optarg+1) == '\0')
2797c478bd9Sstevel@tonic-gate 					break;
2807c478bd9Sstevel@tonic-gate 				delim2 = *(optarg+1);
2817c478bd9Sstevel@tonic-gate 				if (*(optarg+2) != '\0')
2827c478bd9Sstevel@tonic-gate 					optmsg(c, optarg);
2837c478bd9Sstevel@tonic-gate 				break;
2847c478bd9Sstevel@tonic-gate 			default:
2857c478bd9Sstevel@tonic-gate 				optmsg(c, optarg);
2867c478bd9Sstevel@tonic-gate 			} /* end switch char returned from getopt() */
2877c478bd9Sstevel@tonic-gate 		} /* end while getopt */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		argv += optind;
2907c478bd9Sstevel@tonic-gate 		argc -= optind;
2917c478bd9Sstevel@tonic-gate 		optind = 0;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		if (argc > 0) {
2947c478bd9Sstevel@tonic-gate 			if ((iptr = fopen(argv[0], "r")) == NULL)  {
2957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "nl: %s: ", argv[0]);
2967c478bd9Sstevel@tonic-gate 				perror("");
2977c478bd9Sstevel@tonic-gate 				return (1);
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 			++argv;
3007c478bd9Sstevel@tonic-gate 			--argc;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 	} /* end while argc > 0 */
3037c478bd9Sstevel@tonic-gate /* end XPG4 version of argument parsing */
3047c478bd9Sstevel@tonic-gate #else
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  * Solaris:  For backward compatibility, do not allow a space between the
3077c478bd9Sstevel@tonic-gate  *	     options and their arguments.  Option arguments are optional,
3087c478bd9Sstevel@tonic-gate  *	     not required as in the XPG4 version of nl.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate for (j = 1; j < argc; j++) {
3117c478bd9Sstevel@tonic-gate 	if (argv[j][i] == '-' && (c = argv[j][i + 1])) {
3127c478bd9Sstevel@tonic-gate 		if (!option_end) {
3137c478bd9Sstevel@tonic-gate 			switch (c) {
3147c478bd9Sstevel@tonic-gate 			case 'h':
3157c478bd9Sstevel@tonic-gate 				switch (argv[j][i + 2]) {
3167c478bd9Sstevel@tonic-gate 					case 'n':
3177c478bd9Sstevel@tonic-gate 						header = 'n';
3187c478bd9Sstevel@tonic-gate 						break;
3197c478bd9Sstevel@tonic-gate 					case 't':
3207c478bd9Sstevel@tonic-gate 						header = 't';
3217c478bd9Sstevel@tonic-gate 						break;
3227c478bd9Sstevel@tonic-gate 					case 'a':
3237c478bd9Sstevel@tonic-gate 						header = 'a';
3247c478bd9Sstevel@tonic-gate 						break;
3257c478bd9Sstevel@tonic-gate 					case 'p':
3267c478bd9Sstevel@tonic-gate 						s = argv[j];
3277c478bd9Sstevel@tonic-gate 						q = 3;
3287c478bd9Sstevel@tonic-gate 						r = 0;
3297c478bd9Sstevel@tonic-gate 						while (s[q] != '\0') {
3307c478bd9Sstevel@tonic-gate 							pat[r] = s[q];
3317c478bd9Sstevel@tonic-gate 							r++;
3327c478bd9Sstevel@tonic-gate 							q++;
3337c478bd9Sstevel@tonic-gate 						}
3347c478bd9Sstevel@tonic-gate 						pat[r] = '\0';
3357c478bd9Sstevel@tonic-gate 						header = 'h';
33656849bc6SToomas Soome 						hexpbuf =
33756849bc6SToomas Soome 						    compile(pat, NULL, NULL);
33856849bc6SToomas Soome 						if (regerrno)
33956849bc6SToomas Soome 							regerr(regerrno);
3407c478bd9Sstevel@tonic-gate 						break;
3417c478bd9Sstevel@tonic-gate 					case '\0':
3427c478bd9Sstevel@tonic-gate 						header = 'n';
3437c478bd9Sstevel@tonic-gate 						break;
3447c478bd9Sstevel@tonic-gate 					default:
3457c478bd9Sstevel@tonic-gate 						optmsg(argv[j]);
3467c478bd9Sstevel@tonic-gate 				}
3477c478bd9Sstevel@tonic-gate 				break;
3487c478bd9Sstevel@tonic-gate 			case 'b':
3497c478bd9Sstevel@tonic-gate 				switch (argv[j][i + 2]) {
3507c478bd9Sstevel@tonic-gate 					case 't':
3517c478bd9Sstevel@tonic-gate 						body = 't';
3527c478bd9Sstevel@tonic-gate 						break;
3537c478bd9Sstevel@tonic-gate 					case 'a':
3547c478bd9Sstevel@tonic-gate 						body = 'a';
3557c478bd9Sstevel@tonic-gate 						break;
3567c478bd9Sstevel@tonic-gate 					case 'n':
3577c478bd9Sstevel@tonic-gate 						body = 'n';
3587c478bd9Sstevel@tonic-gate 						break;
3597c478bd9Sstevel@tonic-gate 					case 'p':
3607c478bd9Sstevel@tonic-gate 						s = argv[j];
3617c478bd9Sstevel@tonic-gate 						q = 3;
3627c478bd9Sstevel@tonic-gate 						r = 0;
3637c478bd9Sstevel@tonic-gate 						while (s[q] != '\0') {
3647c478bd9Sstevel@tonic-gate 							pat[r] = s[q];
3657c478bd9Sstevel@tonic-gate 							r++;
3667c478bd9Sstevel@tonic-gate 							q++;
3677c478bd9Sstevel@tonic-gate 						}
3687c478bd9Sstevel@tonic-gate 						pat[r] = '\0';
3697c478bd9Sstevel@tonic-gate 						body = 'b';
37056849bc6SToomas Soome 						bexpbuf =
37156849bc6SToomas Soome 						    compile(pat, NULL, NULL);
37256849bc6SToomas Soome 						if (regerrno)
37356849bc6SToomas Soome 							regerr(regerrno);
3747c478bd9Sstevel@tonic-gate 						break;
3757c478bd9Sstevel@tonic-gate 					case '\0':
3767c478bd9Sstevel@tonic-gate 						body = 't';
3777c478bd9Sstevel@tonic-gate 						break;
3787c478bd9Sstevel@tonic-gate 					default:
3797c478bd9Sstevel@tonic-gate 						optmsg(argv[j]);
3807c478bd9Sstevel@tonic-gate 				}
3817c478bd9Sstevel@tonic-gate 				break;
3827c478bd9Sstevel@tonic-gate 			case 'f':
3837c478bd9Sstevel@tonic-gate 				switch (argv[j][i + 2]) {
3847c478bd9Sstevel@tonic-gate 					case 'n':
3857c478bd9Sstevel@tonic-gate 						footer = 'n';
3867c478bd9Sstevel@tonic-gate 						break;
3877c478bd9Sstevel@tonic-gate 					case 't':
3887c478bd9Sstevel@tonic-gate 						footer = 't';
3897c478bd9Sstevel@tonic-gate 						break;
3907c478bd9Sstevel@tonic-gate 					case 'a':
3917c478bd9Sstevel@tonic-gate 						footer = 'a';
3927c478bd9Sstevel@tonic-gate 						break;
3937c478bd9Sstevel@tonic-gate 					case 'p':
3947c478bd9Sstevel@tonic-gate 						s = argv[j];
3957c478bd9Sstevel@tonic-gate 						q = 3;
3967c478bd9Sstevel@tonic-gate 						r = 0;
3977c478bd9Sstevel@tonic-gate 						while (s[q] != '\0') {
3987c478bd9Sstevel@tonic-gate 							pat[r] = s[q];
3997c478bd9Sstevel@tonic-gate 							r++;
4007c478bd9Sstevel@tonic-gate 							q++;
4017c478bd9Sstevel@tonic-gate 						}
4027c478bd9Sstevel@tonic-gate 						pat[r] = '\0';
4037c478bd9Sstevel@tonic-gate 						footer = 'f';
40456849bc6SToomas Soome 						fexpbuf =
40556849bc6SToomas Soome 						    compile(pat, NULL, NULL);
40656849bc6SToomas Soome 						if (regerrno)
40756849bc6SToomas Soome 							regerr(regerrno);
4087c478bd9Sstevel@tonic-gate 						break;
4097c478bd9Sstevel@tonic-gate 					case '\0':
4107c478bd9Sstevel@tonic-gate 						footer = 'n';
4117c478bd9Sstevel@tonic-gate 						break;
4127c478bd9Sstevel@tonic-gate 					default:
4137c478bd9Sstevel@tonic-gate 						optmsg(argv[j]);
4147c478bd9Sstevel@tonic-gate 				}
4157c478bd9Sstevel@tonic-gate 				break;
4167c478bd9Sstevel@tonic-gate 			case 'p':
4177c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
4187c478bd9Sstevel@tonic-gate 				cntck = 'y';
4197c478bd9Sstevel@tonic-gate 				else
4207c478bd9Sstevel@tonic-gate 				{
4217c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
4227c478bd9Sstevel@tonic-gate 				}
4237c478bd9Sstevel@tonic-gate 				break;
4247c478bd9Sstevel@tonic-gate 			case 'v':
4257c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
4267c478bd9Sstevel@tonic-gate 				startcnt = 1;
4277c478bd9Sstevel@tonic-gate 				else
4287c478bd9Sstevel@tonic-gate 				startcnt = convert(argv[j]);
4297c478bd9Sstevel@tonic-gate 				break;
4307c478bd9Sstevel@tonic-gate 			case 'i':
4317c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
4327c478bd9Sstevel@tonic-gate 				increment = 1;
4337c478bd9Sstevel@tonic-gate 				else
4347c478bd9Sstevel@tonic-gate 				increment = convert(argv[j]);
4357c478bd9Sstevel@tonic-gate 				break;
4367c478bd9Sstevel@tonic-gate 			case 'w':
4377c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
4387c478bd9Sstevel@tonic-gate 				width = 6;
4397c478bd9Sstevel@tonic-gate 				else
4407c478bd9Sstevel@tonic-gate 				width = convert(argv[j]);
4417c478bd9Sstevel@tonic-gate 				break;
4427c478bd9Sstevel@tonic-gate 			case 'l':
4437c478bd9Sstevel@tonic-gate 				if (argv[j][i+2] == '\0')
4447c478bd9Sstevel@tonic-gate 				blank = 1;
4457c478bd9Sstevel@tonic-gate 				else
4467c478bd9Sstevel@tonic-gate 				blank = convert(argv[j]);
4477c478bd9Sstevel@tonic-gate 				break;
4487c478bd9Sstevel@tonic-gate 			case 'n':
4497c478bd9Sstevel@tonic-gate 				switch (argv[j][i+2]) {
4507c478bd9Sstevel@tonic-gate 					case 'l':
4517c478bd9Sstevel@tonic-gate 						if (argv[j][i+3] == 'n')
4527c478bd9Sstevel@tonic-gate 						format = 'l';
4537c478bd9Sstevel@tonic-gate 						else
4547c478bd9Sstevel@tonic-gate 				{
4557c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
4567c478bd9Sstevel@tonic-gate 				}
4577c478bd9Sstevel@tonic-gate 						break;
4587c478bd9Sstevel@tonic-gate 					case 'r':
4597c478bd9Sstevel@tonic-gate 						if ((argv[j][i+3] == 'n') ||
4607c478bd9Sstevel@tonic-gate 						    (argv[j][i+3] == 'z'))
4617c478bd9Sstevel@tonic-gate 						format = argv[j][i+3];
4627c478bd9Sstevel@tonic-gate 						else
4637c478bd9Sstevel@tonic-gate 				{
4647c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
4657c478bd9Sstevel@tonic-gate 				}
4667c478bd9Sstevel@tonic-gate 						break;
4677c478bd9Sstevel@tonic-gate 					case '\0':
4687c478bd9Sstevel@tonic-gate 						format = 'n';
4697c478bd9Sstevel@tonic-gate 						break;
4707c478bd9Sstevel@tonic-gate 					default:
4717c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
4727c478bd9Sstevel@tonic-gate 					break;
4737c478bd9Sstevel@tonic-gate 				}
4747c478bd9Sstevel@tonic-gate 				break;
4757c478bd9Sstevel@tonic-gate 			case 's':
4767c478bd9Sstevel@tonic-gate 				if (argv[j][i + 2] != '\0') {
4777c478bd9Sstevel@tonic-gate 					s = argv[j];
4787c478bd9Sstevel@tonic-gate 					q = 2;
4797c478bd9Sstevel@tonic-gate 					r = 0;
4807c478bd9Sstevel@tonic-gate 					while (s[q] != '\0') {
4817c478bd9Sstevel@tonic-gate 						sep[r] = s[q];
4827c478bd9Sstevel@tonic-gate 						r++;
4837c478bd9Sstevel@tonic-gate 						q++;
4847c478bd9Sstevel@tonic-gate 					}
4857c478bd9Sstevel@tonic-gate 					sep[r] = '\0';
4867c478bd9Sstevel@tonic-gate 				}
4877c478bd9Sstevel@tonic-gate 				/* else default sep is tab (set above) */
4887c478bd9Sstevel@tonic-gate 				break;
4897c478bd9Sstevel@tonic-gate 			case 'd':
4907c478bd9Sstevel@tonic-gate 				tempchr = argv[j][i+2];
4917c478bd9Sstevel@tonic-gate 				if (tempchr == '\0')break;
4927c478bd9Sstevel@tonic-gate 				delim1 = tempchr;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 				tempchr = argv[j][i+3];
4957c478bd9Sstevel@tonic-gate 				if (tempchr == '\0')break;
4967c478bd9Sstevel@tonic-gate 				delim2 = tempchr;
4977c478bd9Sstevel@tonic-gate 				if (argv[j][i+4] != '\0')optmsg(argv[j]);
4987c478bd9Sstevel@tonic-gate 				break;
4997c478bd9Sstevel@tonic-gate 			case '-':
5007c478bd9Sstevel@tonic-gate 				if (argv[j][i + 2] == '\0') {
5017c478bd9Sstevel@tonic-gate 					option_end = 1;
5027c478bd9Sstevel@tonic-gate 					break;
5037c478bd9Sstevel@tonic-gate 				}
5046da563efSToomas Soome 				/* FALLTHROUGH */
5057c478bd9Sstevel@tonic-gate 			default:
5067c478bd9Sstevel@tonic-gate 				optmsg(argv[j]);
5077c478bd9Sstevel@tonic-gate 			}
5087c478bd9Sstevel@tonic-gate 		} else if ((iptr = fopen(argv[j], "r")) == NULL)  {
5097c478bd9Sstevel@tonic-gate 			/* end of options, filename starting with '-' */
5107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "nl: %s: ", argv[j]);
5117c478bd9Sstevel@tonic-gate 			perror("");
5127c478bd9Sstevel@tonic-gate 			return (1);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	} else if ((iptr = fopen(argv[j], "r")) == NULL)  {
5157c478bd9Sstevel@tonic-gate 		/* filename starting with char other than '-' */
5167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "nl: %s: ", argv[j]);
5177c478bd9Sstevel@tonic-gate 		perror("");
5187c478bd9Sstevel@tonic-gate 		return (1);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate } /* closing brace of for loop */
5217c478bd9Sstevel@tonic-gate /* end Solaris version of argument parsing */
5227c478bd9Sstevel@tonic-gate #endif
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/* ON FIRST PASS ONLY, SET LINE COUNTER (cnt) = startcnt & */
5257c478bd9Sstevel@tonic-gate 	/* SET DEFAULT BODY TYPE TO NUMBER ALL LINES.	*/
5267c478bd9Sstevel@tonic-gate 	if (pass1) {
5277c478bd9Sstevel@tonic-gate 		cnt = startcnt;
5287c478bd9Sstevel@tonic-gate 		type = body;
5297c478bd9Sstevel@tonic-gate 		last = 'b';
5307c478bd9Sstevel@tonic-gate 		pass1 = 0;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate  *		DO WHILE THERE IS INPUT
5357c478bd9Sstevel@tonic-gate  *		CHECK TO SEE IF LINE IS NUMBERED,
5367c478bd9Sstevel@tonic-gate  *		IF SO, CALCULATE NUM, PRINT NUM,
5377c478bd9Sstevel@tonic-gate  *		THEN OUTPUT SEPERATOR CHAR AND LINE
5387c478bd9Sstevel@tonic-gate  */
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	while ((p = fgets(line, sizeof (line), iptr)) != NULL) {
5417c478bd9Sstevel@tonic-gate 	if (p[0] == delim1 && p[1] == delim2) {
5427c478bd9Sstevel@tonic-gate 		if (p[2] == delim1 &&
5437c478bd9Sstevel@tonic-gate 		    p[3] == delim2 &&
5447c478bd9Sstevel@tonic-gate 		    p[4] == delim1 &&
5457c478bd9Sstevel@tonic-gate 		    p[5] == delim2 &&
5467c478bd9Sstevel@tonic-gate 		    p[6] == '\n') {
5477c478bd9Sstevel@tonic-gate 			if (cntck != 'y')
5487c478bd9Sstevel@tonic-gate 				cnt = startcnt;
5497c478bd9Sstevel@tonic-gate 			type = header;
5507c478bd9Sstevel@tonic-gate 			last = 'h';
5517c478bd9Sstevel@tonic-gate 			swtch = 'y';
5527c478bd9Sstevel@tonic-gate 		} else {
5537c478bd9Sstevel@tonic-gate 			if (p[2] == delim1 && p[3] == delim2 && p[4] == '\n') {
5547c478bd9Sstevel@tonic-gate 				if (cntck != 'y' && last != 'h')
5557c478bd9Sstevel@tonic-gate 					cnt = startcnt;
5567c478bd9Sstevel@tonic-gate 				type = body;
5577c478bd9Sstevel@tonic-gate 				last = 'b';
5587c478bd9Sstevel@tonic-gate 				swtch = 'y';
5597c478bd9Sstevel@tonic-gate 			} else {
5607c478bd9Sstevel@tonic-gate 				if (p[0] == delim1 && p[1] == delim2 &&
5617c478bd9Sstevel@tonic-gate 							p[2] == '\n') {
5627c478bd9Sstevel@tonic-gate 					if (cntck != 'y' && last == 'f')
5637c478bd9Sstevel@tonic-gate 						cnt = startcnt;
5647c478bd9Sstevel@tonic-gate 					type = footer;
5657c478bd9Sstevel@tonic-gate 					last = 'f';
5667c478bd9Sstevel@tonic-gate 					swtch = 'y';
5677c478bd9Sstevel@tonic-gate 				}
5687c478bd9Sstevel@tonic-gate 			}
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	if (p[0] != '\n') {
5727c478bd9Sstevel@tonic-gate 		lnt = strlen(p);
5737c478bd9Sstevel@tonic-gate 		if (p[lnt-1] == '\n')
574*6a7605d4SToomas Soome 			p[lnt-1] = '\0';
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (swtch == 'y') {
5787c478bd9Sstevel@tonic-gate 		swtch = 'n';
5797c478bd9Sstevel@tonic-gate 		(void) fprintf(optr, "\n");
5807c478bd9Sstevel@tonic-gate 	} else {
5817c478bd9Sstevel@tonic-gate 		switch (type) {
5827c478bd9Sstevel@tonic-gate 		case 'n':
5837c478bd9Sstevel@tonic-gate 			npad(width, sep);
5847c478bd9Sstevel@tonic-gate 			break;
5857c478bd9Sstevel@tonic-gate 		case 't':
5867c478bd9Sstevel@tonic-gate 			/*
5877c478bd9Sstevel@tonic-gate 			 * XPG4: The wording of Spec 1170 is misleading;
5887c478bd9Sstevel@tonic-gate 			 * the official interpretation is to number all
5897c478bd9Sstevel@tonic-gate 			 * non-empty lines, ie: the Solaris code has not
5907c478bd9Sstevel@tonic-gate 			 * been changed.
5917c478bd9Sstevel@tonic-gate 			 */
5927c478bd9Sstevel@tonic-gate 			if (p[0] != '\n') {
5937c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
5947c478bd9Sstevel@tonic-gate 				cnt += increment;
5957c478bd9Sstevel@tonic-gate 			} else {
5967c478bd9Sstevel@tonic-gate 				npad(width, sep);
5977c478bd9Sstevel@tonic-gate 			}
5987c478bd9Sstevel@tonic-gate 			break;
5997c478bd9Sstevel@tonic-gate 		case 'a':
6007c478bd9Sstevel@tonic-gate 			if (p[0] == '\n') {
6017c478bd9Sstevel@tonic-gate 				blankctr++;
6027c478bd9Sstevel@tonic-gate 				if (blank == blankctr) {
6037c478bd9Sstevel@tonic-gate 					blankctr = 0;
6047c478bd9Sstevel@tonic-gate 					pnum(cnt, sep);
6057c478bd9Sstevel@tonic-gate 					cnt += increment;
6067c478bd9Sstevel@tonic-gate 				} else
6077c478bd9Sstevel@tonic-gate 					npad(width, sep);
6087c478bd9Sstevel@tonic-gate 			} else {
6097c478bd9Sstevel@tonic-gate 				blankctr = 0;
6107c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
6117c478bd9Sstevel@tonic-gate 				cnt += increment;
6127c478bd9Sstevel@tonic-gate 			}
6137c478bd9Sstevel@tonic-gate 			break;
6147c478bd9Sstevel@tonic-gate 		case 'b':
6157c478bd9Sstevel@tonic-gate 			if (step(p, bexpbuf)) {
6167c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
6177c478bd9Sstevel@tonic-gate 				cnt += increment;
6187c478bd9Sstevel@tonic-gate 			} else {
6197c478bd9Sstevel@tonic-gate 				npad(width, sep);
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 			break;
6227c478bd9Sstevel@tonic-gate 		case 'h':
6237c478bd9Sstevel@tonic-gate 			if (step(p, hexpbuf)) {
6247c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
6257c478bd9Sstevel@tonic-gate 				cnt += increment;
6267c478bd9Sstevel@tonic-gate 			} else {
6277c478bd9Sstevel@tonic-gate 				npad(width, sep);
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 			break;
6307c478bd9Sstevel@tonic-gate 		case 'f':
6317c478bd9Sstevel@tonic-gate 			if (step(p, fexpbuf)) {
6327c478bd9Sstevel@tonic-gate 				pnum(cnt, sep);
6337c478bd9Sstevel@tonic-gate 				cnt += increment;
6347c478bd9Sstevel@tonic-gate 			} else {
6357c478bd9Sstevel@tonic-gate 				npad(width, sep);
6367c478bd9Sstevel@tonic-gate 			}
6377c478bd9Sstevel@tonic-gate 			break;
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 		if (p[0] != '\n')
6407c478bd9Sstevel@tonic-gate 			p[lnt-1] = '\n';
6417c478bd9Sstevel@tonic-gate 		(void) fprintf(optr, "%s", line);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	}	/* Closing brace of "else" */
6447c478bd9Sstevel@tonic-gate 	}	/* Closing brace of "while". */
6457c478bd9Sstevel@tonic-gate 	(void) fclose(iptr);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	return (0);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*		REGEXP ERR ROUTINE		*/
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate static void
regerr(int c)65356849bc6SToomas Soome regerr(int c)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
65656849bc6SToomas Soome 	    "nl: invalid regular expression: error code %d\n"), c);
6577c478bd9Sstevel@tonic-gate 	exit(1);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*		CALCULATE NUMBER ROUTINE	*/
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate static void
pnum(int n,char * sep)66356849bc6SToomas Soome pnum(int n, char *sep)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	register int	i;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (format == 'z') {
6687c478bd9Sstevel@tonic-gate 		pad = '0';
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	for (i = 0; i < width; i++)
6717c478bd9Sstevel@tonic-gate 		nbuf[i] = pad;
6727c478bd9Sstevel@tonic-gate 	num(n, width - 1);
6737c478bd9Sstevel@tonic-gate 	if (format == 'l') {
6747c478bd9Sstevel@tonic-gate 		while (nbuf[0] == ' ') {
6757c478bd9Sstevel@tonic-gate 			for (i = 0; i < width; i++)
6767c478bd9Sstevel@tonic-gate 				nbuf[i] = nbuf[i+1];
6777c478bd9Sstevel@tonic-gate 			nbuf[width-1] = ' ';
6787c478bd9Sstevel@tonic-gate 		}
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 	(void) printf("%s%s", nbuf, sep);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate /*		IF NUM > 10, THEN USE THIS CALCULATE ROUTINE		*/
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate static void
num(int v,int p)68656849bc6SToomas Soome num(int v, int p)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	if (v < 10)
6897c478bd9Sstevel@tonic-gate 		nbuf[p] = v + '0';
6907c478bd9Sstevel@tonic-gate 	else {
6917c478bd9Sstevel@tonic-gate 		nbuf[p] = (v % 10) + '0';
6927c478bd9Sstevel@tonic-gate 		if (p > 0)
6937c478bd9Sstevel@tonic-gate 			num(v / 10, p - 1);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate /*		CONVERT ARG STRINGS TO STRING ARRAYS	*/
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate #ifdef XPG4
7007c478bd9Sstevel@tonic-gate static int
convert(int c,char * option_arg)70156849bc6SToomas Soome convert(int c, char *option_arg)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	s = option_arg;
7047c478bd9Sstevel@tonic-gate 	q = r = 0;
7057c478bd9Sstevel@tonic-gate 	while (s[q] != '\0') {
7067c478bd9Sstevel@tonic-gate 		if (s[q] >= '0' && s[q] <= '9') {
7077c478bd9Sstevel@tonic-gate 			s1[r] = s[q];
7087c478bd9Sstevel@tonic-gate 			r++;
7097c478bd9Sstevel@tonic-gate 			q++;
7107c478bd9Sstevel@tonic-gate 		} else
7117c478bd9Sstevel@tonic-gate 			optmsg(c, option_arg);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 	s1[r] = '\0';
7147c478bd9Sstevel@tonic-gate 	k = atoi(s1);
7157c478bd9Sstevel@tonic-gate 	return (k);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate #else
7187c478bd9Sstevel@tonic-gate /* Solaris version */
7197c478bd9Sstevel@tonic-gate static int
convert(char * argv)72056849bc6SToomas Soome convert(char *argv)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	s = (char *)argv;
7237c478bd9Sstevel@tonic-gate 	q = 2;
7247c478bd9Sstevel@tonic-gate 	r = 0;
7257c478bd9Sstevel@tonic-gate 	while (s[q] != '\0') {
72656849bc6SToomas Soome 		if (s[q] >= '0' && s[q] <= '9') {
72756849bc6SToomas Soome 			s1[r] = s[q];
72856849bc6SToomas Soome 			r++;
72956849bc6SToomas Soome 			q++;
73056849bc6SToomas Soome 		} else {
73156849bc6SToomas Soome 			optmsg(argv);
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	s1[r] = '\0';
7357c478bd9Sstevel@tonic-gate 	k = atoi(s1);
7367c478bd9Sstevel@tonic-gate 	return (k);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate #endif
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*		CALCULATE NUM/TEXT SEPRATOR		*/
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate static void
npad(int width,char * sep)74356849bc6SToomas Soome npad(int width, char *sep)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	register int i;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	pad = ' ';
7487c478bd9Sstevel@tonic-gate 	for (i = 0; i < width; i++)
7497c478bd9Sstevel@tonic-gate 		nbuf[i] = pad;
7507c478bd9Sstevel@tonic-gate 	(void) printf("%s", nbuf);
7517c478bd9Sstevel@tonic-gate 
75256849bc6SToomas Soome 	for (i = 0; i < (int)strlen(sep); i++)
7537c478bd9Sstevel@tonic-gate 		(void) printf(" ");
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate #ifdef XPG4
7577c478bd9Sstevel@tonic-gate static void
optmsg(int option,char * option_arg)75856849bc6SToomas Soome optmsg(int option, char *option_arg)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	if (option_arg != (char *)NULL) {
7617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
76256849bc6SToomas Soome 		    "nl: invalid option (-%c %s)\n"), option, option_arg);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	/* else getopt() will print illegal option message */
7657c478bd9Sstevel@tonic-gate 	usage();
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate #else
7687c478bd9Sstevel@tonic-gate /* Solaris version */
7697c478bd9Sstevel@tonic-gate static void
optmsg(char * option)77056849bc6SToomas Soome optmsg(char *option)
7717c478bd9Sstevel@tonic-gate {
77256849bc6SToomas Soome 	(void) fprintf(stderr, gettext("nl: invalid option (%s)\n"), option);
7737c478bd9Sstevel@tonic-gate 	usage();
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate #endif
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate void
usage()7787c478bd9Sstevel@tonic-gate usage()
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(USAGE));
7817c478bd9Sstevel@tonic-gate 	exit(1);
7827c478bd9Sstevel@tonic-gate }
783