[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] | 59 | int 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] | 72 | int 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] | 83 | long 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] | 94 | long 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] | 106 | div_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] | 118 | ldiv_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] | 130 | lldiv_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] | 145 | void *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 | }
|
---|
| 167 |
|
---|
| 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] | 180 | char *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] | 191 | int 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 | */
|
---|
[7f9df7b9] | 206 | int system(const char *string) {
|
---|
[cc3652db] | 207 | // TODO: does nothing at the moment
|
---|
[e965dec] | 208 | not_implemented();
|
---|
[cc3652db] | 209 | return 0;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[2fc5072] | 212 | /**
|
---|
[087c8798] | 213 | * Resolve absolute pathname.
|
---|
[1b20da0] | 214 | *
|
---|
[087c8798] | 215 | * @param name Pathname to be resolved.
|
---|
| 216 | * @param resolved Either buffer for the resolved absolute pathname or NULL.
|
---|
| 217 | * @return On success, either resolved (if it was not NULL) or pointer to the
|
---|
| 218 | * newly allocated buffer containing the absolute pathname (if resolved was
|
---|
| 219 | * NULL). Otherwise NULL.
|
---|
| 220 | *
|
---|
[2fc5072] | 221 | */
|
---|
[7f9df7b9] | 222 | char *realpath(const char *restrict name, char *restrict resolved)
|
---|
[2fc5072] | 223 | {
|
---|
[2b83add] | 224 | #ifndef PATH_MAX
|
---|
| 225 | assert(resolved == NULL);
|
---|
| 226 | #endif
|
---|
| 227 |
|
---|
| 228 | if (name == NULL) {
|
---|
| 229 | errno = EINVAL;
|
---|
| 230 | return NULL;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | // TODO: symlink resolution
|
---|
| 234 |
|
---|
| 235 | /* Function absolutize is implemented in libc and declared in vfs.h.
|
---|
| 236 | * No more processing is required as HelenOS doesn't have symlinks
|
---|
| 237 | * so far (as far as I can tell), although this function will need
|
---|
| 238 | * to be updated when that support is implemented.
|
---|
| 239 | */
|
---|
[6afc9d7] | 240 | char* absolute = vfs_absolutize(name, NULL);
|
---|
[2b83add] | 241 |
|
---|
| 242 | if (absolute == NULL) {
|
---|
| 243 | /* POSIX requires some specific errnos to be set
|
---|
| 244 | * for some cases, but there is no way to find out from
|
---|
| 245 | * absolutize().
|
---|
| 246 | */
|
---|
| 247 | errno = EINVAL;
|
---|
| 248 | return NULL;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if (resolved == NULL) {
|
---|
| 252 | return absolute;
|
---|
| 253 | } else {
|
---|
| 254 | #ifdef PATH_MAX
|
---|
| 255 | str_cpy(resolved, PATH_MAX, absolute);
|
---|
| 256 | #endif
|
---|
| 257 | free(absolute);
|
---|
| 258 | return resolved;
|
---|
| 259 | }
|
---|
[ceebf0a] | 260 | }
|
---|
| 261 |
|
---|
| 262 | /**
|
---|
[63fc519] | 263 | * Converts a string representation of a floating-point number to
|
---|
[7f9df7b9] | 264 | * its native representation. See strtold().
|
---|
[63fc519] | 265 | *
|
---|
[087c8798] | 266 | * @param nptr String representation of a floating-point number.
|
---|
| 267 | * @return Double-precision number resulting from the string conversion.
|
---|
[ceebf0a] | 268 | */
|
---|
[7f9df7b9] | 269 | double atof(const char *nptr)
|
---|
[ceebf0a] | 270 | {
|
---|
[7f9df7b9] | 271 | return strtod(nptr, NULL);
|
---|
[ceebf0a] | 272 | }
|
---|
| 273 |
|
---|
| 274 | /**
|
---|
[63fc519] | 275 | * Converts a string representation of a floating-point number to
|
---|
[7f9df7b9] | 276 | * its native representation. See strtold().
|
---|
[63fc519] | 277 | *
|
---|
[087c8798] | 278 | * @param nptr String representation of a floating-point number.
|
---|
| 279 | * @param endptr Pointer to the final part of the string which
|
---|
| 280 | * was not used for conversion.
|
---|
| 281 | * @return Single-precision number resulting from the string conversion.
|
---|
[ceebf0a] | 282 | */
|
---|
[7f9df7b9] | 283 | float strtof(const char *restrict nptr, char **restrict endptr)
|
---|
[ceebf0a] | 284 | {
|
---|
[7f9df7b9] | 285 | return (float) strtold(nptr, endptr);
|
---|
[2fc5072] | 286 | }
|
---|
| 287 |
|
---|
[09b0b1fb] | 288 | /**
|
---|
[2b83add] | 289 | * Converts a string representation of a floating-point number to
|
---|
[7f9df7b9] | 290 | * its native representation. See strtold().
|
---|
[2b83add] | 291 | *
|
---|
[087c8798] | 292 | * @param nptr String representation of a floating-point number.
|
---|
| 293 | * @param endptr Pointer to the final part of the string which
|
---|
| 294 | * was not used for conversion.
|
---|
| 295 | * @return Double-precision number resulting from the string conversion.
|
---|
[09b0b1fb] | 296 | */
|
---|
[7f9df7b9] | 297 | double strtod(const char *restrict nptr, char **restrict endptr)
|
---|
[d856110] | 298 | {
|
---|
[7f9df7b9] | 299 | return (double) strtold(nptr, endptr);
|
---|
[d856110] | 300 | }
|
---|
| 301 |
|
---|
[823a929] | 302 | /**
|
---|
[11544f4] | 303 | * Creates and opens an unique temporary file from template.
|
---|
| 304 | *
|
---|
| 305 | * @param tmpl Template. Last six characters must be XXXXXX.
|
---|
| 306 | * @return The opened file descriptor or -1 on error.
|
---|
| 307 | */
|
---|
[7f9df7b9] | 308 | int mkstemp(char *tmpl)
|
---|
[11544f4] | 309 | {
|
---|
| 310 | int fd = -1;
|
---|
| 311 |
|
---|
[7f9df7b9] | 312 | char *tptr = tmpl + strlen(tmpl) - 6;
|
---|
[11544f4] | 313 |
|
---|
| 314 | while (fd < 0) {
|
---|
[7f9df7b9] | 315 | if (*mktemp(tmpl) == '\0') {
|
---|
[11544f4] | 316 | /* Errno set by mktemp(). */
|
---|
| 317 | return -1;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[7f9df7b9] | 320 | fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
---|
[11544f4] | 321 |
|
---|
| 322 | if (fd == -1) {
|
---|
| 323 | /* Restore template to it's original state. */
|
---|
| 324 | snprintf(tptr, 7, "XXXXXX");
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | return fd;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
| 332 | * Creates an unique temporary file name from template.
|
---|
| 333 | *
|
---|
| 334 | * @param tmpl Template. Last six characters must be XXXXXX.
|
---|
| 335 | * @return The value of tmpl. The template is modified in place.
|
---|
| 336 | * If no temporary file name can be created, template is
|
---|
| 337 | * reduced to an empty string.
|
---|
[823a929] | 338 | */
|
---|
[7f9df7b9] | 339 | char *mktemp(char *tmpl)
|
---|
[823a929] | 340 | {
|
---|
[7f9df7b9] | 341 | int tmpl_len = strlen(tmpl);
|
---|
[11544f4] | 342 | if (tmpl_len < 6) {
|
---|
| 343 | errno = EINVAL;
|
---|
| 344 | *tmpl = '\0';
|
---|
| 345 | return tmpl;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | char *tptr = tmpl + tmpl_len - 6;
|
---|
[7f9df7b9] | 349 | if (strcmp(tptr, "XXXXXX") != 0) {
|
---|
[11544f4] | 350 | errno = EINVAL;
|
---|
| 351 | *tmpl = '\0';
|
---|
| 352 | return tmpl;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | static int seq = 0;
|
---|
| 356 |
|
---|
| 357 | for (; seq < 1000000; ++seq) {
|
---|
| 358 | snprintf(tptr, 7, "%06d", seq);
|
---|
| 359 |
|
---|
| 360 | int orig_errno = errno;
|
---|
| 361 | errno = 0;
|
---|
| 362 | /* Check if the file exists. */
|
---|
[7f9df7b9] | 363 | if (access(tmpl, F_OK) == -1) {
|
---|
[11544f4] | 364 | if (errno == ENOENT) {
|
---|
| 365 | errno = orig_errno;
|
---|
| 366 | break;
|
---|
| 367 | } else {
|
---|
| 368 | /* errno set by access() */
|
---|
| 369 | *tmpl = '\0';
|
---|
| 370 | return tmpl;
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | if (seq == 10000000) {
|
---|
| 376 | errno = EEXIST;
|
---|
| 377 | *tmpl = '\0';
|
---|
| 378 | return tmpl;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | return tmpl;
|
---|
[823a929] | 382 | }
|
---|
| 383 |
|
---|
[3acff69] | 384 | /**
|
---|
[087c8798] | 385 | * Get system load average statistics.
|
---|
[3acff69] | 386 | *
|
---|
[087c8798] | 387 | * @param loadavg Array where the load averages shall be placed.
|
---|
| 388 | * @param nelem Maximum number of elements to be placed into the array.
|
---|
| 389 | * @return Number of elements placed into the array on success, -1 otherwise.
|
---|
[3acff69] | 390 | */
|
---|
| 391 | int bsd_getloadavg(double loadavg[], int nelem)
|
---|
| 392 | {
|
---|
[0d33863] | 393 | assert(nelem > 0);
|
---|
| 394 |
|
---|
| 395 | size_t count;
|
---|
| 396 | load_t *loads = stats_get_load(&count);
|
---|
| 397 |
|
---|
| 398 | if (loads == NULL) {
|
---|
| 399 | return -1;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | if (((size_t) nelem) < count) {
|
---|
| 403 | count = nelem;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | for (size_t i = 0; i < count; ++i) {
|
---|
| 407 | loadavg[i] = (double) loads[i];
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | free(loads);
|
---|
| 411 | return count;
|
---|
[3acff69] | 412 | }
|
---|
| 413 |
|
---|
[2fc5072] | 414 | /** @}
|
---|
| 415 | */
|
---|