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