Changes in uspace/lib/posix/stdlib.c [63fc519:ec18957a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/stdlib.c
r63fc519 rec18957a 36 36 #define LIBPOSIX_INTERNAL 37 37 38 #include "internal/common.h" 38 39 #include "stdlib.h" 39 #include "internal/common.h" 40 41 /** 42 * 40 41 #include "errno.h" 42 43 #include "libc/sort.h" 44 #include "libc/str.h" 45 #include "libc/vfs/vfs.h" 46 47 /** 48 * 43 49 * @param array 44 50 * @param count … … 46 52 * @param compare 47 53 */ 54 int posix_atexit(void (*func)(void)) 55 { 56 // TODO: low priority, just a compile-time dependency of binutils 57 not_implemented(); 58 } 59 60 /** 61 * 62 * @param i Input value. 63 * @return Absolute value of the parameter. 64 */ 65 int posix_abs(int i) 66 { 67 return i < 0 ? -i : i; 68 } 69 70 /** 71 * 72 * @param i Input value. 73 * @return Absolute value of the parameter. 74 */ 75 long posix_labs(long i) 76 { 77 return i < 0 ? -i : i; 78 } 79 80 /** 81 * 82 * @param i Input value. 83 * @return Absolute value of the parameter. 84 */ 85 long long posix_llabs(long long i) 86 { 87 return i < 0 ? -i : i; 88 } 89 90 posix_div_t posix_div(int numer, int denom) 91 { 92 return (posix_div_t) { .quot = numer / denom, .rem = numer % denom }; 93 } 94 95 posix_ldiv_t posix_ldiv(long numer, long denom) 96 { 97 return (posix_ldiv_t) { .quot = numer / denom, .rem = numer % denom }; 98 } 99 100 posix_lldiv_t posix_lldiv(long long numer, long long denom) 101 { 102 return (posix_lldiv_t) { .quot = numer / denom, .rem = numer % denom }; 103 } 104 105 /** 106 * Private helper function that serves as a compare function for qsort(). 107 * 108 * @param elem1 First element to compare. 109 * @param elem2 Second element to compare. 110 * @param compare Comparison function without userdata parameter. 111 * 112 * @return Relative ordering of the elements. 113 */ 114 static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata) 115 { 116 int (*compare)(const void *, const void *) = userdata; 117 return compare(elem1, elem2); 118 } 119 120 /** 121 * Array sorting utilizing the quicksort algorithm. 122 * 123 * @param array 124 * @param count 125 * @param size 126 * @param compare 127 */ 48 128 void posix_qsort(void *array, size_t count, size_t size, 49 129 int (*compare)(const void *, const void *)) 50 130 { 51 // TODO 52 not_implemented(); 53 } 54 55 /** 131 /* Implemented in libc with one extra argument. */ 132 qsort(array, count, size, sort_compare_wrapper, compare); 133 } 134 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 */ 145 void *posix_bsearch(const void *key, const void *base, 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 171 /** 172 * Retrieve a value of the given environment variable. 173 * Since HelenOS doesn't support env variables at the moment, 174 * this function always returns NULL. 56 175 * 57 176 * @param name 58 * @return 177 * @return Always NULL. 59 178 */ 60 179 char *posix_getenv(const char *name) 61 180 { 62 // TODO 63 not_implemented(); 181 return NULL; 64 182 } 65 183 … … 70 188 * @return 71 189 */ 190 int posix_putenv(char *string) 191 { 192 // TODO: low priority, just a compile-time dependency of binutils 193 not_implemented(); 194 } 195 196 /** 197 * 198 * @param string String to be passed to a command interpreter. 199 * @return 200 */ 201 int posix_system(const char *string) { 202 // TODO: does nothing at the moment 203 return 0; 204 } 205 206 /** 207 * 208 * @param name 209 * @param resolved 210 * @return 211 */ 72 212 char *posix_realpath(const char *name, char *resolved) 73 213 { 74 // TODO 75 not_implemented(); 214 #ifndef PATH_MAX 215 assert(resolved == NULL); 216 #endif 217 218 if (name == NULL) { 219 errno = EINVAL; 220 return NULL; 221 } 222 223 // TODO: symlink resolution 224 225 /* Function absolutize is implemented in libc and declared in vfs.h. 226 * No more processing is required as HelenOS doesn't have symlinks 227 * so far (as far as I can tell), although this function will need 228 * to be updated when that support is implemented. 229 */ 230 char* absolute = absolutize(name, NULL); 231 232 if (absolute == NULL) { 233 /* POSIX requires some specific errnos to be set 234 * for some cases, but there is no way to find out from 235 * absolutize(). 236 */ 237 errno = EINVAL; 238 return NULL; 239 } 240 241 if (resolved == NULL) { 242 return absolute; 243 } else { 244 #ifdef PATH_MAX 245 str_cpy(resolved, PATH_MAX, absolute); 246 #endif 247 free(absolute); 248 return resolved; 249 } 76 250 } 77 251 … … 81 255 * 82 256 * @param nptr 83 * @param endptr 84 * @return 85 */ 86 float posix_strtof(const char *restrict nptr, char **restrict endptr) 87 { 88 return (float) posix_strtold(nptr, endptr); 257 * @return 258 */ 259 double posix_atof(const char *nptr) 260 { 261 return posix_strtod(nptr, NULL); 89 262 } 90 263 … … 97 270 * @return 98 271 */ 272 float posix_strtof(const char *restrict nptr, char **restrict endptr) 273 { 274 return (float) posix_strtold(nptr, endptr); 275 } 276 277 /** 278 * Converts a string representation of a floating-point number to 279 * its native representation. See posix_strtold(). 280 * 281 * @param nptr 282 * @param endptr 283 * @return 284 */ 99 285 double posix_strtod(const char *restrict nptr, char **restrict endptr) 100 286 { … … 103 289 104 290 /** 105 * 106 * @param str 107 * @return 108 */ 109 int posix_atoi(const char *str) 110 { 111 // TODO 291 * 292 * @param size 293 * @return 294 */ 295 void *posix_malloc(size_t size) 296 { 297 return malloc(size); 298 } 299 300 /** 301 * 302 * @param nelem 303 * @param elsize 304 * @return 305 */ 306 void *posix_calloc(size_t nelem, size_t elsize) 307 { 308 return calloc(nelem, elsize); 309 } 310 311 /** 312 * 313 * @param ptr 314 * @param size 315 * @return 316 */ 317 void *posix_realloc(void *ptr, size_t size) 318 { 319 return realloc(ptr, size); 320 } 321 322 /** 323 * 324 * @param ptr 325 */ 326 void posix_free(void *ptr) 327 { 328 free(ptr); 329 } 330 331 /** 332 * 333 * @param tmpl 334 * @return 335 */ 336 char *posix_mktemp(char *tmpl) 337 { 338 // TODO: low priority, just a compile-time dependency of binutils 112 339 not_implemented(); 113 340 } 114 341 342 /** 343 * Should read system load statistics. Not supported. Always returns -1. 344 * 345 * @param loadavg 346 * @param nelem 347 * @return 348 */ 349 int bsd_getloadavg(double loadavg[], int nelem) 350 { 351 return -1; 352 } 353 115 354 /** @} 116 355 */
Note:
See TracChangeset
for help on using the changeset viewer.