]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/ctm/ctm/ctm_ed.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / ctm / ctm / ctm_ed.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  *
11  */
12
13 #include "ctm.h"
14
15 int
16 ctm_edit(u_char *script, int length, char *filein, char *fileout)
17 {
18     u_char *ep, cmd;
19     int ln, ln2, iln, ret=0, c;
20     FILE *fi=0,*fo=0;
21
22     fi = fopen(filein,"r");
23     if(!fi) {
24         warn("%s", filein);
25         return 8;
26     }
27
28     fo = fopen(fileout,"w");
29     if(!fo) {
30         warn("%s", fileout);
31         fclose(fi);
32         return 4;
33     }
34     iln = 1;
35     for(ep=script;ep < script+length;) {
36         cmd = *ep++;
37         if(cmd != 'a' && cmd != 'd') { ret = 1; goto bye; }
38         ln = 0;
39         while(isdigit(*ep)) {
40             ln *= 10;
41             ln += (*ep++ - '0');
42         }
43         if(*ep++ != ' ') { ret = 1; goto bye; }
44         ln2 = 0;
45         while(isdigit(*ep)) {
46             ln2 *= 10;
47             ln2 += (*ep++ - '0');
48         }
49         if(*ep++ != '\n') { ret = 1; goto bye; }
50
51
52         if(cmd == 'd') {
53             while(iln < ln) {
54                 c = getc(fi);
55                 if(c == EOF) { ret = 1; goto bye; }
56                 putc(c,fo);
57                 if(c == '\n')
58                     iln++;
59             }
60             while(ln2) {
61                 c = getc(fi);
62                 if(c == EOF) { ret = 1; goto bye; }
63                 if(c != '\n')
64                     continue;
65                 ln2--;
66                 iln++;
67             }
68             continue;
69         }
70         if(cmd == 'a') {
71             while(iln <= ln) {
72                 c = getc(fi);
73                 if(c == EOF) { ret = 1; goto bye; }
74                 putc(c,fo);
75                 if(c == '\n')
76                     iln++;
77             }
78             while(ln2) {
79                 c = *ep++;
80                 putc(c,fo);
81                 if(c != '\n')
82                     continue;
83                 ln2--;
84             }
85             continue;
86         }
87         ret = 1;
88         goto bye;
89     }
90     while(1) {
91         c = getc(fi);
92         if(c == EOF) break;
93         putc(c,fo);
94     }
95     ret = 0;
96 bye:
97     if(fi) {
98         if(fclose(fi) != 0) {
99             warn("%s", filein);
100             ret = 1;
101         }
102     }
103     if(fo) {
104         if(fflush(fo) != 0) {
105             warn("%s", fileout);
106             ret = 1;
107         }
108         if(fclose(fo) != 0) {
109             warn("%s", fileout);
110             ret = 1;
111         }
112     }
113     return ret;
114 }