File: atlas_waitfile.c

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 (74 lines) | stat: -rw-r--r-- 1,502 bytes parent folder | download | duplicates (7)
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
#include <stdio.h>
#include <stdlib.h>

void PrintUsage(char *nam)
{
   fprintf(stderr, "\n\nUSAGE: %s -f <filename> [-s <nsec>]\n\n", nam);
   exit(-1);
}

void GetFlags(int nargs, char **args, int *nsec, char **fnam)
{
   int i;

   *nsec = 5;
   *fnam = NULL;
   for (i=1; i < nargs; i++)
   {
      if (args[i][0] != '-') PrintUsage(args[0]);
      switch(args[i][1])
      {
      case 'f':
         *fnam = args[++i];
         break;
      case 's':
         *nsec = atoi(args[++i]);
         break;
      default:
         PrintUsage(args[0]);
      }
   }
   if (*fnam == NULL) PrintUsage(args[0]);
}

int WaitForIt(int nsec, char *fnam)
/*
 * only probe for file once every 1/5 of a second to avoid beating the crap
 * out of NFS server
 */
{
   FILE *fp;
   double t0, t1, dsec=(double)nsec, dwait;
   double ATL_walltime(void);

   fp = fopen(fnam, "r");
   if (!fp)
   {
      t0 = ATL_walltime();
      do
      {
         t1 = ATL_walltime();
         while(ATL_walltime()-t1 < 0.2);
         if (ATL_walltime()-t0 > dsec) return(1);
         fp = fopen(fnam, "r");
      }
      while(!fp);
   }
   fclose(fp);
   return(0);
}

int main(int nargs, char **args)
/*
 * This routine waits at last nsecs for a file to appear; used to get
 * around NFS probs, particularly when cross-compiling
 */
{
   int nsec, i;
   char *fnam;

   GetFlags(nargs, args, &nsec, &fnam);
   i = WaitForIt(nsec, fnam);
   if (i) fprintf(stderr, "\n\nTimeout waiting for file %s!!\n\n", fnam);
   return(0);
}