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
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 /* 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 }
167
168 return NULL;
169}
170
171/**
172 * Retrieve a value of the given environment variable.
173 *
174 * Since HelenOS doesn't support env variables at the moment,
175 * this function always returns NULL.
176 *
177 * @param name Name of the variable.
178 * @return Value of the variable or NULL if such variable does not exist.
179 */
180char *getenv(const char *name)
181{
182 return NULL;
183}
184
185/**
186 *
187 * @param name
188 * @param resolved
189 * @return
190 */
191int putenv(char *string)
192{
193 // TODO: low priority, just a compile-time dependency of binutils
194 not_implemented();
195 return 0;
196}
197
198/**
199 * Issue a command.
200 *
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).
205 */
206int system(const char *string)
207{
208 // TODO: does nothing at the moment
209 not_implemented();
210 return 0;
211}
212
213/**
214 * Resolve absolute pathname.
215 *
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 *
222 */
223char *realpath(const char *restrict name, char *restrict resolved)
224{
225#ifndef PATH_MAX
226 assert(resolved == NULL);
227#endif
228
229 if (name == NULL) {
230 errno = EINVAL;
231 return NULL;
232 }
233
234 // TODO: symlink resolution
235
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 */
241 char *absolute = vfs_absolutize(name, NULL);
242
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 }
251
252 if (resolved == NULL) {
253 return absolute;
254 } else {
255#ifdef PATH_MAX
256 str_cpy(resolved, PATH_MAX, absolute);
257#endif
258 free(absolute);
259 return resolved;
260 }
261}
262
263/**
264 * Converts a string representation of a floating-point number to
265 * its native representation. See strtold().
266 *
267 * @param nptr String representation of a floating-point number.
268 * @return Double-precision number resulting from the string conversion.
269 */
270double atof(const char *nptr)
271{
272 return strtod(nptr, NULL);
273}
274
275/**
276 * Converts a string representation of a floating-point number to
277 * its native representation. See strtold().
278 *
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.
283 */
284float strtof(const char *restrict nptr, char **restrict endptr)
285{
286 return (float) strtold(nptr, endptr);
287}
288
289/**
290 * Converts a string representation of a floating-point number to
291 * its native representation. See strtold().
292 *
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.
297 */
298double strtod(const char *restrict nptr, char **restrict endptr)
299{
300 return (double) strtold(nptr, endptr);
301}
302
303/**
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 */
309int mkstemp(char *tmpl)
310{
311 int fd = -1;
312
313 char *tptr = tmpl + strlen(tmpl) - 6;
314
315 while (fd < 0) {
316 if (*mktemp(tmpl) == '\0') {
317 /* Errno set by mktemp(). */
318 return -1;
319 }
320
321 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
322
323 if (fd == -1) {
324 /* Restore template to it's original state. */
325 snprintf(tptr, 7, "XXXXXX");
326 }
327 }
328
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.
339 */
340char *mktemp(char *tmpl)
341{
342 int tmpl_len = strlen(tmpl);
343 if (tmpl_len < 6) {
344 errno = EINVAL;
345 *tmpl = '\0';
346 return tmpl;
347 }
348
349 char *tptr = tmpl + tmpl_len - 6;
350 if (strcmp(tptr, "XXXXXX") != 0) {
351 errno = EINVAL;
352 *tmpl = '\0';
353 return tmpl;
354 }
355
356 static int seq = 0;
357
358 for (; seq < 1000000; ++seq) {
359 snprintf(tptr, 7, "%06d", seq);
360
361 int orig_errno = errno;
362 errno = 0;
363 /* Check if the file exists. */
364 if (access(tmpl, F_OK) == -1) {
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 }
375
376 if (seq == 10000000) {
377 errno = EEXIST;
378 *tmpl = '\0';
379 return tmpl;
380 }
381
382 return tmpl;
383}
384
385/**
386 * Get system load average statistics.
387 *
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.
391 */
392int bsd_getloadavg(double loadavg[], int nelem)
393{
394 assert(nelem > 0);
395
396 size_t count;
397 load_t *loads = stats_get_load(&count);
398
399 if (loads == NULL) {
400 return -1;
401 }
402
403 if (((size_t) nelem) < count) {
404 count = nelem;
405 }
406
407 for (size_t i = 0; i < count; ++i) {
408 loadavg[i] = (double) loads[i];
409 }
410
411 free(loads);
412 return count;
413}
414
415/** @}
416 */
Note: See TracBrowser for help on using the repository browser.