source: mainline/uspace/lib/posix/src/stdlib.c@ 1433ecda

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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