--- bc-stage/usr/src/cmd/exstr/exstr.c	Fri Jul 22 12:31:54 2005
+++ bc/usr/src/cmd/exstr/exstr.c	Fri Jul 22 12:30:50 2005
@@ -34,13 +34,12 @@
 
 /* external functions */
 
-extern	char 	*malloc();
 extern	int	getopt();
 extern	void	exit();
 extern	int	atoi();
 extern	int	_filbuf();
 extern	char	*optarg;
-extern	int	optind, opterr;	
+extern	int	optind, opterr;
 
 /* static functions */
 
@@ -55,13 +54,13 @@
 /* static variables */
 
 static	int	eflg;		/* find strings in source file(s) */
-static	int	dflg;           /* use replaced string a second argument */
+static	int	dflg;		/* use replaced string a second argument */
 static  int	rflg;		/* replace strings by function calls */
-static  int	errflg;         /* syntax error on command line */
-static  char	*Fname;		/* name of source file */ 	
+static  int	errflg;		/* syntax error on command line */
+static  char	*Fname;		/* name of source file */
 static  int	Lineno;		/* line number in source file */
 static	int	Posno;		/* character position within line */
-static  int	flag;           /* sets when newline is encountered */
+static  int	flag;		/* sets when newline is encountered */
 static  jmp_buf	to_eof;
 
 
