val.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 713 行 · 第 1/2 页
C
713 行
#ifndef lintstatic char *sccsid = "@(#)val.c 4.1 (ULTRIX) 7/17/90";#endif lint/************************************************************************//* *//* val - *//* val [-mname] [-rSID] [-s] [-ytype] file ... *//* *//************************************************************************/# include "../hdr/defines.h"# include "../hdr/had.h"# define FILARG_ERR 0200 /* no file name given */# define UNKDUP_ERR 0100 /* unknown or duplicate keyletter */# define CORRUPT_ERR 040 /* corrupt file error code */# define FILENAM_ERR 020 /* file name error code */# define INVALSID_ERR 010 /* invalid or ambiguous SID error */# define NONEXSID_ERR 04 /* non-existent SID error code */# define TYPE_ERR 02 /* type arg value error code */# define NAME_ERR 01 /* name arg value error code */# define TRUE 1# define FALSE 0# define BLANK(l) while (!(*l == ' ' || *l == '\t')) l++;int ret_code; /* prime return code from 'main' program */int inline_err; /* input line error code (from 'process') */int infile_err; /* file error code (from 'validate') */int inpstd; /* TRUE = args from standard input */struct packet gpkt;char had[26]; /* had flag used in 'process' function */char path[FILESIZE]; /* storage for file name value */char sid[50]; /* storage for sid (-r) value */char type[50]; /* storage for type (-y) value */char name[50]; /* storage for name (-m) value */char line[BUFSIZ];char *get_line(); /* function returning ptr to line read */char *getval(); /* function returning adjusted ptr to line */char *fmalloc(); /* function returning ptr */char *fgets(); /* function returning i/o ptr */struct delent { /* structure for delta table entry */ char type; char *osid; char *datetime; char *pgmr; char *serial; char *pred;} del;/* This is the main program that determines whether the command line * comes from the standard input or read off the original command * line. See VAL(I) for more information.*/main(argc,argv)int argc;char *argv[];{ FILE *iop; register int j; ret_code = 0; if (argc == 2 && argv[1][0] == '-' && !(argv[1][1])) { inpstd = TRUE; iop = stdin; /* read from standard input */ while (fgets(line,BUFSIZ,iop) != NULL) { if (line[0] != '\n') { repl (line,'\n','\0'); process(line); ret_code |= inline_err; } } } else { inpstd = FALSE; for (j = 1; j < argc; j++) sprintf(&(line[strlen(line)]),"%s ",argv[j]); j = strlen(line) - 1; line[j > 0 ? j : 0] = NULL; process(line); ret_code = inline_err; } exit(ret_code);}/* This function processes the line sent by the main routine. It * determines which keyletter values are present on the command * line and assigns the values to the correct storage place. It * then calls validate for each file name on the command line * It will return to main if the input line contains an error, * otherwise it returns any error code found by validate.*/process(p_line)char *p_line;{ register int j; register int testklt; register int line_sw; int silent; int num_files; char filelist[50][FILESIZE]; char *savelinep; char c; silent = FALSE; path[0] = sid[0] = type[0] = name[0] = 0; num_files = inline_err = 0; /* make copy of 'line' for use later */ savelinep = p_line; /* clear out had flags for each 'line' processed */ for (j = 0; j < 27; j++) had[j] = 0; /* execute loop until all characters in 'line' are checked. */ while (*p_line) { testklt = 1; NONBLANK(p_line); if (*p_line == '-') { p_line += 1; c = *p_line; p_line++; switch (c) { case 's': testklt = 0; /* turn on 'silent' flag. */ silent = TRUE; break; case 'r': p_line = getval(p_line,sid); break; case 'y': p_line = getval(p_line,type); break; case 'm': p_line = getval(p_line,name); break; default: inline_err |= UNKDUP_ERR; } /* use 'had' array and determine if the keyletter was given twice. */ if (had[c - 'a']++ && testklt++) inline_err |= UNKDUP_ERR; } else { /* assume file name if no '-' preceeded argument */ p_line = getval(p_line,filelist[num_files]); num_files++; } } /* check if any files were named as arguments */ if (num_files == 0) inline_err |= FILARG_ERR; /* check for error in command line. */ if (inline_err && !silent) { if (inpstd) report(inline_err,savelinep,""); else report(inline_err,"",""); return; /* return to 'main' routine */ } line_sw = 1; /* print command line flag */ /* loop through 'validate' for each file on command line. */ for (j = 0; j < num_files; j++) { /* read a file from 'filelist' and place into 'path'. */ sprintf(path,"%s",filelist[j]); validate(path,sid,type,name); inline_err |= infile_err; /* check for error from 'validate' and call 'report' depending on 'silent' flag. */ if (infile_err && !silent) { if (line_sw && inpstd) { report(infile_err,savelinep,path); line_sw = 0; } else report(infile_err,"",path); } } return; /* return to 'main' routine */}/* This function actually does the validation on the named file. * It determines whether the file is an SCCS-file or if the file * exists. It also determines if the values given for type, SID, * and name match those in the named file. An error code is returned * if any mismatch occurs. See VAL(I) for more information.*/validate(c_path,c_sid,c_type,c_name)char *c_path;char *c_sid;char *c_type;char *c_name;{ char *auxf(); register char *l; int goods,goodt,goodn,hadmflag; infile_err = goods = goodt = goodn = hadmflag = 0; sinit(&gpkt,c_path); if (!sccsfile(c_path) || (gpkt.p_iop = fopen(c_path,"r")) == NULL) infile_err |= FILENAM_ERR; else { l = get_line(&gpkt); /* read first line in file */ /* check that it is header line. */ if (*l++ != CTLCHAR || *l++ != HEAD) infile_err |= CORRUPT_ERR; else { /* get old file checksum count */ satoi(l,&gpkt.p_ihash); gpkt.p_chash = 0; if (HADR) /* check for invalid or ambiguous SID. */ if (invalid(c_sid)) infile_err |= INVALSID_ERR; /* read delta table checking for errors and/or SID. */ if (do_delt(&gpkt,goods,c_sid)) { fclose(gpkt.p_iop); infile_err |= CORRUPT_ERR; return; } read_to(EUSERNAM,&gpkt); if (HADY || HADM) { /* read flag section of delta table. */ while ((l = get_line(&gpkt)) && *l++ == CTLCHAR && *l++ == FLAG) { NONBLANK(l); repl(l,'\n','\0'); if (*l == TYPEFLAG) { l += 2; if (equal(c_type,l)) goodt++; } else if (*l == MODFLAG) { hadmflag++; l += 2; if (equal(c_name,l)) goodn++; } } if (*(--l) != BUSERTXT) { fclose(gpkt.p_iop); infile_err |= CORRUPT_ERR; return; } /* check if 'y' flag matched '-y' arg value. */ if (!goodt && HADY) infile_err |= TYPE_ERR; /* check if 'm' flag matched '-m' arg value. */ if (HADM && !hadmflag) { if (!equal(auxf(sname(c_path),'g'),c_name)) infile_err |= NAME_ERR; } else if (HADM && hadmflag && !goodn) infile_err |= NAME_ERR; } else read_to(BUSERTXT,&gpkt); read_to(EUSERTXT,&gpkt); gpkt.p_chkeof = 1; /* read remainder of file so 'read_mod' can check for corruptness. */ while (read_mod(&gpkt)) ; } fclose(gpkt.p_iop); /* close file pointer */ } return; /* return to 'process' function */}/* This function reads the 'delta' line from the named file and stores * the information into the structure 'del'.*/getdel(delp,lp)register struct delent *delp;register char *lp;{ NONBLANK(lp); delp->type = *lp++; NONBLANK(lp); delp->osid = lp; BLANK(lp); *lp++ = '\0'; NONBLANK(lp); delp->datetime = lp; BLANK(lp); NONBLANK(lp); BLANK(lp); *lp++ = '\0'; NONBLANK(lp); delp->pgmr = lp; BLANK(lp); *lp++ = '\0'; NONBLANK(lp); delp->serial = lp; BLANK(lp); *lp++ = '\0'; NONBLANK(lp); delp->pred = lp; repl(lp,'\n','\0');}/* This function does a read through the named file until it finds * the character sent over as an argument.*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?