]> git.sur5r.net Git - openldap/blob - libraries/liblutil/utils.c
slashpath from HEAD
[openldap] / libraries / liblutil / utils.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/time.h>
23 #ifdef HAVE_IO_H
24 #include <io.h>
25 #endif
26 #ifdef HAVE_FCNTL_H
27 #include <fcntl.h>
28 #endif
29
30 #include <lutil.h>
31 #include <ldap_defaults.h>
32
33 #ifdef HAVE_EBCDIC
34 int _trans_argv = 1;
35 #endif
36
37 #ifdef _WIN32
38 /* Some Windows versions accept both forward and backslashes in
39  * directory paths, but we always use backslashes when generating
40  * and parsing...
41  */
42 void lutil_slashpath( char *path )
43 {
44         char *c, *p;
45
46         p = path;
47         while (( c=strchr( p, '/' ))) {
48                 *c++ = '\\';
49                 p = c;
50         }
51 }
52 #endif
53
54 char* lutil_progname( const char* name, int argc, char *argv[] )
55 {
56         char *progname;
57
58         if(argc == 0) {
59                 return (char *)name;
60         }
61
62 #ifdef HAVE_EBCDIC
63         if (_trans_argv) {
64                 int i;
65                 for (i=0; i<argc; i++) __etoa(argv[i]);
66                 _trans_argv = 0;
67         }
68 #endif
69         LUTIL_SLASHPATH( argv[0] );
70         progname = strrchr ( argv[0], *LDAP_DIRSEP );
71         progname = progname ? &progname[1] : argv[0];
72         return progname;
73 }
74
75 #if 0
76 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
77 {
78         size_t ret;
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 #ifdef HAVE_EBCDIC
87 #pragma convlit(resume)
88         __etoa( s );
89 #endif
90         return ret;
91 }
92 #endif
93
94 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
95 {
96         size_t  ret;
97         char    *p;
98
99         if ( smax < 16 ) {      /* YYYYmmddHHMMSSZ */
100                 return 0;
101         }
102
103 #ifdef HAVE_EBCDIC
104 /* We've been compiling in ASCII so far, but we want EBCDIC now since
105  * strftime only understands EBCDIC input.
106  */
107 #pragma convlit(suspend)
108 #endif
109         ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
110 #ifdef HAVE_EBCDIC
111 #pragma convlit(resume)
112         __etoa( s );
113 #endif
114         if ( delta == 0 || ret == 0 ) {
115                 return ret;
116         }
117
118         if ( smax < 20 ) {      /* YYYYmmddHHMMSS+HHMM */
119                 return 0;
120         }
121
122         p = s + 14;
123
124         if ( delta < 0 ) {
125                 p[ 0 ] = '-';
126                 delta = -delta;
127         } else {
128                 p[ 0 ] = '+';
129         }
130         p++;
131
132         snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
133                         ( delta % 3600 ) / 60 );
134
135         return ret + 5;
136 }
137
138
139 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
140  * the result string. This allows fast construction of catenated strings
141  * without the overhead of strlen/strcat.
142  */
143 char *
144 lutil_strcopy(
145         char *a,
146         const char *b
147 )
148 {
149         if (!a || !b)
150                 return a;
151         
152         while ((*a++ = *b++)) ;
153         return a-1;
154 }
155
156 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
157  * the result string. This allows fast construction of catenated strings
158  * without the overhead of strlen/strcat.
159  */
160 char *
161 lutil_strncopy(
162         char *a,
163         const char *b,
164         size_t n
165 )
166 {
167         if (!a || !b || n == 0)
168                 return a;
169         
170         while ((*a++ = *b++) && n-- > 0) ;
171         return a-1;
172 }
173
174 #ifndef HAVE_MKSTEMP
175 int mkstemp( char * template )
176 {
177 #ifdef HAVE_MKTEMP
178         return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
179 #else
180         return -1;
181 #endif
182 }
183 #endif