File: file-sub.c

package info (click to toggle)
gcl 2.6.14-21
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 60,864 kB
  • sloc: ansic: 177,407; lisp: 151,509; asm: 128,169; sh: 22,510; cpp: 11,923; tcl: 3,181; perl: 2,930; makefile: 2,360; sed: 334; yacc: 226; lex: 95; awk: 30; fortran: 24; csh: 23
file content (71 lines) | stat: -rw-r--r-- 1,416 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*

# Substitute the region between BEGIN and END in FILE1 into FILE2

 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void scanCopyToLine(FILE *fp, char *line,FILE *outstream);

int
main(int argc,char *argv[])
{
  if (argc < 5)
    {
    ERROR:
      fprintf(stderr,"Usage: file-sub subFile FileToSubInto BEGIN END [outfile -]");
      exit(1);
    }
  {
    FILE *file1;
    FILE *file2;
    FILE *outstream = stdout;
    char *begin=argv[3];
    char *end=argv[4];
   
    file2= fopen(argv[2],"rb");
    file1= fopen(argv[1],"rb");
    if (argc>=6 && strcmp(argv[5],"-")!=0) {
      outstream= fopen(argv[5],"wb");
    }
   
    if (file1==0 || file2==0) goto ERROR;
    {
      scanCopyToLine(file2,begin,outstream);
      scanCopyToLine(file1,begin,0);
      scanCopyToLine(file1,end,outstream);
      scanCopyToLine(file2,end,0);
      scanCopyToLine(file2,0,outstream);
    }
    if (outstream != stdout) fclose(outstream);
  }

  return 0;
}




/* copy from fp to outstream all lines up to and including
   one beginning with LINE
*/   
void
scanCopyToLine(FILE *fp, char *line,FILE *outstream)
{    
  int length=0;
  int finish=0;
  char buf[5000];
  if (line) length = strlen(line);
  while (!finish && !feof(fp)) {
    char *s = fgets(buf,sizeof(buf),fp);
    if (line && s && strncmp(line,s,length)==0) {
      finish=1;
    }
    if (s && outstream)
      fputs(s,outstream);
  }

}