* Copyright (c) 1980 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)xstr.c 5.7 (Berkeley) 2/26/91";
* xstr - extract and hash strings in a C program
#define ignore(a) ((void) a)
char *strings
= "strings";
while (argc
> 0 && argv
[0][0] == '-') {
register char *cp
= &(*argv
++)[1];
fprintf(stderr
, "usage: xstr [ -v ] [ -c ] [ - ] [ name ... ]\n");
if (signal(SIGINT
, SIG_IGN
) == SIG_DFL
)
if (cflg
|| argc
== 0 && !readstd
)
strings
= mktemp(strdup(_PATH_TMP
));
while (readstd
|| argc
> 0) {
if (freopen("x.c", "w", stdout
) == NULL
)
if (!readstd
&& freopen(argv
[0], "r", stdin
) == NULL
)
perror(argv
[0]), exit(2);
printf("extern char\txstr[];\n");
if (fgets(linebuf
, sizeof linebuf
, stdin
) == NULL
) {
if (linebuf
[1] == ' ' && isdigit(linebuf
[2]))
printf("#line%s", &linebuf
[1]);
for (cp
= linebuf
; c
= *cp
++;) switch (c
) {
if ((ret
= (int) yankstr(&cp
)) == -1)
printf("(&xstr[%d])", ret
);
if (incomm
|| *cp
!= '*')
if (incomm
&& *cp
== '/') {
register char *cp
= *cpp
;
register char *dp
= dbuf
;
if (fgets(linebuf
, sizeof linebuf
, stdin
)
for (tp
= "b\bt\tr\rn\nf\f\\\\\"\""; ch
= *tp
++; tp
++)
c
<<= 3, c
+= *cp
++ - '0';
c
<<= 3, c
+= *cp
++ - '0';
return (hashit(dbuf
, 1));
return (isdigit(c
) && c
!= '8' && c
!= '9');
register FILE *mesgread
= fopen(strings
, "r");
if (fgetNUL(buf
, sizeof buf
, mesgread
) == NULL
)
ignore(fclose(mesgread
));
fgetNUL(obuf
, rmdr
, file
)
register char *buf
= obuf
;
while (--rmdr
> 0 && (c
= xgetc(file
)) != 0 && c
!= EOF
)
return ((feof(file
) || ferror(file
)) ? NULL
: 1);
register struct hash
*hp
, *hp0
;
hp
= hp0
= &bucket
[lastchr(str
) & 0177];
i
= istail(str
, hp
->hstr
);
if ((hp
= (struct hash
*) calloc(1, sizeof (*hp
))) == NULL
) {
if (!(hp
->hstr
= strdup(str
))) {
(void)fprintf(stderr
, "xstr: %s\n", strerror(errno
));
mesgpt
+= strlen(hp
->hstr
) + 1;
register struct hash
*hp
;
register int old
= 0, new = 0;
for (i
= 0; i
< BUCKETS
; i
++)
for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
)
if (new == 0 && old
!= 0)
mesgwrit
= fopen(strings
, old
? "r+" : "w");
perror(strings
), exit(4);
for (i
= 0; i
< BUCKETS
; i
++)
for (hp
= bucket
[i
].hnext
; hp
!= NULL
; hp
= hp
->hnext
) {
found(hp
->hnew
, hp
->hpt
, hp
->hstr
);
fseek(mesgwrit
, hp
->hpt
, 0);
ignore(fwrite(hp
->hstr
, strlen(hp
->hstr
) + 1, 1, mesgwrit
));
perror(strings
), exit(4);
if (fclose(mesgwrit
) == EOF
)
perror(strings
), exit(4);
fprintf(stderr
, "found at %d:", (int) off
);
fprintf(stderr
, "new at %d:", (int) off
);
while (c
= (*cp
++ & 0377))
fprintf(stderr
, "^%c", c
+ '`');
fprintf(stderr
, "\\%03o", c
);
fprintf(stderr
, "%c", c
);
register FILE *strf
= fopen(strings
, "r");
perror(strings
), exit(5);
xdotcf
= fopen("xs.c", "w");
fprintf(xdotcf
, "char\txstr[] = {\n");
for (i
= 0; i
< 8; i
++) {
fprintf(xdotcf
, "0x%02x,", c
);
register int d
= strlen(of
) - strlen(str
);
if (d
< 0 || strcmp(&of
[d
], str
) != 0)
ignore(signal(SIGINT
, SIG_IGN
));