]> andersk Git - moira.git/blob - gen/util.c
70be012f86c0d059d52b6b9bfe03eb558c140479
[moira.git] / gen / util.c
1 /* $Header$
2  *
3  * Utility routines used by the SMS extraction programs.
4  */
5
6
7 #include <stdio.h>
8 #include <sys/time.h>
9 #include <sms.h>
10 #include <sms_app.h>
11
12
13 /* ingres_date_and_time: passed a unix time_t, returns a string that ingres
14  * can parse to obtain that time.
15  */
16
17 static char *month_name[] = {
18     "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
19     "nov", "dec"
20   };
21
22
23 char *ingres_date(), *ingres_time();
24
25 char *ingres_date_and_time(l)
26 long l;
27 {
28         char *ans = NULL, *date, *time;
29       
30         if ((date = ingres_date(l)) && (time = ingres_time(l))) {
31                 char buf[BUFSIZ];
32                 sprintf(buf, "%s %s", date, time);
33                 ans = strsave(buf);
34         }
35         if (date)
36                 free(date);
37         if (time)
38                 free(time);
39         return ans;
40 }
41
42 char *ingres_time(t)
43         long t;
44 {
45         struct tm *tm;
46
47         if (t == (long) 0)
48                 (void) time(&t);
49
50         if ((tm = localtime(&t)) == (struct tm *) NULL) {
51                 return NULL;
52         } else {
53                 char buf[BUFSIZ];
54
55                 sprintf(buf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
56                         tm->tm_sec);
57                 return strsave(buf);
58         }
59 }
60
61 char *ingres_date(t)
62         long t;
63 {
64         struct tm *tm;
65
66         if (t == (long) 0)
67                 (void) time(&t);
68
69         if ((tm = localtime(&t)) == (struct tm *) NULL) {
70                 return NULL;
71         } else {
72                 char buf[BUFSIZ];
73
74                 sprintf(buf, "%02d-%3.3s-%04d", tm->tm_mday,
75                         month_name[tm->tm_mon], 1900 + tm->tm_year);
76                 return strsave(buf);
77         }
78 }
79
80
81 fix_file(targetfile)
82 char *targetfile;
83 {
84     char oldfile[64], filename[64];
85
86     sprintf(oldfile, "%s.old", targetfile);
87     sprintf(filename, "%s~", targetfile);
88     if (rename(targetfile, oldfile) == 0) {
89         if (rename(filename, targetfile) < 0) {
90             rename(oldfile, targetfile);
91             perror("Unable to install new file (rename failed)\n");
92             fprintf(stderr, "Filename = %s\n", targetfile);
93             exit(SMS_CCONFIG);
94         }
95     } else {
96         if (rename(filename, targetfile) < 0) {
97             perror("Unable to rename old file\n");
98             fprintf(stderr, "Filename = %s\n", targetfile);
99             exit(SMS_CCONFIG);
100         }
101     }
102     unlink(oldfile);
103 }
104
105
106 char *dequote(s)
107 register char *s;
108 {
109     char *last = s;
110
111     while (*s) {
112         if (*s == '"')
113           *s = '\'';
114         else if (*s != ' ')
115           last = s;
116         s++;
117     }
118     if (*last == ' ')
119       *last = '\0';
120     else
121       *(++last) = '\0';
122 }
This page took 0.041815 seconds and 3 git commands to generate.