2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
22 #include <ac/unistd.h>
32 #include <ldap_defaults.h>
39 /* Some Windows versions accept both forward and backslashes in
40 * directory paths, but we always use backslashes when generating
43 void lutil_slashpath( char *path )
48 while (( c=strchr( p, '/' ))) {
55 char* lutil_progname( const char* name, int argc, char *argv[] )
66 for (i=0; i<argc; i++) __etoa(argv[i]);
70 LUTIL_SLASHPATH( argv[0] );
71 progname = strrchr ( argv[0], *LDAP_DIRSEP );
72 progname = progname ? &progname[1] : argv[0];
77 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
81 /* We've been compiling in ASCII so far, but we want EBCDIC now since
82 * strftime only understands EBCDIC input.
84 #pragma convlit(suspend)
86 ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
88 #pragma convlit(resume)
95 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
100 if ( smax < 16 ) { /* YYYYmmddHHMMSSZ */
105 /* We've been compiling in ASCII so far, but we want EBCDIC now since
106 * strftime only understands EBCDIC input.
108 #pragma convlit(suspend)
110 ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
112 #pragma convlit(resume)
115 if ( delta == 0 || ret == 0 ) {
119 if ( smax < 20 ) { /* YYYYmmddHHMMSS+HHMM */
133 snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
134 ( delta % 3600 ) / 60 );
139 int lutil_tm2time( struct lutil_tm *tm, struct lutil_timet *tt )
141 static int moffset[12] = {
147 tt->tt_usec = tm->tm_usec;
149 /* special case 0000/01/01+00:00:00 is returned as zero */
150 if ( tm->tm_year == -1900 && tm->tm_mon == 0 && tm->tm_mday == 1 &&
151 tm->tm_hour == 0 && tm->tm_min == 0 && tm->tm_sec == 0 ) {
157 /* tm->tm_year is years since 1900 */
158 /* calculate days from years since 1970 (epoch) */
159 tt->tt_sec = tm->tm_year - 70;
162 /* count leap days in preceding years */
163 tt->tt_sec += ((tm->tm_year -69) >> 2);
165 /* calculate days from months */
166 tt->tt_sec += moffset[tm->tm_mon];
168 /* add in this year's leap day, if any */
169 if (((tm->tm_year & 3) == 0) && (tm->tm_mon > 1)) {
173 /* add in days in this month */
174 tt->tt_sec += (tm->tm_mday - 1);
176 /* this function can handle a range of about 17408 years... */
177 /* 86400 seconds in a day, divided by 128 = 675 */
180 /* move high 7 bits into tt_gsec */
181 tt->tt_gsec = tt->tt_sec >> 25;
182 tt->tt_sec -= tt->tt_gsec << 25;
187 /* convert to minutes */
191 /* convert to seconds */
195 /* add remaining seconds */
203 int lutil_parsetime( char *atm, struct lutil_tm *tm )
209 /* Is the stamp reasonably long? */
210 for (i=0; isdigit((unsigned char) atm[i]); i++);
211 if (i < sizeof("00000101000000")-1)
215 * parse the time into a struct tm
217 /* 4 digit year to year - 1900 */
218 tm->tm_year = *ptr++ - '0';
219 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
220 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
221 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
223 /* month 01-12 to 0-11 */
224 tm->tm_mon = *ptr++ - '0';
225 tm->tm_mon *=10; tm->tm_mon += *ptr++ - '0';
226 if (tm->tm_mon < 1 || tm->tm_mon > 12) break;
229 /* day of month 01-31 */
230 tm->tm_mday = *ptr++ - '0';
231 tm->tm_mday *=10; tm->tm_mday += *ptr++ - '0';
232 if (tm->tm_mday < 1 || tm->tm_mday > 31) break;
235 tm->tm_hour = *ptr++ - '0';
236 tm->tm_hour *=10; tm->tm_hour += *ptr++ - '0';
237 if (tm->tm_hour < 0 || tm->tm_hour > 23) break;
240 tm->tm_min = *ptr++ - '0';
241 tm->tm_min *=10; tm->tm_min += *ptr++ - '0';
242 if (tm->tm_min < 0 || tm->tm_min > 59) break;
245 tm->tm_sec = *ptr++ - '0';
246 tm->tm_sec *=10; tm->tm_sec += *ptr++ - '0';
247 if (tm->tm_sec < 0 || tm->tm_sec > 61) break;
249 /* Fractions of seconds */
250 for (i = 0, fracs = 0; isdigit((unsigned char) *ptr); ) {
251 i*=10; i+= *ptr++ - '0';
256 for (i = fracs; i<6; i++)
261 if (*ptr != 'Z') break;
268 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
269 * the result string. This allows fast construction of catenated strings
270 * without the overhead of strlen/strcat.
281 while ((*a++ = *b++)) ;
285 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
286 * the result string. This allows fast construction of catenated strings
287 * without the overhead of strlen/strcat.
296 if (!a || !b || n == 0)
299 while ((*a++ = *b++) && n-- > 0) ;
304 int mkstemp( char * template )
307 return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
315 * Memory Reverse Search
318 lutil_memrchr(const void *b, int c, size_t n)
321 const unsigned char *s, *bb = b, cc = c;
323 for ( s = bb + n; s > bb; ) {
334 lutil_atoix( int *v, const char *s, int x )
342 i = strtol( s, &next, x );
343 if ( next == s || next[ 0 ] != '\0' ) {
347 if ( (long)(int)i != i ) {
357 lutil_atoux( unsigned *v, const char *s, int x )
365 u = strtoul( s, &next, x );
366 if ( next == s || next[ 0 ] != '\0' ) {
370 if ( (unsigned long)(unsigned)u != u ) {
380 lutil_atolx( long *v, const char *s, int x )
388 l = strtol( s, &next, x );
389 if ( next == s || next[ 0 ] != '\0' ) {
399 lutil_atoulx( unsigned long *v, const char *s, int x )
407 ul = strtoul( s, &next, x );
408 if ( next == s || next[ 0 ] != '\0' ) {
417 static char time_unit[] = "dhms";
428 scale[] = { 86400, 3600, 60, 1 };
432 for ( s = (char *)in; s[ 0 ] != '\0'; ) {
436 u = strtoul( s, &next, 10 );
441 if ( next[ 0 ] == '\0' ) {
447 what = strchr( time_unit, next[ 0 ] );
448 if ( what == NULL ) {
452 if ( what - time_unit <= sofar ) {
456 sofar = what - time_unit;
457 t += u * scale[ sofar ];
473 unsigned long v[ 4 ];
477 v[ 1 ] = (t%86400)/3600;
478 v[ 2 ] = (t%3600)/60;
481 for ( i = 0; i < 4; i++ ) {
482 if ( v[i] > 0 || ( i == 3 && ptr == buf ) ) {
483 len = snprintf( ptr, buflen, "%lu%c", v[ i ], time_unit[ i ] );
484 if ( len < 0 || (unsigned)len >= buflen ) {