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

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

abs, labs, llabs.

  • Property mode set to 100644
File size: 6.6 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 name
55 * @param resolved
56 * @return
57 */
[7f9df7b9]58int putenv(char *string)
[823a929]59{
60 // TODO: low priority, just a compile-time dependency of binutils
61 not_implemented();
[e965dec]62 return 0;
[823a929]63}
64
[2fc5072]65/**
[087c8798]66 * Resolve absolute pathname.
[1b20da0]67 *
[087c8798]68 * @param name Pathname to be resolved.
69 * @param resolved Either buffer for the resolved absolute pathname or NULL.
70 * @return On success, either resolved (if it was not NULL) or pointer to the
71 * newly allocated buffer containing the absolute pathname (if resolved was
72 * NULL). Otherwise NULL.
73 *
[2fc5072]74 */
[7f9df7b9]75char *realpath(const char *restrict name, char *restrict resolved)
[2fc5072]76{
[1433ecda]77#ifndef PATH_MAX
78 assert(resolved == NULL);
79#endif
[a35b458]80
[2b83add]81 if (name == NULL) {
82 errno = EINVAL;
83 return NULL;
84 }
[a35b458]85
[2b83add]86 // TODO: symlink resolution
[a35b458]87
[7c3fb9b]88 /*
89 * Function absolutize is implemented in libc and declared in vfs.h.
[2b83add]90 * No more processing is required as HelenOS doesn't have symlinks
91 * so far (as far as I can tell), although this function will need
92 * to be updated when that support is implemented.
93 */
[1433ecda]94 char *absolute = vfs_absolutize(name, NULL);
[a35b458]95
[2b83add]96 if (absolute == NULL) {
[7c3fb9b]97 /*
98 * POSIX requires some specific errnos to be set
[2b83add]99 * for some cases, but there is no way to find out from
100 * absolutize().
101 */
102 errno = EINVAL;
103 return NULL;
104 }
[a35b458]105
[2b83add]106 if (resolved == NULL) {
107 return absolute;
108 } else {
[1433ecda]109#ifdef PATH_MAX
110 str_cpy(resolved, PATH_MAX, absolute);
111#endif
[2b83add]112 free(absolute);
113 return resolved;
114 }
[ceebf0a]115}
116
117/**
[63fc519]118 * Converts a string representation of a floating-point number to
[7f9df7b9]119 * its native representation. See strtold().
[63fc519]120 *
[087c8798]121 * @param nptr String representation of a floating-point number.
122 * @return Double-precision number resulting from the string conversion.
[ceebf0a]123 */
[7f9df7b9]124double atof(const char *nptr)
[ceebf0a]125{
[7f9df7b9]126 return strtod(nptr, NULL);
[ceebf0a]127}
128
129/**
[63fc519]130 * Converts a string representation of a floating-point number to
[7f9df7b9]131 * its native representation. See strtold().
[63fc519]132 *
[087c8798]133 * @param nptr String representation of a floating-point number.
134 * @param endptr Pointer to the final part of the string which
135 * was not used for conversion.
136 * @return Single-precision number resulting from the string conversion.
[ceebf0a]137 */
[7f9df7b9]138float strtof(const char *restrict nptr, char **restrict endptr)
[ceebf0a]139{
[7f9df7b9]140 return (float) strtold(nptr, endptr);
[2fc5072]141}
142
[09b0b1fb]143/**
[2b83add]144 * Converts a string representation of a floating-point number to
[7f9df7b9]145 * its native representation. See strtold().
[2b83add]146 *
[087c8798]147 * @param nptr String representation of a floating-point number.
148 * @param endptr Pointer to the final part of the string which
149 * was not used for conversion.
150 * @return Double-precision number resulting from the string conversion.
[09b0b1fb]151 */
[7f9df7b9]152double strtod(const char *restrict nptr, char **restrict endptr)
[d856110]153{
[7f9df7b9]154 return (double) strtold(nptr, endptr);
[d856110]155}
156
[823a929]157/**
[11544f4]158 * Creates and opens an unique temporary file from template.
159 *
160 * @param tmpl Template. Last six characters must be XXXXXX.
161 * @return The opened file descriptor or -1 on error.
162 */
[7f9df7b9]163int mkstemp(char *tmpl)
[11544f4]164{
165 int fd = -1;
[a35b458]166
[7f9df7b9]167 char *tptr = tmpl + strlen(tmpl) - 6;
[a35b458]168
[11544f4]169 while (fd < 0) {
[7f9df7b9]170 if (*mktemp(tmpl) == '\0') {
[11544f4]171 /* Errno set by mktemp(). */
172 return -1;
173 }
[a35b458]174
[7f9df7b9]175 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
[a35b458]176
[11544f4]177 if (fd == -1) {
178 /* Restore template to it's original state. */
179 snprintf(tptr, 7, "XXXXXX");
180 }
181 }
[a35b458]182
[11544f4]183 return fd;
184}
185
186/**
187 * Creates an unique temporary file name from template.
188 *
189 * @param tmpl Template. Last six characters must be XXXXXX.
190 * @return The value of tmpl. The template is modified in place.
191 * If no temporary file name can be created, template is
192 * reduced to an empty string.
[823a929]193 */
[7f9df7b9]194char *mktemp(char *tmpl)
[823a929]195{
[7f9df7b9]196 int tmpl_len = strlen(tmpl);
[11544f4]197 if (tmpl_len < 6) {
198 errno = EINVAL;
199 *tmpl = '\0';
200 return tmpl;
201 }
[a35b458]202
[11544f4]203 char *tptr = tmpl + tmpl_len - 6;
[7f9df7b9]204 if (strcmp(tptr, "XXXXXX") != 0) {
[11544f4]205 errno = EINVAL;
206 *tmpl = '\0';
207 return tmpl;
208 }
[a35b458]209
[11544f4]210 static int seq = 0;
[a35b458]211
[11544f4]212 for (; seq < 1000000; ++seq) {
213 snprintf(tptr, 7, "%06d", seq);
[a35b458]214
[11544f4]215 int orig_errno = errno;
216 errno = 0;
217 /* Check if the file exists. */
[7f9df7b9]218 if (access(tmpl, F_OK) == -1) {
[11544f4]219 if (errno == ENOENT) {
220 errno = orig_errno;
221 break;
222 } else {
223 /* errno set by access() */
224 *tmpl = '\0';
225 return tmpl;
226 }
227 }
228 }
[a35b458]229
[11544f4]230 if (seq == 10000000) {
231 errno = EEXIST;
232 *tmpl = '\0';
233 return tmpl;
234 }
[a35b458]235
[11544f4]236 return tmpl;
[823a929]237}
238
[3acff69]239/**
[087c8798]240 * Get system load average statistics.
[3acff69]241 *
[087c8798]242 * @param loadavg Array where the load averages shall be placed.
243 * @param nelem Maximum number of elements to be placed into the array.
244 * @return Number of elements placed into the array on success, -1 otherwise.
[3acff69]245 */
246int bsd_getloadavg(double loadavg[], int nelem)
247{
[0d33863]248 assert(nelem > 0);
[a35b458]249
[0d33863]250 size_t count;
251 load_t *loads = stats_get_load(&count);
[a35b458]252
[0d33863]253 if (loads == NULL) {
254 return -1;
255 }
[a35b458]256
[0d33863]257 if (((size_t) nelem) < count) {
258 count = nelem;
259 }
[a35b458]260
[0d33863]261 for (size_t i = 0; i < count; ++i) {
262 loadavg[i] = (double) loads[i];
263 }
[a35b458]264
[0d33863]265 free(loads);
266 return count;
[3acff69]267}
268
[2fc5072]269/** @}
270 */
Note: See TracBrowser for help on using the repository browser.