=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/patch/inp.c,v retrieving revision 1.15 retrieving revision 1.16 diff -c -r1.15 -r1.16 *** src/usr.bin/patch/inp.c 2003/07/21 21:01:45 1.15 --- src/usr.bin/patch/inp.c 2003/07/22 17:18:49 1.16 *************** *** 1,9 **** ! /* $OpenBSD: inp.c,v 1.15 2003/07/21 21:01:45 otto Exp $ */ #ifndef lint ! static char rcsid[] = "$OpenBSD: inp.c,v 1.15 2003/07/21 21:01:45 otto Exp $"; #endif /* not lint */ #include "EXTERN.h" #include "common.h" #include "util.h" --- 1,20 ---- ! /* $OpenBSD: inp.c,v 1.16 2003/07/22 17:18:49 otto Exp $ */ #ifndef lint ! static const char rcsid[] = "$OpenBSD: inp.c,v 1.16 2003/07/22 17:18:49 otto Exp $"; #endif /* not lint */ + #include + #include + #include + + #include + #include + #include + #include + #include + #include + #include "EXTERN.h" #include "common.h" #include "util.h" *************** *** 11,17 **** #include "INTERN.h" #include "inp.h" - extern bool check_only; /* Input-file-with-indexable-lines abstract type */ --- 22,27 ---- *************** *** 25,31 **** static LINENUM lines_per_buf; /* how many lines per buffer */ static int tireclen; /* length of records in tmp file */ ! void re_input(void); /* New patch--prepare to edit another file. */ --- 35,43 ---- static LINENUM lines_per_buf; /* how many lines per buffer */ static int tireclen; /* length of records in tmp file */ ! static bool rev_in_string(char *); ! static bool plan_a(char *); /* returns false if insufficient memory */ ! static void plan_b(char *); /* New patch--prepare to edit another file. */ *************** *** 34,54 **** { if (using_plan_a) { i_size = 0; ! #ifndef lint ! if (i_ptr != Null(char **)) ! free((char *) i_ptr); ! #endif ! if (i_womp != Nullch) ! free(i_womp); ! i_womp = Nullch; ! i_ptr = Null(char **); } else { using_plan_a = TRUE; /* maybe the next one is smaller */ close(tifd); tifd = -1; free(tibuf[0]); free(tibuf[1]); ! tibuf[0] = tibuf[1] = Nullch; tiline[0] = tiline[1] = -1; tireclen = 0; } --- 46,62 ---- { if (using_plan_a) { i_size = 0; ! free(i_ptr); ! free(i_womp); ! i_womp = NULL; ! i_ptr = NULL; } else { using_plan_a = TRUE; /* maybe the next one is smaller */ close(tifd); tifd = -1; free(tibuf[0]); free(tibuf[1]); ! tibuf[0] = tibuf[1] = NULL; tiline[0] = tiline[1] = -1; tireclen = 0; } *************** *** 69,75 **** /* Try keeping everything in memory. */ ! bool plan_a(char *filename) { int ifd, statfailed; --- 77,83 ---- /* Try keeping everything in memory. */ ! static bool plan_a(char *filename) { int ifd, statfailed; *************** *** 103,109 **** (filestat.st_mode & 0222) == 0 || /* I can't write to it. */ ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) { ! char *cs = Nullch, *filebase, *filedir; struct stat cstat; filebase = basename(filename); --- 111,117 ---- (filestat.st_mode & 0222) == 0 || /* I can't write to it. */ ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) { ! char *cs = NULL, *filebase, *filedir; struct stat cstat; filebase = basename(filename); *************** *** 170,187 **** out_of_mem = FALSE; return FALSE; /* force plan b because plan a bombed */ } ! i_womp = malloc(i_size + 2); ! if (i_womp == Nullch) return FALSE; if ((ifd = open(filename, O_RDONLY)) < 0) pfatal("can't open file %s", filename); ! #ifndef lint if (read(ifd, i_womp, (size_t) i_size) != i_size) { close(ifd); /* probably means i_size > 15 or 16 bits worth */ free(i_womp); /* at this point it doesn't matter if i_womp was */ return FALSE; /* undersized. */ } ! #endif close(ifd); if (i_size && i_womp[i_size - 1] != '\n') i_womp[i_size++] = '\n'; --- 178,197 ---- out_of_mem = FALSE; return FALSE; /* force plan b because plan a bombed */ } ! if (i_size > SIZE_MAX - 2) ! fatal("block too large to allocate"); ! i_womp = malloc((size_t)(i_size + 2)); ! if (i_womp == NULL) return FALSE; if ((ifd = open(filename, O_RDONLY)) < 0) pfatal("can't open file %s", filename); ! if (read(ifd, i_womp, (size_t) i_size) != i_size) { close(ifd); /* probably means i_size > 15 or 16 bits worth */ free(i_womp); /* at this point it doesn't matter if i_womp was */ return FALSE; /* undersized. */ } ! close(ifd); if (i_size && i_womp[i_size - 1] != '\n') i_womp[i_size++] = '\n'; *************** *** 194,205 **** if (*s == '\n') iline++; } ! #ifdef lint ! i_ptr = Null(char **); ! #else i_ptr = (char **) malloc((iline + 2) * sizeof(char *)); ! #endif ! if (i_ptr == Null(char **)) { /* shucks, it was a near thing */ free((char *) i_womp); return FALSE; } --- 204,213 ---- if (*s == '\n') iline++; } ! i_ptr = (char **) malloc((iline + 2) * sizeof(char *)); ! ! if (i_ptr == NULL) { /* shucks, it was a near thing */ free((char *) i_womp); return FALSE; } *************** *** 216,222 **** /* now check for revision, if any */ ! if (revision != Nullch) { if (!rev_in_string(i_womp)) { if (force) { if (verbose) --- 224,230 ---- /* now check for revision, if any */ ! if (revision != NULL) { if (!rev_in_string(i_womp)) { if (force) { if (verbose) *************** *** 243,268 **** /* Keep (virtually) nothing in memory. */ ! void plan_b(char *filename) { FILE *ifp; int i = 0, maxlen = 1; ! bool found_revision = (revision == Nullch); using_plan_a = FALSE; ! if ((ifp = fopen(filename, "r")) == Nullfp) pfatal("can't open file %s", filename); (void) unlink(TMPINNAME); if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0) pfatal("can't open file %s", TMPINNAME); ! while (fgets(buf, sizeof buf, ifp) != Nullch) { ! if (revision != Nullch && !found_revision && rev_in_string(buf)) found_revision = TRUE; if ((i = strlen(buf)) > maxlen) maxlen = i; /* find longest line */ } ! if (revision != Nullch) { if (!found_revision) { if (force) { if (verbose) --- 251,276 ---- /* Keep (virtually) nothing in memory. */ ! static void plan_b(char *filename) { FILE *ifp; int i = 0, maxlen = 1; ! bool found_revision = (revision == NULL); using_plan_a = FALSE; ! if ((ifp = fopen(filename, "r")) == NULL) pfatal("can't open file %s", filename); (void) unlink(TMPINNAME); if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0) pfatal("can't open file %s", TMPINNAME); ! while (fgets(buf, sizeof buf, ifp) != NULL) { ! if (revision != NULL && !found_revision && rev_in_string(buf)) found_revision = TRUE; if ((i = strlen(buf)) > maxlen) maxlen = i; /* find longest line */ } ! if (revision != NULL) { if (!found_revision) { if (force) { if (verbose) *************** *** 284,304 **** say("Good. This file appears to be the %s version.\n", revision); } ! fseek(ifp, 0L, 0); /* rewind file */ lines_per_buf = BUFFERSIZE / maxlen; tireclen = maxlen; tibuf[0] = malloc(BUFFERSIZE + 1); ! if (tibuf[0] == Nullch) fatal("out of memory\n"); tibuf[1] = malloc(BUFFERSIZE + 1); ! if (tibuf[1] == Nullch) fatal("out of memory\n"); for (i = 1;; i++) { if (!(i % lines_per_buf)) /* new block */ if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) pfatal("can't write temp file"); if (fgets(tibuf[0] + maxlen * (i % lines_per_buf), ! maxlen + 1, ifp) == Nullch) { input_lines = i - 1; if (i % lines_per_buf) if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) --- 292,312 ---- say("Good. This file appears to be the %s version.\n", revision); } ! fseek(ifp, 0L, SEEK_SET); /* rewind file */ lines_per_buf = BUFFERSIZE / maxlen; tireclen = maxlen; tibuf[0] = malloc(BUFFERSIZE + 1); ! if (tibuf[0] == NULL) fatal("out of memory\n"); tibuf[1] = malloc(BUFFERSIZE + 1); ! if (tibuf[1] == NULL) fatal("out of memory\n"); for (i = 1;; i++) { if (!(i % lines_per_buf)) /* new block */ if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) pfatal("can't write temp file"); if (fgets(tibuf[0] + maxlen * (i % lines_per_buf), ! maxlen + 1, ifp) == NULL) { input_lines = i - 1; if (i % lines_per_buf) if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE) *************** *** 334,343 **** whichbuf = 1; else { tiline[whichbuf] = baseline; ! #ifndef lint /* complains of long accuracy */ lseek(tifd, (off_t) (baseline / lines_per_buf * ! BUFFERSIZE), 0); ! #endif if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0) pfatal("error reading tmp file %s", TMPINNAME); } --- 342,351 ---- whichbuf = 1; else { tiline[whichbuf] = baseline; ! lseek(tifd, (off_t) (baseline / lines_per_buf * ! BUFFERSIZE), SEEK_SET); ! if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0) pfatal("error reading tmp file %s", TMPINNAME); } *************** *** 348,360 **** /* * True if the string argument contains the revision number we want. */ ! bool rev_in_string(char *string) { char *s; int patlen; ! if (revision == Nullch) return TRUE; patlen = strlen(revision); if (strnEQ(string, revision, patlen) && isspace(string[patlen])) --- 356,368 ---- /* * True if the string argument contains the revision number we want. */ ! static bool rev_in_string(char *string) { char *s; int patlen; ! if (revision == NULL) return TRUE; patlen = strlen(revision); if (strnEQ(string, revision, patlen) && isspace(string[patlen]))