Mapper
file_age.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <math.h>
6 #include <sys/time.h>
7 
8 /* returns the age of a file in hours */
9 
10 int file_age(char *directory, char *file, int debug)
11 {
12 FILE *pf, *popen();
13 char fmt[80];
14 char filename[256];
15 char line[256];
16 char info[256];
17 int age;
18 int yr,mo,dy,hr,mn,sd;
19 float fsd;
20 long t_file,t_system;
21 
22  strcpy(filename,directory);
23  strcat(filename,"/");
24  strcat(filename,file);
25 
26  if(debug)
27  {
28  printf("dir: <%s>\n",directory);
29  printf("file: <%s>\n",file);
30  printf("both: <%s>\n",filename);
31  }
32 
33  strncpy(fmt,"%z",2);
34  *(fmt+2)='\0';
35  sprintf(line,"stat -c %s %s",fmt,filename);
36 
37  if(debug)
38  printf("cmd: <%s>\n",line);
39 
40  pf=popen(line,"r");
41  fgets(info,sizeof(info),pf);
42  if(debug)
43  printf("info: <%s>\n",info);
44  pclose(pf);
45 
46  if(strlen(info) < 10)
47  {
48  if(debug)
49  printf("File does not exist.\n");
50  age=-1;
51  }
52  else
53  {
54  sscanf(info,"%d-%d-%d %d:%d:%f",&yr,&mo,&dy,&hr,&mn,&fsd);
55  sd=(int)fsd;
56  if(debug)
57  printf("%02d/%02d/%04d %02d:%02d:%02d\n",mo,dy,yr,hr,mn,sd);
58 
59  t_file=ouptime(yr,mo,dy,hr,mn,sd);
60  if(debug)
61  printf("file_time: %d\n",t_file);
62 
63  strncpy(fmt,"'+%Y %m %d %H %M %S'",20);
64  *(fmt+20)='\0';
65  sprintf(line,"date %s",fmt);
66  if(debug)
67  printf("cmd: <%s>\n",line);
68  pf=popen(line,"r");
69  fgets(info,sizeof(info),pf);
70  if(debug)
71  printf("info: <%s>\n",info);
72  pclose(pf);
73 
74  sscanf(info,"%d %d %d %d %d %d",&yr,&mo,&dy,&hr,&mn,&sd);
75  t_system=ouptime(yr,mo,dy,hr,mn,sd);
76  if(debug)
77  printf("system_time: %d\n",t_system);
78 
79  age=(int)((t_system - t_file)/3600);
80 
81  }
82  if(debug)
83  printf("file_age is %d hrs.\n",age);
84 
85  return(age);
86 }
87 
char directory[100]
Definition: decode_HDP.c:3
int file_age(char *directory, char *file, int debug)
Definition: file_age.c:10
int debug
Definition: hydro_data.h:53
char filename[200]
Definition: is_file_closed.c:5
long ouptime(int year, int month, int day, int hour, int minute, int second)
Definition: ouptime.c:10
sprintf(fbuf,"/usr/mapper/nexrad/ngrid.%02d-%02d-%02d-%02d", year, month, day, hour)
printf("fbuf is %s\n", fbuf)
int dy