File: atlas_sys.h

package info (click to toggle)
atlas 3.10.3-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 38,308 kB
  • sloc: ansic: 486,789; fortran: 66,209; asm: 7,267; makefile: 1,466; sh: 604
file content (292 lines) | stat: -rw-r--r-- 6,677 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef ATLAS_SYS_H
   #define ATLAS_SYS_H
/*
 * This file contains routines to interact with the system (as in the C
 * `system' command), and related I/O
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

static char *NewStringCopy(char *old)
/*
 * RETURNS: newly allocates string containing copy of string old
 * NOTE: old is not modified.
 */
{
   char *new;
   if (!old)
      return(NULL);
   new = malloc(sizeof(char)*(strlen(old)+1));
   assert(new);
   strcpy(new, old);
   return(new);
}

static char *NewAppendedString0(char *old, char *app)
/*
 * RETURNS: string holding : old + app
 * NOTE: frees old string after copy
 */
{
   char *new;
   if (!old)
   {
      new = malloc(sizeof(char)*(strlen(app)+1));
      assert(new);
      strcpy(new, app);
   }
   else
   {
      new = malloc(sizeof(char)*(strlen(old) + strlen(app)+1));
      assert(new);
      strcpy(new, old);
      strcat(new, app);
      free(old);
   }
   return(new);
}

static char *NewAppendedString(char *old, char *app)
/*
 * RETURNS: string holding : old + " " + app
 * NOTE: frees old string after copy
 */

{
   char *new;
   if (!old)
   {
      new = malloc(sizeof(char)*(strlen(app)+1));
      assert(new);
      strcpy(new, app);
   }
   else
   {
      new = malloc(sizeof(char)*(strlen(old) + strlen(app)+2));
      assert(new);
      strcpy(new, old);
      strcat(new, " ");
      strcat(new, app);
      free(old);
   }
   return(new);
}

static char *NewAppendedStrings(char *old, char *app0, char *app1)
/*
 * RETURNS: string holding : old + " " + app0 + " " + app1
 * NOTE: frees old string after copy
 */

{
   char *new;
   int len;

   assert(app0 && app1);
   len = strlen(app0) + strlen(app1) + 2;
   if (!old)
   {
      new = malloc(sizeof(char)*len);
      assert(new);
      sprintf(new, "%s %s", app0, app1);
   }
   else
   {
      len += strlen(old) + 1;
      new = malloc(sizeof(char)*len);
      assert(new);
      sprintf(new, "%s %s %s", old, app0, app1);
      free(old);
   }
   return(new);
}

static char *ATL_fgets(char *sout, int *plen, FILE *fpin)
/*
 * This routine returns a pointer to a single line of of file fpin.
 * If the plen-length string sout is long enough to hold the file's line,
 * then sout will be the return value.  Otherwise sout will be freed and
 * a new string will be returned.
 * Upon EOF/error: sout is de-allocated, *len=0, & NULL is returned;
 * *len is the length of sout on input, and of the returned string on output.
 */
{
   int len = *plen;
   if (!sout || len < 1)
   {
      *plen = len = 128;
      sout = malloc(len*sizeof(char));
      assert(sout);
   }
/*
 * See if there is a line left in file
 */
   if (fgets(sout, len, fpin))
   {
      int i;
      for (i=0; sout[i]; i++);
      assert(i > 0);
      if (sout[i-1] == '\n')    /* if this is complete line */
         return(sout);          /* we are done, return it */
/*
 *    Continue doubling string length until we can fit the whole string
 */
      while (sout[i-1] != '\n')
      {
         char *sp;
         int len0 = len;

         *plen = (len += len);
         sp = malloc(len*sizeof(char));
         assert(sp);
         strcpy(sp, sout);
         free(sout);
         sout = sp;
         sp += i;
         if (!fgets(sp, len0, fpin))
            return(sout);
         for (; sout[i]; i++);
      }
      return(sout);

   }
   else
   {
      *plen = 0;
      free(sout);
   }
   return(NULL);
}

