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