4
* This file contains routines to interact with the system (as in the C
5
* `system' command), and related I/O
13
static char *NewStringCopy(char *old)
15
* RETURNS: newly allocates string containing copy of string old
16
* NOTE: old is not modified.
22
new = malloc(sizeof(char)*(strlen(old)+1));
28
static char *NewAppendedString0(char *old, char *app)
30
* RETURNS: string holding : old + app
31
* NOTE: frees old string after copy
37
new = malloc(sizeof(char)*(strlen(app)+1));
43
new = malloc(sizeof(char)*(strlen(old) + strlen(app)+1));
52
static char *NewAppendedString(char *old, char *app)
54
* RETURNS: string holding : old + " " + app
55
* NOTE: frees old string after copy
62
new = malloc(sizeof(char)*(strlen(app)+1));
68
new = malloc(sizeof(char)*(strlen(old) + strlen(app)+2));
78
static char *NewAppendedStrings(char *old, char *app0, char *app1)
80
* RETURNS: string holding : old + " " + app0 + " " + app1
81
* NOTE: frees old string after copy
89
len = strlen(app0) + strlen(app1) + 2;
92
new = malloc(sizeof(char)*len);
94
sprintf(new, "%s %s", app0, app1);
98
len += strlen(old) + 1;
99
new = malloc(sizeof(char)*len);
101
sprintf(new, "%s %s %s", old, app0, app1);
107
static char *ATL_fgets(char *sout, int *plen, FILE *fpin)
109
* This routine returns a pointer to a single line of of file fpin.
110
* If the plen-length string sout is long enough to hold the file's line,
111
* then sout will be the return value. Otherwise sout will be freed and
112
* a new string will be returned.
113
* Upon EOF/error: sout is de-allocated, *len=0, & NULL is returned;
114
* *len is the length of sout on input, and of the returned string on output.
118
if (!sout || len < 1)
121
sout = malloc(len*sizeof(char));
125
* See if there is a line left in file
127
if (fgets(sout, len, fpin))
130
for (i=0; sout[i]; i++);
132
if (sout[i-1] == '\n') /* if this is complete line */
133
return(sout); /* we are done, return it */
135
* Continue doubling string length until we can fit the whole string
137
while (sout[i-1] != '\n')
142
*plen = (len += len);
143
sp = malloc(len*sizeof(char));
149
if (!fgets(sp, len0, fpin))
151
for (; sout[i]; i++);
164
static char *ATL_fgets_CWS(char *sout, int *plen, FILE *fpin)
166
* This routine returns a pointer to a single line of of file fpin.
167
* It then compresses the whitespace in the line for ease of parsing:
168
* (1) The first character in the line is non-whitespace
169
* (2) The last character in the line is non-whitespace
170
* (3) Any whitespace string of 1 or more ws chars is replaced with 1 ' '
171
* (4) If the entire line is whitespace, get another until EOF or non-ws
172
* If the size-len string sout is long enough to hold the file's line,
173
* then sout will be the return value. Otherwise sout will be freed and
174
* a new string will be returned.
175
* Upon EOF/error: sout is de-allocated, *len=0, & NULL is returned;
176
* *len is the length of sout in input, and of the returned string on output.
182
* Find the end of any preceding whitespace line; if the whole line is
183
* whitespace, keep getting lines until we've got one with some non-ws chars
187
sout = ATL_fgets(sout, plen, fpin);
190
for (i=0; isspace(sout[i]); i++);
192
while (sout[i] == '\0');
194
* Now, go through line, replacing all whitespace with single ' '
196
for (sp=sout+i,j=0; sp[j]; j++)
201
while (isspace(sp[j])) sp++;
208
* Shave off any trailing ws (can only be one due to above)
210
if (isspace(sout[j-1]))
217
static char *ATL_tmpnam(void)
219
static char tnam[L_tmpnam];
220
static char FirstTime=1;
224
assert(tmpnam(tnam));
229
static FILE *atlsys(char *targ, char *cmnd, int verb, int IgnoreErr)
231
* Executes command cmnd, returns open ("r" mode) file stream to output of
232
* command. If IgnoreErr is 0, then return NULL on error.
242
i = strlen(targ) + strlen(cmnd) + strlen(tnam) + 24;
243
sp = malloc(i*sizeof(char));
245
sprintf(sp, "ssh %s \"%s\" > %s 2>&1 \n", targ, cmnd, tnam);
249
i = strlen(cmnd) + strlen(tnam) + 16;
250
sp = malloc(i*sizeof(char));
252
sprintf(sp, "%s > %s 2>&1\n", cmnd, tnam);
257
fprintf(stderr, "\nierr=%d in command='%s'!\n\n", i, cmnd);
260
fprintf(stderr, "OUTPUT:\n=======\n");
261
sprintf(sp, "cat %s", tnam);
267
output = fopen(tnam, "r");
271
static char *atlsys_1L(char *targ, char *cmnd, int verb, int CWS)
273
* Executes system(cmnd), returns 1st line as allocated string. Returns NULL
281
fp = atlsys(targ, cmnd, verb, 0);
285
ln = ATL_fgets_CWS(ln, &len, fp);
287
ln = ATL_fgets(ln, &len, fp);