author | Mikael Berthe <mikael@lilotux.net> |
Sun, 24 Jul 2005 14:37:27 +0100 | |
changeset 362 | d8f147d6e872 |
parent 281 | f562b9af2de7 |
child 364 | 33b8e801ffa6 |
permissions | -rw-r--r-- |
241 | 1 |
/* |
2 |
* utils.c -- Various utility functions |
|
3 |
* |
|
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> |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
34 |
#include "screen.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 |
||
138 | 43 |
if (!level) { |
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 |
|
77 | 65 |
fp = fopen(FName, "w"); |
76 | 66 |
if (!fp) return; |
24 | 67 |
fprintf(fp, "Debugging mode started...\n" |
68 |
"-----------------------------------\n"); |
|
69 |
fclose(fp); |
|
70 |
} |
|
71 |
||
72 |
void ut_WriteLog(const char *fmt, ...) |
|
73 |
{ |
|
74 |
FILE *fp = NULL; |
|
75 |
time_t ahora; |
|
76 |
va_list ap; |
|
77 |
char *buffer = NULL; |
|
78 |
||
138 | 79 |
if (DebugEnabled && FName) { |
77 | 80 |
fp = fopen(FName, "a+"); |
76 | 81 |
if (!fp) return; |
24 | 82 |
buffer = (char *) calloc(1, 64); |
83 |
||
84 |
ahora = time(NULL); |
|
85 |
strftime(buffer, 64, "[%H:%M:%S] ", localtime(&ahora)); |
|
86 |
fprintf(fp, "%s", buffer); |
|
87 |
||
88 |
va_start(ap, fmt); |
|
89 |
vfprintf(fp, fmt, ap); |
|
90 |
va_end(ap); |
|
91 |
||
92 |
free(buffer); |
|
93 |
fclose(fp); |
|
94 |
} |
|
95 |
} |
|
96 |
||
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
97 |
// checkset_perm(name, setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
98 |
// Check the permissions of the "name" file/dir |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
99 |
// 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
|
100 |
// 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
|
101 |
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
|
102 |
{ |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
103 |
int fd; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
104 |
struct stat buf; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
105 |
|
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
106 |
fd = lstat(name, &buf); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
107 |
if (fd == -1) return -1; |
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_uid != geteuid()) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 |
scr_LogPrint("Wrong file owner [%s]", name); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 |
return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
112 |
} |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
113 |
|
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
if (setmode) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
117 |
mode_t newmode = 0; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
118 |
scr_LogPrint("Bad permissions [%s]", name); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
119 |
if (S_ISDIR(buf.st_mode)) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
120 |
newmode |= S_IXUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
121 |
newmode |= S_IRUSR | S_IWUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
122 |
if (chmod(name, newmode)) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
123 |
scr_LogPrint("WARNING: Failed to correct permissions!"); |
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 |
scr_LogPrint("Permissions have been corrected"); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
127 |
} else { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
128 |
scr_LogPrint("WARNING: Bad permissions [%s]", name); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
129 |
return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
130 |
} |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
131 |
} |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
132 |
|
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
133 |
return 0; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
134 |
} |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
135 |
|
241 | 136 |
// to_iso8601(dststr, timestamp) |
137 |
// Convert timestamp to iso8601 format, and store it in dststr. |
|
138 |
// NOTE: dststr should be at last 19 chars long. |
|
139 |
// Return should be 0 |
|
140 |
int to_iso8601(char *dststr, time_t timestamp) |
|
141 |
{ |
|
142 |
struct tm *tm_time; |
|
143 |
int ret; |
|
144 |
||
145 |
tm_time = gmtime(×tamp); |
|
146 |
||
147 |
ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", |
|
148 |
1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, |
|
149 |
tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); |
|
150 |
||
151 |
return ((ret == -1) ? -1 : 0); |
|
152 |
} |
|
153 |
||
154 |
// from_iso8601(timestamp, utc) |
|
155 |
// This function comes from the Gaim project, gaim_str_to_time(). |
|
156 |
// (Actually date may not be pure iso-8601) |
|
157 |
// Thanks, guys! |
|
158 |
time_t from_iso8601(const char *timestamp, int utc) |
|
159 |
{ |
|
160 |
struct tm t; |
|
161 |
time_t retval = 0; |
|
162 |
char buf[32]; |
|
163 |
char *c; |
|
164 |
int tzoff = 0; |
|
165 |
||
166 |
time(&retval); |
|
167 |
localtime_r(&retval, &t); |
|
168 |
||
169 |
snprintf(buf, sizeof(buf), "%s", timestamp); |
|
170 |
c = buf; |
|
171 |
||
172 |
/* 4 digit year */ |
|
173 |
if (!sscanf(c, "%04d", &t.tm_year)) return 0; |
|
174 |
c+=4; |
|
175 |
if (*c == '-') |
|
176 |
c++; |
|
177 |
||
178 |
t.tm_year -= 1900; |
|
179 |
||
180 |
/* 2 digit month */ |
|
181 |
if (!sscanf(c, "%02d", &t.tm_mon)) return 0; |
|
182 |
c+=2; |
|
183 |
if (*c == '-') |
|
184 |
c++; |
|
185 |
||
186 |
t.tm_mon -= 1; |
|
187 |
||
188 |
/* 2 digit day */ |
|
189 |
if (!sscanf(c, "%02d", &t.tm_mday)) return 0; |
|
190 |
c+=2; |
|
191 |
if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ |
|
192 |
c++; /* skip the "T" */ |
|
193 |
||
194 |
/* 2 digit hour */ |
|
195 |
if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || |
|
196 |
sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { |
|
197 |
int tzhrs, tzmins; |
|
198 |
c+=8; |
|
199 |
if (*c == '.') /* dealing with precision we don't care about */ |
|
200 |
c += 4; |
|
201 |
||
202 |
if ((*c == '+' || *c == '-') && |
|
203 |
sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { |
|
204 |
tzoff = tzhrs*60*60 + tzmins*60; |
|
205 |
if (*c == '+') |
|
206 |
tzoff *= -1; |
|
207 |
} |
|
208 |
||
209 |
if (tzoff || utc) { |
|
210 |
||
211 |
//#ifdef HAVE_TM_GMTOFF |
|
212 |
tzoff += t.tm_gmtoff; |
|
213 |
//#else |
|
214 |
//# ifdef HAVE_TIMEZONE |
|
215 |
// tzset(); /* making sure */ |
|
216 |
// tzoff -= timezone; |
|
217 |
//# endif |
|
218 |
//#endif |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
t.tm_isdst = -1; |
|
224 |
||
225 |
retval = mktime(&t); |
|
226 |
||
227 |
retval += tzoff; |
|
228 |
||
229 |
return retval; |
|
230 |
} |