source: mainline/uspace/lib/posix/src/stdlib.c@ 75c430e3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 75c430e3 was 75c430e3, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Bsearch, getenv, system need to go to libc. (although we might eventually want different implementation for POSIX mode.)

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2011 Petr Koupy
3 * Copyright (c) 2011 Jiri Zarevucky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Standard library definitions.
34 */
35
36#include "internal/common.h"
37#include "posix/stdlib.h"
38
39#include <errno.h>
40
41#include "posix/fcntl.h"
42#include "posix/limits.h"
43#include "posix/string.h"
44#include "posix/sys/stat.h"
45#include "posix/unistd.h"
46
47#include "libc/qsort.h"
48#include "libc/str.h"
49#include "libc/vfs/vfs.h"
50#include "libc/stats.h"
51
52/**
53 * Integer absolute value.
54 *
55 * @param i Input value.
56 * @return Absolute value of the parameter.
57 */
58int abs(int i)
59{
60 return i < 0 ? -i : i;
61}
62
63/**
64 * Long integer absolute value.
65 *
66 * @param i Input value.
67 * @return Absolute value of the parameter.
68 */
69long labs(long i)
70{
71 return i < 0 ? -i : i;
72}
73
74/**
75 * Long long integer absolute value.
76 *
77 * @param i Input value.
78 * @return Absolute value of the parameter.
79 */
80long long llabs(long long i)
81{
82 return i < 0 ? -i : i;
83}
84
85/**
86 *
87 * @param name
88 * @param resolved
89 * @return
90 */
91int putenv(char *string)
92{
93 // TODO: low priority, just a compile-time dependency of binutils
94 not_implemented();
95 return 0;
96}
97
98/**
99 * Resolve absolute pathname.
100 *
101 * @param name Pathname to be resolved.
102 * @param resolved Either buffer for the resolved absolute pathname or NULL.
103 * @return On success, either resolved (if it was not NULL) or pointer to the
104 * newly allocated buffer containing the absolute pathname (if resolved was
105 * NULL). Otherwise NULL.
106 *
107 */
108char *realpath(const char *restrict name, char *restrict resolved)
109{
110#ifndef PATH_MAX
111 assert(resolved == NULL);
112#endif
113
114 if (name == NULL) {
115 errno = EINVAL;
116 return NULL;
117 }
118
119 // TODO: symlink resolution
120
121 /*
122 * Function absolutize is implemented in libc and declared in vfs.h.
123 * No more processing is required as HelenOS doesn't have symlinks
124 * so far (as far as I can tell), although this function will need
125 * to be updated when that support is implemented.
126 */
127 char *absolute = vfs_absolutize(name, NULL);
128
129 if (absolute == NULL) {
130 /*
131 * POSIX requires some specific errnos to be set
132 * for some cases, but there is no way to find out from
133 * absolutize().
134 */
135 errno = EINVAL;
136 return NULL;
137 }
138
139 if (resolved == NULL) {
140 return absolute;
141 } else {
142#ifdef PATH_MAX
143 str_cpy(resolved, PATH_MAX, absolute);
144#endif
145 free(absolute);
146 return resolved;
147 }
148}
149
150/**
151 * Converts a string representation of a floating-point number to
152 * its native representation. See strtold().
153 *
154 * @param nptr String representation of a floating-point number.
155 * @return Double-precision number resulting from the string conversion.
156 */
157double atof(const char *nptr)
158{
159 return strtod(nptr, NULL);
160}
161
162/**
163 * Converts a string representation of a floating-point number to
164 * its native representation. See strtold().
165 *
166 * @param nptr String representation of a floating-point number.
167 * @param endptr Pointer to the final part of the string which
168 * was not used for conversion.
169 * @return Single-precision number resulting from the string conversion.
170 */
171float strtof(const char *restrict nptr, char **restrict endptr)
172{
173 return (float) strtold(nptr, endptr);
174}
175
176/**
177 * Converts a string representation of a floating-point number to
178 * its native representation. See strtold().
179 *
180 * @param nptr String representation of a floating-point number.
181 * @param endptr Pointer to the final part of the string which
182 * was not used for conversion.
183 * @return Double-precision number resulting from the string conversion.
184 */
185double strtod(const char *restrict nptr, char **restrict endptr)
186{
187 return (double) strtold(nptr, endptr);
188}
189
190/**
191 * Creates and opens an unique temporary file from template.
192 *
193 * @param tmpl Template. Last six characters must be XXXXXX.
194 * @return The opened file descriptor or -1 on error.
195 */
196int mkstemp(char *tmpl)
197{
198 int fd = -1;
199
200 char *tptr = tmpl + strlen(tmpl) - 6;
201
202 while (fd < 0) {
203 if (*mktemp(tmpl) == '\0') {
204 /* Errno set by mktemp(). */
205 return -1;
206 }
207
208 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
209
210 if (fd == -1) {
211 /* Restore template to it's original state. */
212 snprintf(tptr, 7, "XXXXXX");
213 }
214 }
215
216 return fd;
217}
218
219/**
220 * Creates an unique temporary file name from template.
221 *
222 * @param tmpl Template. Last six characters must be XXXXXX.
223 * @return The value of tmpl. The template is modified in place.
224 * If no temporary file name can be created, template is
225 * reduced to an empty string.
226 */
227char *mktemp(char *tmpl)
228{
229 int tmpl_len = strlen(tmpl);
230 if (tmpl_len < 6) {
231 errno = EINVAL;
232 *tmpl = '\0';
233 return tmpl;
234 }
235
236 char *tptr = tmpl + tmpl_len - 6;
237 if (strcmp(tptr, "XXXXXX") != 0) {
238 errno = EINVAL;
239 *tmpl = '\0';
240 return tmpl;
241 }
242
243 static int seq = 0;
244
245 for (; seq < 1000000; ++seq) {
246 snprintf(tptr, 7, "%06d", seq);
247
248 int orig_errno = errno;
249 errno = 0;
250 /* Check if the file exists. */
251 if (access(tmpl, F_OK) == -1) {
252 if (errno == ENOENT) {
253 errno = orig_errno;
254 break;
255 } else {
256 /* errno set by access() */
257 *tmpl = '\0';
258 return tmpl;
259 }
260 }
261 }
262
263 if (seq == 10000000) {
264 errno = EEXIST;
265 *tmpl = '\0';
266 return tmpl;
267 }
268
269 return tmpl;
270}
271
272/**
273 * Get system load average statistics.
274 *
275 * @param loadavg Array where the load averages shall be placed.
276 * @param nelem Maximum number of elements to be placed into the array.
277 * @return Number of elements placed into the array on success, -1 otherwise.
278 */
279int bsd_getloadavg(double loadavg[], int nelem)
280{
281 assert(nelem > 0);
282
283 size_t count;
284 load_t *loads = stats_get_load(&count);
285
286 if (loads == NULL) {
287 return -1;
288 }
289
290 if (((size_t) nelem) < count) {
291 count = nelem;
292 }
293
294 for (size_t i = 0; i < count; ++i) {
295 loadavg[i] = (double) loads[i];
296 }
297
298 free(loads);
299 return count;
300}
301
302/** @}
303 */
Note: See TracBrowser for help on using the repository browser.