static char *ATL_fgets_CWS(char *sout, int *plen, FILE *fpin)
/*
 * This routine returns a pointer to a single line of of file fpin.
 * It then compresses the whitespace in the line for ease of parsing:
 * (1) The first character in the line is non-whitespace
 * (2) The last character in the line is non-whitespace
 * (3) Any whitespace string of 1 or more ws chars is replaced with 1 ' '
 * (4) If the entire line is whitespace, get another until EOF or non-ws
 * If the size-len string sout is long enough to hold the file's line,
 * then sout will be the return value.  Otherwise sout will be freed and
 * a new string will be returned.
 * Upon EOF/error: sout is de-allocated, *len=0, & NULL is returned;
 * *len is the length of sout in input, and of the returned string on output.
 */
{
   int i, j;
   char *sp;
/*
 * Find the end of any preceding whitespace line; if the whole line is
 * whitespace, keep getting lines until we've got one with some non-ws chars
 */
   do
   {
      sout = ATL_fgets(sout, plen, fpin);
      if (!sout)
         return(NULL);
      for (i=0; isspace(sout[i]); i++);
   }
   while (sout[i] == '\0');
/*
 * Now, go through line, replacing all whitespace with single ' '
 */
   for (sp=sout+i,j=0; sp[j]; j++)
   {
      if (isspace(sp[j]))
      {
         sout[j] = ' ';
         while (isspace(sp[j])) sp++;
         sp--;
      }
      else
         sout[j] = sp[j];
   }
/*
 * Shave off any trailing ws (can only be one due to above)
 */
   if (isspace(sout[j-1]))
      sout[j-1] = '\0';
   else
      sout[j] = '\0';
   return(sout);
}

static char *ATL_tmpnam(void)
{
   static char tnam[L_tmpnam];
   static char FirstTime=1;
   if (FirstTime)
   {
      FirstTime = 0;
      assert(tmpnam(tnam));
   }
   return(tnam);
}

static FILE *atlsys(char *targ, char *cmnd, int verb, int IgnoreErr)
/*
 * Executes command cmnd, returns open ("r" mode) file stream to output of
 * command.  If IgnoreErr is 0, then return NULL on error.
 */
{
   char *tnam, *sp;
   int i;
   FILE *output=NULL;

   tnam = ATL_tmpnam();
   if (targ)
   {
      i = strlen(targ) + strlen(cmnd) + strlen(tnam) + 24;
      sp = malloc(i*sizeof(char));
      assert(sp);
      sprintf(sp, "ssh %s \"%s\" > %s 2>&1 \n", targ, cmnd, tnam);
   }
   else
   {
      i = strlen(cmnd) + strlen(tnam) + 16;
      sp = malloc(i*sizeof(char));
      assert(sp);
      sprintf(sp, "%s > %s 2>&1\n", cmnd, tnam);
   }
   i = system(sp);
   if (i && verb)
   {
      fprintf(stderr, "\nierr=%d in command='%s'!\n\n", i, cmnd);
      if (verb > 1)
      {
         fprintf(stderr, "OUTPUT:\n=======\n");
         sprintf(sp, "cat %s", tnam);
         system(sp);
      }
   }
   free(sp);
   if (!i || IgnoreErr)
      output = fopen(tnam, "r");
   return(output);
}

static char *atlsys_1L(char *targ, char *cmnd, int verb, int CWS)
/*
 * Executes system(cmnd), returns 1st line as allocated string.  Returns NULL
 * on error.
 */
{
   FILE *fp;
   char *ln=NULL;
   int len=0;

   fp = atlsys(targ, cmnd, verb, 0);
   if (fp)
   {
      if (CWS)
         ln = ATL_fgets_CWS(ln, &len, fp);
      else
         ln = ATL_fgets(ln, &len, fp);
      fclose(fp);
   }
   return(ln);
}
#endif