filehand.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 617 行 · 第 1/2 页
C
617 行
for (i = 0; fmatch[i] != EMPTY; i++) TR("%d (%s)\n", i, fmatch[i], EMPTY);#endif if ((fp1 = fopen (file1, READ)) == NULL) { if (opcode == PUTNOW || (opcode & INSERT) > 0) { if ((fp1 = fopen (file1, APP)) == NULL) return (NOSOURCE); if (fclose (fp1) == ERROR) return (NOSOURCE); if ((fp1 = fopen (file1, READ)) == NULL) return (NOSOURCE); TR("Sweep: made a blank file1\n", EMPTY, EMPTY, EMPTY); opcode = PUTNOW; /* Since buffers are unneeded. */ } else return (NOSOURCE); } if ((opcode & VERIFY) == 0) { if ((fp2 = fopen (file2, READ)) != NULL) { if (fclose (fp2) == ERROR || unlink (file2) == ERROR) { if (fclose (fp1) == ERROR) return (RECURSE + LIVEDEST); return (LIVEDEST); } } if ((fp2 = fopen (file2, APP)) == NULL) { if (fclose (fp1) == ERROR) return (RECURSE + NODEST); return (NODEST); } action += OFILE2; /* Flag file2 as open. */ } action += OFILE1; /* Flag file1 as open. */ TR("Sweep: file1%s open\n", ((action & OFILE2) > 0) ? " and file2" : "", EMPTY, EMPTY); if (opcode == PUTNOW) goto begin; /* Because we need no allocations. */ /* At this point, we begin allocating the three areas that this routine * needs. realrec will contain a copy of what was really read. hackrec * will provide space to store its individual fields. arg will provide * space for the pointers the those fields. The general scheme used for * allocation and use of these areas is as follows: * * Given: usrbuf -- usrbuf -- * usrmatch usrmatch -- -- * --------------------------------------------------- * arg usrmatch/1 usrmatch/2 ALLOC3 ALLOC3 * --------------------------------------------------- * realrec ALLOC1 ALLOC1 usrbuf/3 ALLOC1 * --------------------------------------------------- * hackrec usrbuf/4 ALLOC2 ALLOC2 ALLOC2 * --------------------------------------------------- * * Note 1: On DELETE this array will hold pointers to the actual fields * of the DELETED record. On INSERT and REPLACE, it will hold pointers * into usrbuf which will be MEANINGLESS. On VERIFY, it will also hold * pointers, but they are to the matched record. On PUTNOW, this array * is unaffected. * * Note 2: On all operations, the pointers in this array will be useless * upon return. * * Note 3: On DELETE and VERIFY, this array will hold the actual record * deleted or matched. On INSERT or REPLACE, this record will hold the * actual record inserted or replaced (and is not used internally). * PUTNOW has no effect. * * Note 4: On DELETE and VERIFY, this array will hold the actual record * deleted or matched. On INSERT and REPLACE, this array will hold the * actual record inserted or replaced (in which case, the specification * of usrmatch will put only junk into that array, as in note 1). This * area is not used internally for an INSERT or REPLACE. This array is * not affected by PUTNOW. */ if (maxlen <= 0) { retcode = BADSIZE; goto bye; } retcode = NOSPACE; /* All errors will be for this. */ if (usrbuf == EMPTY) { /* We must allocate a buffer. */ TR("Sweep: no usrbuf\n", EMPTY, EMPTY, EMPTY); if ((realrec = malloc ((unsigned) maxlen)) == EMPTY) goto bye; action += ALLOC1; /* A buffer was allocated. */ TR("Sweep: allocated (1) realrec=%d\n", realrec, EMPTY, EMPTY); if ((hackrec = malloc ((unsigned) maxlen)) == EMPTY) goto bye; action += ALLOC2; TR("Sweep: allocated (2) hackrec=%d\n", hackrec, EMPTY, EMPTY); if (usrmatch == (char**) NULL) { /* User provided no field pointers. */ j = (maxlen / 2 + 1) * sizeof (char*); if ((arg = (char**) malloc ((unsigned) j)) == (char**) NULL) goto bye; action += ALLOC3; /* Pointer space was allocated. */ TR("Sweep: allocated (3) arg=%d\n", arg, EMPTY, EMPTY); } else arg = usrmatch; } else { /* A usrbuf area was specified */ TR("Sweep: usrbuf given\n", EMPTY, EMPTY, EMPTY); if (usrmatch == (char**) NULL) { TR("Sweep: no usrmatch\n", EMPTY, EMPTY, EMPTY); if ((opcode & REPLACE) > 0 || (opcode & INSERT) > 0) { /* Alas, we cannot use the area. */ if ((realrec = malloc ((unsigned) maxlen)) == EMPTY) goto bye; action += ALLOC1; TR("Sweep: allocated (4) realrec=%d\n", realrec, EMPTY, EMPTY); } else realrec = usrbuf; if ((hackrec = malloc ((unsigned) maxlen)) == EMPTY) goto bye; action += ALLOC2; TR("Sweep: allocated (5) hackrec=%d\n", hackrec, EMPTY, EMPTY); j = (maxlen / 2 + 1) * sizeof (char*); if ((arg = (char**) malloc ((unsigned) j)) == (char**) NULL) goto bye; action += ALLOC3; TR("Sweep: allocated (6) arg=%d\n", arg, EMPTY, EMPTY); } else { /* We have usrbuf and usrmatch. */ if ((opcode & REPLACE) > 0 || (opcode & INSERT) > 0) { if ((hackrec = malloc ((unsigned) maxlen)) == EMPTY) goto bye; action += ALLOC2; TR("Sweep: allocated (7) hackrec=%d\n", hackrec, EMPTY, EMPTY); } else hackrec = usrbuf; arg = usrmatch; if ((realrec = malloc ((unsigned) maxlen)) == EMPTY) goto bye; action += ALLOC1; TR("Sweep: allocated (8) realrec=%d\n", realrec, EMPTY, EMPTY); } }begin: if (opcode == PUTNOW) { /* Insert a message at beginning. */ if ((action & OFILE2) > 0) { fprintf (fp2, "%s%c", usrbuf, recsep); retcode = DONE; goto bye; } else { retcode = NOTPUT; goto bye; } } if (recsep == NULL) rectype = FIXED; else rectype = VARIED; while ((i = getrec (fp1, realrec, recsep, rectype, maxlen)) == DONE) { /* Copy the buffer */ strncpy (hackrec, realrec, maxlen); /* Seperate the command into arguments. */ if (chop == (int (*)()) NULL) i = argchop (hackrec, fldsep, arg); else i = (*chop) (hackrec, fldsep, arg); if (i != DONE) break; /* Something funny in record. */ /* Now, determine the alphabetic status of this record. */ if (compare == (int (*)()) NULL) cmpstat = rec_cmp (arg, fmatch, recsep); else cmpstat = (*compare) (arg, fmatch, recsep); /* Now, decide to continue or not. */ if ((opcode & SEQUENTIAL) == 0 && cmpstat != SAME) continue; /* We continue if the search is not sequential and the record * did not match. */ if (cmpstat == SAME) { TR("Sweep: Enter SAME section\n", EMPTY, EMPTY, EMPTY); if ((opcode & INSERT) > 0) retcode = NOTNEW; else if ((opcode & VERIFY) > 0) retcode = FOUND; else { if ((opcode & REPLACE) > 0 && usrbuf != EMPTY) fprintf (fp2, "%s%c", usrbuf, recsep); /* And if this was a DELETE, nothing is printed. */ retcode = DONE; } goto bye; } else if (cmpstat == BEFORE) { TR("Sweep: Enter BEFORE section\n", EMPTY, EMPTY, EMPTY); /* Put the record into the "scratch" file. */ if ((opcode & VERIFY) == 0) fprintf (fp2, "%s%c", realrec, recsep); } else { /* cmpstat == AFTER. */ TR("Sweep: enter AFTER section\n", EMPTY, EMPTY, EMPTY); /* Match goes (or should have come) before this one. */ if (opcode == SEQINSERT) { if (usrbuf != EMPTY) fprintf (fp2, "%s%c", usrbuf, recsep); /* Put in the INSERTed one, then replace the old one. */ fprintf (fp2, "%s%c", realrec, recsep); retcode = DONE; } else /* We never get here on non-sequential searches. */ retcode = NOTFOUND; goto bye; } } /* End of while loop. */ TR("Sweep: end of record loop\n", EMPTY, EMPTY, EMPTY); if (i != FILE1EOF) { TR("Sweep: funny death\n", EMPTY, EMPTY, EMPTY); retcode = i; /* So user knows about bad data. */ goto bye; } if (fclose (fp1) == ERROR) retcode = NOCLOSE; action -= OFILE1; if (((opcode & VERIFY) | (opcode & REPLACE) | (opcode & DELETE)) > 0) { TR("Sweep: not there\n", EMPTY, EMPTY, EMPTY); if ((opcode & VERIFY) == 0) { if (fclose (fp2) == ERROR) retcode = RESETERR; action -= OFILE2; if (unlink (file2) == ERROR) { retcode = RESETERR; /* Should never happen? */ goto bye; } } retcode = NOTFOUND; goto bye; } if ((opcode & INSERT) > 0) { TR("Sweep: insert tail record\n", EMPTY, EMPTY, EMPTY); if (usrbuf != EMPTY) fprintf (fp2, "%s%c", usrbuf, recsep); retcode = DONE; } else retcode = ABEND; /* Should never happen? */bye: TR("Sweep: closing retcode=%d action=%d\n", retcode, action, EMPTY); if (retcode == DONE) { /* Successful on a file update. */ if ((action & OFILE1) > 0 && (action & OFILE2) > 0) retcode = copyrest (fp1, fp2, EMPTY, BUFSIZE); } if ((action & OFILE1) > 0) { if (fclose (fp1) == ERROR) retcode = NOCLOSE; TR("Sweep: file1 closed\n", EMPTY, EMPTY, EMPTY); } if ((action & OFILE2) > 0) { if (fclose (fp2) == ERROR) retcode = NOCLOSE; TR("Sweep: file2 closed\n", EMPTY, EMPTY, EMPTY); } /* With the files closed, we now replace file1 with file2 (if needed). */ if (retcode == DONE) { if ((retcode = ftrans (MOVE, file2, file1, EMPTY, BUFSIZE)) != DONE) retcode += RECURSE; TR("Sweep: final transfer\n", EMPTY, EMPTY, EMPTY); } if ((action & ALLOC1) > 0) { TR("Sweep: free realrec\n", EMPTY, EMPTY, EMPTY); free (realrec); } if ((action & ALLOC2) > 0) { TR("Sweep: free hackrec\n", EMPTY, EMPTY, EMPTY); free (hackrec); } if ((action & ALLOC3) > 0) { TR("Sweep: free arg\n", EMPTY, EMPTY, EMPTY); free ((char*) arg); } return (retcode);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?