[352] | 1 | /* from-mondo.c
|
---|
| 2 |
|
---|
| 3 | - subroutines copied from Mondo
|
---|
| 4 |
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #include "structs.h"
|
---|
[1168] | 8 | #include "mr_str.h"
|
---|
[352] | 9 |
|
---|
| 10 |
|
---|
[353] | 11 | //extern void log_it_SUB(char*, t_loglevel, char*);
|
---|
[352] | 12 | char *call_program_and_get_last_line_of_output(char*);
|
---|
| 13 | int call_program_and_log_output (char *);
|
---|
| 14 | bool does_file_exist (char *);
|
---|
| 15 | int make_hole_for_file (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 |
|
---|
| 29 | char *
|
---|
| 30 | call_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 | }
|
---|
[1168] | 55 | mr_strip_spaces (result);
|
---|
[352] | 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 | *************************************************************************/
|
---|
| 71 | int
|
---|
| 72 | call_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 | */
|
---|
[1168] | 126 | mr_strip_spaces (incoming);
|
---|
[352] | 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 | *************************************************************************/
|
---|
| 152 | bool
|
---|
| 153 | does_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 | *************************************************************************/
|
---|
| 185 | int
|
---|
| 186 | make_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 |
|
---|