makedbm.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 259 行
C
259 行
#ifndef lintstatic char *sccsid = "@(#)makedbm.c 4.1 ULTRIX 7/2/90";#endif lint/**************************************************************** * * * Licensed to Digital Equipment Corporation, Maynard, MA * * Copyright 1985 Sun Microsystems, Inc. * * All rights reserved. * * * ****************************************************************/#include <dbm.h>#undef NULL#include <stdio.h>#include <sys/file.h>#include <sys/param.h>#include <sys/stat.h>#define LASCII_DATE 10#define MAXLINE 4096 /* max length of input line */#define MAXHOST 255 /* max length of host name */static char *get_date();static char *any();main(argc, argv) char **argv;{ FILE *outfp, *infp; datum key, content, tmp; char buf[MAXLINE]; char pagbuf[MAXPATHLEN]; char tmppagbuf[MAXPATHLEN]; char dirbuf[MAXPATHLEN]; char tmpdirbuf[MAXPATHLEN]; char *p; char *infile, *outfile; char *infilename, *outfilename, *mastername, *domainname; char local_host[MAXHOST]; int cnt; infile = outfile = NULL; /* where to get files */ /* name to imbed in database */ infilename = outfilename = mastername = domainname = NULL; argv++; argc--; while (argc > 0) { if (argv[0][0] == '-' && argv[0][1]) { switch(argv[0][1]) { case 'i': infilename = argv[1]; break; case 'o': outfilename = argv[1]; break; case 'm': mastername = argv[1]; break; case 'd': domainname = argv[1]; break; case 'u': unmake(argv[1]); return; default: usage(); } argv++; argc--; } else if (infile == NULL) infile = argv[0]; else if (outfile == NULL) outfile = argv[0]; else usage(); argv++; argc--; } if (infile == NULL || outfile == NULL) usage(); if (strcmp(infile, "-") != 0) infp = fopen(infile, "r"); else infp = stdin; if (infp == NULL) { fprintf(stderr, "makedbm: can't open %s\n", infile); exit(1); } strcpy(tmppagbuf, outfile); strcat(tmppagbuf, ".tmp"); strcpy(tmpdirbuf, tmppagbuf); strcat(tmpdirbuf, ".dir"); strcat(tmppagbuf, ".pag"); if ((outfp = fopen(tmpdirbuf, "w")) == NULL) { fprintf(stderr, "makedbm: can't create %s\n", tmpdirbuf); exit(1); } if ((outfp = fopen(tmppagbuf, "w")) == NULL) { fprintf(stderr, "makedbm: can't create %s\n", tmppagbuf); exit(1); } strcpy(dirbuf, outfile); strcat(dirbuf, ".tmp"); if (dbminit(dirbuf) != 0) { fprintf(stderr, "makedbm: can't init %s\n", dirbuf); exit(1); } strcpy(dirbuf, outfile); strcpy(pagbuf, outfile); strcat(dirbuf, ".dir"); strcat(pagbuf, ".pag"); while (fgets(buf, sizeof(buf), infp) != NULL) { p = buf; cnt = strlen(buf) - 1; /* erase trailing newline */ while (p[cnt-1] == '\\') { p+=cnt-1; if (fgets(p, sizeof(buf)-(p-buf), infp) == NULL) goto breakout; cnt = strlen(p) - 1; } p = any(buf, " \t\n"); if(p != buf) { /* check for null keys */ key.dptr = buf; key.dsize = p - buf; while (1) { if (p == NULL || *p == NULL) { fprintf(stderr, "makedbm: yikes!\n"); exit(1); } if (*p != ' ' && *p != '\t') break; p++; } content.dptr = p; content.dsize = strlen(p) - 1; /* erase trailing newline */ tmp = fetch(key); if (tmp.dptr == NULL) { if (store(key, content) != 0) { printf("problem storing %.*s %.*s\n", key.dsize, key.dptr, content.dsize, content.dptr); exit(1); } }#ifdef DEBUG else { printf("duplicate: %.*s %.*s\n", key.dsize, key.dptr, content.dsize, content.dptr); }#endif } } breakout: addpair("YP_LAST_MODIFIED", get_date(infile)); if (infilename) addpair("YP_INPUT_FILE", infilename); if (outfilename) addpair("YP_OUTPUT_NAME", outfilename); if (domainname) addpair("YP_DOMAIN_NAME", domainname); if (!mastername) { gethostname(local_host, sizeof (local_host) - 1); mastername = local_host; } addpair("YP_MASTER_NAME", mastername); if (rename(tmppagbuf, pagbuf) < 0) perror("makedbm: rename"); if (rename(tmpdirbuf, dirbuf) < 0) perror("makedbm: rename");}/* * scans cp, looking for a match with any character * in match. Returns pointer to place in cp that matched * (or NULL if no match) */static char *any(cp, match) register char *cp; char *match;{ register char *mp, c; while (c = *cp) { for (mp = match; *mp; mp++) if (*mp == c) return (cp); cp++; } return ((char *)0);}static char *get_date(name) char *name;{ struct stat filestat; static char ans[LASCII_DATE];/* ASCII numeric string */ if (strcmp(name, "-") == 0) sprintf(ans, "%010d", time(0)); else { if (stat(name, &filestat) < 0) { fprintf(stderr, "makedbm: can't stat %s\n", name); exit(1); } sprintf(ans, "%010d", filestat.st_mtime); } return ans;}usage(){ fprintf(stderr,"usage: makedbm -u file\n makedbm [-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE] [-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME] infile outfile\n"); exit(1);}addpair(str1, str2) char *str1, *str2;{ datum key; datum content; key.dptr = str1; key.dsize = strlen(str1); content.dptr = str2; content.dsize = strlen(str2); if (store(key, content) != 0){ printf("makedbm: problem storing %.*s %.*s\n", key.dsize, key.dptr, content.dsize, content.dptr); exit(1); }}unmake(file) char *file;{ datum key, content; if (file == NULL) usage(); if (dbminit(file) != 0) { fprintf(stderr, "makedbm: couldn't init %s\n", file); exit(1); } for (key = firstkey(); key.dptr != NULL; key = nextkey(key)) { content = fetch(key); printf("%.*s %.*s\n", key.dsize, key.dptr, content.dsize, content.dptr); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?