1 |
|
|
/* $OpenBSD: sel_subs.c,v 1.25 2014/11/23 05:47:49 guenther Exp $ */ |
2 |
|
|
/* $NetBSD: sel_subs.c,v 1.5 1995/03/21 09:07:42 cgd Exp $ */ |
3 |
|
|
|
4 |
|
|
/*- |
5 |
|
|
* Copyright (c) 1992 Keith Muller. |
6 |
|
|
* Copyright (c) 1992, 1993 |
7 |
|
|
* The Regents of the University of California. All rights reserved. |
8 |
|
|
* |
9 |
|
|
* This code is derived from software contributed to Berkeley by |
10 |
|
|
* Keith Muller of the University of California, San Diego. |
11 |
|
|
* |
12 |
|
|
* Redistribution and use in source and binary forms, with or without |
13 |
|
|
* modification, are permitted provided that the following conditions |
14 |
|
|
* are met: |
15 |
|
|
* 1. Redistributions of source code must retain the above copyright |
16 |
|
|
* notice, this list of conditions and the following disclaimer. |
17 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
18 |
|
|
* notice, this list of conditions and the following disclaimer in the |
19 |
|
|
* documentation and/or other materials provided with the distribution. |
20 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
21 |
|
|
* may be used to endorse or promote products derived from this software |
22 |
|
|
* without specific prior written permission. |
23 |
|
|
* |
24 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
25 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
26 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
28 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
29 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
30 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
31 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
33 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 |
|
|
* SUCH DAMAGE. |
35 |
|
|
*/ |
36 |
|
|
|
37 |
|
|
#include <sys/types.h> |
38 |
|
|
#include <sys/time.h> |
39 |
|
|
#include <sys/stat.h> |
40 |
|
|
#include <ctype.h> |
41 |
|
|
#include <grp.h> |
42 |
|
|
#include <pwd.h> |
43 |
|
|
#include <stdio.h> |
44 |
|
|
#include <stdlib.h> |
45 |
|
|
#include <string.h> |
46 |
|
|
#include <unistd.h> |
47 |
|
|
#include "pax.h" |
48 |
|
|
#include "sel_subs.h" |
49 |
|
|
#include "extern.h" |
50 |
|
|
|
51 |
|
|
static int str_sec(const char *, time_t *); |
52 |
|
|
static int usr_match(ARCHD *); |
53 |
|
|
static int grp_match(ARCHD *); |
54 |
|
|
static int trng_match(ARCHD *); |
55 |
|
|
|
56 |
|
|
static TIME_RNG *trhead = NULL; /* time range list head */ |
57 |
|
|
static TIME_RNG *trtail = NULL; /* time range list tail */ |
58 |
|
|
static USRT **usrtb = NULL; /* user selection table */ |
59 |
|
|
static GRPT **grptb = NULL; /* group selection table */ |
60 |
|
|
|
61 |
|
|
/* |
62 |
|
|
* Routines for selection of archive members |
63 |
|
|
*/ |
64 |
|
|
|
65 |
|
|
/* |
66 |
|
|
* sel_chk() |
67 |
|
|
* check if this file matches a specified uid, gid or time range |
68 |
|
|
* Return: |
69 |
|
|
* 0 if this archive member should be processed, 1 if it should be skipped |
70 |
|
|
*/ |
71 |
|
|
|
72 |
|
|
int |
73 |
|
|
sel_chk(ARCHD *arcn) |
74 |
|
138 |
{ |
75 |
✗✓✗✗ ✗✓✗✗ ✗✓✗✗
|
138 |
if (((usrtb != NULL) && usr_match(arcn)) || |
76 |
|
|
((grptb != NULL) && grp_match(arcn)) || |
77 |
|
|
((trhead != NULL) && trng_match(arcn))) |
78 |
|
|
return(1); |
79 |
|
138 |
return(0); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
/* |
83 |
|
|
* User/group selection routines |
84 |
|
|
* |
85 |
|
|
* Routines to handle user selection of files based on the file uid/gid. To |
86 |
|
|
* add an entry, the user supplies either the name or the uid/gid starting with |
87 |
|
|
* a # on the command line. A \# will escape the #. |
88 |
|
|
*/ |
89 |
|
|
|
90 |
|
|
/* |
91 |
|
|
* usr_add() |
92 |
|
|
* add a user match to the user match hash table |
93 |
|
|
* Return: |
94 |
|
|
* 0 if added ok, -1 otherwise; |
95 |
|
|
*/ |
96 |
|
|
|
97 |
|
|
int |
98 |
|
|
usr_add(char *str) |
99 |
|
|
{ |
100 |
|
|
u_int indx; |
101 |
|
|
USRT *pt; |
102 |
|
|
struct passwd *pw; |
103 |
|
|
uid_t uid; |
104 |
|
|
|
105 |
|
|
/* |
106 |
|
|
* create the table if it doesn't exist |
107 |
|
|
*/ |
108 |
|
|
if ((str == NULL) || (*str == '\0')) |
109 |
|
|
return(-1); |
110 |
|
|
if ((usrtb == NULL) && |
111 |
|
|
((usrtb = calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) { |
112 |
|
|
paxwarn(1, "Unable to allocate memory for user selection table"); |
113 |
|
|
return(-1); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* |
117 |
|
|
* figure out user spec |
118 |
|
|
*/ |
119 |
|
|
if (str[0] != '#') { |
120 |
|
|
/* |
121 |
|
|
* it is a user name, \# escapes # as first char in user name |
122 |
|
|
*/ |
123 |
|
|
if ((str[0] == '\\') && (str[1] == '#')) |
124 |
|
|
++str; |
125 |
|
|
if ((pw = getpwnam(str)) == NULL) { |
126 |
|
|
paxwarn(1, "Unable to find uid for user: %s", str); |
127 |
|
|
return(-1); |
128 |
|
|
} |
129 |
|
|
uid = (uid_t)pw->pw_uid; |
130 |
|
|
} else |
131 |
|
|
uid = (uid_t)strtoul(str+1, NULL, 10); |
132 |
|
|
endpwent(); |
133 |
|
|
|
134 |
|
|
/* |
135 |
|
|
* hash it and go down the hash chain (if any) looking for it |
136 |
|
|
*/ |
137 |
|
|
indx = ((unsigned)uid) % USR_TB_SZ; |
138 |
|
|
if ((pt = usrtb[indx]) != NULL) { |
139 |
|
|
while (pt != NULL) { |
140 |
|
|
if (pt->uid == uid) |
141 |
|
|
return(0); |
142 |
|
|
pt = pt->fow; |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/* |
147 |
|
|
* uid is not yet in the table, add it to the front of the chain |
148 |
|
|
*/ |
149 |
|
|
if ((pt = malloc(sizeof(USRT))) != NULL) { |
150 |
|
|
pt->uid = uid; |
151 |
|
|
pt->fow = usrtb[indx]; |
152 |
|
|
usrtb[indx] = pt; |
153 |
|
|
return(0); |
154 |
|
|
} |
155 |
|
|
paxwarn(1, "User selection table out of memory"); |
156 |
|
|
return(-1); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
/* |
160 |
|
|
* usr_match() |
161 |
|
|
* check if this files uid matches a selected uid. |
162 |
|
|
* Return: |
163 |
|
|
* 0 if this archive member should be processed, 1 if it should be skipped |
164 |
|
|
*/ |
165 |
|
|
|
166 |
|
|
static int |
167 |
|
|
usr_match(ARCHD *arcn) |
168 |
|
|
{ |
169 |
|
|
USRT *pt; |
170 |
|
|
|
171 |
|
|
/* |
172 |
|
|
* hash and look for it in the table |
173 |
|
|
*/ |
174 |
|
|
pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ]; |
175 |
|
|
while (pt != NULL) { |
176 |
|
|
if (pt->uid == arcn->sb.st_uid) |
177 |
|
|
return(0); |
178 |
|
|
pt = pt->fow; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
/* |
182 |
|
|
* not found |
183 |
|
|
*/ |
184 |
|
|
return(1); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
/* |
188 |
|
|
* grp_add() |
189 |
|
|
* add a group match to the group match hash table |
190 |
|
|
* Return: |
191 |
|
|
* 0 if added ok, -1 otherwise; |
192 |
|
|
*/ |
193 |
|
|
|
194 |
|
|
int |
195 |
|
|
grp_add(char *str) |
196 |
|
|
{ |
197 |
|
|
u_int indx; |
198 |
|
|
GRPT *pt; |
199 |
|
|
struct group *gr; |
200 |
|
|
gid_t gid; |
201 |
|
|
|
202 |
|
|
/* |
203 |
|
|
* create the table if it doesn't exist |
204 |
|
|
*/ |
205 |
|
|
if ((str == NULL) || (*str == '\0')) |
206 |
|
|
return(-1); |
207 |
|
|
if ((grptb == NULL) && |
208 |
|
|
((grptb = calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) { |
209 |
|
|
paxwarn(1, "Unable to allocate memory fo group selection table"); |
210 |
|
|
return(-1); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
/* |
214 |
|
|
* figure out user spec |
215 |
|
|
*/ |
216 |
|
|
if (str[0] != '#') { |
217 |
|
|
/* |
218 |
|
|
* it is a group name, \# escapes # as first char in group name |
219 |
|
|
*/ |
220 |
|
|
if ((str[0] == '\\') && (str[1] == '#')) |
221 |
|
|
++str; |
222 |
|
|
if ((gr = getgrnam(str)) == NULL) { |
223 |
|
|
paxwarn(1,"Cannot determine gid for group name: %s", str); |
224 |
|
|
return(-1); |
225 |
|
|
} |
226 |
|
|
gid = (gid_t)gr->gr_gid; |
227 |
|
|
} else |
228 |
|
|
gid = (gid_t)strtoul(str+1, NULL, 10); |
229 |
|
|
endgrent(); |
230 |
|
|
|
231 |
|
|
/* |
232 |
|
|
* hash it and go down the hash chain (if any) looking for it |
233 |
|
|
*/ |
234 |
|
|
indx = ((unsigned)gid) % GRP_TB_SZ; |
235 |
|
|
if ((pt = grptb[indx]) != NULL) { |
236 |
|
|
while (pt != NULL) { |
237 |
|
|
if (pt->gid == gid) |
238 |
|
|
return(0); |
239 |
|
|
pt = pt->fow; |
240 |
|
|
} |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/* |
244 |
|
|
* gid not in the table, add it to the front of the chain |
245 |
|
|
*/ |
246 |
|
|
if ((pt = malloc(sizeof(GRPT))) != NULL) { |
247 |
|
|
pt->gid = gid; |
248 |
|
|
pt->fow = grptb[indx]; |
249 |
|
|
grptb[indx] = pt; |
250 |
|
|
return(0); |
251 |
|
|
} |
252 |
|
|
paxwarn(1, "Group selection table out of memory"); |
253 |
|
|
return(-1); |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
/* |
257 |
|
|
* grp_match() |
258 |
|
|
* check if this files gid matches a selected gid. |
259 |
|
|
* Return: |
260 |
|
|
* 0 if this archive member should be processed, 1 if it should be skipped |
261 |
|
|
*/ |
262 |
|
|
|
263 |
|
|
static int |
264 |
|
|
grp_match(ARCHD *arcn) |
265 |
|
|
{ |
266 |
|
|
GRPT *pt; |
267 |
|
|
|
268 |
|
|
/* |
269 |
|
|
* hash and look for it in the table |
270 |
|
|
*/ |
271 |
|
|
pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ]; |
272 |
|
|
while (pt != NULL) { |
273 |
|
|
if (pt->gid == arcn->sb.st_gid) |
274 |
|
|
return(0); |
275 |
|
|
pt = pt->fow; |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/* |
279 |
|
|
* not found |
280 |
|
|
*/ |
281 |
|
|
return(1); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
/* |
285 |
|
|
* Time range selection routines |
286 |
|
|
* |
287 |
|
|
* Routines to handle user selection of files based on the modification and/or |
288 |
|
|
* inode change time falling within a specified time range (the non-standard |
289 |
|
|
* -T flag). The user may specify any number of different file time ranges. |
290 |
|
|
* Time ranges are checked one at a time until a match is found (if at all). |
291 |
|
|
* If the file has a mtime (and/or ctime) which lies within one of the time |
292 |
|
|
* ranges, the file is selected. Time ranges may have a lower and/or a upper |
293 |
|
|
* value. These ranges are inclusive. When no time ranges are supplied to pax |
294 |
|
|
* with the -T option, all members in the archive will be selected by the time |
295 |
|
|
* range routines. When only a lower range is supplied, only files with a |
296 |
|
|
* mtime (and/or ctime) equal to or younger are selected. When only a upper |
297 |
|
|
* range is supplied, only files with a mtime (and/or ctime) equal to or older |
298 |
|
|
* are selected. When the lower time range is equal to the upper time range, |
299 |
|
|
* only files with a mtime (or ctime) of exactly that time are selected. |
300 |
|
|
*/ |
301 |
|
|
|
302 |
|
|
/* |
303 |
|
|
* trng_add() |
304 |
|
|
* add a time range match to the time range list. |
305 |
|
|
* This is a non-standard pax option. Lower and upper ranges are in the |
306 |
|
|
* format: [[[[[cc]yy]mm]dd]HH]MM[.SS] and are comma separated. |
307 |
|
|
* Time ranges are based on current time, so 1234 would specify a time of |
308 |
|
|
* 12:34 today. |
309 |
|
|
* Return: |
310 |
|
|
* 0 if the time range was added to the list, -1 otherwise |
311 |
|
|
*/ |
312 |
|
|
|
313 |
|
|
int |
314 |
|
|
trng_add(char *str) |
315 |
|
|
{ |
316 |
|
|
TIME_RNG *pt; |
317 |
|
|
char *up_pt = NULL; |
318 |
|
|
char *stpt; |
319 |
|
|
char *flgpt; |
320 |
|
|
int dot = 0; |
321 |
|
|
|
322 |
|
|
/* |
323 |
|
|
* throw out the badly formed time ranges |
324 |
|
|
*/ |
325 |
|
|
if ((str == NULL) || (*str == '\0')) { |
326 |
|
|
paxwarn(1, "Empty time range string"); |
327 |
|
|
return(-1); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
/* |
331 |
|
|
* locate optional flags suffix /{cm}. |
332 |
|
|
*/ |
333 |
|
|
if ((flgpt = strrchr(str, '/')) != NULL) |
334 |
|
|
*flgpt++ = '\0'; |
335 |
|
|
|
336 |
|
|
for (stpt = str; *stpt != '\0'; ++stpt) { |
337 |
|
|
if ((*stpt >= '0') && (*stpt <= '9')) |
338 |
|
|
continue; |
339 |
|
|
if ((*stpt == ',') && (up_pt == NULL)) { |
340 |
|
|
*stpt = '\0'; |
341 |
|
|
up_pt = stpt + 1; |
342 |
|
|
dot = 0; |
343 |
|
|
continue; |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
/* |
347 |
|
|
* allow only one dot per range (secs) |
348 |
|
|
*/ |
349 |
|
|
if ((*stpt == '.') && (!dot)) { |
350 |
|
|
++dot; |
351 |
|
|
continue; |
352 |
|
|
} |
353 |
|
|
paxwarn(1, "Improperly specified time range: %s", str); |
354 |
|
|
goto out; |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
/* |
358 |
|
|
* allocate space for the time range and store the limits |
359 |
|
|
*/ |
360 |
|
|
if ((pt = malloc(sizeof(TIME_RNG))) == NULL) { |
361 |
|
|
paxwarn(1, "Unable to allocate memory for time range"); |
362 |
|
|
return(-1); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
/* |
366 |
|
|
* by default we only will check file mtime, but user can specify |
367 |
|
|
* mtime, ctime (inode change time) or both. |
368 |
|
|
*/ |
369 |
|
|
if ((flgpt == NULL) || (*flgpt == '\0')) |
370 |
|
|
pt->flgs = CMPMTME; |
371 |
|
|
else { |
372 |
|
|
pt->flgs = 0; |
373 |
|
|
while (*flgpt != '\0') { |
374 |
|
|
switch (*flgpt) { |
375 |
|
|
case 'M': |
376 |
|
|
case 'm': |
377 |
|
|
pt->flgs |= CMPMTME; |
378 |
|
|
break; |
379 |
|
|
case 'C': |
380 |
|
|
case 'c': |
381 |
|
|
pt->flgs |= CMPCTME; |
382 |
|
|
break; |
383 |
|
|
default: |
384 |
|
|
paxwarn(1, "Bad option %c with time range %s", |
385 |
|
|
*flgpt, str); |
386 |
|
|
free(pt); |
387 |
|
|
goto out; |
388 |
|
|
} |
389 |
|
|
++flgpt; |
390 |
|
|
} |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
/* |
394 |
|
|
* start off with the current time |
395 |
|
|
*/ |
396 |
|
|
pt->low_time = pt->high_time = time(NULL); |
397 |
|
|
if (*str != '\0') { |
398 |
|
|
/* |
399 |
|
|
* add lower limit |
400 |
|
|
*/ |
401 |
|
|
if (str_sec(str, &(pt->low_time)) < 0) { |
402 |
|
|
paxwarn(1, "Illegal lower time range %s", str); |
403 |
|
|
free(pt); |
404 |
|
|
goto out; |
405 |
|
|
} |
406 |
|
|
pt->flgs |= HASLOW; |
407 |
|
|
} |
408 |
|
|
|
409 |
|
|
if ((up_pt != NULL) && (*up_pt != '\0')) { |
410 |
|
|
/* |
411 |
|
|
* add upper limit |
412 |
|
|
*/ |
413 |
|
|
if (str_sec(up_pt, &(pt->high_time)) < 0) { |
414 |
|
|
paxwarn(1, "Illegal upper time range %s", up_pt); |
415 |
|
|
free(pt); |
416 |
|
|
goto out; |
417 |
|
|
} |
418 |
|
|
pt->flgs |= HASHIGH; |
419 |
|
|
|
420 |
|
|
/* |
421 |
|
|
* check that the upper and lower do not overlap |
422 |
|
|
*/ |
423 |
|
|
if (pt->flgs & HASLOW) { |
424 |
|
|
if (pt->low_time > pt->high_time) { |
425 |
|
|
paxwarn(1, "Upper %s and lower %s time overlap", |
426 |
|
|
up_pt, str); |
427 |
|
|
free(pt); |
428 |
|
|
return(-1); |
429 |
|
|
} |
430 |
|
|
} |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
pt->fow = NULL; |
434 |
|
|
if (trhead == NULL) { |
435 |
|
|
trtail = trhead = pt; |
436 |
|
|
return(0); |
437 |
|
|
} |
438 |
|
|
trtail->fow = pt; |
439 |
|
|
trtail = pt; |
440 |
|
|
return(0); |
441 |
|
|
|
442 |
|
|
out: |
443 |
|
|
paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]"); |
444 |
|
|
return(-1); |
445 |
|
|
} |
446 |
|
|
|
447 |
|
|
/* |
448 |
|
|
* trng_match() |
449 |
|
|
* check if this files mtime/ctime falls within any supplied time range. |
450 |
|
|
* Return: |
451 |
|
|
* 0 if this archive member should be processed, 1 if it should be skipped |
452 |
|
|
*/ |
453 |
|
|
|
454 |
|
|
static int |
455 |
|
|
trng_match(ARCHD *arcn) |
456 |
|
|
{ |
457 |
|
|
TIME_RNG *pt; |
458 |
|
|
|
459 |
|
|
/* |
460 |
|
|
* have to search down the list one at a time looking for a match. |
461 |
|
|
* remember time range limits are inclusive. |
462 |
|
|
*/ |
463 |
|
|
pt = trhead; |
464 |
|
|
while (pt != NULL) { |
465 |
|
|
switch (pt->flgs & CMPBOTH) { |
466 |
|
|
case CMPBOTH: |
467 |
|
|
/* |
468 |
|
|
* user wants both mtime and ctime checked for this |
469 |
|
|
* time range |
470 |
|
|
*/ |
471 |
|
|
if (((pt->flgs & HASLOW) && |
472 |
|
|
(arcn->sb.st_mtime < pt->low_time) && |
473 |
|
|
(arcn->sb.st_ctime < pt->low_time)) || |
474 |
|
|
((pt->flgs & HASHIGH) && |
475 |
|
|
(arcn->sb.st_mtime > pt->high_time) && |
476 |
|
|
(arcn->sb.st_ctime > pt->high_time))) { |
477 |
|
|
pt = pt->fow; |
478 |
|
|
continue; |
479 |
|
|
} |
480 |
|
|
break; |
481 |
|
|
case CMPCTME: |
482 |
|
|
/* |
483 |
|
|
* user wants only ctime checked for this time range |
484 |
|
|
*/ |
485 |
|
|
if (((pt->flgs & HASLOW) && |
486 |
|
|
(arcn->sb.st_ctime < pt->low_time)) || |
487 |
|
|
((pt->flgs & HASHIGH) && |
488 |
|
|
(arcn->sb.st_ctime > pt->high_time))) { |
489 |
|
|
pt = pt->fow; |
490 |
|
|
continue; |
491 |
|
|
} |
492 |
|
|
break; |
493 |
|
|
case CMPMTME: |
494 |
|
|
default: |
495 |
|
|
/* |
496 |
|
|
* user wants only mtime checked for this time range |
497 |
|
|
*/ |
498 |
|
|
if (((pt->flgs & HASLOW) && |
499 |
|
|
(arcn->sb.st_mtime < pt->low_time)) || |
500 |
|
|
((pt->flgs & HASHIGH) && |
501 |
|
|
(arcn->sb.st_mtime > pt->high_time))) { |
502 |
|
|
pt = pt->fow; |
503 |
|
|
continue; |
504 |
|
|
} |
505 |
|
|
break; |
506 |
|
|
} |
507 |
|
|
break; |
508 |
|
|
} |
509 |
|
|
|
510 |
|
|
if (pt == NULL) |
511 |
|
|
return(1); |
512 |
|
|
return(0); |
513 |
|
|
} |
514 |
|
|
|
515 |
|
|
/* |
516 |
|
|
* str_sec() |
517 |
|
|
* Convert a time string in the format of [[[[[cc]yy]mm]dd]HH]MM[.SS] to |
518 |
|
|
* seconds UTC. Tval already has current time loaded into it at entry. |
519 |
|
|
* Return: |
520 |
|
|
* 0 if converted ok, -1 otherwise |
521 |
|
|
*/ |
522 |
|
|
|
523 |
|
|
static int |
524 |
|
|
str_sec(const char *p, time_t *tval) |
525 |
|
|
{ |
526 |
|
|
struct tm *lt; |
527 |
|
|
const char *dot, *t; |
528 |
|
|
size_t len; |
529 |
|
|
int bigyear; |
530 |
|
|
int yearset; |
531 |
|
|
|
532 |
|
|
yearset = 0; |
533 |
|
|
len = strlen(p); |
534 |
|
|
|
535 |
|
|
for (t = p, dot = NULL; *t; ++t) { |
536 |
|
|
if (isdigit((unsigned char)*t)) |
537 |
|
|
continue; |
538 |
|
|
if (*t == '.' && dot == NULL) { |
539 |
|
|
dot = t; |
540 |
|
|
continue; |
541 |
|
|
} |
542 |
|
|
return(-1); |
543 |
|
|
} |
544 |
|
|
|
545 |
|
|
lt = localtime(tval); |
546 |
|
|
|
547 |
|
|
if (dot != NULL) { /* .SS */ |
548 |
|
|
if (strlen(++dot) != 2) |
549 |
|
|
return(-1); |
550 |
|
|
lt->tm_sec = ATOI2(dot); |
551 |
|
|
if (lt->tm_sec > 61) |
552 |
|
|
return(-1); |
553 |
|
|
len -= 3; |
554 |
|
|
} else |
555 |
|
|
lt->tm_sec = 0; |
556 |
|
|
|
557 |
|
|
switch (len) { |
558 |
|
|
case 12: /* cc */ |
559 |
|
|
bigyear = ATOI2(p); |
560 |
|
|
lt->tm_year = (bigyear * 100) - 1900; |
561 |
|
|
yearset = 1; |
562 |
|
|
/* FALLTHROUGH */ |
563 |
|
|
case 10: /* yy */ |
564 |
|
|
if (yearset) { |
565 |
|
|
lt->tm_year += ATOI2(p); |
566 |
|
|
} else { |
567 |
|
|
lt->tm_year = ATOI2(p); |
568 |
|
|
if (lt->tm_year < 69) /* hack for 2000 ;-} */ |
569 |
|
|
lt->tm_year += (2000 - 1900); |
570 |
|
|
} |
571 |
|
|
/* FALLTHROUGH */ |
572 |
|
|
case 8: /* mm */ |
573 |
|
|
lt->tm_mon = ATOI2(p); |
574 |
|
|
if ((lt->tm_mon > 12) || !lt->tm_mon) |
575 |
|
|
return(-1); |
576 |
|
|
--lt->tm_mon; /* time struct is 0 - 11 */ |
577 |
|
|
/* FALLTHROUGH */ |
578 |
|
|
case 6: /* dd */ |
579 |
|
|
lt->tm_mday = ATOI2(p); |
580 |
|
|
if ((lt->tm_mday > 31) || !lt->tm_mday) |
581 |
|
|
return(-1); |
582 |
|
|
/* FALLTHROUGH */ |
583 |
|
|
case 4: /* HH */ |
584 |
|
|
lt->tm_hour = ATOI2(p); |
585 |
|
|
if (lt->tm_hour > 23) |
586 |
|
|
return(-1); |
587 |
|
|
/* FALLTHROUGH */ |
588 |
|
|
case 2: /* MM */ |
589 |
|
|
lt->tm_min = ATOI2(p); |
590 |
|
|
if (lt->tm_min > 59) |
591 |
|
|
return(-1); |
592 |
|
|
break; |
593 |
|
|
default: |
594 |
|
|
return(-1); |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
/* convert broken-down time to UTC clock time seconds */ |
598 |
|
|
if ((*tval = mktime(lt)) == -1) |
599 |
|
|
return(-1); |
600 |
|
|
return(0); |
601 |
|
|
} |