source: MondoRescue/branches/stable/monitas/from-mondo.c@ 352

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

monitas v0.1a

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