xref: /freebsd/usr.bin/xstr/xstr.c (revision c451c0e6bd81905962e6062c9dcdcb58443c5152)
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 
34c451c0e6SMark Murray #include <sys/cdefs.h>
35c451c0e6SMark Murray 
36c451c0e6SMark Murray __FBSDID("$FreeBSD$");
37c451c0e6SMark Murray 
389b50d902SRodney W. Grimes #ifndef lint
397720a19dSPhilippe Charnier static const char copyright[] =
409b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1993\n\
419b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
42c451c0e6SMark Murray #endif
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #ifndef lint
45c451c0e6SMark Murray static const char sccsid[] = "@(#)xstr.c	8.1 (Berkeley) 6/9/93";
467720a19dSPhilippe Charnier #endif
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/types.h>
49c451c0e6SMark Murray 
509b50d902SRodney W. Grimes #include <ctype.h>
517720a19dSPhilippe Charnier #include <err.h>
527720a19dSPhilippe Charnier #include <stdio.h>
537720a19dSPhilippe Charnier #include <stdlib.h>
547720a19dSPhilippe Charnier #include <signal.h>
559b50d902SRodney W. Grimes #include <string.h>
567720a19dSPhilippe Charnier #include <unistd.h>
57c451c0e6SMark Murray 
589b50d902SRodney W. Grimes #include "pathnames.h"
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes /*
619b50d902SRodney W. Grimes  * xstr - extract and hash strings in a C program
629b50d902SRodney W. Grimes  *
639b50d902SRodney W. Grimes  * Bill Joy UCB
649b50d902SRodney W. Grimes  * November, 1978
659b50d902SRodney W. Grimes  */
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes #define	ignore(a)	((void) a)
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes off_t	tellpt;
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes off_t	mesgpt;
72c451c0e6SMark Murray char	cstrings[] =	"strings";
73c451c0e6SMark Murray char	*strings =	cstrings;
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes int	cflg;
769b50d902SRodney W. Grimes int	vflg;
779b50d902SRodney W. Grimes int	readstd;
789b50d902SRodney W. Grimes 
797720a19dSPhilippe Charnier char lastchr __P((char *));
80c451c0e6SMark Murray 
817720a19dSPhilippe Charnier int fgetNUL __P((char *, int, FILE *));
82c451c0e6SMark Murray int istail __P((char *, char *));
837720a19dSPhilippe Charnier int octdigit __P((char));
84c451c0e6SMark Murray int xgetc __P((FILE *));
85c451c0e6SMark Murray 
86c451c0e6SMark Murray off_t hashit __P((char *, int));
87c451c0e6SMark Murray off_t yankstr __P((char **));
88c451c0e6SMark Murray 
89c451c0e6SMark Murray static void usage __P((void));
90c451c0e6SMark Murray 
91c451c0e6SMark Murray void flushsh __P((void));
92c451c0e6SMark Murray void found __P((int, off_t, char *));
93c451c0e6SMark Murray void inithash __P((void));
94c451c0e6SMark Murray void onintr __P((int));
95c451c0e6SMark Murray void process __P((const char *));
96c451c0e6SMark Murray void prstr __P((char *));
97c451c0e6SMark Murray void xsdotc __P((void));
987720a19dSPhilippe Charnier 
997720a19dSPhilippe Charnier int
1009b50d902SRodney W. Grimes main(argc, argv)
1019b50d902SRodney W. Grimes 	int argc;
1029b50d902SRodney W. Grimes 	char *argv[];
1039b50d902SRodney W. Grimes {
1047720a19dSPhilippe Charnier 	int c;
1059b50d902SRodney W. Grimes 
1067720a19dSPhilippe Charnier 	while ((c = getopt(argc, argv, "-cv")) != -1)
1077720a19dSPhilippe Charnier 		switch (c) {
1087720a19dSPhilippe Charnier 		case '-':
1099b50d902SRodney W. Grimes 			readstd++;
1107720a19dSPhilippe Charnier 			break;
1119b50d902SRodney W. Grimes 		case 'c':
1129b50d902SRodney W. Grimes 			cflg++;
1137720a19dSPhilippe Charnier 			break;
1149b50d902SRodney W. Grimes 		case 'v':
1159b50d902SRodney W. Grimes 			vflg++;
1167720a19dSPhilippe Charnier 			break;
1179b50d902SRodney W. Grimes 		default:
1187720a19dSPhilippe Charnier 			usage();
1199b50d902SRodney W. Grimes 		}
1207720a19dSPhilippe Charnier 	argc -= optind;
1217720a19dSPhilippe Charnier 	argv += optind;
1227720a19dSPhilippe Charnier 
1239b50d902SRodney W. Grimes 	if (signal(SIGINT, SIG_IGN) == SIG_DFL)
1249b50d902SRodney W. Grimes 		signal(SIGINT, onintr);
1257720a19dSPhilippe Charnier 	if (cflg || (argc == 0 && !readstd))
1269b50d902SRodney W. Grimes 		inithash();
1279b50d902SRodney W. Grimes 	else
1289b50d902SRodney W. Grimes 		strings = mktemp(strdup(_PATH_TMP));
1299b50d902SRodney W. Grimes 	while (readstd || argc > 0) {
1309b50d902SRodney W. Grimes 		if (freopen("x.c", "w", stdout) == NULL)
1317720a19dSPhilippe Charnier 			err(1, "x.c");
1329b50d902SRodney W. Grimes 		if (!readstd && freopen(argv[0], "r", stdin) == NULL)
1337720a19dSPhilippe Charnier 			err(2, "%s", argv[0]);
1349b50d902SRodney W. Grimes 		process("x.c");
1359b50d902SRodney W. Grimes 		if (readstd == 0)
1369b50d902SRodney W. Grimes 			argc--, argv++;
1379b50d902SRodney W. Grimes 		else
1389b50d902SRodney W. Grimes 			readstd = 0;
1399b50d902SRodney W. Grimes 	};
1409b50d902SRodney W. Grimes 	flushsh();
1419b50d902SRodney W. Grimes 	if (cflg == 0)
1429b50d902SRodney W. Grimes 		xsdotc();
1439b50d902SRodney W. Grimes 	if (strings[0] == '/')
1449b50d902SRodney W. Grimes 		ignore(unlink(strings));
1459b50d902SRodney W. Grimes 	exit(0);
1469b50d902SRodney W. Grimes }
1479b50d902SRodney W. Grimes 
1487720a19dSPhilippe Charnier static void
1497720a19dSPhilippe Charnier usage()
1507720a19dSPhilippe Charnier {
1517720a19dSPhilippe Charnier 	fprintf(stderr, "usage: xstr [-v] [-c] [-] [name ...]\n");
1527720a19dSPhilippe Charnier 	exit (1);
1537720a19dSPhilippe Charnier }
1547720a19dSPhilippe Charnier 
1559b50d902SRodney W. Grimes char linebuf[BUFSIZ];
1569b50d902SRodney W. Grimes 
1577720a19dSPhilippe Charnier void
1589b50d902SRodney W. Grimes process(name)
159c451c0e6SMark Murray 	const char *name;
1609b50d902SRodney W. Grimes {
1619b50d902SRodney W. Grimes 	char *cp;
162c451c0e6SMark Murray 	int c;
163c451c0e6SMark Murray 	int incomm = 0;
1649b50d902SRodney W. Grimes 	int ret;
1659b50d902SRodney W. Grimes 
1669b50d902SRodney W. Grimes 	printf("extern char\txstr[];\n");
1679b50d902SRodney W. Grimes 	for (;;) {
1689b50d902SRodney W. Grimes 		if (fgets(linebuf, sizeof linebuf, stdin) == NULL) {
1697720a19dSPhilippe Charnier 			if (ferror(stdin))
1707720a19dSPhilippe Charnier 				err(3, "%s", name);
1719b50d902SRodney W. Grimes 			break;
1729b50d902SRodney W. Grimes 		}
1739b50d902SRodney W. Grimes 		if (linebuf[0] == '#') {
1749b50d902SRodney W. Grimes 			if (linebuf[1] == ' ' && isdigit(linebuf[2]))
1759b50d902SRodney W. Grimes 				printf("#line%s", &linebuf[1]);
1769b50d902SRodney W. Grimes 			else
1779b50d902SRodney W. Grimes 				printf("%s", linebuf);
1789b50d902SRodney W. Grimes 			continue;
1799b50d902SRodney W. Grimes 		}
1807720a19dSPhilippe Charnier 		for (cp = linebuf; (c = *cp++);) switch (c) {
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes 		case '"':
1839b50d902SRodney W. Grimes 			if (incomm)
1849b50d902SRodney W. Grimes 				goto def;
1859b50d902SRodney W. Grimes 			if ((ret = (int) yankstr(&cp)) == -1)
1869b50d902SRodney W. Grimes 				goto out;
1879b50d902SRodney W. Grimes 			printf("(&xstr[%d])", ret);
1889b50d902SRodney W. Grimes 			break;
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes 		case '\'':
1919b50d902SRodney W. Grimes 			if (incomm)
1929b50d902SRodney W. Grimes 				goto def;
1939b50d902SRodney W. Grimes 			putchar(c);
1949b50d902SRodney W. Grimes 			if (*cp)
1959b50d902SRodney W. Grimes 				putchar(*cp++);
1969b50d902SRodney W. Grimes 			break;
1979b50d902SRodney W. Grimes 
1989b50d902SRodney W. Grimes 		case '/':
1999b50d902SRodney W. Grimes 			if (incomm || *cp != '*')
2009b50d902SRodney W. Grimes 				goto def;
2019b50d902SRodney W. Grimes 			incomm = 1;
2029b50d902SRodney W. Grimes 			cp++;
2039b50d902SRodney W. Grimes 			printf("/*");
2049b50d902SRodney W. Grimes 			continue;
2059b50d902SRodney W. Grimes 
2069b50d902SRodney W. Grimes 		case '*':
2079b50d902SRodney W. Grimes 			if (incomm && *cp == '/') {
2089b50d902SRodney W. Grimes 				incomm = 0;
2099b50d902SRodney W. Grimes 				cp++;
2109b50d902SRodney W. Grimes 				printf("*/");
2119b50d902SRodney W. Grimes 				continue;
2129b50d902SRodney W. Grimes 			}
2139b50d902SRodney W. Grimes 			goto def;
2149b50d902SRodney W. Grimes 
2159b50d902SRodney W. Grimes def:
2169b50d902SRodney W. Grimes 		default:
2179b50d902SRodney W. Grimes 			putchar(c);
2189b50d902SRodney W. Grimes 			break;
2199b50d902SRodney W. Grimes 		}
2209b50d902SRodney W. Grimes 	}
2219b50d902SRodney W. Grimes out:
2229b50d902SRodney W. Grimes 	if (ferror(stdout))
223c451c0e6SMark Murray 		warn("x.c"), onintr(0);
2249b50d902SRodney W. Grimes }
2259b50d902SRodney W. Grimes 
2269b50d902SRodney W. Grimes off_t
2279b50d902SRodney W. Grimes yankstr(cpp)
228c451c0e6SMark Murray 	char **cpp;
2299b50d902SRodney W. Grimes {
230c451c0e6SMark Murray 	char *cp = *cpp;
231c451c0e6SMark Murray 	int c, ch;
2329b50d902SRodney W. Grimes 	char dbuf[BUFSIZ];
233c451c0e6SMark Murray 	char *dp = dbuf;
234c451c0e6SMark Murray 	char *tp;
235c451c0e6SMark Murray 	static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\"";
2369b50d902SRodney W. Grimes 
2377720a19dSPhilippe Charnier 	while ((c = *cp++)) {
2389b50d902SRodney W. Grimes 		switch (c) {
2399b50d902SRodney W. Grimes 
2409b50d902SRodney W. Grimes 		case '"':
2419b50d902SRodney W. Grimes 			cp++;
2429b50d902SRodney W. Grimes 			goto out;
2439b50d902SRodney W. Grimes 
2449b50d902SRodney W. Grimes 		case '\\':
2459b50d902SRodney W. Grimes 			c = *cp++;
2469b50d902SRodney W. Grimes 			if (c == 0)
2479b50d902SRodney W. Grimes 				break;
2489b50d902SRodney W. Grimes 			if (c == '\n') {
2499b50d902SRodney W. Grimes 				if (fgets(linebuf, sizeof linebuf, stdin)
2509b50d902SRodney W. Grimes 				    == NULL) {
2517720a19dSPhilippe Charnier 					if (ferror(stdin))
2527720a19dSPhilippe Charnier 						err(3, "x.c");
2539b50d902SRodney W. Grimes 					return(-1);
2549b50d902SRodney W. Grimes 				}
2559b50d902SRodney W. Grimes 				cp = linebuf;
2569b50d902SRodney W. Grimes 				continue;
2579b50d902SRodney W. Grimes 			}
258c451c0e6SMark Murray 			for (tp = tmp; (ch = *tp++); tp++)
2599b50d902SRodney W. Grimes 				if (c == ch) {
2609b50d902SRodney W. Grimes 					c = *tp;
2619b50d902SRodney W. Grimes 					goto gotc;
2629b50d902SRodney W. Grimes 				}
2639b50d902SRodney W. Grimes 			if (!octdigit(c)) {
2649b50d902SRodney W. Grimes 				*dp++ = '\\';
2659b50d902SRodney W. Grimes 				break;
2669b50d902SRodney W. Grimes 			}
2679b50d902SRodney W. Grimes 			c -= '0';
2689b50d902SRodney W. Grimes 			if (!octdigit(*cp))
2699b50d902SRodney W. Grimes 				break;
2709b50d902SRodney W. Grimes 			c <<= 3, c += *cp++ - '0';
2719b50d902SRodney W. Grimes 			if (!octdigit(*cp))
2729b50d902SRodney W. Grimes 				break;
2739b50d902SRodney W. Grimes 			c <<= 3, c += *cp++ - '0';
2749b50d902SRodney W. Grimes 			break;
2759b50d902SRodney W. Grimes 		}
2769b50d902SRodney W. Grimes gotc:
2779b50d902SRodney W. Grimes 		*dp++ = c;
2789b50d902SRodney W. Grimes 	}
2799b50d902SRodney W. Grimes out:
2809b50d902SRodney W. Grimes 	*cpp = --cp;
2819b50d902SRodney W. Grimes 	*dp = 0;
2829b50d902SRodney W. Grimes 	return (hashit(dbuf, 1));
2839b50d902SRodney W. Grimes }
2849b50d902SRodney W. Grimes 
2857720a19dSPhilippe Charnier int
2869b50d902SRodney W. Grimes octdigit(c)
2879b50d902SRodney W. Grimes 	char c;
2889b50d902SRodney W. Grimes {
2899b50d902SRodney W. Grimes 	return (isdigit(c) && c != '8' && c != '9');
2909b50d902SRodney W. Grimes }
2919b50d902SRodney W. Grimes 
2927720a19dSPhilippe Charnier void
2939b50d902SRodney W. Grimes inithash()
2949b50d902SRodney W. Grimes {
2959b50d902SRodney W. Grimes 	char buf[BUFSIZ];
296c451c0e6SMark Murray 	FILE *mesgread = fopen(strings, "r");
2979b50d902SRodney W. Grimes 
2989b50d902SRodney W. Grimes 	if (mesgread == NULL)
2999b50d902SRodney W. Grimes 		return;
3009b50d902SRodney W. Grimes 	for (;;) {
3019b50d902SRodney W. Grimes 		mesgpt = tellpt;
3027720a19dSPhilippe Charnier 		if (fgetNUL(buf, sizeof buf, mesgread) == 0)
3039b50d902SRodney W. Grimes 			break;
3049b50d902SRodney W. Grimes 		ignore(hashit(buf, 0));
3059b50d902SRodney W. Grimes 	}
3069b50d902SRodney W. Grimes 	ignore(fclose(mesgread));
3079b50d902SRodney W. Grimes }
3089b50d902SRodney W. Grimes 
3097720a19dSPhilippe Charnier int
3109b50d902SRodney W. Grimes fgetNUL(obuf, rmdr, file)
3119b50d902SRodney W. Grimes 	char *obuf;
312c451c0e6SMark Murray 	int rmdr;
3139b50d902SRodney W. Grimes 	FILE *file;
3149b50d902SRodney W. Grimes {
315c451c0e6SMark Murray 	int c;
316c451c0e6SMark Murray 	char *buf = obuf;
3179b50d902SRodney W. Grimes 
3189b50d902SRodney W. Grimes 	while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF)
3199b50d902SRodney W. Grimes 		*buf++ = c;
3209b50d902SRodney W. Grimes 	*buf++ = 0;
3217720a19dSPhilippe Charnier 	return ((feof(file) || ferror(file)) ? 0 : 1);
3229b50d902SRodney W. Grimes }
3239b50d902SRodney W. Grimes 
3247720a19dSPhilippe Charnier int
3259b50d902SRodney W. Grimes xgetc(file)
3269b50d902SRodney W. Grimes 	FILE *file;
3279b50d902SRodney W. Grimes {
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes 	tellpt++;
3309b50d902SRodney W. Grimes 	return (getc(file));
3319b50d902SRodney W. Grimes }
3329b50d902SRodney W. Grimes 
3339b50d902SRodney W. Grimes #define	BUCKETS	128
3349b50d902SRodney W. Grimes 
3359b50d902SRodney W. Grimes struct	hash {
3369b50d902SRodney W. Grimes 	off_t	hpt;
3379b50d902SRodney W. Grimes 	char	*hstr;
3389b50d902SRodney W. Grimes 	struct	hash *hnext;
3399b50d902SRodney W. Grimes 	short	hnew;
3409b50d902SRodney W. Grimes } bucket[BUCKETS];
3419b50d902SRodney W. Grimes 
3429b50d902SRodney W. Grimes off_t
3439b50d902SRodney W. Grimes hashit(str, new)
3449b50d902SRodney W. Grimes 	char *str;
3459b50d902SRodney W. Grimes 	int new;
3469b50d902SRodney W. Grimes {
3479b50d902SRodney W. Grimes 	int i;
348c451c0e6SMark Murray 	struct hash *hp, *hp0;
3499b50d902SRodney W. Grimes 
3509b50d902SRodney W. Grimes 	hp = hp0 = &bucket[lastchr(str) & 0177];
3519b50d902SRodney W. Grimes 	while (hp->hnext) {
3529b50d902SRodney W. Grimes 		hp = hp->hnext;
3539b50d902SRodney W. Grimes 		i = istail(str, hp->hstr);
3549b50d902SRodney W. Grimes 		if (i >= 0)
3559b50d902SRodney W. Grimes 			return (hp->hpt + i);
3569b50d902SRodney W. Grimes 	}
3577720a19dSPhilippe Charnier 	if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL)
3587720a19dSPhilippe Charnier 		errx(8, "calloc");
3599b50d902SRodney W. Grimes 	hp->hpt = mesgpt;
3607720a19dSPhilippe Charnier 	if (!(hp->hstr = strdup(str)))
3617720a19dSPhilippe Charnier 		err(1, NULL);
3629b50d902SRodney W. Grimes 	mesgpt += strlen(hp->hstr) + 1;
3639b50d902SRodney W. Grimes 	hp->hnext = hp0->hnext;
3649b50d902SRodney W. Grimes 	hp->hnew = new;
3659b50d902SRodney W. Grimes 	hp0->hnext = hp;
3669b50d902SRodney W. Grimes 	return (hp->hpt);
3679b50d902SRodney W. Grimes }
3689b50d902SRodney W. Grimes 
3697720a19dSPhilippe Charnier void
3709b50d902SRodney W. Grimes flushsh()
3719b50d902SRodney W. Grimes {
372c451c0e6SMark Murray 	int i;
373c451c0e6SMark Murray 	struct hash *hp;
374c451c0e6SMark Murray 	FILE *mesgwrit;
375c451c0e6SMark Murray 	int old = 0, new = 0;
3769b50d902SRodney W. Grimes 
3779b50d902SRodney W. Grimes 	for (i = 0; i < BUCKETS; i++)
3789b50d902SRodney W. Grimes 		for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext)
3799b50d902SRodney W. Grimes 			if (hp->hnew)
3809b50d902SRodney W. Grimes 				new++;
3819b50d902SRodney W. Grimes 			else
3829b50d902SRodney W. Grimes 				old++;
3839b50d902SRodney W. Grimes 	if (new == 0 && old != 0)
3849b50d902SRodney W. Grimes 		return;
3859b50d902SRodney W. Grimes 	mesgwrit = fopen(strings, old ? "r+" : "w");
3869b50d902SRodney W. Grimes 	if (mesgwrit == NULL)
3879b50d902SRodney W. Grimes 		perror(strings), exit(4);
3889b50d902SRodney W. Grimes 	for (i = 0; i < BUCKETS; i++)
3899b50d902SRodney W. Grimes 		for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) {
3909b50d902SRodney W. Grimes 			found(hp->hnew, hp->hpt, hp->hstr);
3919b50d902SRodney W. Grimes 			if (hp->hnew) {
3929b50d902SRodney W. Grimes 				fseek(mesgwrit, hp->hpt, 0);
3939b50d902SRodney W. Grimes 				ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit));
3949b50d902SRodney W. Grimes 				if (ferror(mesgwrit))
3957720a19dSPhilippe Charnier 					err(4, "%s", strings);
3969b50d902SRodney W. Grimes 			}
3979b50d902SRodney W. Grimes 		}
3989b50d902SRodney W. Grimes 	if (fclose(mesgwrit) == EOF)
3997720a19dSPhilippe Charnier 		err(4, "%s", strings);
4009b50d902SRodney W. Grimes }
4019b50d902SRodney W. Grimes 
4027720a19dSPhilippe Charnier void
4039b50d902SRodney W. Grimes found(new, off, str)
4049b50d902SRodney W. Grimes 	int new;
4059b50d902SRodney W. Grimes 	off_t off;
4069b50d902SRodney W. Grimes 	char *str;
4079b50d902SRodney W. Grimes {
4089b50d902SRodney W. Grimes 	if (vflg == 0)
4099b50d902SRodney W. Grimes 		return;
4109b50d902SRodney W. Grimes 	if (!new)
4119b50d902SRodney W. Grimes 		fprintf(stderr, "found at %d:", (int) off);
4129b50d902SRodney W. Grimes 	else
4139b50d902SRodney W. Grimes 		fprintf(stderr, "new at %d:", (int) off);
4149b50d902SRodney W. Grimes 	prstr(str);
4159b50d902SRodney W. Grimes 	fprintf(stderr, "\n");
4169b50d902SRodney W. Grimes }
4179b50d902SRodney W. Grimes 
4187720a19dSPhilippe Charnier void
4199b50d902SRodney W. Grimes prstr(cp)
420c451c0e6SMark Murray 	char *cp;
4219b50d902SRodney W. Grimes {
422c451c0e6SMark Murray 	int c;
4239b50d902SRodney W. Grimes 
4247720a19dSPhilippe Charnier 	while ((c = (*cp++ & 0377)))
4259b50d902SRodney W. Grimes 		if (c < ' ')
4269b50d902SRodney W. Grimes 			fprintf(stderr, "^%c", c + '`');
4279b50d902SRodney W. Grimes 		else if (c == 0177)
4289b50d902SRodney W. Grimes 			fprintf(stderr, "^?");
4299b50d902SRodney W. Grimes 		else if (c > 0200)
4309b50d902SRodney W. Grimes 			fprintf(stderr, "\\%03o", c);
4319b50d902SRodney W. Grimes 		else
4329b50d902SRodney W. Grimes 			fprintf(stderr, "%c", c);
4339b50d902SRodney W. Grimes }
4349b50d902SRodney W. Grimes 
4357720a19dSPhilippe Charnier void
4369b50d902SRodney W. Grimes xsdotc()
4379b50d902SRodney W. Grimes {
438c451c0e6SMark Murray 	FILE *strf = fopen(strings, "r");
439c451c0e6SMark Murray 	FILE *xdotcf;
4409b50d902SRodney W. Grimes 
4419b50d902SRodney W. Grimes 	if (strf == NULL)
4427720a19dSPhilippe Charnier 		err(5, "%s", strings);
4439b50d902SRodney W. Grimes 	xdotcf = fopen("xs.c", "w");
4449b50d902SRodney W. Grimes 	if (xdotcf == NULL)
4457720a19dSPhilippe Charnier 		err(6, "xs.c");
4469b50d902SRodney W. Grimes 	fprintf(xdotcf, "char\txstr[] = {\n");
4479b50d902SRodney W. Grimes 	for (;;) {
448c451c0e6SMark Murray 		int i, c;
4499b50d902SRodney W. Grimes 
4509b50d902SRodney W. Grimes 		for (i = 0; i < 8; i++) {
4519b50d902SRodney W. Grimes 			c = getc(strf);
4529b50d902SRodney W. Grimes 			if (ferror(strf)) {
4537720a19dSPhilippe Charnier 				warn("%s", strings);
454c451c0e6SMark Murray 				onintr(0);
4559b50d902SRodney W. Grimes 			}
4569b50d902SRodney W. Grimes 			if (feof(strf)) {
4579b50d902SRodney W. Grimes 				fprintf(xdotcf, "\n");
4589b50d902SRodney W. Grimes 				goto out;
4599b50d902SRodney W. Grimes 			}
4609b50d902SRodney W. Grimes 			fprintf(xdotcf, "0x%02x,", c);
4619b50d902SRodney W. Grimes 		}
4629b50d902SRodney W. Grimes 		fprintf(xdotcf, "\n");
4639b50d902SRodney W. Grimes 	}
4649b50d902SRodney W. Grimes out:
4659b50d902SRodney W. Grimes 	fprintf(xdotcf, "};\n");
4669b50d902SRodney W. Grimes 	ignore(fclose(xdotcf));
4679b50d902SRodney W. Grimes 	ignore(fclose(strf));
4689b50d902SRodney W. Grimes }
4699b50d902SRodney W. Grimes 
4707720a19dSPhilippe Charnier char
4719b50d902SRodney W. Grimes lastchr(cp)
472c451c0e6SMark Murray 	char *cp;
4739b50d902SRodney W. Grimes {
4749b50d902SRodney W. Grimes 
4759b50d902SRodney W. Grimes 	while (cp[0] && cp[1])
4769b50d902SRodney W. Grimes 		cp++;
4779b50d902SRodney W. Grimes 	return (*cp);
4789b50d902SRodney W. Grimes }
4799b50d902SRodney W. Grimes 
4807720a19dSPhilippe Charnier int
4819b50d902SRodney W. Grimes istail(str, of)
482c451c0e6SMark Murray 	char *str, *of;
4839b50d902SRodney W. Grimes {
484c451c0e6SMark Murray 	int d = strlen(of) - strlen(str);
4859b50d902SRodney W. Grimes 
4869b50d902SRodney W. Grimes 	if (d < 0 || strcmp(&of[d], str) != 0)
4879b50d902SRodney W. Grimes 		return (-1);
4889b50d902SRodney W. Grimes 	return (d);
4899b50d902SRodney W. Grimes }
4909b50d902SRodney W. Grimes 
4919b50d902SRodney W. Grimes void
492c451c0e6SMark Murray onintr(dummy)
493c451c0e6SMark Murray 	int dummy __unused;
4949b50d902SRodney W. Grimes {
4959b50d902SRodney W. Grimes 
4969b50d902SRodney W. Grimes 	ignore(signal(SIGINT, SIG_IGN));
4979b50d902SRodney W. Grimes 	if (strings[0] == '/')
4989b50d902SRodney W. Grimes 		ignore(unlink(strings));
4999b50d902SRodney W. Grimes 	ignore(unlink("x.c"));
5009b50d902SRodney W. Grimes 	ignore(unlink("xs.c"));
5019b50d902SRodney W. Grimes 	exit(7);
5029b50d902SRodney W. Grimes }
503