]> git.sur5r.net Git - openldap/blob - libraries/liblutil/utils.c
ee0c25cd25cdc81f9acf8d9a6a3a46937e892c00
[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 #if 0
50 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
51 {
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 }
66 #endif
67
68 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
69 {
70         size_t  ret;
71         char    *p;
72
73         if ( smax < 16 ) {      /* YYYYmmddHHMMSSZ */
74                 return 0;
75         }
76
77 #ifdef HAVE_EBCDIC
78 /* We've been compiling in ASCII so far, but we want EBCDIC now since
79  * strftime only understands EBCDIC input.
80  */
81 #pragma convlit(suspend)
82 #endif
83         ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
84         if ( delta == 0 || ret == 0 ) {
85                 return ret;
86         }
87
88         if ( smax < 20 ) {      /* YYYYmmddHHMMSS+HHMM */
89                 return 0;
90         }
91
92         p = s + 14;
93
94         if ( delta < 0 ) {
95                 p[ 0 ] = '-';
96                 delta = -delta;
97         } else {
98                 p[ 0 ] = '+';
99         }
100         p++;
101
102         snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
103                         ( delta % 3600 ) / 60 );
104
105 #ifdef HAVE_EBCDIC
106 #pragma convlit(resume)
107         __etoa( s );
108 #endif
109         return ret + 5;
110 }
111
112
113 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
114  * the result string. This allows fast construction of catenated strings
115  * without the overhead of strlen/strcat.
116  */
117 char *
118 lutil_strcopy(
119         char *a,
120         const char *b
121 )
122 {
123         if (!a || !b)
124                 return a;
125         
126         while ((*a++ = *b++)) ;
127         return a-1;
128 }
129
130 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
131  * the result string. This allows fast construction of catenated strings
132  * without the overhead of strlen/strcat.
133  */
134 char *
135 lutil_strncopy(
136         char *a,
137         const char *b,
138         size_t n
139 )
140 {
141         if (!a || !b || n == 0)
142                 return a;
143         
144         while ((*a++ = *b++) && n-- > 0) ;
145         return a-1;
146 }
147
148 #ifndef HAVE_MKSTEMP
149 int mkstemp( char * template )
150 {
151 #ifdef HAVE_MKTEMP
152         return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
153 #else
154         return -1;
155 #endif
156 }
157 #endif