|
|
1.1 root 1: /* delete.c: given a filename, the number of lines before the entry
2: * to delete, and the number of lines in the entry, delete the
3: * entry by copying the lines before the entry to a temp file,
4: * skipping the lines the entry we wish to delete occuppies,
5: * then copying the remaining lines to the temp file. Delete
6: * the old file and link the temp file to the newly modified file.
7: * A similar scheme is used to add a modified entry.
8: */
9:
10: #include "uuinstall.h"
11:
12: delete_entry(fname,before,during,insert_flag)
13: char * fname; /* file name to work with */
14: int before, during;
15: int insert_flag; /* insert a modified entry? */
16: {
17: FILE *filefd;
18: FILE *tmpfd;
19: FILE *newfd;
20: struct termio thisterm;
21: int x;
22: char *env;
23: char workstring[88];
24: char syscmd[65]; /* used to build system command to edit */
25: char b;
26:
27: extern char * getenv();
28:
29: wclear(portwin);
30:
31: /* open file for reading */
32: if ((filefd = fopen(fname,"r")) == NULL){
33: mvwaddstr(portwin,12,29,"Error opening file for reading!");
34: wrefresh(portwin);
35: sleep(1);
36: wclear(portwin);
37: wrefresh(portwin);
38: return;
39: }
40:
41: /* open edit file for writing */
42: if(insert_flag){
43: if ((newfd = fopen(UUEDIT,"w")) == NULL){
44: mvwaddstr(portwin,12,29,"Error opening file for reading!");
45: wrefresh(portwin);
46: sleep(1);
47: wclear(portwin);
48: wrefresh(portwin);
49: fclose(filefd);
50: return;
51: }
52: }
53: /* open tmp file for writing */
54: if ((tmpfd = fopen(UUTMP,"w")) == NULL){
55: mvwaddstr(portwin,12,29,"Error opening file for writing!");
56: wrefresh(portwin);
57: sleep(1);
58: wclear(portwin);
59: wrefresh(portwin);
60: fclose(filefd);
61: fclose(tmpfd);
62: return;
63: }
64:
65: wmove(portwin,12,0);
66: wclrtoeol(portwin);
67: if(!insert_flag){
68: mvwaddstr(portwin,12,29,"Updating...");
69: }else{
70: mvwaddstr(portwin,12,29,"Standby ...");
71: }
72: wrefresh(portwin);
73:
74: /* skip the lines we don't want to delete */
75:
76: for(x = 0 ; x < before ; x++){
77: fgets(workstring, sizeof(workstring) -1, filefd);
78: fputs(workstring, tmpfd);
79: fflush(tmpfd);
80: }
81:
82: /* now read the entry to delete, but don't copy it to the tmp file...
83: * UNLESS we're going to edit an entry, in which case, we write the
84: * indicated entry to another temp file and then invoke an editor on it.
85: */
86:
87: during++;
88:
89: for(x = 0; x < during ; x++){
90: fgets(workstring, sizeof(workstring) -1, filefd);
91:
92: /* write to edit file */
93: if(insert_flag){
94: fputs(workstring,newfd);
95: }
96: }
97:
98: strcpy(workstring,"");
99:
100: /* if edit, get the EDITOR environment variable. If there is none, use emacs */
101:
102: if (insert_flag){
103: fputs("\n",newfd);
104: fclose(newfd);
105:
106: /* build system command to edit temp editfile */
107: if( (env = getenv("EDITOR")) != NULL){
108: sprintf(syscmd,"%s %s",env, UUEDIT);
109: }else{
110: sprintf(syscmd,"%s %s","/usr/bin/me", UUEDIT);
111: }
112:
113: /* save our current terminal characteristics. I can't
114: * ass/u/me that the editor will return us to our
115: * current state when it exits.
116: */
117: ioctl(fileno(stdin), TCGETA, &thisterm);
118:
119: /* invoke editor */
120: system(syscmd);
121:
122: /* now restore our saved information */
123: ioctl(fileno(stdin), TCSETA, &thisterm);
124:
125: /* if we editted an entry, we need to insert the tmp editfile now.
126: * Prompt to really save changes, re-open the edit file and
127: * copy its contents to the temp file.
128: */
129:
130: wclear(portwin);
131: mvwaddstr(portwin,12,23,"Do you wish to save changes? (y/n)");
132: wrefresh(portwin);
133: do{
134: b = ' ';
135: b = wgetch(portwin);
136: }
137: while ((b != 'y') && (b != 'n'));
138:
139: if(b == 'y'){
140: if((newfd = fopen(UUEDIT,"r")) == NULL){
141: wclear(portwin);
142: mvwaddstr(portwin,12,16,"Failed to open temp file to get modified entry!");
143: mvwaddstr(portwin,14,20,"Leaving all files in original condition.");
144: wrefresh(portwin);
145: sleep(2);
146: fclose(tmpfd);
147: fclose(filefd);
148: return;
149: }
150:
151: while(fgets(workstring,sizeof(workstring) -1,newfd) != NULL){
152: fputs(workstring, tmpfd);
153: fflush(tmpfd);
154: }
155: fclose(newfd);
156: }else{
157: /* user doesn't want to save changes, so close the
158: * files we opened and delete the temp files.
159: */
160:
161: fclose(tmpfd);
162: fclose(filefd);
163: unlink(UUTMP); /* don't care if this fails, it may */
164: unlink(UUEDIT); /* prove useful to keep these files */
165: return; /* around anyways. */
166: }
167: }
168:
169: /* now copy the rest of the file */
170: while (fgets(workstring,sizeof(workstring)-1,filefd) != NULL){
171: fputs(workstring, tmpfd);
172: fflush(tmpfd);
173: }
174:
175: /* close the files */
176:
177: fclose(filefd);
178: fclose(tmpfd);
179:
180: /* now unlink the old and link in the new! */
181:
182: if (unlink(fname) == -1){
183: wclear(portwin);
184: mvwaddstr(portwin,12,28,"Error deleting old file!");
185: wmove(portwin,13,15);
186: wprintw(portwin,"Leaving old file intact. New data saved to %s.",UUTMP);
187: wrefresh(portwin);
188: sleep(2);
189: return;
190: }
191:
192: if(link(UUTMP,fname) == -1){
193: wclear(portwin);
194: wmove(portwin,12,15);
195: wprintw(portwin,"Error writing data to %s. Data is saved in %s!",fname, UUTMP);
196: wrefresh(portwin);
197: sleep(2);
198: return;
199: }
200:
201: /* We don't care about the tmp file being unlinked, as there is no
202: * code here that appends to a tmp file.
203: */
204:
205: unlink(UUTMP);
206:
207: wclear(portwin);
208: wrefresh(portwin);
209: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.