fritz@unocss.UUCP (Tim Russell) (08/27/89)
+-+-+-+ Beginning of part 3 +-+-+-+ X`009int bstart; X`009int bend; X`123 X`009char c; X X`009/* X`009 * This catches a "dummy" last entry`032 X`009 */ X`009if (astart > aend && bstart > bend) X`009`009return; X`009c = (astart > aend) ? 'a' : (bstart > bend) ? 'd' : 'c'; X`009if (cflag) X`009`009fputs("**************\n*** ", stdout); X X`009if (c == 'a' && !cflag) X`009`009range(astart - 1, astart - 1, 0);`009`009/* Addition: just print one X`009`009`009`009`009`009`009`009`009`009`009`009 * odd # */ X`009else X`009`009range(astart, aend, 0);`009/* Print both, if different */ X`009if (!cflag) `123 X`009`009putchar(c); X`009`009if (!eflag) `123 X`009`009`009if (c == 'd') V`009`009`009`009range(bstart - 1, bstart - 1, 1);`009`009/* Deletion: just pr Xint X`009`009`009`009`009`009`009`009`009`009`009`009`009`009 * one odd # */ X`009`009`009else X`009`009`009`009range(bstart, bend, 1);`009/* Print both, if different */ X`009`009`125 X`009`125 X`009putchar('\n'); X`009if ((!eflag && c != 'a') `124`124 cflag) `123 X`009`009fetch(oldseek, astart, aend, lenA, infd[0], X`009`009`009 cflag ? (c == 'd' ? "- " : "! ") : "< "); X`009`009if (cflag) `123 X`009`009`009fputs("--- ", stdout); X`009`009`009range(bstart, bend, 1); X`009`009`009fputs(" -----\n", stdout); X`009`009`125 else if (astart <= aend && bstart <= bend) X`009`009`009printf("---\n"); X`009`125 X`009fetch(newseek, bstart, bend, lenB, infd[1], X`009`009 cflag ? (c == 'a' ? "+ " : "! ") : (eflag ? "" : "> ")); X`009if (eflag && bstart <= bend) X`009`009printf(".\n"); X`125 X X X/* X * Print a range X */ X Xrange(from, to, w) X`009int from; X`009int to; X`009int w; X`123 X`009if (cflag) `123 X`009`009if ((from -= cflag) <= 0) X`009`009`009from = 1; X`009`009if ((to += cflag) > len[w]) X`009`009`009to = len[w]; X`009`125 X`009if (to > from) `123 X`009`009printf("%d,%d", from, to); X`009`125 else if (to < from) `123 X`009`009printf("%d,%d", to, from); X`009`125 else `123 X`009`009printf("%d", from); X`009`125 X`125 X X X/* X * Print the appropriate text X */ X Xfetch(seekvec, start, end, trueend, fd, pfx) X`009long *seekvec; X`009register int start; X`009register int end; X`009int trueend; X`009FILE *fd; X`009char *pfx; X`123 X`009register int i; X`009register int first; X`009register int last; X X`009if (cflag) `123 X`009`009if ((first = start - cflag) <= 0) X`009`009`009first = 1; X`009`009if ((last = end + cflag) > trueend) X`009`009`009last = trueend; X`009`125 else `123 X`009`009first = start; X`009`009last = end; X`009`125 X`009if (fseek(fd, seekvec[first], 0) != 0) `123 X`009`009printf("?Can't read line %d at %08lx (hex) in file%c\n", X`009`009`009 start, seekvec[first], X`009`009`009 (fd == infd[0]) ? 'A' : 'B'); X`009`125 else `123 X`009`009for (i = first; i <= last; i++) `123 X`009`009`009if (fgetss(text, sizeof text, fd) == NULL) `123 X`009`009`009`009printf("** Unexpected end of file\n"); X`009`009`009`009break; X`009`009`009`125 X#ifdef DEBUG X`009`009`009printf("%5d: %s%s\n", i, pfx, text); X#else /* !DEBUG */ V`009`009`009fputs((cflag && (i < start `124`124 i > end)) ? " " : pfx, stdou Xt); X`009`009`009fputs(text, stdout); X`009`009`009putchar('\n'); X#endif /* DEBUG */ X`009`009`125 X`009`125 X`125 X X X/* X * Input routine, read one line to buffer[], return TRUE on eof, else FALSE. X * The terminating newline is always removed. If "-b" was given, trailing X * whitespace (blanks and tabs) are removed and strings of blanks and X * tabs are replaced by a single blank. Getline() does all hacking for X * redirected input files. X */ X Xint Xgetline(fd, buffer) X`009FILE *fd; X`009char *buffer; X`123 X`009register char *top; X`009register char *fromp; X`009register char c; X X`009if (fgetss(buffer, sizeof text, fd) == NULL) `123 X`009`009*buffer = EOS; X`009`009return (TRUE); X`009`125 X`009if (fd == stdin) X`009`009fputss(buffer, tempfd); X`009if (bflag `124`124 iflag) `123 X`009`009top = buffer; X`009`009fromp = buffer; X`009`009while ((c = *fromp++) != EOS) `123 X`009`009`009if (bflag && (c == ' ' `124`124 c == '\t')) `123 X`009`009`009`009c = ' '; X`009`009`009`009while (*fromp == ' ' `124`124 *fromp == '\t') X`009`009`009`009`009fromp++; X`009`009`009`125 X`009`009`009if (iflag) X`009`009`009`009c = tolower(c); X`009`009`009*top++ = c; X`009`009`125 X`009`009if (bflag && top[-1] == ' ') X`009`009`009top--; X`009`009*top = EOS; X`009`125 X`009return (FALSE); X`125 X X Xstatic unsigned short crc16a[] = `123 X`009`009`009`009`009`009`009`009 0000000, 0140301, 0140601, 0000500, X`009`009`009`009`009`009`009`009 0141401, 0001700, 0001200, 0141101, X`009`009`009`009`009`009`009`009 0143001, 0003300, 0003600, 0143501, X`009`009`009`009`009`009`009`009 0002400, 0142701, 0142201, 0002100, X`125; X Xstatic unsigned short crc16b[] = `123 X`009`009`009`009`009`009`009`009 0000000, 0146001, 0154001, 0012000, X`009`009`009`009`009`009`009`009 0170001, 0036000, 0024000, 0162001, X`009`009`009`009`009`009`009`009 0120001, 0066000, 0074000, 0132001, X`009`009`009`009`009`009`009`009 0050000, 0116001, 0104001, 0043000, X`125; X X X/* X * Return the CRC16 hash code for the buffer X * Algorithm from Stu Wecker (Digital memo 130-959-002-00). X */ X Xunsigned short Xhash(buffer) X`009char *buffer; X`123 X`009register unsigned short crc; X`009register char *tp; X`009register short temp; X X`009crc = 0; X`009for (tp = buffer; *tp != EOS;) `123 X`009`009temp = *tp++ `094 crc;`009`009/* XOR crc with new char */ X`009`009crc = (crc >> 8) X`009`009`009`094 crc16a[(temp & 0017)] X`009`009`009`094 crc16b[(temp & 0360) >> 4]; X`009`125 X#ifdef DEBUG_ALL X`009printf("%06o: %s\n", (crc == 0) ? 1 : crc, buffer); X#endif /* DEBUG_ALL */ X`009return ((crc == 0) ? (unsigned short) 1 : crc); X`125 X X X#ifdef VMS Xopendir(which, arg, okfd) X`009int which;`009`009/* Which file to open (0 or 1) `009 */ X`009char **arg;`009`009/* File name argument, &argv[which] */ X`009FILE *okfd;`009`009/* File name (already open) `009 */ X`123 X`009register char *tp; X`009register int c; X`009register char *newname; X X`009fgetname(okfd, text); X X`009/* X`009 * Skip over device name`032 X`009 */ X`009for (tp = text; (c = *tp) && c != ':'; tp++); X`009if (c) X`009`009tp++; X`009else X`009`009tp = text; X X`009/* X`009 * Skip over [UIC] or [PPN] if present`032 X`009 */ X`009if (*tp == '[' `124`124 *tp == '(') `123 X`009`009while ((c = *tp++) && c != ']' && c != ')'); X`009`009if (c == 0) `123 X`009`009`009fprintf(stderr, "?Bug: bad file name \"%s\"\n", X`009`009`009`009`009text); X`009`009`009tp--; X`009`009`125 X`009`125 X`009strcpy(text, tp); X X`009/* X`009 * Don't include version`032 X`009 */ X`009for (tp = text; (c = *tp) && c != ';'; tp++); X`009*tp = 0; X X`009/* X`009 * Now, text has the file name, tp - text, its length, and *arg the X`009 * (possible) directory name. Create a new file name for opening.`032 X`009 */ X#ifndef`009OSK X`009if ((newname = malloc(tp - text + strlen(*arg) + 1)) == NULL) X`009`009error("Out of space at start"); X#ifdef`009AMIGA X`009savsiz = tp - text + strlen(*arg) + 1; X`009savptr = newname; X#endif /* AMIGA */ X#else /* OSK */ X`009newname = myalloc(tp - text + strlen(*arg) + 1, "Out of space at start"); X#endif /* OSK */ X`009newname = strcat(*arg, text); X/*`009concat(newname, *arg, text, NULL); */ X`009if ((infd[which] = fopen(newname, "r")) == NULL) X`009`009cant(*arg, "constructed input", 1); X`009else X`009`009*arg = newname; X`125 X/* Amiga C doesn't have all these extensions for directory... */ X#endif /* vms */ X X X/* X * Allocate or crash. X */ X Xchar * Xmyalloc(amount, why) X`009unsigned amount; X`009char *why; X`123 X`009register char *pointer; X X#ifdef OSK X`009amount += sizeof(int); X#endif /* OSK */ X`009if ((pointer = malloc((unsigned) amount)) == NULL) X`009`009noroom(why); X#ifdef OSK X`009*((int *) pointer) = amount; X`009pointer += sizeof(int); X#ifdef DEBUG X`009fprintf(stderr, "Myalloc: %d at %06o\n", amount, pointer); X#endif /* DEBUG */ X#endif /* OSK */ X#ifdef`009AMIGA X`009savsiz = amount; X`009savptr = pointer; X#endif /* AMIGA */ X X`009return (pointer); X`125 X X X/* X * Reallocate pointer, compacting storage X * X * The "compacting storage" part is probably not relevant any more. X * There used to be horrid code here that malloc'd one byte and freed X * it at magic times, to cause garbage collection of the freespace X * or something. It's safely gone now, you didn't have to see it. X *`009-- John Gilmore, Nebula Consultants, Sept 26, 1986 X */ X Xchar * Xcompact(pointer, new_amount, why) X`009char *pointer; X`009unsigned new_amount; X`009char *why; X`123 X`009register char *new_pointer; X X#ifndef AMIGA X#ifndef OSK X#ifdef TURBO X`009extern void *realloc(); X#else /* !TURBO */ X`009extern char *realloc(); X#endif /* TURBO */ X X`009if ((new_pointer = realloc(pointer, (unsigned) new_amount)) == NULL) `123 X`009`009noroom(why); X`009`125 X#else /* OSK */ X`009register int old_amount; X`009new_amount += sizeof(int); X`009if ((new_pointer = malloc(new_amount)) == NULL) X`009`009noroom(why); X`009*(int *) new_pointer = new_amount; X`009new_pointer += sizeof(int); X`009old_amount = *(((int *) pointer) - 1); X`009/* _strass is like bcopy with the first two arguments reversted */ X`009_strass(new_pointer, pointer, (new_amount <= old_amount ? X`009`009`009`009`009`009`009`009 new_amount : old_amount) - sizeof(int)); X#ifdef DEBUG X`009fprintf(stderr, "compact %d to %d from %06o to %06o\n", X`009`009`009old_amount, new_amount, pointer, new_pointer); X#endif /* DEBUG */ X`009free(pointer - sizeof(int)); X#endif /* OSK */ X#else /* AMIGA */ X X`009/* X`009 * This routine is heavily dependent on C storage allocator hacks For X`009 * Amiga, we can't rely on storage being left alone "up to" the boundary X`009 * of allocation as in VMS or RSX. Therefore we have to be different here X`009 * and allocate a new larger segment, then free the old one. Messy but X`009 * hopefully it will work.`032 X`009 */ X`009extern char *malloc(); X X`009/* No realloc(). Do a malloc and copy it. */ X`009if ((new_pointer = malloc((unsigned) new_amount)) == NULL) `123 X`009`009noroom(why); X`009`125 X /* This MUST assume the program calls compact using the old pointer as the X last call of malloc... Reason is that RSX version is really simpleminded */ X`009cpysiz = savsiz; X /* Complain if deallocate area not same as last allocate area */ X`009if (savptr != pointer) X`009`009bogus(why); X`009wrk2 = new_pointer; X`009for (wrk = pointer; cpysiz > 0; cpysiz--) `123 X /* copy data to new area */ X`009`009*wrk2++ = *wrk++; X`009`125 X /* when done, free old memory area. */ X`009free(pointer); X#endif /* AMIGA */ X X#ifndef OSK X#ifdef DEBUG X`009if (new_pointer != pointer) `123 X`009`009fprintf(stderr, "moved from %06o to %06o\n", X`009`009`009`009pointer, new_pointer); X`009`125 X/* rdump(new_pointer, why); X*/ X#endif /* DEBUG */ X#endif /* OSK */ X`009return (new_pointer); X`125 X X Xnoroom(why) X`009char *why; X`123 X`009fprintf(stderr, "?DIFF-F-out of room when %s\n", why); X`009exit(IO_ERROR); X`125 X X X#ifdef`009AMIGA Xbogus(why) X`009char *why; X`123 X`009fprintf(stderr, "?DIFF-F-invalid compaction when %s\n", why); X`009exit(IO_ERROR); X`125 X#endif`009/* AMIGA */ X X X#ifdef DEBUG X/* X * Dump memory block X */ X Xrdump(pointer, why) X`009int *pointer; X`009char *why; X`123 X`009int *last; X`009int count; X X`009last = ((int **) pointer)[-1]; X`009fprintf(stderr, "dump %s of %06o -> %06o, %d words", X`009`009`009why, pointer, last, last - pointer); X`009last = (int *) (((int) last) & `1261); X`009for (count = 0; pointer < last; ++count) `123 X`009`009if ((count & 07) == 0) `123 X`009`009`009fprintf(stderr, "\n%06o", pointer); X`009`009`125 X`009`009fprintf(stderr, "\t%06o", *pointer); X`009`009pointer++; X`009`125 X`009fprintf(stderr, "\n"); X`125 X#endif /* DEBUG */ X X X/* X * Can't open file message X */ X Xcant(filename, what, fatalflag) X`009char *filename; X`009char *what; X`009int fatalflag; X`123 X`009fprintf(stderr, "Can't open %s file \"%s\": ", what, filename); X#ifndef`009OSK X`009perror((char *) NULL); X#else X`009prerr(0, errno); X#endif X`009if (fatalflag) `123 X`009`009exit(fatalflag); X`009`125 X`125 X X X#ifdef DEBUG Xdump(d_linep, d_len, d_which) X`009LINE *d_linep; X`009int`009`009`009`009d_len; X`009int`009`009`009`009d_which; X`123 X`009register int i; X X`009printf("Dump of file%c, %d elements\n", "AB"[d_which], d_len); X`009printf("linep @ %06o\n", d_linep); X`009for (i = 0; i <= d_len; i++) `123 X`009`009printf("%3d %6d %06o\n", i, X`009`009`009 d_linep[i].serial, d_linep[i].hash); X`009`125 X`125 X X X/* X * Dump klist X */ X Xdumpklist(kmax, why) X`009int kmax; X`009char *why; X`123 X`009register int i; X`009register CANDIDATE *cp; X`009register int count; X X`009printf("\nklist[0..%d] %s, clength = %d\n", kmax, why, clength); X`009for (i = 0; i <= kmax; i++) `123 X`009`009cp = &clist[klist[i]]; X`009`009printf("%2d %2d", i, klist[i]); X`009`009if (cp >= &clist[0] && cp < &clist[clength]) X`009`009`009printf(" (%2d %2d -> %2d)\n", cp->a, cp->b, cp->link); X`009`009else if (klist[i] == -1) X`009`009`009printf(" End of chain\n"); X`009`009else X`009`009`009printf(" illegal klist element\n"); X`009`125 X`009for (i = 0; i <= kmax; i++) `123 X`009`009count = -1; X`009`009for (cp = (CANDIDATE *) klist[i]; cp > &clist[0]; X`009`009`009 cp = (CANDIDATE *) & cp->link) `123 X`009`009`009if (++count >= 6) `123 X`009`009`009`009printf("\n "); X`009`009`009`009count = 0; X`009`009`009`125 X`009`009`009printf(" (%2d: %2d,%2d -> %d)", X`009`009`009`009 cp - clist, cp->a, cp->b, cp->link); X`009`009`125 X`009`009printf("\n"); X`009`125 X`009printf("*\n"); X`125 X#endif`009/* DEBUG */ X X X X#ifdef TIMING X X/* X * Dump time buffer X */ X Xptime(why) X`009char *why; X`123 X`009long ttemp; X X`009ttemp = time(NULL); X`009printf("%ld seconds for %s\n", X`009`009 ttemp - sectiontime, why); X`009sectiontime = ttemp; X`125 X#endif`009/* TIMING */ X X X/* X * TRUE if strings are identical X */ X Xint Xstreq(s1, s2) X`009register char *s1; X`009register char *s2; X`123 X`009while (*s1++ == *s2) `123 X`009`009if (*s2++ == EOS) X`009`009`009return (TRUE); X`009`125 X`009return (FALSE); X`125 X X X/* X * Error message before retiring. X */ X X/* VARARGS */ Xerror(format, args) X`009char *format; X`123 X`009fprintf(stderr, format, &args); -+-+-+-+-+ End of part 3 +-+-+-+-+- -- ---------------------------------+-------------------------------------------- Tim Russell, Computer Operator | Internet: russell@zeus.unl.edu Campus Computing | Bitnet: russell@unoma1 University of Nebraska at Omaha | UUCP: uunet!zeus.unl.edu!russell