]> git.sur5r.net Git - openldap/blob - libraries/liblutil/utils.c
62f4d4d65a832ac45b087354d590acb0b146c277
[openldap] / libraries / liblutil / utils.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 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 char* lutil_progname( const char* name, int argc, char *argv[] )
38 {
39         char *progname;
40
41         if(argc == 0) {
42                 return (char *)name;
43         }
44
45 #ifdef HAVE_EBCDIC
46         if (_trans_argv) {
47                 int i;
48                 for (i=0; i<argc; i++) __etoa(argv[i]);
49                 _trans_argv = 0;
50         }
51 #endif
52         progname = strrchr ( argv[0], *LDAP_DIRSEP );
53         progname = progname ? &progname[1] : argv[0];
54
55         return progname;
56 }
57
58 #if 0
59 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
60 {
61         size_t ret;
62 #ifdef HAVE_EBCDIC
63 /* We've been compiling in ASCII so far, but we want EBCDIC now since
64  * strftime only understands EBCDIC input.
65  */
66 #pragma convlit(suspend)
67 #endif
68         ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
69 #ifdef HAVE_EBCDIC
70 #pragma convlit(resume)
71         __etoa( s );
72 #endif
73         return ret;
74 }
75 #endif
76
77 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
78 {
79         size_t  ret;
80         char    *p;
81
82         if ( smax < 16 ) {      /* YYYYmmddHHMMSSZ */
83                 return 0;
84         }
85
86 #ifdef HAVE_EBCDIC
87 /* We've been compiling in ASCII so far, but we want EBCDIC now since
88  * strftime only understands EBCDIC input.
89  */
90 #pragma convlit(suspend)
91 #endif
92         ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
93 #ifdef HAVE_EBCDIC
94 #pragma convlit(resume)
95         __etoa( s );
96 #endif
97         if ( delta == 0 || ret == 0 ) {
98                 return ret;
99         }
100
101         if ( smax < 20 ) {      /* YYYYmmddHHMMSS+HHMM */
102                 return 0;
103         }
104
105         p = s + 14;
106
107         if ( delta < 0 ) {
108                 p[ 0 ] = '-';
109                 delta = -delta;
110         } else {
111                 p[ 0 ] = '+';
112         }
113         p++;
114
115         snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
116                         ( delta % 3600 ) / 60 );
117
118         return ret + 5;
119 }
120
121
122 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
123  * the result string. This allows fast construction of catenated strings
124  * without the overhead of strlen/strcat.
125  */
126 char *
127 lutil_strcopy(
128         char *a,
129         const char *b
130 )
131 {
132         if (!a || !b)
133                 return a;
134         
135         while ((*a++ = *b++)) ;
136         return a-1;
137 }
138
139 /* strncopy 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_strncopy(
145         char *a,
146         const char *b,
147         size_t n
148 )
149 {
150         if (!a || !b || n == 0)
151                 return a;
152         
153         while ((*a++ = *b++) && n-- > 0) ;
154         return a-1;
155 }
156
157 #ifndef HAVE_MKSTEMP
158 int mkstemp( char * template )
159 {
160 #ifdef HAVE_MKTEMP
161         return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
162 #else
163         return -1;
164 #endif
165 }
166 #endif