]> git.sur5r.net Git - openldap/blob - libraries/liblutil/utils.c
d12bac851c7cd1c37cbe55b1887c57b804276419
[openldap] / libraries / liblutil / utils.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13 #include <ac/time.h>
14 #ifdef HAVE_IO_H
15 #include <io.h>
16 #endif
17 #ifdef HAVE_FCNTL_H
18 #include <fcntl.h>
19 #endif
20
21 #include <lutil.h>
22 #include <ldap_defaults.h>
23
24 #ifdef HAVE_EBCDIC
25 int _trans_argv = 1;
26 #endif
27
28 char* lutil_progname( const char* name, int argc, char *argv[] )
29 {
30         char *progname;
31
32         if(argc == 0) {
33                 return (char *)name;
34         }
35
36 #ifdef HAVE_EBCDIC
37         if (_trans_argv) {
38                 int i;
39                 for (i=0; i<argc; i++) __etoa(argv[i]);
40                 _trans_argv = 0;
41         }
42 #endif
43         progname = strrchr ( argv[0], *LDAP_DIRSEP );
44         progname = progname ? &progname[1] : argv[0];
45
46         return progname;
47 }
48
49 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
50 {
51 #if 0
52         size_t ret;
53 #ifdef HAVE_EBCDIC
54 /* We've been compiling in ASCII so far, but we want EBCDIC now since
55  * strftime only understands EBCDIC input.
56  */
57 #pragma convlit(suspend)
58 #endif
59         ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
60 #ifdef HAVE_EBCDIC
61 #pragma convlit(resume)
62         __etoa( s );
63 #endif
64         return ret;
65 #else
66         return lutil_localtime( s, smax, tm, 0 );
67 #endif
68 }
69
70 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
71 {
72         size_t  ret;
73         char    *p;
74
75         if ( smax < 20 ) {
76                 return -1;
77         }
78
79 #ifdef HAVE_EBCDIC
80 /* We've been compiling in ASCII so far, but we want EBCDIC now since
81  * strftime only understands EBCDIC input.
82  */
83 #pragma convlit(suspend)
84 #endif
85         ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
86         if ( ret == 0 ) {
87                 return ret;
88         }
89
90         if ( delta == 0 ) {
91                 return ret;
92         }
93
94         p = s + 14;
95
96         if ( delta < 0 ) {
97                 p[ 0 ] = '-';
98                 delta = -delta;
99         } else {
100                 p[ 0 ] = '+';
101         }
102         p++;
103
104         snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
105                         ( delta % 3600 ) / 60 );
106
107 #ifdef HAVE_EBCDIC
108 #pragma convlit(resume)
109         __etoa( s );
110 #endif
111         return ret + 5;
112 }
113
114
115 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
116  * the result string. This allows fast construction of catenated strings
117  * without the overhead of strlen/strcat.
118  */
119 char *
120 lutil_strcopy(
121         char *a,
122         const char *b
123 )
124 {
125         if (!a || !b)
126                 return a;
127         
128         while ((*a++ = *b++)) ;
129         return a-1;
130 }
131
132 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
133  * the result string. This allows fast construction of catenated strings
134  * without the overhead of strlen/strcat.
135  */
136 char *
137 lutil_strncopy(
138         char *a,
139         const char *b,
140         size_t n
141 )
142 {
143         if (!a || !b || n == 0)
144                 return a;
145         
146         while ((*a++ = *b++) && n-- > 0) ;
147         return a-1;
148 }
149
150 #ifndef HAVE_MKSTEMP
151 int mkstemp( char * template )
152 {
153 #ifdef HAVE_MKTEMP
154         return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
155 #else
156         return -1;
157 #endif
158 }
159 #endif