source: mainline/uspace/lib/posix/src/stdlib.c@ 4e2d387

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

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 9.7 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 *
54 * @param array
55 * @param count
56 * @param size
57 * @param compare
58 */
59int atexit(void (*func)(void))
60{
61 // TODO: low priority, just a compile-time dependency of binutils
62 not_implemented();
63 return 0;
64}
65
66/**
67 * Integer absolute value.
68 *
69 * @param i Input value.
70 * @return Absolute value of the parameter.
71 */
72int abs(int i)
73{
74 return i < 0 ? -i : i;
75}
76
77/**
78 * Long integer absolute value.
79 *
80 * @param i Input value.
81 * @return Absolute value of the parameter.
82 */
83long labs(long i)
84{
85 return i < 0 ? -i : i;
86}
87
88/**
89 * Long long integer absolute value.
90 *
91 * @param i Input value.
92 * @return Absolute value of the parameter.
93 */
94long long llabs(long long i)
95{
96 return i < 0 ? -i : i;
97}
98
99/**
100 * Compute the quotient and remainder of an integer division.
101 *
102 * @param numer Numerator.
103 * @param denom Denominator.
104 * @return Quotient and remainder packed into structure.
105 */
106div_t div(int numer, int denom)
107{
108 return (div_t) { .quot = numer / denom, .rem = numer % denom };
109}
110
111/**
112 * Compute the quotient and remainder of a long integer division.
113 *
114 * @param numer Numerator.
115 * @param denom Denominator.
116 * @return Quotient and remainder packed into structure.
117 */
118ldiv_t ldiv(long numer, long denom)
119{
120 return (ldiv_t) { .quot = numer / denom, .rem = numer % denom };
121}
122
123/**
124 * Compute the quotient and remainder of a long long integer division.
125 *
126 * @param numer Numerator.
127 * @param denom Denominator.
128 * @return Quotient and remainder packed into structure.
129 */
130lldiv_t lldiv(long long numer, long long denom)
131{
132 return (lldiv_t) { .quot = numer / denom, .rem = numer % denom };
133}
134
135/**
136 * Binary search in a sorted array.
137 *
138 * @param key Object to search for.
139 * @param base Pointer to the first element of the array.
140 * @param nmemb Number of elements in the array.
141 * @param size Size of each array element.
142 * @param compar Comparison function.
143 * @return Pointer to a matching element, or NULL if none can be found.
144 */
145void *bsearch(const void *key, const void *base,
146 size_t nmemb, size_t size, int (*compar)(const void *, const void *))
147{
148 while (nmemb > 0) {
149 const void *middle = base + (nmemb / 2) * size;
150 int cmp = compar(key, middle);
151 if (cmp == 0) {
152 return (void *) middle;
153 }
154 if (middle == base) {
155 /*
156 * There is just one member left to check and it
157 * didn't match the key. Avoid infinite loop.
158 */
159 break;
160 }
161 if (cmp < 0) {
162 nmemb = nmemb / 2;
163 } else if (cmp > 0) {
164 nmemb = nmemb - (nmemb / 2);
165 base = middle;
166 }
167 }
168
169 return NULL;
170}
171
172/**
173 * Retrieve a value of the given environment variable.
174 *
175 * Since HelenOS doesn't support env variables at the moment,
176 * this function always returns NULL.
177 *
178 * @param name Name of the variable.
179 * @return Value of the variable or NULL if such variable does not exist.
180 */
181char *getenv(const char *name)
182{
183 return NULL;
184}
185
186/**
187 *
188 * @param name
189 * @param resolved
190 * @return
191 */
192int putenv(char *string)
193{
194 // TODO: low priority, just a compile-time dependency of binutils
195 not_implemented();
196 return 0;
197}
198
199/**
200 * Issue a command.
201 *
202 * @param string String to be passed to a command interpreter or NULL.
203 * @return Termination status of the command if the command is not NULL,
204 * otherwise indicate whether there is a command interpreter (non-zero)
205 * or not (zero).
206 */
207int system(const char *string)
208{
209 // TODO: does nothing at the moment
210 not_implemented();
211 return 0;
212}
213
214/**
215 * Resolve absolute pathname.
216 *
217 * @param name Pathname to be resolved.
218 * @param resolved Either buffer for the resolved absolute pathname or NULL.
219 * @return On success, either resolved (if it was not NULL) or pointer to the
220 * newly allocated buffer containing the absolute pathname (if resolved was
221 * NULL). Otherwise NULL.
222 *
223 */
224char *realpath(const char *restrict name, char *restrict resolved)
225{
226#ifndef PATH_MAX
227 assert(resolved == NULL);
228#endif
229
230 if (name == NULL) {
231 errno = EINVAL;
232 return NULL;
233 }
234
235 // TODO: symlink resolution
236
237 /*
238 * Function absolutize is implemented in libc and declared in vfs.h.
239 * No more processing is required as HelenOS doesn't have symlinks
240 * so far (as far as I can tell), although this function will need
241 * to be updated when that support is implemented.
242 */
243 char *absolute = vfs_absolutize(name, NULL);
244
245 if (absolute == NULL) {
246 /*
247 * POSIX requires some specific errnos to be set
248 * for some cases, but there is no way to find out from
249 * absolutize().
250 */
251 errno = EINVAL;
252 return NULL;
253 }
254
255 if (resolved == NULL) {
256 return absolute;
257 } else {
258#ifdef PATH_MAX
259 str_cpy(resolved, PATH_MAX, absolute);
260#endif
261 free(absolute);
262 return resolved;
263 }
264}
265
266/**
267 * Converts a string representation of a floating-point number to
268 * its native representation. See strtold().
269 *
270 * @param nptr String representation of a floating-point number.
271 * @return Double-precision number resulting from the string conversion.
272 */
273double atof(const char *nptr)
274{
275 return strtod(nptr, NULL);
276}
277
278/**
279 * Converts a string representation of a floating-point number to
280 * its native representation. See strtold().
281 *
282 * @param nptr String representation of a floating-point number.
283 * @param endptr Pointer to the final part of the string which
284 * was not used for conversion.
285 * @return Single-precision number resulting from the string conversion.
286 */
287float strtof(const char *restrict nptr, char **restrict endptr)
288{
289 return (float) strtold(nptr, endptr);
290}
291
292/**
293 * Converts a string representation of a floating-point number to
294 * its native representation. See strtold().
295 *
296 * @param nptr String representation of a floating-point number.
297 * @param endptr Pointer to the final part of the string which
298 * was not used for conversion.
299 * @return Double-precision number resulting from the string conversion.
300 */
301double strtod(const char *restrict nptr, char **restrict endptr)
302{
303 return (double) strtold(nptr, endptr);
304}
305
306/**
307 * Creates and opens an unique temporary file from template.
308 *
309 * @param tmpl Template. Last six characters must be XXXXXX.
310 * @return The opened file descriptor or -1 on error.
311 */
312int mkstemp(char *tmpl)
313{
314 int fd = -1;
315
316 char *tptr = tmpl + strlen(tmpl) - 6;
317
318 while (fd < 0) {
319 if (*mktemp(tmpl) == '\0') {
320 /* Errno set by mktemp(). */
321 return -1;
322 }
323
324 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
325
326 if (fd == -1) {
327 /* Restore template to it's original state. */
328 snprintf(tptr, 7, "XXXXXX");
329 }
330 }
331
332 return fd;
333}
334
335/**
336 * Creates an unique temporary file name from template.
337 *
338 * @param tmpl Template. Last six characters must be XXXXXX.
339 * @return The value of tmpl. The template is modified in place.
340 * If no temporary file name can be created, template is
341 * reduced to an empty string.
342 */
343char *mktemp(char *tmpl)
344{
345 int tmpl_len = strlen(tmpl);
346 if (tmpl_len < 6) {
347 errno = EINVAL;
348 *tmpl = '\0';
349 return tmpl;
350 }
351
352 char *tptr = tmpl + tmpl_len - 6;
353 if (strcmp(tptr, "XXXXXX") != 0) {
354 errno = EINVAL;
355 *tmpl = '\0';
356 return tmpl;
357 }
358
359 static int seq = 0;
360
361 for (; seq < 1000000; ++seq) {
362 snprintf(tptr, 7, "%06d", seq);
363
364 int orig_errno = errno;
365 errno = 0;
366 /* Check if the file exists. */
367 if (access(tmpl, F_OK) == -1) {
368 if (errno == ENOENT) {
369 errno = orig_errno;
370 break;
371 } else {
372 /* errno set by access() */
373 *tmpl = '\0';
374 return tmpl;
375 }
376 }
377 }
378
379 if (seq == 10000000) {
380 errno = EEXIST;
381 *tmpl = '\0';
382 return tmpl;
383 }
384
385 return tmpl;
386}
387
388/**
389 * Get system load average statistics.
390 *
391 * @param loadavg Array where the load averages shall be placed.
392 * @param nelem Maximum number of elements to be placed into the array.
393 * @return Number of elements placed into the array on success, -1 otherwise.
394 */
395int bsd_getloadavg(double loadavg[], int nelem)
396{
397 assert(nelem > 0);
398
399 size_t count;
400 load_t *loads = stats_get_load(&count);
401
402 if (loads == NULL) {
403 return -1;
404 }
405
406 if (((size_t) nelem) < count) {
407 count = nelem;
408 }
409
410 for (size_t i = 0; i < count; ++i) {
411 loadavg[i] = (double) loads[i];
412 }
413
414 free(loads);
415 return count;
416}
417
418/** @}
419 */
Note: See TracBrowser for help on using the repository browser.