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