source: MondoRescue/branches/2.0.7/monitas/from-mondo.c@ 2183

Last change on this file since 2183 was 353, checked in by bcornec, 18 years ago

monitas latest version

File size: 8.9 KB
Line 
1/* from-mondo.c
2
3- subroutines copied from Mondo
4
5*/
6
7#include "structs.h"
8
9
10//extern void log_it_SUB(char*, t_loglevel, char*);
11char *call_program_and_get_last_line_of_output(char*);
12int call_program_and_log_output (char *);
13bool does_file_exist (char *);
14int make_hole_for_file (char *);
15void strip_spaces(char*);
16
17
18
19
20
21/*************************************************************************
22 * *call_program_and_get_last_line_of_ouput() -- Hugo Rabson *
23 * *
24 * Purpose: *
25 * Called by: *
26 * Returns: *
27 *************************************************************************/
28
29char *
30call_program_and_get_last_line_of_output (char *call)
31{
32 /** buffers ******************************************************/
33 static char result[MAX_STR_LEN+1];
34 char tmp[MAX_STR_LEN+1];
35
36 /** pointers *****************************************************/
37 FILE *fin;
38
39 /** initialize data **********************************************/
40 result[0] = '\0';
41
42 /***********************************************************************/
43 if ((fin = popen (call, "r")))
44 {
45 for (fgets (tmp, MAX_STR_LEN, fin); !feof (fin);
46 fgets (tmp, MAX_STR_LEN, fin))
47 {
48 if (strlen (tmp) > 1)
49 {
50 strcpy (result, tmp);
51 }
52 }
53 pclose (fin);
54 }
55 strip_spaces (result);
56 return (result);
57}
58
59
60
61
62
63
64/*************************************************************************
65 * call_program_and_log_output() -- Hugo Rabson *
66 * *
67 * Purpose: *
68 * Called by: *
69 * Returns: *
70 *************************************************************************/
71int
72call_program_and_log_output (char *program)
73{
74 /** buffer *******************************************************/
75 char callstr[MAX_STR_LEN+1];
76 char incoming[MAX_STR_LEN+1];
77 char tmp[MAX_STR_LEN+1];
78
79 /** int **********************************************************/
80 int res;
81 int i;
82 int len;
83
84 /** pointers ****************************************************/
85 FILE *fin;
86 char *p;
87
88 /** end vars ****************************************************/
89 sprintf (callstr, "%s > /tmp/mondo-run-prog.tmp 2> /tmp/mondo-run-prog.err",
90 program);
91 while ((p = strchr (callstr, '\r')))
92 {
93 *p = ' ';
94 }
95 while ((p = strchr (callstr, '\n')))
96 {
97 *p = ' ';
98 } /* single '=' is intentional */
99 res = system (callstr);
100 len = strlen (program);
101 for (i = 0; i < 35 - len / 2; i++)
102 {
103 tmp[i] = '-';
104 }
105 tmp[i] = '\0';
106 strcat (tmp, " ");
107 strcat (tmp, program);
108 strcat (tmp, " ");
109 for (i = 0; i < 35 - len / 2; i++)
110 {
111 strcat (tmp, "-");
112 }
113 log_it (debug, tmp);
114 system ("cat /tmp/mondo-run-prog.err >> /tmp/mondo-run-prog.tmp");
115 unlink ("/tmp/mondo-run-prog.err");
116 fin = fopen ("/tmp/mondo-run-prog.tmp", "r");
117 if (fin)
118 {
119 for (fgets (incoming, MAX_STR_LEN, fin); !feof (fin);
120 fgets (incoming, MAX_STR_LEN, fin))
121 {
122/* for(i=0; incoming[i]!='\0'; i++);
123 for(; i>0 && incoming[i-1]<=32; i--);
124 incoming[i]='\0';
125*/
126 strip_spaces (incoming);
127 log_it (debug, incoming);
128 }
129 fclose (fin);
130 }
131 log_it
132 (debug, "--------------------------------end of output------------------------------");
133 if (res)
134 {
135 log_it (debug, "...ran with errors.");
136 }
137 /* else { log_it(debug, "...ran just fine. :-)"); } */
138 return (res);
139}
140
141
142
143
144
145/*************************************************************************
146 * does_file_exist() -- Hugo Rabson *
147 * *
148 * Purpose: *
149 * Called by: *
150 * Returns: *
151 *************************************************************************/
152bool
153does_file_exist (char *filename)
154{
155
156 /** structures ***************************************************/
157 struct stat buf;
158 /*****************************************************************/
159
160 if (lstat (filename, &buf))
161 {
162 return (false);
163 }
164 else
165 {
166 return (true);
167 }
168}
169
170
171
172
173
174/*-----------------------------------------------------------*/
175
176
177
178/*************************************************************************
179 * make_hole_for_file() -- Hugo Rabson *
180 * *
181 * Purpose: *
182 * Called by: *
183 * Returns: *
184 *************************************************************************/
185int
186make_hole_for_file (char *outfile_fname)
187{
188 /** buffer *******************************************************/
189 char command[MAX_STR_LEN+1];
190
191 /** int *********************************************************/
192 int res = 0;
193
194 /** end vars ****************************************************/
195
196 sprintf (command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
197 res += system (command);
198 sprintf (command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
199 res += system (command);
200 sprintf (command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
201 res += system (command);
202 unlink (outfile_fname);
203 return (0);
204}
205
206
207
208
209
210
211
212
213
214/*************************************************************************
215 * strip_spaces() -- Hugo Rabson *
216 * rewitten Stefan Huebner *
217 * *
218 * Purpose: remove leading whitespaces and control characters, replace *
219 * TABs by SPACES, remove previous char when followed with BS,*
220 * skip control chars, terminate string with '\0' when CR or *
221 * NL is detected *
222 * afterwards remove trailing whitespace *
223 * Called by: *
224 * Returns: *
225 *************************************************************************/
226void
227strip_spaces (char *in_out)
228{
229 char *r, *w;
230 r = w = in_out; // initialize read and write pointer
231
232 while ( *r && *r <= ' ' )
233 ++r; // skip leading whitespace and control chars
234
235 for ( ; *r ; ++r ) // until end of existing string do:
236 {
237 if (*r == '\t') // TAB
238 { *w++ = ' '; // write SPACE instead
239 continue; }
240 if (*r == (char) 8) // BACKSPACE
241 { if (w > in_out) --w; // remove previous character (if any ;-)
242 continue; }
243 if (*r == '\n' || *r == '\r') // CR and LF
244 break; // terminate copying
245 if (*r < ' ') // Control chars
246 continue; // are ignored (skipped)
247 *w++ = *r; // all other chars are copied
248 }
249 // all characters are copied
250 *w = '\0'; // terminate the copied string
251 for (--w; w>in_out && *w == ' '; --w)
252 *w = '\0'; // remove trailing whitespaces
253 return;
254 /** buffers ******************************************************/
255// char tmp[MAX_STR_LEN+1];
256
257 /** pointers *****************************************************/
258// char *p;
259
260 /** int *********************************************************/
261// int i;
262
263 /** end vars ****************************************************/
264/* for (i = 0; in_out[i] <= ' ' && i < strlen (in_out); i++);
265 strcpy (tmp, in_out + i);
266 for (i = strlen (tmp); i>0 && tmp[i - 1] <= 32; i--);
267 tmp[i] = '\0';
268 for (i = 0; i < 80; i++)
269 {
270 in_out[i] = ' ';
271 }
272 in_out[i] = '\0';
273 i = 0;
274 p = tmp;
275 while (*p != '\0')
276 {
277 in_out[i] = *(p++);
278 in_out[i + 1] = '\0';
279 if (in_out[i] < 32 && i > 0)
280 {
281 if (in_out[i] == 8)
282 {
283 i--;
284 }
285 else if (in_out[i] == 9)
286 {
287 in_out[i++] = ' ';
288 }
289 else if (in_out[i] == '\t')
290 {
291 for (i++; i % 5; i++);
292 }
293 else if (in_out[i] >= 10 && in_out[i] <= 13)
294 {
295 break;
296 }
297 else
298 {
299 i--;
300 }
301 }
302 else
303 {
304 i++;
305 }
306 }
307 in_out[i] = '\0';
308*/
309/* for(i=strlen(in_out); i>0 && in_out[i-1]<=32; i--) {in_out[i-1]='\0';} */
310}
311
312
313
314
315
316
317
318
319
320
321
Note: See TracBrowser for help on using the repository browser.