xref: /freebsd/usr.bin/xstr/xstr.c (revision 7720a19d5d7456232e8e11909dd72d31ced622d1)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
357720a19dSPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
417720a19dSPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)xstr.c	8.1 (Berkeley) 6/9/93";
437720a19dSPhilippe Charnier #endif
447720a19dSPhilippe Charnier static const char rcsid[] =
457720a19dSPhilippe Charnier 	"$Id$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/types.h>
499b50d902SRodney W. Grimes #include <ctype.h>
507720a19dSPhilippe Charnier #include <err.h>
517720a19dSPhilippe Charnier #include <stdio.h>
527720a19dSPhilippe Charnier #include <stdlib.h>
537720a19dSPhilippe Charnier #include <signal.h>
549b50d902SRodney W. Grimes #include <string.h>
557720a19dSPhilippe Charnier #include <unistd.h>
569b50d902SRodney W. Grimes #include "pathnames.h"
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes /*
599b50d902SRodney W. Grimes  * xstr - extract and hash strings in a C program
609b50d902SRodney W. Grimes  *
619b50d902SRodney W. Grimes  * Bill Joy UCB
629b50d902SRodney W. Grimes  * November, 1978
639b50d902SRodney W. Grimes  */
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes #define	ignore(a)	((void) a)
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes off_t	tellpt;
689b50d902SRodney W. Grimes off_t	hashit();
699b50d902SRodney W. Grimes void	onintr();
709b50d902SRodney W. Grimes char	*savestr();
719b50d902SRodney W. Grimes off_t	yankstr();
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes off_t	mesgpt;
749b50d902SRodney W. Grimes char	*strings =	"strings";
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes int	cflg;
779b50d902SRodney W. Grimes int	vflg;
789b50d902SRodney W. Grimes int	readstd;
799b50d902SRodney W. Grimes 
807720a19dSPhilippe Charnier static void usage __P((void));
817720a19dSPhilippe Charnier int istail __P((char *, char *));
827720a19dSPhilippe Charnier char lastchr __P((char *));
837720a19dSPhilippe Charnier void xsdotc __P((void));
847720a19dSPhilippe Charnier void prstr __P((char *));
857720a19dSPhilippe Charnier void found __P((int, off_t, char *));
867720a19dSPhilippe Charnier void flushsh __P((void));
877720a19dSPhilippe Charnier int xgetc __P((FILE *));
887720a19dSPhilippe Charnier int fgetNUL __P((char *, int, FILE *));
897720a19dSPhilippe Charnier void inithash __P((void));
907720a19dSPhilippe Charnier int octdigit __P((char));
917720a19dSPhilippe Charnier void process __P((char *));
927720a19dSPhilippe Charnier 
937720a19dSPhilippe Charnier int
949b50d902SRodney W. Grimes main(argc, argv)
959b50d902SRodney W. Grimes 	int argc;
969b50d902SRodney W. Grimes 	char *argv[];
979b50d902SRodney W. Grimes {
987720a19dSPhilippe Charnier 	int c;
999b50d902SRodney W. Grimes 
1007720a19dSPhilippe Charnier 	while ((c = getopt(argc, argv, "-cv")) != -1)
1017720a19dSPhilippe Charnier 		switch (c) {
1027720a19dSPhilippe Charnier 		case '-':
1039b50d902SRodney W. Grimes 			readstd++;
1047720a19dSPhilippe Charnier 			break;
1059b50d902SRodney W. Grimes 		case 'c':
1069b50d902SRodney W. Grimes 			cflg++;
1077720a19dSPhilippe Charnier 			break;
1089b50d902SRodney W. Grimes 		case 'v':
1099b50d902SRodney W. Grimes 			vflg++;
1107720a19dSPhilippe Charnier 			break;
1119b50d902SRodney W. Grimes 		default:
1127720a19dSPhilippe Charnier 			usage();
1139b50d902SRodney W. Grimes 		}
1147720a19dSPhilippe Charnier 	argc -= optind;
1157720a19dSPhilippe Charnier 	argv += optind;
1167720a19dSPhilippe Charnier 
1179b50d902SRodney W. Grimes 	if (signal(SIGINT, SIG_IGN) == SIG_DFL)
1189b50d902SRodney W. Grimes 		signal(SIGINT, onintr);
1197720a19dSPhilippe Charnier 	if (cflg || (argc == 0 && !readstd))
1209b50d902SRodney W. Grimes 		inithash();
1219b50d902SRodney W. Grimes 	else
1229b50d902SRodney W. Grimes 		strings = mktemp(strdup(_PATH_TMP));
1239b50d902SRodney W. Grimes 	while (readstd || argc > 0) {
1249b50d902SRodney W. Grimes 		if (freopen("x.c", "w", stdout) == NULL)
1257720a19dSPhilippe Charnier 			err(1, "x.c");
1269b50d902SRodney W. Grimes 		if (!readstd && freopen(argv[0], "r", stdin) == NULL)
1277720a19dSPhilippe Charnier 			err(2, "%s", argv[0]);
1289b50d902SRodney W. Grimes 		process("x.c");
1299b50d902SRodney W. Grimes 		if (readstd == 0)
1309b50d902SRodney W. Grimes 			argc--, argv++;
1319b50d902SRodney W. Grimes 		else
1329b50d902SRodney W. Grimes 			readstd = 0;
1339b50d902SRodney W. Grimes 	};
1349b50d902SRodney W. Grimes 	flushsh();
1359b50d902SRodney W. Grimes 	if (cflg == 0)
1369b50d902SRodney W. Grimes 		xsdotc();
1379b50d902SRodney W. Grimes 	if (strings[0] == '/')
1389b50d902SRodney W. Grimes 		ignore(unlink(strings));
1399b50d902SRodney W. Grimes 	exit(0);
1409b50d902SRodney W. Grimes }
1419b50d902SRodney W. Grimes 
1427720a19dSPhilippe Charnier static void
1437720a19dSPhilippe Charnier usage()
1447720a19dSPhilippe Charnier {
1457720a19dSPhilippe Charnier 	fprintf(stderr, "usage: xstr [-v] [-c] [-] [name ...]\n");
1467720a19dSPhilippe Charnier 	exit (1);
1477720a19dSPhilippe Charnier }
1487720a19dSPhilippe Charnier 
1499b50d902SRodney W. Grimes char linebuf[BUFSIZ];
1509b50d902SRodney W. Grimes 
1517720a19dSPhilippe Charnier void
1529b50d902SRodney W. Grimes process(name)
1539b50d902SRodney W. Grimes 	char *name;
1549b50d902SRodney W. Grimes {
1559b50d902SRodney W. Grimes 	char *cp;
1569b50d902SRodney W. Grimes 	register int c;
1579b50d902SRodney W. Grimes 	register int incomm = 0;
1589b50d902SRodney W. Grimes 	int ret;
1599b50d902SRodney W. Grimes 
1609b50d902SRodney W. Grimes 	printf("extern char\txstr[];\n");
1619b50d902SRodney W. Grimes 	for (;;) {
1629b50d902SRodney W. Grimes 		if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
1637720a19dSPhilippe Charnier 			if (ferror(stdin))
1647720a19dSPhilippe Charnier 				err(3, "%s", name);
1659b50d902SRodney W. Grimes 			break;
1669b50d902SRodney W. Grimes 		}
1679b50d902SRodney W. Grimes 		if (linebuf[0] == '#') {
1689b50d902SRodney W. Grimes 			if (linebuf[1] == ' ' && isdigit(linebuf[2]))
1699b50d902SRodney W. Grimes 				printf("#line%s", &linebuf[1]);
1709b50d902SRodney W. Grimes 			else
1719b50d902SRodney W. Grimes 				printf("%s", linebuf);
1729b50d902SRodney W. Grimes 			continue;
1739b50d902SRodney W. Grimes 		}
1747720a19dSPhilippe Charnier 		for (cp = linebuf; (c = *cp++);) switch (c) {
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes 		case '"':
1779b50d902SRodney W. Grimes 			if (incomm)
1789b50d902SRodney W. Grimes 				goto def;
1799b50d902SRodney W. Grimes 			if ((ret = (int) yankstr(&cp)) == -1)
1809b50d902SRodney W. Grimes 				goto out;
1819b50d902SRodney W. Grimes 			printf("(&xstr[%d])", ret);
1829b50d902SRodney W. Grimes 			break;
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes 		case '\'':
1859b50d902SRodney W. Grimes 			if (incomm)
1869b50d902SRodney W. Grimes 				goto def;
1879b50d902SRodney W. Grimes 			putchar(c);
1889b50d902SRodney W. Grimes 			if (*cp)
1899b50d902SRodney W. Grimes 				putchar(*cp++);
1909b50d902SRodney W. Grimes 			break;
1919b50d902SRodney W. Grimes 
1929b50d902SRodney W. Grimes 		case '/':
1939b50d902SRodney W. Grimes 			if (incomm || *cp != '*')
1949b50d902SRodney W. Grimes 				goto def;
1959b50d902SRodney W. Grimes 			incomm = 1;
1969b50d902SRodney W. Grimes 			cp++;
1979b50d902SRodney W. Grimes 			printf("/*");
1989b50d902SRodney W. Grimes 			continue;
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes 		case '*':
2019b50d902SRodney W. Grimes 			if (incomm && *cp == '/') {
2029b50d902SRodney W. Grimes 				incomm = 0;
2039b50d902SRodney W. Grimes 				cp++;
2049b50d902SRodney W. Grimes 				printf("*/");
2059b50d902SRodney W. Grimes 				continue;
2069b50d902SRodney W. Grimes 			}
2079b50d902SRodney W. Grimes 			goto def;
2089b50d902SRodney W. Grimes 
2099b50d902SRodney W. Grimes def:
2109b50d902SRodney W. Grimes 		default:
2119b50d902SRodney W. Grimes 			putchar(c);
2129b50d902SRodney W. Grimes 			break;
2139b50d902SRodney W. Grimes 		}
2149b50d902SRodney W. Grimes 	}
2159b50d902SRodney W. Grimes out:
2169b50d902SRodney W. Grimes 	if (ferror(stdout))
2177720a19dSPhilippe Charnier 		warn("x.c"), onintr();
2189b50d902SRodney W. Grimes }
2199b50d902SRodney W. Grimes 
2209b50d902SRodney W. Grimes off_t
2219b50d902SRodney W. Grimes yankstr(cpp)
2229b50d902SRodney W. Grimes 	register char **cpp;
2239b50d902SRodney W. Grimes {
2249b50d902SRodney W. Grimes 	register char *cp = *cpp;
2259b50d902SRodney W. Grimes 	register int c, ch;
2269b50d902SRodney W. Grimes 	char dbuf[BUFSIZ];
2279b50d902SRodney W. Grimes 	register char *dp = dbuf;
2289b50d902SRodney W. Grimes 	register char *tp;
2299b50d902SRodney W. Grimes 
2307720a19dSPhilippe Charnier 	while ((c = *cp++)) {
2319b50d902SRodney W. Grimes 		switch (c) {
2329b50d902SRodney W. Grimes 
2339b50d902SRodney W. Grimes 		case '"':
2349b50d902SRodney W. Grimes 			cp++;
2359b50d902SRodney W. Grimes 			goto out;
2369b50d902SRodney W. Grimes 
2379b50d902SRodney W. Grimes 		case '\\':
2389b50d902SRodney W. Grimes 			c = *cp++;
2399b50d902SRodney W. Grimes 			if (c == 0)
2409b50d902SRodney W. Grimes 				break;
2419b50d902SRodney W. Grimes 			if (c == '\n') {
2429b50d902SRodney W. Grimes 				if (fgets(linebuf, sizeof linebuf, stdin)
2439b50d902SRodney W. Grimes 				    == NULL) {
2447720a19dSPhilippe Charnier 					if (ferror(stdin))
2457720a19dSPhilippe Charnier 						err(3, "x.c");
2469b50d902SRodney W. Grimes 					return(-1);
2479b50d902SRodney W. Grimes 				}
2489b50d902SRodney W. Grimes 				cp = linebuf;
2499b50d902SRodney W. Grimes 				continue;
2509b50d902SRodney W. Grimes 			}
2517720a19dSPhilippe Charnier 			for (tp = "b\bt\tr\rn\nf\f\\\\\"\""; (ch = *tp++); tp++)
2529b50d902SRodney W. Grimes 				if (c == ch) {
2539b50d902SRodney W. Grimes 					c = *tp;
2549b50d902SRodney W. Grimes 					goto gotc;
2559b50d902SRodney W. Grimes 				}
2569b50d902SRodney W. Grimes 			if (!octdigit(c)) {
2579b50d902SRodney W. Grimes 				*dp++ = '\\';
2589b50d902SRodney W. Grimes 				break;
2599b50d902SRodney W. Grimes 			}
2609b50d902SRodney W. Grimes 			c -= '0';
2619b50d902SRodney W. Grimes 			if (!octdigit(*cp))
2629b50d902SRodney W. Grimes 				break;
2639b50d902SRodney W. Grimes 			c <<= 3, c += *cp++ - '0';
2649b50d902SRodney W. Grimes 			if (!octdigit(*cp))
2659b50d902SRodney W. Grimes 				break;
2669b50d902SRodney W. Grimes 			c <<= 3, c += *cp++ - '0';
2679b50d902SRodney W. Grimes 			break;
2689b50d902SRodney W. Grimes 		}
2699b50d902SRodney W. Grimes gotc:
2709b50d902SRodney W. Grimes 		*dp++ = c;
2719b50d902SRodney W. Grimes 	}
2729b50d902SRodney W. Grimes out:
2739b50d902SRodney W. Grimes 	*cpp = --cp;
2749b50d902SRodney W. Grimes 	*dp = 0;
2759b50d902SRodney W. Grimes 	return (hashit(dbuf, 1));
2769b50d902SRodney W. Grimes }
2779b50d902SRodney W. Grimes 
2787720a19dSPhilippe Charnier int
2799b50d902SRodney W. Grimes octdigit(c)
2809b50d902SRodney W. Grimes 	char c;
2819b50d902SRodney W. Grimes {
2829b50d902SRodney W. Grimes 	return (isdigit(c) && c != '8' && c != '9');
2839b50d902SRodney W. Grimes }
2849b50d902SRodney W. Grimes 
2857720a19dSPhilippe Charnier void
2869b50d902SRodney W. Grimes inithash()
2879b50d902SRodney W. Grimes {
2889b50d902SRodney W. Grimes 	char buf[BUFSIZ];
2899b50d902SRodney W. Grimes 	register FILE *mesgread = fopen(strings, "r");
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes 	if (mesgread == NULL)
2929b50d902SRodney W. Grimes 		return;
2939b50d902SRodney W. Grimes 	for (;;) {
2949b50d902SRodney W. Grimes 		mesgpt = tellpt;
2957720a19dSPhilippe Charnier 		if (fgetNUL(buf, sizeof buf, mesgread) == 0)
2969b50d902SRodney W. Grimes 			break;
2979b50d902SRodney W. Grimes 		ignore(hashit(buf, 0));
2989b50d902SRodney W. Grimes 	}
2999b50d902SRodney W. Grimes 	ignore(fclose(mesgread));
3009b50d902SRodney W. Grimes }
3019b50d902SRodney W. Grimes 
3027720a19dSPhilippe Charnier int
3039b50d902SRodney W. Grimes fgetNUL(obuf, rmdr, file)
3049b50d902SRodney W. Grimes 	char *obuf;
3059b50d902SRodney W. Grimes 	register int rmdr;
3069b50d902SRodney W. Grimes 	FILE *file;
3079b50d902SRodney W. Grimes {
3089b50d902SRodney W. Grimes 	register c;
3099b50d902SRodney W. Grimes 	register char *buf = obuf;
3109b50d902SRodney W. Grimes 
3119b50d902SRodney W. Grimes 	while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
3129b50d902SRodney W. Grimes 		*buf++ = c;
3139b50d902SRodney W. Grimes 	*buf++ = 0;
3147720a19dSPhilippe Charnier 	return ((feof(file) || ferror(file)) ? 0 : 1);
3159b50d902SRodney W. Grimes }
3169b50d902SRodney W. Grimes 
3177720a19dSPhilippe Charnier int
3189b50d902SRodney W. Grimes xgetc(file)
3199b50d902SRodney W. Grimes 	FILE *file;
3209b50d902SRodney W. Grimes {
3219b50d902SRodney W. Grimes 
3229b50d902SRodney W. Grimes 	tellpt++;
3239b50d902SRodney W. Grimes 	return (getc(file));
3249b50d902SRodney W. Grimes }
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes #define	BUCKETS	128
3279b50d902SRodney W. Grimes 
3289b50d902SRodney W. Grimes struct	hash {
3299b50d902SRodney W. Grimes 	off_t	hpt;
3309b50d902SRodney W. Grimes 	char	*hstr;
3319b50d902SRodney W. Grimes 	struct	hash *hnext;
3329b50d902SRodney W. Grimes 	short	hnew;
3339b50d902SRodney W. Grimes } bucket[BUCKETS];
3349b50d902SRodney W. Grimes 
3359b50d902SRodney W. Grimes off_t
3369b50d902SRodney W. Grimes hashit(str, new)
3379b50d902SRodney W. Grimes 	char *str;
3389b50d902SRodney W. Grimes 	int new;
3399b50d902SRodney W. Grimes {
3409b50d902SRodney W. Grimes 	int i;
3419b50d902SRodney W. Grimes 	register struct hash *hp, *hp0;
3429b50d902SRodney W. Grimes 
3439b50d902SRodney W. Grimes 	hp = hp0 = &bucket[lastchr(str) & 0177];
3449b50d902SRodney W. Grimes 	while (hp->hnext) {
3459b50d902SRodney W. Grimes 		hp = hp->hnext;
3469b50d902SRodney W. Grimes 		i = istail(str, hp->hstr);
3479b50d902SRodney W. Grimes 		if (i >= 0)
3489b50d902SRodney W. Grimes 			return (hp->hpt + i);
3499b50d902SRodney W. Grimes 	}
3507720a19dSPhilippe Charnier 	if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
3517720a19dSPhilippe Charnier 		errx(8, "calloc");
3529b50d902SRodney W. Grimes 	hp->hpt = mesgpt;
3537720a19dSPhilippe Charnier 	if (!(hp->hstr = strdup(str)))
3547720a19dSPhilippe Charnier 		err(1, NULL);
3559b50d902SRodney W. Grimes 	mesgpt += strlen(hp->hstr) + 1;
3569b50d902SRodney W. Grimes 	hp->hnext = hp0->hnext;
3579b50d902SRodney W. Grimes 	hp->hnew = new;
3589b50d902SRodney W. Grimes 	hp0->hnext = hp;
3599b50d902SRodney W. Grimes 	return (hp->hpt);
3609b50d902SRodney W. Grimes }
3619b50d902SRodney W. Grimes 
3627720a19dSPhilippe Charnier void
3639b50d902SRodney W. Grimes flushsh()
3649b50d902SRodney W. Grimes {
3659b50d902SRodney W. Grimes 	register int i;
3669b50d902SRodney W. Grimes 	register struct hash *hp;
3679b50d902SRodney W. Grimes 	register FILE *mesgwrit;
3689b50d902SRodney W. Grimes 	register int old = 0, new = 0;
3699b50d902SRodney W. Grimes 
3709b50d902SRodney W. Grimes 	for (i = 0; i < BUCKETS; i++)
3719b50d902SRodney W. Grimes 		for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
3729b50d902SRodney W. Grimes 			if (hp->hnew)
3739b50d902SRodney W. Grimes 				new++;
3749b50d902SRodney W. Grimes 			else
3759b50d902SRodney W. Grimes 				old++;
3769b50d902SRodney W. Grimes 	if (new == 0 && old != 0)
3779b50d902SRodney W. Grimes 		return;
3789b50d902SRodney W. Grimes 	mesgwrit = fopen(strings, old ? "r+" : "w");
3799b50d902SRodney W. Grimes 	if (mesgwrit == NULL)
3809b50d902SRodney W. Grimes 		perror(strings), exit(4);
3819b50d902SRodney W. Grimes 	for (i = 0; i < BUCKETS; i++)
3829b50d902SRodney W. Grimes 		for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
3839b50d902SRodney W. Grimes 			found(hp->hnew, hp->hpt, hp->hstr);
3849b50d902SRodney W. Grimes 			if (hp->hnew) {
3859b50d902SRodney W. Grimes 				fseek(mesgwrit, hp->hpt, 0);
3869b50d902SRodney W. Grimes 				ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
3879b50d902SRodney W. Grimes 				if (ferror(mesgwrit))
3887720a19dSPhilippe Charnier 					err(4, "%s", strings);
3899b50d902SRodney W. Grimes 			}
3909b50d902SRodney W. Grimes 		}
3919b50d902SRodney W. Grimes 	if (fclose(mesgwrit) == EOF)
3927720a19dSPhilippe Charnier 		err(4, "%s", strings);
3939b50d902SRodney W. Grimes }
3949b50d902SRodney W. Grimes 
3957720a19dSPhilippe Charnier void
3969b50d902SRodney W. Grimes found(new, off, str)
3979b50d902SRodney W. Grimes 	int new;
3989b50d902SRodney W. Grimes 	off_t off;
3999b50d902SRodney W. Grimes 	char *str;
4009b50d902SRodney W. Grimes {
4019b50d902SRodney W. Grimes 	if (vflg == 0)
4029b50d902SRodney W. Grimes 		return;
4039b50d902SRodney W. Grimes 	if (!new)
4049b50d902SRodney W. Grimes 		fprintf(stderr, "found at %d:", (int) off);
4059b50d902SRodney W. Grimes 	else
4069b50d902SRodney W. Grimes 		fprintf(stderr, "new at %d:", (int) off);
4079b50d902SRodney W. Grimes 	prstr(str);
4089b50d902SRodney W. Grimes 	fprintf(stderr, "\n");
4099b50d902SRodney W. Grimes }
4109b50d902SRodney W. Grimes 
4117720a19dSPhilippe Charnier void
4129b50d902SRodney W. Grimes prstr(cp)
4139b50d902SRodney W. Grimes 	register char *cp;
4149b50d902SRodney W. Grimes {
4159b50d902SRodney W. Grimes 	register int c;
4169b50d902SRodney W. Grimes 
4177720a19dSPhilippe Charnier 	while ((c = (*cp++ & 0377)))
4189b50d902SRodney W. Grimes 		if (c < ' ')
4199b50d902SRodney W. Grimes 			fprintf(stderr, "^%c", c + '`');
4209b50d902SRodney W. Grimes 		else if (c == 0177)
4219b50d902SRodney W. Grimes 			fprintf(stderr, "^?");
4229b50d902SRodney W. Grimes 		else if (c > 0200)
4239b50d902SRodney W. Grimes 			fprintf(stderr, "\\%03o", c);
4249b50d902SRodney W. Grimes 		else
4259b50d902SRodney W. Grimes 			fprintf(stderr, "%c", c);
4269b50d902SRodney W. Grimes }
4279b50d902SRodney W. Grimes 
4287720a19dSPhilippe Charnier void
4299b50d902SRodney W. Grimes xsdotc()
4309b50d902SRodney W. Grimes {
4319b50d902SRodney W. Grimes 	register FILE *strf = fopen(strings, "r");
4329b50d902SRodney W. Grimes 	register FILE *xdotcf;
4339b50d902SRodney W. Grimes 
4349b50d902SRodney W. Grimes 	if (strf == NULL)
4357720a19dSPhilippe Charnier 		err(5, "%s", strings);
4369b50d902SRodney W. Grimes 	xdotcf = fopen("xs.c", "w");
4379b50d902SRodney W. Grimes 	if (xdotcf == NULL)
4387720a19dSPhilippe Charnier 		err(6, "xs.c");
4399b50d902SRodney W. Grimes 	fprintf(xdotcf, "char\txstr[] = {\n");
4409b50d902SRodney W. Grimes 	for (;;) {
4419b50d902SRodney W. Grimes 		register int i, c;
4429b50d902SRodney W. Grimes 
4439b50d902SRodney W. Grimes 		for (i = 0; i < 8; i++) {
4449b50d902SRodney W. Grimes 			c = getc(strf);
4459b50d902SRodney W. Grimes 			if (ferror(strf)) {
4467720a19dSPhilippe Charnier 				warn("%s", strings);
4479b50d902SRodney W. Grimes 				onintr();
4489b50d902SRodney W. Grimes 			}
4499b50d902SRodney W. Grimes 			if (feof(strf)) {
4509b50d902SRodney W. Grimes 				fprintf(xdotcf, "\n");
4519b50d902SRodney W. Grimes 				goto out;
4529b50d902SRodney W. Grimes 			}
4539b50d902SRodney W. Grimes 			fprintf(xdotcf, "0x%02x,", c);
4549b50d902SRodney W. Grimes 		}
4559b50d902SRodney W. Grimes 		fprintf(xdotcf, "\n");
4569b50d902SRodney W. Grimes 	}
4579b50d902SRodney W. Grimes out:
4589b50d902SRodney W. Grimes 	fprintf(xdotcf, "};\n");
4599b50d902SRodney W. Grimes 	ignore(fclose(xdotcf));
4609b50d902SRodney W. Grimes 	ignore(fclose(strf));
4619b50d902SRodney W. Grimes }
4629b50d902SRodney W. Grimes 
4637720a19dSPhilippe Charnier char
4649b50d902SRodney W. Grimes lastchr(cp)
4659b50d902SRodney W. Grimes 	register char *cp;
4669b50d902SRodney W. Grimes {
4679b50d902SRodney W. Grimes 
4689b50d902SRodney W. Grimes 	while (cp[0] && cp[1])
4699b50d902SRodney W. Grimes 		cp++;
4709b50d902SRodney W. Grimes 	return (*cp);
4719b50d902SRodney W. Grimes }
4729b50d902SRodney W. Grimes 
4737720a19dSPhilippe Charnier int
4749b50d902SRodney W. Grimes istail(str, of)
4759b50d902SRodney W. Grimes 	register char *str, *of;
4769b50d902SRodney W. Grimes {
4779b50d902SRodney W. Grimes 	register int d = strlen(of) - strlen(str);
4789b50d902SRodney W. Grimes 
4799b50d902SRodney W. Grimes 	if (d < 0 || strcmp(&of[d], str) != 0)
4809b50d902SRodney W. Grimes 		return (-1);
4819b50d902SRodney W. Grimes 	return (d);
4829b50d902SRodney W. Grimes }
4839b50d902SRodney W. Grimes 
4849b50d902SRodney W. Grimes void
4859b50d902SRodney W. Grimes onintr()
4869b50d902SRodney W. Grimes {
4879b50d902SRodney W. Grimes 
4889b50d902SRodney W. Grimes 	ignore(signal(SIGINT, SIG_IGN));
4899b50d902SRodney W. Grimes 	if (strings[0] == '/')
4909b50d902SRodney W. Grimes 		ignore(unlink(strings));
4919b50d902SRodney W. Grimes 	ignore(unlink("x.c"));
4929b50d902SRodney W. Grimes 	ignore(unlink("xs.c"));
4939b50d902SRodney W. Grimes 	exit(7);
4949b50d902SRodney W. Grimes }
495