Mercurial > hg
annotate mcabber/src/utils.c @ 408:73433860665c
Tune previous usleep() change
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 27 Aug 2005 18:59:48 +0200 |
parents | f8f3c7493457 |
children | 2f9852610cf4 |
rev | line source |
---|---|
241 | 1 /* |
2 * utils.c -- Various utility functions | |
393 | 3 * |
241 | 4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
5 * ut_* functions are derived from Cabber debug/log code. | |
6 * from_iso8601() comes from the Gaim project. | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or (at | |
11 * your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
21 * USA | |
22 */ | |
23 | |
24 | 24 #include <stdio.h> |
25 #include <stdlib.h> | |
77 | 26 #include <string.h> |
24 | 27 #include <stdarg.h> |
28 #include <time.h> | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
29 #include <unistd.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
30 #include <sys/types.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
31 #include <sys/stat.h> |
24 | 32 |
241 | 33 #include <config.h> |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
34 #include "logprint.h" |
241 | 35 |
76 | 36 static int DebugEnabled; |
77 | 37 static char *FName; |
24 | 38 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
241
diff
changeset
|
39 void ut_InitDebug(unsigned int level, const char *filename) |
24 | 40 { |
77 | 41 FILE *fp; |
42 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
43 if (level < 1) { |
138 | 44 DebugEnabled = 0; |
45 FName = NULL; | |
46 return; | |
47 } | |
48 | |
49 if (filename) | |
50 FName = strdup(filename); | |
77 | 51 else { |
138 | 52 FName = getenv("HOME"); |
53 if (!FName) | |
54 FName = "/tmp/mcabberlog"; | |
55 else { | |
56 char *tmpname = malloc(strlen(FName) + 12); | |
57 strcpy(tmpname, FName); | |
58 strcat(tmpname, "/mcabberlog"); | |
59 FName = tmpname; | |
60 } | |
77 | 61 } |
24 | 62 |
63 DebugEnabled = level; | |
76 | 64 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
65 fp = fopen(FName, "a"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
66 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
67 fprintf(stderr, "ERROR: Cannot open tracelog file\n"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
68 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
69 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
70 fprintf(fp, "New trace log started.\n" |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
71 "----------------------\n"); |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
72 fchmod(fileno(fp), S_IRUSR|S_IWUSR); |
24 | 73 fclose(fp); |
74 } | |
75 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
76 void ut_WriteLog(unsigned int flag, const char *data) |
24 | 77 { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
78 if (!DebugEnabled || !FName) return; |
393 | 79 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
80 if (((DebugEnabled == 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) || |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
81 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
82 FILE *fp = fopen(FName, "a+"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
83 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
84 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
85 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
86 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
87 fputs(data, fp); |
24 | 88 fclose(fp); |
89 } | |
90 } | |
91 | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
92 // checkset_perm(name, setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
93 // Check the permissions of the "name" file/dir |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
94 // If setmode is true, correct the permissions if they are wrong |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
95 // Return values: -1 == bad file/dir, 0 == success, 1 == cannot correct |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
96 int checkset_perm(const char *name, unsigned int setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
97 { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
98 int fd; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
99 struct stat buf; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
100 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
101 fd = lstat(name, &buf); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
102 if (fd == -1) return -1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
103 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
104 if (buf.st_uid != geteuid()) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
105 scr_LogPrint(LPRINT_LOGNORM, "Wrong file owner [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
106 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
107 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
108 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
109 if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP) || |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 buf.st_mode & (S_IROTH | S_IWOTH | S_IXOTH)) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 if (setmode) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
112 mode_t newmode = 0; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
113 scr_LogPrint(LPRINT_LOGNORM, "Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
114 if (S_ISDIR(buf.st_mode)) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
115 newmode |= S_IXUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
116 newmode |= S_IRUSR | S_IWUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
117 if (chmod(name, newmode)) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
118 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Failed to correct permissions!"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
119 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
120 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
121 scr_LogPrint(LPRINT_LOGNORM, "Permissions have been corrected"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
122 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
123 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
124 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
125 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
126 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
127 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
128 return 0; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
129 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
130 |
241 | 131 // to_iso8601(dststr, timestamp) |
132 // Convert timestamp to iso8601 format, and store it in dststr. | |
133 // NOTE: dststr should be at last 19 chars long. | |
134 // Return should be 0 | |
135 int to_iso8601(char *dststr, time_t timestamp) | |
136 { | |
137 struct tm *tm_time; | |
138 int ret; | |
139 | |
140 tm_time = gmtime(×tamp); | |
141 | |
142 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", | |
143 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, | |
144 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); | |
145 | |
146 return ((ret == -1) ? -1 : 0); | |
147 } | |
148 | |
149 // from_iso8601(timestamp, utc) | |
150 // This function comes from the Gaim project, gaim_str_to_time(). | |
151 // (Actually date may not be pure iso-8601) | |
152 // Thanks, guys! | |
153 time_t from_iso8601(const char *timestamp, int utc) | |
154 { | |
155 struct tm t; | |
156 time_t retval = 0; | |
157 char buf[32]; | |
158 char *c; | |
159 int tzoff = 0; | |
160 | |
161 time(&retval); | |
162 localtime_r(&retval, &t); | |
163 | |
164 snprintf(buf, sizeof(buf), "%s", timestamp); | |
165 c = buf; | |
166 | |
167 /* 4 digit year */ | |
168 if (!sscanf(c, "%04d", &t.tm_year)) return 0; | |
169 c+=4; | |
170 if (*c == '-') | |
171 c++; | |
172 | |
173 t.tm_year -= 1900; | |
174 | |
175 /* 2 digit month */ | |
176 if (!sscanf(c, "%02d", &t.tm_mon)) return 0; | |
177 c+=2; | |
178 if (*c == '-') | |
179 c++; | |
180 | |
181 t.tm_mon -= 1; | |
182 | |
183 /* 2 digit day */ | |
184 if (!sscanf(c, "%02d", &t.tm_mday)) return 0; | |
185 c+=2; | |
186 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ | |
187 c++; /* skip the "T" */ | |
188 | |
189 /* 2 digit hour */ | |
190 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || | |
191 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { | |
192 int tzhrs, tzmins; | |
193 c+=8; | |
194 if (*c == '.') /* dealing with precision we don't care about */ | |
195 c += 4; | |
196 | |
197 if ((*c == '+' || *c == '-') && | |
198 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
199 tzoff = tzhrs*60*60 + tzmins*60; | |
200 if (*c == '+') | |
201 tzoff *= -1; | |
202 } | |
203 | |
204 if (tzoff || utc) { | |
205 | |
206 //#ifdef HAVE_TM_GMTOFF | |
207 tzoff += t.tm_gmtoff; | |
208 //#else | |
209 //# ifdef HAVE_TIMEZONE | |
210 // tzset(); /* making sure */ | |
211 // tzoff -= timezone; | |
212 //# endif | |
213 //#endif | |
214 } | |
215 } | |
216 } | |
217 | |
218 t.tm_isdst = -1; | |
219 | |
220 retval = mktime(&t); | |
221 | |
222 retval += tzoff; | |
223 | |
224 return retval; | |
225 } |