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

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

abs, labs, llabs.

  • Property mode set to 100644
File size: 6.6 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 name
55 * @param resolved
56 * @return
57 */
58int putenv(char *string)
59{
60 // TODO: low priority, just a compile-time dependency of binutils
61 not_implemented();
62 return 0;
63}
64
65/**
66 * Resolve absolute pathname.
67 *
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 *
74 */
75char *realpath(const char *restrict name, char *restrict resolved)
76{
77#ifndef PATH_MAX
78 assert(resolved == NULL);
79#endif
80
81 if (name == NULL) {
82 errno = EINVAL;
83 return NULL;
84 }
85
86 // TODO: symlink resolution
87
88 /*
89 * Function absolutize is implemented in libc and declared in vfs.h.
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 */
94 char *absolute = vfs_absolutize(name, NULL);
95
96 if (absolute == NULL) {
97 /*
98 * POSIX requires some specific errnos to be set
99 * for some cases, but there is no way to find out from
100 * absolutize().
101 */
102 errno = EINVAL;
103 return NULL;
104 }
105
106 if (resolved == NULL) {
107 return absolute;
108 } else {
109#ifdef PATH_MAX
110 str_cpy(resolved, PATH_MAX, absolute);
111#endif
112 free(absolute);
113 return resolved;
114 }
115}
116
117/**
118 * Converts a string representation of a floating-point number to
119 * its native representation. See strtold().
120 *
121 * @param nptr String representation of a floating-point number.
122 * @return Double-precision number resulting from the string conversion.
123 */
124double atof(const char *nptr)
125{
126 return strtod(nptr, NULL);
127}
128
129/**
130 * Converts a string representation of a floating-point number to
131 * its native representation. See strtold().
132 *
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.
137 */
138float strtof(const char *restrict nptr, char **restrict endptr)
139{
140 return (float) strtold(nptr, endptr);
141}
142
143/**
144 * Converts a string representation of a floating-point number to
145 * its native representation. See strtold().
146 *
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.
151 */
152double strtod(const char *restrict nptr, char **restrict endptr)
153{
154 return (double) strtold(nptr, endptr);
155}
156
157/**
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 */
163int mkstemp(char *tmpl)
164{
165 int fd = -1;
166
167 char *tptr = tmpl + strlen(tmpl) - 6;
168
169 while (fd < 0) {
170 if (*mktemp(tmpl) == '\0') {
171 /* Errno set by mktemp(). */
172 return -1;
173 }
174
175 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
176
177 if (fd == -1) {
178 /* Restore template to it's original state. */
179 snprintf(tptr, 7, "XXXXXX");
180 }
181 }
182
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.
193 */
194char *mktemp(char *tmpl)
195{
196 int tmpl_len = strlen(tmpl);
197 if (tmpl_len < 6) {
198 errno = EINVAL;
199 *tmpl = '\0';
200 return tmpl;
201 }
202
203 char *tptr = tmpl + tmpl_len - 6;
204 if (strcmp(tptr, "XXXXXX") != 0) {
205 errno = EINVAL;
206 *tmpl = '\0';
207 return tmpl;
208 }
209
210 static int seq = 0;
211
212 for (; seq < 1000000; ++seq) {
213 snprintf(tptr, 7, "%06d", seq);
214
215 int orig_errno = errno;
216 errno = 0;
217 /* Check if the file exists. */
218 if (access(tmpl, F_OK) == -1) {
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 }
229
230 if (seq == 10000000) {
231 errno = EEXIST;
232 *tmpl = '\0';
233 return tmpl;
234 }
235
236 return tmpl;
237}
238
239/**
240 * Get system load average statistics.
241 *
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.
245 */
246int bsd_getloadavg(double loadavg[], int nelem)
247{
248 assert(nelem > 0);
249
250 size_t count;
251 load_t *loads = stats_get_load(&count);
252
253 if (loads == NULL) {
254 return -1;
255 }
256
257 if (((size_t) nelem) < count) {
258 count = nelem;
259 }
260
261 for (size_t i = 0; i < count; ++i) {
262 loadavg[i] = (double) loads[i];
263 }
264
265 free(loads);
266 return count;
267}
268
269/** @}
270 */
Note: See TracBrowser for help on using the repository browser.