]> git.sur5r.net Git - openldap/blob - libraries/liblutil/utils.c
Fix abstract oc check
[openldap] / libraries / liblutil / utils.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <ac/stdlib.h>
10 #include <ac/string.h>
11 #include <ac/unistd.h>
12 #include <ac/time.h>
13 #ifdef HAVE_IO_H
14 #include <io.h>
15 #endif
16 #ifdef HAVE_FCNTL_H
17 #include <fcntl.h>
18 #endif
19
20 #include <lutil.h>
21 #include <ldap_defaults.h>
22
23 #ifdef HAVE_EBCDIC
24 int _trans_argv = 1;
25 #endif
26
27 char* lutil_progname( const char* name, int argc, char *argv[] )
28 {
29         char *progname;
30
31         if(argc == 0) {
32                 return (char *)name;
33         }
34
35 #ifdef HAVE_EBCDIC
36         if (_trans_argv) {
37                 int i;
38                 for (i=0; i<argc; i++) __etoa(argv[i]);
39                 _trans_argv = 0;
40         }
41 #endif
42         progname = strrchr ( argv[0], *LDAP_DIRSEP );
43         progname = progname ? &progname[1] : argv[0];
44
45         return progname;
46 }
47
48 size_t lutil_gentime( char *s, size_t max, const struct tm *tm )
49 {
50         size_t ret;
51 #ifdef HAVE_EBCDIC
52 /* We've been compiling in ASCII so far, but we want EBCDIC now since
53  * strftime only understands EBCDIC input.
54  */
55 #pragma convlit(suspend)
56 #endif
57         ret = strftime( s, max, "%Y%m%d%H%M%SZ", tm );
58 #ifdef HAVE_EBCDIC
59 #pragma convlit(resume)
60         __etoa( s );
61 #endif
62         return ret;
63 }
64
65 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
66  * the result string. This allows fast construction of catenated strings
67  * without the overhead of strlen/strcat.
68  */
69 char *
70 lutil_strcopy(
71         char *a,
72         const char *b
73 )
74 {
75         if (!a || !b)
76                 return a;
77         
78         while ((*a++ = *b++)) ;
79         return a-1;
80 }
81
82 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
83  * the result string. This allows fast construction of catenated strings
84  * without the overhead of strlen/strcat.
85  */
86 char *
87 lutil_strncopy(
88         char *a,
89         const char *b,
90         size_t n
91 )
92 {
93         if (!a || !b || n == 0)
94                 return a;
95         
96         while ((*a++ = *b++) && n-- > 0) ;
97         return a-1;
98 }
99
100 #ifndef HAVE_MKSTEMP
101 int mkstemp( char * template )
102 {
103 #ifdef HAVE_MKTEMP
104         return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
105 #else
106         return -1;
107 #endif
108 }
109 #endif