1 | /* $Id: compat.c 13 2011-04-20 15:41:43Z mmamonski $ */ |
---|
2 | /* |
---|
3 | * PSNC DRMAA 2.0 utilities library |
---|
4 | * Copyright (C) 2012 Poznan Supercomputing and Networking Center |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | # include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <stdarg.h> |
---|
25 | #include <stdio.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <unistd.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <string.h> |
---|
30 | #include <assert.h> |
---|
31 | #include <drmaa_utils/compat.h> |
---|
32 | |
---|
33 | |
---|
34 | #ifndef lint |
---|
35 | static char rcsid[] |
---|
36 | # ifdef __GNUC__ |
---|
37 | __attribute__ ((unused)) |
---|
38 | # endif |
---|
39 | = "$Id: compat.c 13 2011-04-20 15:41:43Z mmamonski $"; |
---|
40 | #endif |
---|
41 | |
---|
42 | |
---|
43 | #ifndef HAVE_STRLCPY |
---|
44 | size_t |
---|
45 | strlcpy( char *dest, const char *src, size_t size ) |
---|
46 | { |
---|
47 | size_t result = 0; |
---|
48 | if( size == 0 ) |
---|
49 | return 0; |
---|
50 | while( *src && --size > 0 ) |
---|
51 | { |
---|
52 | *dest++ = *src++; |
---|
53 | result++; |
---|
54 | } |
---|
55 | *dest++ = '\0'; |
---|
56 | return result; |
---|
57 | } |
---|
58 | #endif /* ! HAVE_STRLCPY */ |
---|
59 | |
---|
60 | |
---|
61 | #ifndef HAVE_STRNDUP |
---|
62 | char * |
---|
63 | strndup( const char *s, size_t n ) |
---|
64 | { |
---|
65 | char *result; |
---|
66 | if( s == NULL ) |
---|
67 | return NULL; |
---|
68 | result = calloc( n + 1, sizeof(char) ); |
---|
69 | |
---|
70 | if( result == NULL ) |
---|
71 | { |
---|
72 | errno = ENOMEM; |
---|
73 | return NULL; |
---|
74 | } |
---|
75 | |
---|
76 | strlcpy( result, s, n + 1 ); |
---|
77 | return result; |
---|
78 | } |
---|
79 | #endif /* ! HAVE_STRNDUP */ |
---|
80 | |
---|
81 | |
---|
82 | #ifndef HAVE_ASPRINTF |
---|
83 | int |
---|
84 | asprintf( char **strp, const char *fmt, ... ) |
---|
85 | { |
---|
86 | va_list args; |
---|
87 | int result; |
---|
88 | va_start( args, fmt ); |
---|
89 | result = vasprintf( strp, fmt, args ); |
---|
90 | va_end( args ); |
---|
91 | return result; |
---|
92 | } |
---|
93 | #endif /* ! HAVE_ASPRINTF */ |
---|
94 | |
---|
95 | |
---|
96 | #ifndef HAVE_VASPRINTF |
---|
97 | int |
---|
98 | vasprintf( char **strp, const char *format, va_list ap ) |
---|
99 | { |
---|
100 | int size, check_size; |
---|
101 | char *buf = NULL; |
---|
102 | |
---|
103 | #ifdef HAVE_VA_COPY |
---|
104 | va_list aq; |
---|
105 | |
---|
106 | va_copy(aq, ap); |
---|
107 | #else |
---|
108 | # ifdef HAVE___VA_COPY |
---|
109 | va_list aq; |
---|
110 | |
---|
111 | __va_copy(aq, ap); |
---|
112 | # endif |
---|
113 | #endif |
---|
114 | |
---|
115 | *strp = NULL; |
---|
116 | |
---|
117 | #ifndef HAVE_C99_VSNPRINTF |
---|
118 | { |
---|
119 | int res; |
---|
120 | char *tmp; |
---|
121 | |
---|
122 | size = 128; |
---|
123 | do { |
---|
124 | size *= 2; |
---|
125 | if (!(tmp = realloc(buf, size))) { |
---|
126 | if (buf) |
---|
127 | free(buf); |
---|
128 | return -1; |
---|
129 | } |
---|
130 | buf = tmp; |
---|
131 | /* XXX we're assuming here there's no va_copy on this system */ |
---|
132 | res = vsnprintf(buf, size, format, ap); |
---|
133 | } while (res == -1); |
---|
134 | } |
---|
135 | #else |
---|
136 | { |
---|
137 | char tmp[2]; |
---|
138 | |
---|
139 | /* on Solaris vsnprintf fails if buf is empty, so use a small one */ |
---|
140 | size = vsnprintf(tmp, sizeof(tmp), format, ap); |
---|
141 | if (!(buf = malloc(size + 1))) |
---|
142 | return -1; |
---|
143 | } |
---|
144 | #endif |
---|
145 | |
---|
146 | #ifdef HAVE_VA_COPY |
---|
147 | check_size = vsnprintf(buf, size + 1, format, aq); |
---|
148 | va_end(aq); |
---|
149 | #else |
---|
150 | # ifdef HAVE___VA_COPY |
---|
151 | check_size = vsnprintf(buf, size + 1, format, aq); |
---|
152 | va_end(aq); |
---|
153 | # else |
---|
154 | check_size = vsnprintf(buf, size + 1, format, ap); |
---|
155 | # endif |
---|
156 | #endif |
---|
157 | assert(check_size <= size); |
---|
158 | *strp = buf; |
---|
159 | return 0; |
---|
160 | } |
---|
161 | #endif |
---|
162 | |
---|