stdlib.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/StdLibExtras.h>
  29. #include <AK/Types.h>
  30. #include <AK/Utf8View.h>
  31. #include <Kernel/Syscall.h>
  32. #include <alloca.h>
  33. #include <assert.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36. #include <signal.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys/mman.h>
  41. #include <sys/stat.h>
  42. #include <sys/wait.h>
  43. #include <unistd.h>
  44. template<typename T, T min_value, T max_value>
  45. static inline T strtol_impl(const char* nptr, char** endptr, int base)
  46. {
  47. errno = 0;
  48. if (base < 0 || base == 1 || base > 36) {
  49. errno = EINVAL;
  50. if (endptr)
  51. *endptr = const_cast<char*>(nptr);
  52. return 0;
  53. }
  54. const char* p = nptr;
  55. while (isspace(*p))
  56. ++p;
  57. bool is_negative = false;
  58. if (*p == '-') {
  59. is_negative = true;
  60. ++p;
  61. } else {
  62. if (*p == '+')
  63. ++p;
  64. }
  65. if (base == 0 || base == 16) {
  66. if (base == 0)
  67. base = 10;
  68. if (*p == '0') {
  69. if (*(p + 1) == 'X' || *(p + 1) == 'x') {
  70. p += 2;
  71. base = 16;
  72. } else if (base != 16) {
  73. base = 8;
  74. }
  75. }
  76. }
  77. T cutoff_point = is_negative ? (min_value / base) : (max_value / base);
  78. int max_valid_digit_at_cutoff_point = is_negative ? (min_value % base) : (max_value % base);
  79. T num = 0;
  80. bool has_overflowed = false;
  81. unsigned digits_consumed = 0;
  82. for (;;) {
  83. char ch = *(p++);
  84. int digit;
  85. if (isdigit(ch))
  86. digit = ch - '0';
  87. else if (islower(ch))
  88. digit = ch - ('a' - 10);
  89. else if (isupper(ch))
  90. digit = ch - ('A' - 10);
  91. else
  92. break;
  93. if (digit >= base)
  94. break;
  95. if (has_overflowed)
  96. continue;
  97. bool is_past_cutoff = is_negative ? num < cutoff_point : num > cutoff_point;
  98. if (is_past_cutoff || (num == cutoff_point && digit > max_valid_digit_at_cutoff_point)) {
  99. has_overflowed = true;
  100. num = is_negative ? min_value : max_value;
  101. errno = ERANGE;
  102. } else {
  103. num *= base;
  104. num += is_negative ? -digit : digit;
  105. ++digits_consumed;
  106. }
  107. }
  108. if (endptr) {
  109. if (has_overflowed || digits_consumed > 0)
  110. *endptr = const_cast<char*>(p - 1);
  111. else
  112. *endptr = const_cast<char*>(nptr);
  113. }
  114. return num;
  115. }
  116. __attribute__((warn_unused_result)) int __generate_unique_filename(char* pattern)
  117. {
  118. size_t length = strlen(pattern);
  119. if (length < 6 || memcmp(pattern + length - 6, "XXXXXX", 6)) {
  120. errno = EINVAL;
  121. return -1;
  122. }
  123. size_t start = length - 6;
  124. static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
  125. for (int attempt = 0; attempt < 100; ++attempt) {
  126. for (int i = 0; i < 6; ++i)
  127. pattern[start + i] = random_characters[(rand() % sizeof(random_characters))];
  128. struct stat st;
  129. int rc = lstat(pattern, &st);
  130. if (rc < 0 && errno == ENOENT)
  131. return 0;
  132. }
  133. errno = EEXIST;
  134. return -1;
  135. }
  136. extern "C" {
  137. // Itanium C++ ABI methods defined in crt0.cpp
  138. extern int __cxa_atexit(void (*function)(void*), void* paramter, void* dso_handle);
  139. extern void __cxa_finalize(void* dso_handle);
  140. void exit(int status)
  141. {
  142. __cxa_finalize(nullptr);
  143. extern void _fini();
  144. _fini();
  145. fflush(stdout);
  146. fflush(stderr);
  147. _exit(status);
  148. ASSERT_NOT_REACHED();
  149. }
  150. static void __atexit_to_cxa_atexit(void* handler)
  151. {
  152. reinterpret_cast<void (*)()>(handler)();
  153. }
  154. int atexit(void (*handler)())
  155. {
  156. return __cxa_atexit(__atexit_to_cxa_atexit, (void*)handler, nullptr);
  157. }
  158. void abort()
  159. {
  160. raise(SIGABRT);
  161. ASSERT_NOT_REACHED();
  162. }
  163. static HashTable<const char*> s_malloced_environment_variables;
  164. static void free_environment_variable_if_needed(const char* var)
  165. {
  166. if (!s_malloced_environment_variables.contains(var))
  167. return;
  168. free(const_cast<char*>(var));
  169. s_malloced_environment_variables.remove(var);
  170. }
  171. char* getenv(const char* name)
  172. {
  173. size_t vl = strlen(name);
  174. for (size_t i = 0; environ[i]; ++i) {
  175. const char* decl = environ[i];
  176. char* eq = strchr(decl, '=');
  177. if (!eq)
  178. continue;
  179. size_t varLength = eq - decl;
  180. if (vl != varLength)
  181. continue;
  182. if (strncmp(decl, name, varLength) == 0) {
  183. return eq + 1;
  184. }
  185. }
  186. return nullptr;
  187. }
  188. int unsetenv(const char* name)
  189. {
  190. auto new_var_len = strlen(name);
  191. size_t environ_size = 0;
  192. int skip = -1;
  193. for (; environ[environ_size]; ++environ_size) {
  194. char* old_var = environ[environ_size];
  195. char* old_eq = strchr(old_var, '=');
  196. ASSERT(old_eq);
  197. size_t old_var_len = old_eq - old_var;
  198. if (new_var_len != old_var_len)
  199. continue; // can't match
  200. if (strncmp(name, old_var, new_var_len) == 0)
  201. skip = environ_size;
  202. }
  203. if (skip == -1)
  204. return 0; // not found: no failure.
  205. // Shuffle the existing array down by one.
  206. memmove(&environ[skip], &environ[skip + 1], ((environ_size - 1) - skip) * sizeof(environ[0]));
  207. environ[environ_size - 1] = nullptr;
  208. free_environment_variable_if_needed(name);
  209. return 0;
  210. }
  211. int setenv(const char* name, const char* value, int overwrite)
  212. {
  213. if (!overwrite && !getenv(name))
  214. return 0;
  215. auto length = strlen(name) + strlen(value) + 2;
  216. auto* var = (char*)malloc(length);
  217. snprintf(var, length, "%s=%s", name, value);
  218. s_malloced_environment_variables.set(var);
  219. return putenv(var);
  220. }
  221. int putenv(char* new_var)
  222. {
  223. char* new_eq = strchr(new_var, '=');
  224. if (!new_eq)
  225. return unsetenv(new_var);
  226. auto new_var_len = new_eq - new_var;
  227. int environ_size = 0;
  228. for (; environ[environ_size]; ++environ_size) {
  229. char* old_var = environ[environ_size];
  230. char* old_eq = strchr(old_var, '=');
  231. ASSERT(old_eq);
  232. auto old_var_len = old_eq - old_var;
  233. if (new_var_len != old_var_len)
  234. continue; // can't match
  235. if (strncmp(new_var, old_var, new_var_len) == 0) {
  236. free_environment_variable_if_needed(old_var);
  237. environ[environ_size] = new_var;
  238. return 0;
  239. }
  240. }
  241. // At this point, we need to append the new var.
  242. // 2 here: one for the new var, one for the sentinel value.
  243. char** new_environ = (char**)malloc((environ_size + 2) * sizeof(char*));
  244. if (new_environ == nullptr) {
  245. errno = ENOMEM;
  246. return -1;
  247. }
  248. for (int i = 0; environ[i]; ++i) {
  249. new_environ[i] = environ[i];
  250. }
  251. new_environ[environ_size] = new_var;
  252. new_environ[environ_size + 1] = nullptr;
  253. // swap new and old
  254. // note that the initial environ is not heap allocated!
  255. extern bool __environ_is_malloced;
  256. if (__environ_is_malloced)
  257. free(environ);
  258. __environ_is_malloced = true;
  259. environ = new_environ;
  260. return 0;
  261. }
  262. double strtod(const char* str, char** endptr)
  263. {
  264. size_t len = strlen(str);
  265. size_t weight = 1;
  266. int exp_val = 0;
  267. double value = 0.0f;
  268. double fraction = 0.0f;
  269. bool has_sign = false;
  270. bool is_negative = false;
  271. bool is_fractional = false;
  272. bool is_scientific = false;
  273. if (str[0] == '-') {
  274. is_negative = true;
  275. has_sign = true;
  276. }
  277. if (str[0] == '+') {
  278. has_sign = true;
  279. }
  280. size_t i;
  281. for (i = has_sign; i < len; i++) {
  282. // Looks like we're about to start working on the fractional part
  283. if (str[i] == '.') {
  284. is_fractional = true;
  285. continue;
  286. }
  287. if (str[i] == 'e' || str[i] == 'E') {
  288. if (str[i + 1] == '-' || str[i + 1] == '+')
  289. exp_val = atoi(str + i + 2);
  290. else
  291. exp_val = atoi(str + i + 1);
  292. is_scientific = true;
  293. continue;
  294. }
  295. if (str[i] < '0' || str[i] > '9' || exp_val != 0)
  296. continue;
  297. if (is_fractional) {
  298. fraction *= 10;
  299. fraction += str[i] - '0';
  300. weight *= 10;
  301. } else {
  302. value = value * 10;
  303. value += str[i] - '0';
  304. }
  305. }
  306. fraction /= weight;
  307. value += fraction;
  308. if (is_scientific) {
  309. bool divide = exp_val < 0;
  310. if (divide)
  311. exp_val *= -1;
  312. for (int i = 0; i < exp_val; i++) {
  313. if (divide)
  314. value /= 10;
  315. else
  316. value *= 10;
  317. }
  318. }
  319. //FIXME: Not entirely sure if this is correct, but seems to work.
  320. if (endptr)
  321. *endptr = const_cast<char*>(str + i);
  322. return is_negative ? -value : value;
  323. }
  324. long double strtold(const char* str, char** endptr)
  325. {
  326. (void)str;
  327. (void)endptr;
  328. dbgprintf("LibC: strtold: '%s'\n", str);
  329. ASSERT_NOT_REACHED();
  330. }
  331. float strtof(const char* str, char** endptr)
  332. {
  333. (void)str;
  334. (void)endptr;
  335. dbgprintf("LibC: strtof: '%s'\n", str);
  336. ASSERT_NOT_REACHED();
  337. }
  338. double atof(const char* str)
  339. {
  340. size_t len = strlen(str);
  341. size_t weight = 1;
  342. int exp_val = 0;
  343. double value = 0.0f;
  344. double fraction = 0.0f;
  345. bool has_sign = false;
  346. bool is_negative = false;
  347. bool is_fractional = false;
  348. bool is_scientific = false;
  349. if (str[0] == '-') {
  350. is_negative = true;
  351. has_sign = true;
  352. }
  353. if (str[0] == '+') {
  354. has_sign = true;
  355. }
  356. for (size_t i = has_sign; i < len; i++) {
  357. // Looks like we're about to start working on the fractional part
  358. if (str[i] == '.') {
  359. is_fractional = true;
  360. continue;
  361. }
  362. if (str[i] == 'e' || str[i] == 'E') {
  363. if (str[i + 1] == '-' || str[i + 1] == '+')
  364. exp_val = atoi(str + i + 2);
  365. else
  366. exp_val = atoi(str + i + 1);
  367. is_scientific = true;
  368. continue;
  369. }
  370. if (str[i] < '0' || str[i] > '9' || exp_val != 0)
  371. continue;
  372. if (is_fractional) {
  373. fraction *= 10;
  374. fraction += str[i] - '0';
  375. weight *= 10;
  376. } else {
  377. value = value * 10;
  378. value += str[i] - '0';
  379. }
  380. }
  381. fraction /= weight;
  382. value += fraction;
  383. if (is_scientific) {
  384. bool divide = exp_val < 0;
  385. if (divide)
  386. exp_val *= -1;
  387. for (int i = 0; i < exp_val; i++) {
  388. if (divide)
  389. value /= 10;
  390. else
  391. value *= 10;
  392. }
  393. }
  394. return is_negative ? -value : value;
  395. }
  396. int atoi(const char* str)
  397. {
  398. size_t len = strlen(str);
  399. int value = 0;
  400. bool isNegative = false;
  401. for (size_t i = 0; i < len; ++i) {
  402. if (i == 0 && str[0] == '-') {
  403. isNegative = true;
  404. continue;
  405. }
  406. if (str[i] < '0' || str[i] > '9')
  407. return value;
  408. value = value * 10;
  409. value += str[i] - '0';
  410. }
  411. return isNegative ? -value : value;
  412. }
  413. long atol(const char* str)
  414. {
  415. static_assert(sizeof(int) == sizeof(long));
  416. return atoi(str);
  417. }
  418. long long atoll(const char* str)
  419. {
  420. dbgprintf("FIXME(Libc): atoll('%s') passing through to atol()\n", str);
  421. return atol(str);
  422. }
  423. static char ptsname_buf[32];
  424. char* ptsname(int fd)
  425. {
  426. if (ptsname_r(fd, ptsname_buf, sizeof(ptsname_buf)) < 0)
  427. return nullptr;
  428. return ptsname_buf;
  429. }
  430. int ptsname_r(int fd, char* buffer, size_t size)
  431. {
  432. int rc = syscall(SC_ptsname_r, fd, buffer, size);
  433. __RETURN_WITH_ERRNO(rc, rc, -1);
  434. }
  435. static unsigned long s_next_rand = 1;
  436. int rand()
  437. {
  438. s_next_rand = s_next_rand * 1103515245 + 12345;
  439. return ((unsigned)(s_next_rand / ((RAND_MAX + 1) * 2)) % (RAND_MAX + 1));
  440. }
  441. void srand(unsigned seed)
  442. {
  443. s_next_rand = seed;
  444. }
  445. int abs(int i)
  446. {
  447. return i < 0 ? -i : i;
  448. }
  449. long int random()
  450. {
  451. return rand();
  452. }
  453. void srandom(unsigned seed)
  454. {
  455. srand(seed);
  456. }
  457. int system(const char* command)
  458. {
  459. if (!command)
  460. return 1;
  461. auto child = fork();
  462. if (child < 0)
  463. return -1;
  464. if (!child) {
  465. int rc = execl("/bin/sh", "sh", "-c", command, nullptr);
  466. ASSERT(rc < 0);
  467. perror("execl");
  468. exit(127);
  469. }
  470. int wstatus;
  471. waitpid(child, &wstatus, 0);
  472. return WEXITSTATUS(wstatus);
  473. }
  474. char* mktemp(char* pattern)
  475. {
  476. if (__generate_unique_filename(pattern) < 0)
  477. pattern[0] = '\0';
  478. return pattern;
  479. }
  480. int mkstemp(char* pattern)
  481. {
  482. char* path = mktemp(pattern);
  483. int fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); // I'm using the flags I saw glibc using.
  484. if (fd >= 0)
  485. return fd;
  486. return -1;
  487. }
  488. char* mkdtemp(char* pattern)
  489. {
  490. if (__generate_unique_filename(pattern) < 0)
  491. return nullptr;
  492. if (mkdir(pattern, 0700) < 0)
  493. return nullptr;
  494. return pattern;
  495. }
  496. void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int (*compar)(const void*, const void*))
  497. {
  498. int low = 0;
  499. int high = nmemb - 1;
  500. while (low <= high) {
  501. int middle = (low + high) / 2;
  502. void* middle_memb = const_cast<char*>((const char*)base + middle * size);
  503. int comparison = compar(key, middle_memb);
  504. if (comparison < 0)
  505. high = middle - 1;
  506. else if (comparison > 0)
  507. low = middle + 1;
  508. else
  509. return middle_memb;
  510. }
  511. return NULL;
  512. }
  513. div_t div(int numerator, int denominator)
  514. {
  515. div_t result;
  516. result.quot = numerator / denominator;
  517. result.rem = numerator % denominator;
  518. if (numerator >= 0 && result.rem < 0) {
  519. result.quot++;
  520. result.rem -= denominator;
  521. }
  522. return result;
  523. }
  524. ldiv_t ldiv(long numerator, long denominator)
  525. {
  526. ldiv_t result;
  527. result.quot = numerator / denominator;
  528. result.rem = numerator % denominator;
  529. if (numerator >= 0 && result.rem < 0) {
  530. result.quot++;
  531. result.rem -= denominator;
  532. }
  533. return result;
  534. }
  535. size_t mbstowcs(wchar_t*, const char*, size_t)
  536. {
  537. ASSERT_NOT_REACHED();
  538. }
  539. size_t mbtowc(wchar_t* wch, const char* data, size_t data_size)
  540. {
  541. // FIXME: This needs a real implementation.
  542. UNUSED_PARAM(data_size);
  543. if (wch && data) {
  544. *wch = *data;
  545. return 1;
  546. }
  547. if (!wch && data) {
  548. return 1;
  549. }
  550. return 0;
  551. }
  552. int wctomb(char*, wchar_t)
  553. {
  554. ASSERT_NOT_REACHED();
  555. }
  556. size_t wcstombs(char* dest, const wchar_t* src, size_t max)
  557. {
  558. char* originalDest = dest;
  559. while ((size_t)(dest - originalDest) < max) {
  560. StringView v { (const char*)src, sizeof(wchar_t) };
  561. // FIXME: dependent on locale, for now utf-8 is supported.
  562. Utf8View utf8 { v };
  563. if (*utf8.begin() == '\0') {
  564. *dest = '\0';
  565. return (size_t)(dest - originalDest); // Exclude null character in returned size
  566. }
  567. for (auto byte : utf8) {
  568. if (byte != '\0')
  569. *dest++ = byte;
  570. }
  571. ++src;
  572. }
  573. return max;
  574. }
  575. long strtol(const char* str, char** endptr, int base)
  576. {
  577. return strtol_impl<long, LONG_MIN, LONG_MAX>(str, endptr, base);
  578. }
  579. unsigned long strtoul(const char* str, char** endptr, int base)
  580. {
  581. auto value = strtol(str, endptr, base);
  582. ASSERT(value >= 0);
  583. return value;
  584. }
  585. long long strtoll(const char* str, char** endptr, int base)
  586. {
  587. return strtol_impl<long long, LONG_LONG_MIN, LONG_LONG_MAX>(str, endptr, base);
  588. }
  589. unsigned long long strtoull(const char* str, char** endptr, int base)
  590. {
  591. auto value = strtoll(str, endptr, base);
  592. ASSERT(value >= 0);
  593. return value;
  594. }
  595. // Serenity's PRNG is not cryptographically secure. Do not rely on this for
  596. // any real crypto! These functions (for now) are for compatibility.
  597. // TODO: In the future, rand can be made determinstic and this not.
  598. uint32_t arc4random(void)
  599. {
  600. char buf[4];
  601. syscall(SC_getrandom, buf, 4, 0);
  602. return *(uint32_t*)buf;
  603. }
  604. void arc4random_buf(void* buffer, size_t buffer_size)
  605. {
  606. // arc4random_buf should never fail, but user supplied buffers could fail.
  607. // However, if the user passes a garbage buffer, that's on them.
  608. syscall(SC_getrandom, buffer, buffer_size, 0);
  609. }
  610. uint32_t arc4random_uniform(uint32_t max_bounds)
  611. {
  612. // XXX: Should actually apply special rules for uniformity; avoid what is
  613. // called "modulo bias".
  614. return arc4random() % max_bounds;
  615. }
  616. char* realpath(const char* pathname, char* buffer)
  617. {
  618. if (!pathname) {
  619. errno = EFAULT;
  620. return nullptr;
  621. }
  622. size_t size = PATH_MAX;
  623. if (buffer == nullptr)
  624. buffer = (char*)malloc(size);
  625. Syscall::SC_realpath_params params { { pathname, strlen(pathname) }, { buffer, size } };
  626. int rc = syscall(SC_realpath, &params);
  627. if (rc < 0) {
  628. errno = -rc;
  629. return nullptr;
  630. }
  631. errno = 0;
  632. return buffer;
  633. }
  634. }