add defines to provide compatibility with 4.3BSD-Tahoe systems
[unix-history] / usr / src / sbin / dump / unctime.c
CommitLineData
76797561
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
bbbe213d 7#ifndef lint
13298603 8static char sccsid[] = "@(#)unctime.c 5.3 (Berkeley) %G%";
36783b71 9#endif /* not lint */
bbbe213d 10
6e1fe29b 11#include <sys/types.h>
13298603 12#include <time.h>
50953c5b 13#include <stdio.h>
13298603 14#ifdef __STDC__
36783b71
CT
15#include <stdlib.h>
16#include <string.h>
13298603 17#endif
36783b71 18
6e1fe29b
BJ
19/*
20 * Convert a ctime(3) format string into a system format date.
21 * Return the date thus calculated.
22 *
23 * Return -1 if the string is not in ctime format.
24 */
25
26/*
27 * Offsets into the ctime string to various parts.
28 */
29
30#define E_MONTH 4
31#define E_DAY 8
32#define E_HOUR 11
33#define E_MINUTE 14
34#define E_SECOND 17
35#define E_YEAR 20
36
36783b71
CT
37static int lookup();
38
39time_t
40unctime(str)
6e1fe29b
BJ
41 char *str;
42{
43 struct tm then;
44 char dbuf[30];
45 time_t emitl();
46
47 if (strlen(str) != 25)
48 str[25] = 0;
49 strcpy(dbuf, str);
50 dbuf[E_MONTH+3] = 0;
36783b71
CT
51 if ((then.tm_mon = lookup(&dbuf[E_MONTH])) < 0)
52 return (-1);
6e1fe29b
BJ
53 then.tm_mday = atoi(&dbuf[E_DAY]);
54 then.tm_hour = atoi(&dbuf[E_HOUR]);
55 then.tm_min = atoi(&dbuf[E_MINUTE]);
56 then.tm_sec = atoi(&dbuf[E_SECOND]);
57 then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
58 return(emitl(&then));
59}
60
61static char months[] =
62 "JanFebMarAprMayJunJulAugSepOctNovDec";
63
36783b71 64static int
6e1fe29b
BJ
65lookup(str)
66 char *str;
67{
68 register char *cp, *cp2;
69
70 for (cp = months, cp2 = str; *cp != 0; cp += 3)
71 if (strncmp(cp, cp2, 3) == 0)
72 return((cp-months) / 3);
73 return(-1);
74}
75/*
76 * Routine to convert a localtime(3) format date back into
77 * a system format date.
78 *
79 * Use a binary search.
80 */
81
82struct tm *localtime();
36783b71 83static int dcmp();
6e1fe29b 84
36783b71
CT
85time_t
86emitl(dp)
6e1fe29b
BJ
87 struct tm *dp;
88{
89 time_t conv;
90 register int i, bit;
91 struct tm dcopy;
92
93 dcopy = *dp;
94 dp = &dcopy;
95 conv = 0;
50953c5b 96 for (i = 30; i >= 0; i--) {
6e1fe29b
BJ
97 bit = 1 << i;
98 conv |= bit;
99 if (dcmp(localtime(&conv), dp) > 0)
100 conv &= ~bit;
101 }
102 return(conv);
103}
104
105/*
106 * Compare two localtime dates, return result.
107 */
108
109#define DECIDE(a) \
110 if (dp->a > dp2->a) \
111 return(1); \
112 if (dp->a < dp2->a) \
113 return(-1)
114
36783b71 115static int
6e1fe29b
BJ
116dcmp(dp, dp2)
117 register struct tm *dp, *dp2;
118{
119
120 DECIDE(tm_year);
121 DECIDE(tm_mon);
122 DECIDE(tm_mday);
123 DECIDE(tm_hour);
124 DECIDE(tm_min);
125 DECIDE(tm_sec);
126 return(0);
127}