4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1999 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Copyright (c) 1999, by Sun Microsystems, Inc.
32 * All rights reserved.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * University Copyright- Copyright (c) 1982, 1986, 1988
39 * The Regents of the University of California
42 * University Acknowledgment- Portions of this document are derived from
43 * software developed by the University of California, Berkeley, and its
49 #include <sys/types.h>
54 * xstr - extract and hash strings in a C program
57 #define ignore(a) Ignore((char *) a)
69 char *strings
= "strings";
82 while (argc
> 0 && argv
[0][0] == '-') {
83 register char *cp
= &(*argv
++)[1];
102 "usage: xstr [ -v ] [ -c ] [ - ] [ name ... ]\n");
105 if (signal(SIGINT
, SIG_IGN
) == SIG_DFL
)
106 signal(SIGINT
, onintr
);
107 if (cflg
|| argc
== 0 && !readstd
)
110 strings
= savestr("/tmp/xstrXXXXXX");
111 tmpfd
= mkstemp(strings
);
114 (void) free(strings
);
119 while (readstd
|| argc
> 0) {
120 if (freopen("x.c", "w", stdout
) == NULL
)
121 perror("x.c"), (void) cleanup(), exit(1);
122 if (!readstd
&& freopen(argv
[0], "r", stdin
) == NULL
)
123 perror(argv
[0]), (void) cleanup(), exit(2);
141 char linebuf
[BUFSIZ
];
143 register int incomm
= 0;
145 printf("extern\tchar\txstr[];\n");
147 if (fgets(linebuf
, sizeof (linebuf
), stdin
) == NULL
) {
155 if (linebuf
[0] == '#') {
156 if (linebuf
[1] == ' ' && isdigit(linebuf
[2]))
157 printf("#line%s", &linebuf
[1]);
159 printf("%s", linebuf
);
162 for (cp
= linebuf
; c
= *cp
++; ) {
167 printf("(&xstr[%d])",
180 if (incomm
|| *cp
!= '*')
188 if (incomm
&& *cp
== '/') {
203 perror("x.c"), onintr();
210 register char *cp
= *cpp
;
213 register char *dp
= dbuf
;
229 for (tp
= "b\bt\tr\rn\nf\f\\\\\"\""; ch
= *tp
++; tp
++)
241 c
<<= 3, c
+= *cp
++ - '0';
244 c
<<= 3, c
+= *cp
++ - '0';
253 return (hashit(dbuf
, 1));
260 return (isdigit(c
) && c
!= '8' && c
!= '9');
266 register FILE *mesgread
= fopen(strings
, "r");
268 if (mesgread
== NULL
)
272 if (fgetNUL(buf
, sizeof (buf
), mesgread
) == NULL
)
274 ignore(hashit(buf
, 0));
276 ignore(fclose(mesgread
));
279 fgetNUL(obuf
, rmdr
, file
)
285 register char *buf
= obuf
;
287 while (--rmdr
> 0 && (c
= xgetc(file
)) != 0 && c
!= EOF
)
290 return ((feof(file
) || ferror(file
)) ? NULL
: 1);
316 register struct hash
*hp
, *hp0
;
318 hp
= hp0
= &bucket
[lastchr(str
) & 0177];
321 i
= istail(str
, hp
->hstr
);
323 return (hp
->hpt
+ i
);
325 hp
= (struct hash
*) calloc(1, sizeof (*hp
));
327 hp
->hstr
= savestr(str
);
328 mesgpt
+= strlen(hp
->hstr
) + 1;
329 hp
->hnext
= hp0
->hnext
;
338 register struct hash
*hp
;
339 register FILE *mesgwrit
;
340 register int old
= 0, new = 0;
342 for (i
= 0; i
< BUCKETS
; i
++)
343 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
)
348 if (new == 0 && old
!= 0)
350 mesgwrit
= fopen(strings
, old
? "r+" : "w");
351 for (i
= 0; i
< BUCKETS
; i
++)
352 for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
) {
353 found(hp
->hnew
, hp
->hpt
, hp
->hstr
);
355 fseek(mesgwrit
, hp
->hpt
, 0);
356 ignore(fwrite(hp
->hstr
, strlen(hp
->hstr
) + 1,
358 if (ferror(mesgwrit
)) {
365 ignore(fclose(mesgwrit
));
377 fprintf(stderr
, "found at %d:", (int) off
);
379 fprintf(stderr
, "new at %d:", (int) off
);
381 fprintf(stderr
, "\n");
389 while (c
= (*cp
++ & 0377))
391 fprintf(stderr
, "^%c", c
+ '`');
393 fprintf(stderr
, "^?");
395 fprintf(stderr
, "\\%03o", c
);
397 fprintf(stderr
, "%c", c
);
402 register FILE *strf
= fopen(strings
, "r");
403 register FILE *xdotcf
;
406 perror(strings
), exit(5);
407 xdotcf
= fopen("xs.c", "w");
409 perror("xs.c"), exit(6);
410 fprintf(xdotcf
, "char\txstr[] = {\n");
414 for (i
= 0; i
< 8; i
++) {
421 fprintf(xdotcf
, "\n");
424 fprintf(xdotcf
, "0x%02x,", c
);
426 fprintf(xdotcf
, "\n");
429 fprintf(xdotcf
, "};\n");
430 ignore(fclose(xdotcf
));
431 ignore(fclose(strf
));
438 register char *dp
= (char *) calloc(1, strlen(cp
) + 1);
440 return (strcpy(dp
, cp
));
461 while (cp
[0] && cp
[1])
467 register char *str
, *of
;
469 register int d
= strlen(of
) - strlen(str
);
471 if (d
< 0 || strcmp(&of
[d
], str
) != 0)
480 ignorf(signal(SIGINT
, SIG_IGN
));
482 ignore(unlink("x.c"));
483 ignore(unlink("xs.c"));
489 if (strings
[0] == '/') {
490 ignore(unlink(strings
));