version 1.6, 2002/05/27 03:14:22 |
version 1.7, 2002/12/13 14:39:55 |
|
|
*/ |
*/ |
|
|
#ifndef lint |
#ifndef lint |
static char copyright[] = |
static const char copyright[] = |
"@(#) Copyright (c) 1980, 1993\n\ |
"@(#) Copyright (c) 1980, 1993\n\ |
The Regents of the University of California. All rights reserved.\n"; |
The Regents of the University of California. All rights reserved.\n"; |
#endif /* not lint */ |
#endif /* not lint */ |
|
|
#ifndef lint |
#ifndef lint |
#if 0 |
#if 0 |
static char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93"; |
static const char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93"; |
#else |
#else |
static char rcsid[] = "$OpenBSD$"; |
static const char rcsid[] = "$OpenBSD$"; |
#endif |
#endif |
#endif /* not lint */ |
#endif /* not lint */ |
|
|
#include <sys/types.h> |
#include <sys/param.h> |
#include <sys/stat.h> |
#include <sys/stat.h> |
|
|
|
#include <err.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
|
|
|
|
FILE *mesgread, *mesgwrite; |
FILE *mesgread, *mesgwrite; |
char *progname; |
char name[MAXPATHLEN], *np; |
char usagestr[] = "usage: %s [ - ] mesgfile prefix file ...\n"; |
|
char name[100], *np; |
|
|
|
void inithash(void); |
void inithash(void); |
void process(void); |
void process(void); |
int match(char *); |
int match(char *); |
void copystr(void); |
void copystr(void); |
int octdigit(char); |
int octdigit(char); |
unsigned int hashit(char *, char, unsigned); |
unsigned int hashit(char *, char, unsigned int); |
int fgetNUL(char *, int, FILE *); |
int fgetNUL(char *, int, FILE *); |
|
__dead void usage(void); |
|
|
int |
int |
main(argc, argv) |
main(int argc, char **argv) |
int argc; |
|
char *argv[]; |
|
{ |
{ |
|
size_t n; |
char addon = 0; |
char addon = 0; |
|
|
argc--, progname = *argv++; |
argc--, argv++; |
if (argc > 1 && argv[0][0] == '-') |
if (argc > 1 && argv[0][0] == '-') |
addon++, argc--, argv++; |
addon++, argc--, argv++; |
if (argc < 3) |
if (argc < 3) |
fprintf(stderr, usagestr, progname), exit(1); |
usage(); |
mesgwrite = fopen(argv[0], addon ? "a" : "w"); |
mesgwrite = fopen(argv[0], addon ? "a" : "w"); |
if (mesgwrite == NULL) |
if (mesgwrite == NULL) |
perror(argv[0]), exit(1); |
perror(argv[0]), exit(1); |
|
|
perror(argv[0]), exit(1); |
perror(argv[0]), exit(1); |
inithash(); |
inithash(); |
argc--, argv++; |
argc--, argv++; |
strcpy(name, argv[0]); |
if ((n = strlcpy(name, argv[0], sizeof(name))) >= sizeof(name)) |
np = name + strlen(name); |
errx(1, "%s too long", argv[0]); |
|
np = name + n; |
argc--, argv++; |
argc--, argv++; |
do { |
do { |
strcpy(np, argv[0]); |
if (strlcpy(np, argv[0], sizeof(name) - n) >= |
|
sizeof(name) - n) |
|
errx(1, "%s too long", argv[0]); |
if (freopen(name, "w", stdout) == NULL) |
if (freopen(name, "w", stdout) == NULL) |
perror(name), exit(1); |
perror(name), exit(1); |
if (freopen(argv[0], "r", stdin) == NULL) |
if (freopen(argv[0], "r", stdin) == NULL) |
|
|
process(); |
process(); |
argc--, argv++; |
argc--, argv++; |
} while (argc > 0); |
} while (argc > 0); |
return 0; |
exit (0); |
} |
} |
|
|
void |
void |
process() |
process(void) |
{ |
{ |
int c; |
int c; |
|
|
|
|
} |
} |
|
|
int |
int |
match(ocp) |
match(char *ocp) |
char *ocp; |
|
{ |
{ |
char *cp; |
char *cp; |
int c; |
int c; |
|
|
} |
} |
|
|
void |
void |
copystr() |
copystr(void) |
{ |
{ |
int c, ch; |
int c, ch; |
char buf[512]; |
char buf[512]; |
|
|
} |
} |
|
|
int |
int |
octdigit(c) |
octdigit(char c) |
char c; |
|
{ |
{ |
|
|
return (c >= '0' && c <= '7'); |
return (c >= '0' && c <= '7'); |
} |
} |
|
|
void |
void |
inithash() |
inithash(void) |
{ |
{ |
char buf[512]; |
char buf[512]; |
int mesgpt = 0; |
int mesgpt = 0; |
|
|
} *bucket[NBUCKETS]; |
} *bucket[NBUCKETS]; |
|
|
unsigned int |
unsigned int |
hashit(str, really, fakept) |
hashit(char *str, char really, unsigned int fakept) |
char *str; |
|
char really; |
|
unsigned int fakept; |
|
{ |
{ |
int i; |
int i; |
struct hash *hp; |
struct hash *hp; |
|
|
if (hp->hval == hashval) { |
if (hp->hval == hashval) { |
fseek(mesgread, (long) hp->hpt, 0); |
fseek(mesgread, (long) hp->hpt, 0); |
fgetNUL(buf, sizeof buf, mesgread); |
fgetNUL(buf, sizeof buf, mesgread); |
/* |
#ifdef DEBUG |
fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf); |
fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf); |
*/ |
#endif |
if (strcmp(buf, str) == 0) |
if (strcmp(buf, str) == 0) |
break; |
break; |
} |
} |
if (!really || hp == 0) { |
if (!really || hp == 0) { |
hp = (struct hash *) calloc(1, sizeof *hp); |
if ((hp = (struct hash *) calloc(1, sizeof *hp)) == NULL) |
|
err(1, "calloc"); |
hp->hnext = bucket[i]; |
hp->hnext = bucket[i]; |
hp->hval = hashval; |
hp->hval = hashval; |
hp->hpt = really ? ftell(mesgwrite) : fakept; |
hp->hpt = really ? ftell(mesgwrite) : fakept; |
|
|
} |
} |
bucket[i] = hp; |
bucket[i] = hp; |
} |
} |
/* |
#ifdef DEBUG |
fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt); |
fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt); |
*/ |
#endif |
return (hp->hpt); |
return (hp->hpt); |
} |
} |
|
|
int |
int |
fgetNUL(obuf, rmdr, file) |
fgetNUL(char *obuf, int rmdr, FILE *file) |
char *obuf; |
|
int rmdr; |
|
FILE *file; |
|
{ |
{ |
int c; |
int c; |
char *buf = obuf; |
char *buf = obuf; |
|
|
*buf++ = 0; |
*buf++ = 0; |
getc(file); |
getc(file); |
return ((feof(file) || ferror(file)) ? 0 : 1); |
return ((feof(file) || ferror(file)) ? 0 : 1); |
|
} |
|
|
|
__dead void |
|
usage(void) |
|
{ |
|
extern char *__progname; |
|
|
|
fprintf(stderr, "usage: %s [-] mesgfile prefix file ...\n", __progname); |
|
exit(1); |
} |
} |