version 1.3, 2006/07/17 22:28:11 |
version 1.4, 2012/03/04 04:05:15 |
|
|
void cleanup(void); |
void cleanup(void); |
void do_lineno(const char *); |
void do_lineno(const char *); |
void do_rexp(const char *); |
void do_rexp(const char *); |
char *getline(void); |
char *get_line(void); |
void handlesig(int); |
void handlesig(int); |
FILE *newfile(void); |
FILE *newfile(void); |
void toomuch(FILE *, long); |
void toomuch(FILE *, long); |
|
|
/* Copy the rest into a new file. */ |
/* Copy the rest into a new file. */ |
if (!feof(infile)) { |
if (!feof(infile)) { |
ofp = newfile(); |
ofp = newfile(); |
while ((p = getline()) != NULL && fputs(p, ofp) == 0) |
while ((p = get_line()) != NULL && fputs(p, ofp) == 0) |
; |
; |
if (!sflag) |
if (!sflag) |
printf("%jd\n", (intmax_t)ftello(ofp)); |
printf("%jd\n", (intmax_t)ftello(ofp)); |
|
|
|
|
/* Read a line from the input into a static buffer. */ |
/* Read a line from the input into a static buffer. */ |
char * |
char * |
getline(void) |
get_line(void) |
{ |
{ |
static char lbuf[LINE_MAX]; |
static char lbuf[LINE_MAX]; |
FILE *src; |
FILE *src; |
|
|
return (lbuf); |
return (lbuf); |
} |
} |
|
|
/* Conceptually rewind the input (as obtained by getline()) back `n' lines. */ |
/* Conceptually rewind the input (as obtained by get_line()) back `n' lines. */ |
void |
void |
toomuch(FILE *ofp, long n) |
toomuch(FILE *ofp, long n) |
{ |
{ |
|
|
err(1, "%s", currfile); |
err(1, "%s", currfile); |
|
|
/* |
/* |
* getline() will read from here. Next call will truncate to |
* get_line() will read from here. Next call will truncate to |
* truncofs in this file. |
* truncofs in this file. |
*/ |
*/ |
overfile = ofp; |
overfile = ofp; |
|
|
|
|
/* Read and output lines until we get a match. */ |
/* Read and output lines until we get a match. */ |
first = 1; |
first = 1; |
while ((p = getline()) != NULL) { |
while ((p = get_line()) != NULL) { |
if (fputs(p, ofp) != 0) |
if (fputs(p, ofp) != 0) |
break; |
break; |
if (!first && regexec(&cre, p, 0, NULL, 0) == 0) |
if (!first && regexec(&cre, p, 0, NULL, 0) == 0) |
|
|
* Positive offset: copy the requested number of lines |
* Positive offset: copy the requested number of lines |
* after the match. |
* after the match. |
*/ |
*/ |
while (--ofs > 0 && (p = getline()) != NULL) |
while (--ofs > 0 && (p = get_line()) != NULL) |
fputs(p, ofp); |
fputs(p, ofp); |
toomuch(NULL, 0); |
toomuch(NULL, 0); |
nwritten = (intmax_t)ftello(ofp); |
nwritten = (intmax_t)ftello(ofp); |
|
|
while (nfiles < maxfiles - 1) { |
while (nfiles < maxfiles - 1) { |
ofp = newfile(); |
ofp = newfile(); |
while (lineno + 1 != lastline) { |
while (lineno + 1 != lastline) { |
if ((p = getline()) == NULL) |
if ((p = get_line()) == NULL) |
errx(1, "%ld: out of range", lastline); |
errx(1, "%ld: out of range", lastline); |
if (fputs(p, ofp) != 0) |
if (fputs(p, ofp) != 0) |
break; |
break; |