@@ -69,10 +68,10 @@
 int	argc;
 char	*argv[];
 {
-	int	ch;		
+	int	ch;
 
 	while ((ch = getopt(argc, argv, "erd")) != -1)
-		switch(ch) {
+		switch (ch) {
 		    case 'e':
 			if (rflg)
 				errflg++;
@@ -94,10 +93,10 @@
 		    default:
 			errflg++;
 		}
-	if (optind ==  argc || errflg) 
+	if (optind ==  argc || errflg)
 		usage();
 	if (!rflg)
-		for(;optind<argc;optind++)
+		for (; optind < argc; optind++)
 			extract(argv[optind]);
 	else  {
 		if (optind+1 != argc)
@@ -104,7 +103,7 @@
 			usage();
 		replace(argv[optind]);
 	}
-	exit(0);
+	return (0);
 }
 
 static	void
@@ -111,8 +110,9 @@
 extract(name)
 char	*name;
 {
-	if (freopen(name,"r",stdin) == NULL) {
-		(void)fprintf(stderr,"exstr: ERROR: couldn't open file '%s'\n",name);
+	if (freopen(name, "r", stdin) == NULL) {
+		(void) fprintf(
+		    stderr, "exstr: ERROR: couldn't open file '%s'\n", name);
 		exit(1);
 	}
 	Fname = name;
@@ -151,7 +151,7 @@
 			ch = getachar();
 			if (ch == '*') {
 				int level = 0;
-				while(level != 2) {
+				while (level != 2) {
 					ch = getachar();
 					if (level == 0 && ch == '*')
 						level++;
@@ -178,7 +178,7 @@
 	saved_posno = Posno;
 	saved_lineno = Lineno;
 	while ((cc = getachar()) != '"') {
-		if(cc == '\\') {
+		if (cc == '\\') {
 			*dp++ = cc;
 			cc = getachar();
 		}
@@ -189,7 +189,7 @@
 		*dp++ = cc;
 	}
 	*dp = 0;
-	prstr(dbuf,saved_lineno,saved_posno);
+	prstr(dbuf, saved_lineno, saved_posno);
 }
 
 static	void
@@ -197,9 +197,10 @@
 register char *cp;
 {
 	if (eflg)
-		(void)fprintf(stdout, "%s:%d:%d:::%s\n", Fname, lineno, posno, cp);
+		(void) fprintf(stdout, "%s:%d:%d:::%s\n", Fname, lineno, posno,
+		    cp);
 	else
-		(void)fprintf(stdout, "%s:%s\n", Fname, cp);
+		(void) fprintf(stdout, "%s:%s\n", Fname, cp);
 
 }
 
@@ -206,8 +207,8 @@
 static	void
 usage()
 {
-	(void)fprintf(stderr, "usage: exstr [-e] files\n");
-	(void)fprintf(stderr, "or   : exstr -r [-d] file\n");
+	(void) fprintf(stderr, "usage: exstr [-e] files\n");
+	(void) fprintf(stderr, "or   : exstr -r [-d] file\n");
 	exit(1);
 }
 
@@ -227,7 +228,7 @@
 		longjmp(to_eof, 1);
 	if (cc == '\n')
 		flag = 1;
-	return(cc);
+	return (cc);
 }
 
 
@@ -235,7 +236,7 @@
 replace(name)
 char	*name;
 {
-	char	linebuf[BUFSIZ];	/* buffer to read lines from source file */
+	char	linebuf[BUFSIZ];
 	char	*cp;
 	int	curlineno;		/* line number in strings file */
 	int	curposno;		/* character position in string file */
@@ -242,13 +243,15 @@
 	int	savelineno = 0;
 	int	curmsgno;		/* message number in strings file */
 	int	wrong_msg;		/* invalid message number */
-	int	cont_str = 0;		/* string continues in the next line */ 	
+	int	cont_str = 0;		/* string continues in the next line */
 	char	*repstr;
 	char	repbuf[BUFSIZ], *repbufp;
 	char	curline[BUFSIZ];
 	char	outbuf[BUFSIZ];
-	char	*inp;			/* keeps track of character position within input file */
-	char	*outp;			/* keeps track of character position within output buffer */
+	/* keeps track of character position within input file */
+	char	*inp;
+	/* keeps track of character position within output buffer */
+	char	*outp;
 	char	*msgfile;
 	FILE	*fi;			/* input source file pointer */
 
@@ -256,24 +259,25 @@
 	outp = outbuf;
 	linebuf[0] = '\0';
 	/* open input C source file */
-	if ((fi = fopen(name,"r")) == (FILE *)NULL) {
-		(void)fprintf(stderr,"exstr: ERROR: couldn't open file '%s'\n",name);
+	if ((fi = fopen(name, "r")) == (FILE *)NULL) {
+		(void) fprintf(stderr,
+		    "exstr: ERROR: couldn't open file '%s'\n", name);
 		exit(1);
 	}
 	Fname = name;
 
-	(void)fprintf(stdout, "extern char *gettxt();\n");
+	(void) fprintf(stdout, "extern char *gettxt();\n");
 
 	/* process file containing the list of strings */
-	while (fgets(repbuf,sizeof repbuf, stdin) != (char *)NULL) {
+	while (fgets(repbuf, sizeof (repbuf), stdin) != (char *)NULL) {
 
 		wrong_msg = 0;
 
 		/* save a copy of the current line */
-		(void)strcpy(curline, repbuf);
+		(void) strcpy(curline, repbuf);
 
 		/* take apart the input string */
-		repbufp = strchr(repbuf,':');
+		repbufp = strchr(repbuf, ':');
 		if (repbufp == (char *)NULL)
 			badformat(curline);
 		*repbufp++ = '\0';
@@ -280,19 +284,20 @@
 		/* verify that string belongs to the input C source file */
 		if (strcmp(repbuf, name) != NULL)
 			continue;
-		repstr = strchr(repbufp,':');
+		repstr = strchr(repbufp, ':');
 		if (repstr == (char *)NULL)
 			badformat(curline);
 		*repstr++ = '\0';
 		curlineno = atoi(repbufp);
 		if (curlineno < savelineno) {
-			(void)fprintf(stderr, "exstr: ERROR: stdin: line out of order\n");
-			(void)fprintf(stderr, "%s", curline);
+			(void) fprintf(stderr,
+			    "exstr: ERROR: stdin: line out of order\n");
+			(void) fprintf(stderr, "%s", curline);
 			exit(1);
 		}
 		savelineno = curlineno;
 		repbufp = repstr;
-		repstr = strchr(repbufp,':');
+		repstr = strchr(repbufp, ':');
 		if (repstr == (char *)NULL)
 			badformat(curline);
 		repstr[strlen(repstr) - 1 ] = '\0';
@@ -299,47 +304,51 @@
 		*repstr++ = '\0';
 		curposno = atoi(repbufp);
 		repbufp = repstr;
-		repstr = strchr(repbufp,':');
+		repstr = strchr(repbufp, ':');
 		if (repstr == (char *)NULL)
 			badformat(curline);
 		*repstr++ = '\0';
 		msgfile = repbufp;
-		if ( strlen(msgfile) > (size_t) 14 || *msgfile == '\0' ){
-			(void)fprintf(stderr, "exstr: ERROR: stdin: invalid message file name '%s'\n", msgfile);
-			(void)fprintf(stderr, "%s", curline);
+		if (strlen(msgfile) > (size_t)14 || *msgfile == '\0') {
+			(void) fprintf(stderr,
+			    "exstr: ERROR: stdin: invalid message file name "
+			    "'%s'\n", msgfile);
+			(void) fprintf(stderr, "%s", curline);
 			exit(1);
 		}
 		repbufp = repstr;
-		repstr = strchr(repbufp,':');
+		repstr = strchr(repbufp, ':');
 		if (repstr == (char *)NULL)
 			badformat(curline);
 		*repstr++ = '\0';
 		cp = repbufp;
-		while(*cp)
-			if(!isdigit(*cp++)) {
+		while (*cp)
+			if (!isdigit(*cp++)) {
 				wrong_msg++;
 				break;
 			}
-		if ( *repbufp == '\0' || wrong_msg ) {
-			(void)fprintf(stderr, "exstr: ERROR: stdin: invalid message number '%s'\n", repbufp);
-			(void)fprintf(stderr, "%s", curline);
+		if (*repbufp == '\0' || wrong_msg) {
+			(void) fprintf(stderr, "exstr: ERROR: stdin: invalid "
+			    "message number '%s'\n", repbufp);
+			(void) fprintf(stderr, "%s", curline);
 			exit(1);
-			}
+		}
 		curmsgno = atoi(repbufp);
 
 		/* move up to this line */
-		while (Lineno != curlineno ) {
+		while (Lineno != curlineno) {
 			if (outp != outbuf) {
-				while(*inp != '\0')
+				while (*inp != '\0')
 					*outp++ = *inp++;
 				*outp = '\0';
-				(void)fputs(outbuf,stdout);
+				(void) fputs(outbuf, stdout);
 			} else if (*linebuf != '\0')
-				(void)fputs(linebuf,stdout);
+				(void) fputs(linebuf, stdout);
 			outp = outbuf;
 			inp = linebuf;
-			if (fgets(linebuf, sizeof linebuf, fi) == (char *)NULL) {
-				(void)fprintf(stderr, "read error\n");
+			if (fgets(linebuf, sizeof (linebuf), fi)
+			    == (char *)NULL) {
+				(void) fprintf(stderr, "read error\n");
 				exit(1);
 			}
 			Lineno++;
@@ -346,7 +355,8 @@
 			Posno = 0;
 		}
 		if (Posno > curposno) {
-			(void)fprintf(stderr, "Bad input record line number %d\n",Lineno);
+			(void) fprintf(stderr,
+			    "Bad input record line number %d\n", Lineno);
 			exit(1);
 		}
 		while (Posno != curposno) {
@@ -354,13 +364,18 @@
 			Posno++;
 		}
 		if (*inp != '"') {
-			fprintf(stderr, "exstr: ERROR: cannot replace string '%s' in line (%d) of file (%s)\n", repstr, Lineno, Fname);
+			(void) fprintf(stderr, "exstr: ERROR: cannot replace "
+			    "string '%s' in line (%d) of file (%s)\n", repstr,
+			    Lineno, Fname);
 			exit(1);
 		}
 		/* check if string continues in next line */
-		while (inp[strlen(inp)-2] == '\\' && inp[strlen(inp)-1] == '\n') { 
-			if (fgets(linebuf,sizeof linebuf, fi) == (char *)NULL) {				
-				fprintf(stderr, "exstr: ERROR: read error in file (%s)\n", Fname);
+		while (inp[strlen(inp)-2] == '\\' && inp[strlen(inp)-1]
+		    == '\n') {
+			if (fgets(linebuf, sizeof (linebuf), fi)
+			    == (char *)NULL) {
+				(void) fprintf(stderr, "exstr: ERROR: read "
+				    "error in file (%s)\n", Fname);
 				exit(1);
 			}
 			cont_str++;
@@ -368,9 +383,12 @@
 		}
 		if (cont_str) {
 			cp = linebuf;
-			while (*cp != '\0' && *cp++ != '"') ;
+			while (*cp != '\0' && *cp++ != '"')
+				;
 			if (*cp == '\0') {
-				fprintf(stderr, "exstr: ERROR: cannot replace string '%s' in line (%d) of file (%s)\n", repstr, Lineno, Fname);
+				(void) fprintf(stderr, "exstr: ERROR: cannot "
+				    "replace string '%s' in line (%d) of file "
+				    "(%s)\n", repstr, Lineno, Fname);
 				exit(1);
 			}
 			inp = cp;
@@ -377,9 +395,12 @@
 			Posno = cp - linebuf;
 		}
 		if (dflg)
-			outp += sprintf(outp,"gettxt(\"%s:%d\", \"%s\")",msgfile, curmsgno, repstr);
+			outp += snprintf(outp, BUFSIZ - (outp - outbuf),
+			    "gettxt(\"%s:%d\", \"%s\")", msgfile, curmsgno,
+			    repstr);
 		else
-			outp += sprintf(outp,"gettxt(\"%s:%d\", \"\")",msgfile,curmsgno);
+			outp += snprintf(outp, BUFSIZ - (outp - outbuf),
+			    "gettxt(\"%s:%d\", \"\")", msgfile, curmsgno);
 		if (!cont_str) {
 			inp += strlen(repstr)+2;
 			Posno += strlen(repstr)+2;
@@ -388,13 +409,13 @@
 			cont_str = 0;
 	}
 	if (outp != outbuf) {
-		while(*inp != '\0')
+		while (*inp != '\0')
 			*outp++ = *inp++;
 		*outp = '\0';
-		(void)fputs(outbuf,stdout);
+		(void) fputs(outbuf, stdout);
 	}
-	while(fgets(linebuf,sizeof linebuf, fi) != (char *)NULL)
-		(void)fputs(linebuf, stdout);
+	while (fgets(linebuf, sizeof (linebuf), fi) != (char *)NULL)
+		(void) fputs(linebuf, stdout);
 
 	(void) fclose(fi);
 }
@@ -403,6 +424,7 @@
 badformat(line)
 char  	*line;
 {
-	(void)fprintf(stderr, "exstr: ERROR: stdin: Badly formatted replacement string\n%s", line);
+	(void) fprintf(stderr, "exstr: ERROR: stdin: Badly formatted "
+	    "replacement string\n%s", line);
 	exit(1);
 }
