stdio.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  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 are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/LogStream.h>
  28. #include <AK/PrintfImplementation.h>
  29. #include <AK/ScopedValueRollback.h>
  30. #include <AK/StdLibExtras.h>
  31. #include <AK/kmalloc.h>
  32. #include <Kernel/Syscall.h>
  33. #include <assert.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <stdarg.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/wait.h>
  42. #include <unistd.h>
  43. struct FILE {
  44. public:
  45. FILE(int fd, int mode)
  46. : m_fd(fd)
  47. , m_mode(mode)
  48. {
  49. }
  50. ~FILE();
  51. static FILE* create(int fd, int mode);
  52. void setbuf(u8* data, int mode, size_t size) { m_buffer.setbuf(data, mode, size); }
  53. bool flush();
  54. bool close();
  55. int fileno() const { return m_fd; }
  56. bool eof() const { return m_eof; }
  57. int error() const { return m_error; }
  58. void clear_err() { m_error = 0; }
  59. size_t read(u8*, size_t);
  60. size_t write(const u8*, size_t);
  61. bool gets(u8*, size_t);
  62. bool ungetc(u8 byte) { return m_buffer.enqueue_front(byte); }
  63. int seek(long offset, int whence);
  64. long tell();
  65. pid_t popen_child() { return m_popen_child; }
  66. void set_popen_child(pid_t child_pid) { m_popen_child = child_pid; }
  67. private:
  68. struct Buffer {
  69. // A ringbuffer that also transparently implements ungetc().
  70. public:
  71. ~Buffer();
  72. int mode() const { return m_mode; }
  73. void setbuf(u8* data, int mode, size_t size);
  74. // Make sure to call realize() before enqueuing any data.
  75. // Dequeuing can be attempted without it.
  76. void realize(int fd);
  77. void drop();
  78. bool may_use() const { return m_ungotten || m_mode != _IONBF; }
  79. bool is_not_empty() const { return m_ungotten || !m_empty; }
  80. size_t buffered_size() const;
  81. const u8* begin_dequeue(size_t& available_size) const;
  82. void did_dequeue(size_t actual_size);
  83. u8* begin_enqueue(size_t& available_size) const;
  84. void did_enqueue(size_t actual_size);
  85. bool enqueue_front(u8 byte);
  86. private:
  87. // Note: the fields here are arranged this way
  88. // to make sizeof(Buffer) smaller.
  89. u8* m_data { nullptr };
  90. size_t m_capacity { BUFSIZ };
  91. size_t m_begin { 0 };
  92. size_t m_end { 0 };
  93. int m_mode { -1 };
  94. u8 m_unget_buffer { 0 };
  95. bool m_ungotten : 1 { false };
  96. bool m_data_is_malloced : 1 { false };
  97. // When m_begin == m_end, we want to distinguish whether
  98. // the buffer is full or empty.
  99. bool m_empty : 1 { true };
  100. };
  101. // Read or write using the underlying fd, bypassing the buffer.
  102. ssize_t do_read(u8*, size_t);
  103. ssize_t do_write(const u8*, size_t);
  104. // Read some data into the buffer.
  105. bool read_into_buffer();
  106. // Flush *some* data from the buffer.
  107. bool write_from_buffer();
  108. int m_fd { -1 };
  109. int m_mode { 0 };
  110. int m_error { 0 };
  111. bool m_eof { false };
  112. pid_t m_popen_child { -1 };
  113. Buffer m_buffer;
  114. };
  115. FILE::~FILE()
  116. {
  117. bool already_closed = m_fd == -1;
  118. ASSERT(already_closed);
  119. }
  120. FILE* FILE::create(int fd, int mode)
  121. {
  122. void* file = calloc(1, sizeof(FILE));
  123. new (file) FILE(fd, mode);
  124. return (FILE*)file;
  125. }
  126. bool FILE::close()
  127. {
  128. bool flush_ok = flush();
  129. int rc = ::close(m_fd);
  130. m_fd = -1;
  131. if (!flush_ok) {
  132. // Restore the original error from flush().
  133. errno = m_error;
  134. }
  135. return flush_ok && rc == 0;
  136. }
  137. bool FILE::flush()
  138. {
  139. if (m_mode & O_WRONLY && m_buffer.may_use()) {
  140. // When open for writing, write out all the buffered data.
  141. while (m_buffer.is_not_empty()) {
  142. bool ok = write_from_buffer();
  143. if (!ok)
  144. return false;
  145. }
  146. }
  147. if (m_mode & O_RDONLY) {
  148. // When open for reading, just drop the buffered data.
  149. size_t had_buffered = m_buffer.buffered_size();
  150. m_buffer.drop();
  151. // Attempt to reset the underlying file position to what the user
  152. // expects.
  153. int rc = lseek(m_fd, -had_buffered, SEEK_CUR);
  154. if (rc < 0) {
  155. if (errno == ESPIPE) {
  156. // We can't set offset on this file; oh well, the user will just
  157. // have to cope.
  158. errno = 0;
  159. } else {
  160. return false;
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. ssize_t FILE::do_read(u8* data, size_t size)
  167. {
  168. int nread = ::read(m_fd, data, size);
  169. if (nread < 0) {
  170. m_error = errno;
  171. } else if (nread == 0) {
  172. m_eof = true;
  173. }
  174. return nread;
  175. }
  176. ssize_t FILE::do_write(const u8* data, size_t size)
  177. {
  178. int nwritten = ::write(m_fd, data, size);
  179. if (nwritten < 0)
  180. m_error = errno;
  181. return nwritten;
  182. }
  183. bool FILE::read_into_buffer()
  184. {
  185. m_buffer.realize(m_fd);
  186. size_t available_size;
  187. u8* data = m_buffer.begin_enqueue(available_size);
  188. // If we want to read, the buffer must have some space!
  189. ASSERT(available_size);
  190. ssize_t nread = do_read(data, available_size);
  191. if (nread <= 0)
  192. return false;
  193. m_buffer.did_enqueue(nread);
  194. return true;
  195. }
  196. bool FILE::write_from_buffer()
  197. {
  198. size_t size;
  199. const u8* data = m_buffer.begin_dequeue(size);
  200. // If we want to write, the buffer must have something in it!
  201. ASSERT(size);
  202. ssize_t nwritten = do_write(data, size);
  203. if (nwritten < 0)
  204. return false;
  205. m_buffer.did_dequeue(nwritten);
  206. return true;
  207. }
  208. size_t FILE::read(u8* data, size_t size)
  209. {
  210. size_t total_read = 0;
  211. while (size > 0) {
  212. size_t actual_size;
  213. if (m_buffer.may_use()) {
  214. // Let's see if the buffer has something queued for us.
  215. size_t queued_size;
  216. const u8* queued_data = m_buffer.begin_dequeue(queued_size);
  217. if (queued_size == 0) {
  218. // Nothing buffered; we're going to have to read some.
  219. bool read_some_more = read_into_buffer();
  220. if (read_some_more) {
  221. // Great, now try this again.
  222. continue;
  223. }
  224. return total_read;
  225. }
  226. actual_size = min(size, queued_size);
  227. memcpy(data, queued_data, actual_size);
  228. m_buffer.did_dequeue(actual_size);
  229. } else {
  230. // Read directly into the user buffer.
  231. ssize_t nread = do_read(data, size);
  232. if (nread <= 0)
  233. return total_read;
  234. actual_size = nread;
  235. }
  236. total_read += actual_size;
  237. data += actual_size;
  238. size -= actual_size;
  239. }
  240. return total_read;
  241. }
  242. size_t FILE::write(const u8* data, size_t size)
  243. {
  244. size_t total_written = 0;
  245. while (size > 0) {
  246. size_t actual_size;
  247. if (m_buffer.may_use()) {
  248. m_buffer.realize(m_fd);
  249. // Try writing into the buffer.
  250. size_t available_size;
  251. u8* buffer_data = m_buffer.begin_enqueue(available_size);
  252. if (available_size == 0) {
  253. // There's no space in the buffer; we're going to free some.
  254. bool freed_some_space = write_from_buffer();
  255. if (freed_some_space) {
  256. // Great, now try this again.
  257. continue;
  258. }
  259. return total_written;
  260. }
  261. actual_size = min(size, available_size);
  262. memcpy(buffer_data, data, actual_size);
  263. m_buffer.did_enqueue(actual_size);
  264. // See if we have to flush it.
  265. if (m_buffer.mode() == _IOLBF) {
  266. bool includes_newline = memchr(data, '\n', actual_size);
  267. if (includes_newline)
  268. flush();
  269. }
  270. } else {
  271. // Write directly from the user buffer.
  272. ssize_t nwritten = do_write(data, size);
  273. if (nwritten < 0)
  274. return total_written;
  275. actual_size = nwritten;
  276. }
  277. total_written += actual_size;
  278. data += actual_size;
  279. size -= actual_size;
  280. }
  281. return total_written;
  282. }
  283. bool FILE::gets(u8* data, size_t size)
  284. {
  285. // gets() is a lot like read(), but it is different enough in how it
  286. // processes newlines and null-terminates the buffer that it deserves a
  287. // separate implementation.
  288. size_t total_read = 0;
  289. if (size == 0)
  290. return false;
  291. while (size > 1) {
  292. if (m_buffer.may_use()) {
  293. // Let's see if the buffer has something queued for us.
  294. size_t queued_size;
  295. const u8* queued_data = m_buffer.begin_dequeue(queued_size);
  296. if (queued_size == 0) {
  297. // Nothing buffered; we're going to have to read some.
  298. bool read_some_more = read_into_buffer();
  299. if (read_some_more) {
  300. // Great, now try this again.
  301. continue;
  302. }
  303. *data = 0;
  304. return total_read > 0;
  305. }
  306. size_t actual_size = min(size - 1, queued_size);
  307. u8* newline = reinterpret_cast<u8*>(memchr(queued_data, '\n', actual_size));
  308. if (newline)
  309. actual_size = newline - queued_data + 1;
  310. memcpy(data, queued_data, actual_size);
  311. m_buffer.did_dequeue(actual_size);
  312. total_read += actual_size;
  313. data += actual_size;
  314. size -= actual_size;
  315. if (newline)
  316. break;
  317. } else {
  318. // Sadly, we have to actually read these characters one by one.
  319. u8 byte;
  320. ssize_t nread = do_read(&byte, 1);
  321. if (nread <= 0) {
  322. *data = 0;
  323. return total_read > 0;
  324. }
  325. ASSERT(nread == 1);
  326. *data = byte;
  327. total_read++;
  328. data++;
  329. size--;
  330. if (byte == '\n')
  331. break;
  332. }
  333. }
  334. *data = 0;
  335. return total_read > 0;
  336. }
  337. int FILE::seek(long offset, int whence)
  338. {
  339. bool ok = flush();
  340. if (!ok)
  341. return -1;
  342. off_t off = lseek(m_fd, offset, whence);
  343. if (off < 0) {
  344. // Note: do not set m_error.
  345. return off;
  346. }
  347. m_eof = false;
  348. return 0;
  349. }
  350. long FILE::tell()
  351. {
  352. bool ok = flush();
  353. if (!ok)
  354. return -1;
  355. return lseek(m_fd, 0, SEEK_CUR);
  356. }
  357. FILE::Buffer::~Buffer()
  358. {
  359. if (m_data_is_malloced)
  360. free(m_data);
  361. }
  362. void FILE::Buffer::realize(int fd)
  363. {
  364. if (m_mode == -1)
  365. m_mode = isatty(fd) ? _IOLBF : _IOFBF;
  366. if (m_mode != _IONBF && m_data == nullptr) {
  367. m_data = reinterpret_cast<u8*>(malloc(m_capacity));
  368. m_data_is_malloced = true;
  369. }
  370. }
  371. void FILE::Buffer::setbuf(u8* data, int mode, size_t size)
  372. {
  373. drop();
  374. m_mode = mode;
  375. if (data != nullptr) {
  376. m_data = data;
  377. m_capacity = size;
  378. }
  379. }
  380. void FILE::Buffer::drop()
  381. {
  382. if (m_data_is_malloced) {
  383. free(m_data);
  384. m_data = nullptr;
  385. m_data_is_malloced = false;
  386. }
  387. m_begin = m_end = 0;
  388. m_empty = true;
  389. m_ungotten = false;
  390. }
  391. size_t FILE::Buffer::buffered_size() const
  392. {
  393. // Note: does not include the ungetc() buffer.
  394. if (m_empty)
  395. return 0;
  396. if (m_begin < m_end)
  397. return m_end - m_begin;
  398. else
  399. return m_capacity - (m_begin - m_end);
  400. }
  401. const u8* FILE::Buffer::begin_dequeue(size_t& available_size) const
  402. {
  403. if (m_ungotten) {
  404. available_size = 1;
  405. return &m_unget_buffer;
  406. }
  407. if (m_empty) {
  408. available_size = 0;
  409. return nullptr;
  410. }
  411. if (m_begin < m_end)
  412. available_size = m_end - m_begin;
  413. else
  414. available_size = m_capacity - m_begin;
  415. return &m_data[m_begin];
  416. }
  417. void FILE::Buffer::did_dequeue(size_t actual_size)
  418. {
  419. ASSERT(actual_size > 0);
  420. if (m_ungotten) {
  421. ASSERT(actual_size == 1);
  422. m_ungotten = false;
  423. return;
  424. }
  425. m_begin += actual_size;
  426. ASSERT(m_begin <= m_capacity);
  427. if (m_begin == m_capacity) {
  428. // Wrap around.
  429. m_begin = 0;
  430. }
  431. if (m_begin == m_end) {
  432. m_empty = true;
  433. // As an optimization, move both pointers to the beginning of the
  434. // buffer, so that more consecutive space is available next time.
  435. m_begin = m_end = 0;
  436. }
  437. }
  438. u8* FILE::Buffer::begin_enqueue(size_t& available_size) const
  439. {
  440. ASSERT(m_data != nullptr);
  441. if (m_begin < m_end || m_empty)
  442. available_size = m_capacity - m_end;
  443. else
  444. available_size = m_begin - m_end;
  445. return const_cast<u8*>(&m_data[m_end]);
  446. }
  447. void FILE::Buffer::did_enqueue(size_t actual_size)
  448. {
  449. ASSERT(m_data != nullptr);
  450. ASSERT(actual_size > 0);
  451. m_end += actual_size;
  452. ASSERT(m_end <= m_capacity);
  453. if (m_end == m_capacity) {
  454. // Wrap around.
  455. m_end = 0;
  456. }
  457. m_empty = false;
  458. }
  459. bool FILE::Buffer::enqueue_front(u8 byte)
  460. {
  461. if (m_ungotten) {
  462. // Sorry, the place is already taken!
  463. return false;
  464. }
  465. m_ungotten = true;
  466. m_unget_buffer = byte;
  467. return true;
  468. }
  469. extern "C" {
  470. static u8 default_streams[3][sizeof(FILE)];
  471. FILE* stdin = reinterpret_cast<FILE*>(&default_streams[0]);
  472. FILE* stdout = reinterpret_cast<FILE*>(&default_streams[1]);
  473. FILE* stderr = reinterpret_cast<FILE*>(&default_streams[2]);
  474. void __stdio_init()
  475. {
  476. new (stdin) FILE(0, O_RDONLY);
  477. new (stdout) FILE(1, O_WRONLY);
  478. new (stderr) FILE(2, O_WRONLY);
  479. stderr->setbuf(nullptr, _IONBF, 0);
  480. }
  481. int setvbuf(FILE* stream, char* buf, int mode, size_t size)
  482. {
  483. ASSERT(stream);
  484. if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF) {
  485. errno = EINVAL;
  486. return -1;
  487. }
  488. stream->setbuf(reinterpret_cast<u8*>(buf), mode, size);
  489. return 0;
  490. }
  491. void setbuf(FILE* stream, char* buf)
  492. {
  493. setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
  494. }
  495. void setlinebuf(FILE* stream)
  496. {
  497. setvbuf(stream, nullptr, _IOLBF, 0);
  498. }
  499. int fileno(FILE* stream)
  500. {
  501. ASSERT(stream);
  502. return stream->fileno();
  503. }
  504. int feof(FILE* stream)
  505. {
  506. ASSERT(stream);
  507. return stream->eof();
  508. }
  509. int fflush(FILE* stream)
  510. {
  511. if (!stream) {
  512. dbg() << "FIXME: fflush(nullptr) should flush all open streams";
  513. return 0;
  514. }
  515. return stream->flush() ? 0 : EOF;
  516. }
  517. char* fgets(char* buffer, int size, FILE* stream)
  518. {
  519. ASSERT(stream);
  520. bool ok = stream->gets(reinterpret_cast<u8*>(buffer), size);
  521. return ok ? buffer : nullptr;
  522. }
  523. int fgetc(FILE* stream)
  524. {
  525. ASSERT(stream);
  526. char ch;
  527. size_t nread = fread(&ch, sizeof(char), 1, stream);
  528. if (nread == 1)
  529. return ch;
  530. return EOF;
  531. }
  532. int getc(FILE* stream)
  533. {
  534. return fgetc(stream);
  535. }
  536. int getc_unlocked(FILE* stream)
  537. {
  538. return fgetc(stream);
  539. }
  540. int getchar()
  541. {
  542. return getc(stdin);
  543. }
  544. ssize_t getdelim(char** lineptr, size_t* n, int delim, FILE* stream)
  545. {
  546. char *ptr, *eptr;
  547. if (*lineptr == nullptr || *n == 0) {
  548. *n = BUFSIZ;
  549. if ((*lineptr = static_cast<char*>(malloc(*n))) == nullptr) {
  550. return -1;
  551. }
  552. }
  553. for (ptr = *lineptr, eptr = *lineptr + *n;;) {
  554. int c = fgetc(stream);
  555. if (c == -1) {
  556. if (feof(stream)) {
  557. *ptr = '\0';
  558. return ptr == *lineptr ? -1 : ptr - *lineptr;
  559. } else {
  560. return -1;
  561. }
  562. }
  563. *ptr++ = c;
  564. if (c == delim) {
  565. *ptr = '\0';
  566. return ptr - *lineptr;
  567. }
  568. if (ptr + 2 >= eptr) {
  569. char* nbuf;
  570. size_t nbuf_sz = *n * 2;
  571. ssize_t d = ptr - *lineptr;
  572. if ((nbuf = static_cast<char*>(realloc(*lineptr, nbuf_sz))) == nullptr) {
  573. return -1;
  574. }
  575. *lineptr = nbuf;
  576. *n = nbuf_sz;
  577. eptr = nbuf + nbuf_sz;
  578. ptr = nbuf + d;
  579. }
  580. }
  581. }
  582. ssize_t getline(char** lineptr, size_t* n, FILE* stream)
  583. {
  584. return getdelim(lineptr, n, '\n', stream);
  585. }
  586. int ungetc(int c, FILE* stream)
  587. {
  588. ASSERT(stream);
  589. bool ok = stream->ungetc(c);
  590. return ok ? c : EOF;
  591. }
  592. int fputc(int ch, FILE* stream)
  593. {
  594. ASSERT(stream);
  595. u8 byte = ch;
  596. size_t nwritten = stream->write(&byte, 1);
  597. if (nwritten == 0)
  598. return EOF;
  599. ASSERT(nwritten == 1);
  600. return byte;
  601. }
  602. int putc(int ch, FILE* stream)
  603. {
  604. return fputc(ch, stream);
  605. }
  606. int putchar(int ch)
  607. {
  608. return putc(ch, stdout);
  609. }
  610. int fputs(const char* s, FILE* stream)
  611. {
  612. ASSERT(stream);
  613. size_t len = strlen(s);
  614. size_t nwritten = stream->write(reinterpret_cast<const u8*>(s), len);
  615. if (nwritten < len)
  616. return EOF;
  617. return 1;
  618. }
  619. int puts(const char* s)
  620. {
  621. int rc = fputs(s, stdout);
  622. if (rc == EOF)
  623. return EOF;
  624. return fputc('\n', stdout);
  625. }
  626. void clearerr(FILE* stream)
  627. {
  628. ASSERT(stream);
  629. stream->clear_err();
  630. }
  631. int ferror(FILE* stream)
  632. {
  633. ASSERT(stream);
  634. return stream->error();
  635. }
  636. size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
  637. {
  638. ASSERT(stream);
  639. ASSERT(!Checked<size_t>::multiplication_would_overflow(size, nmemb));
  640. size_t nread = stream->read(reinterpret_cast<u8*>(ptr), size * nmemb);
  641. return nread / size;
  642. }
  643. size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
  644. {
  645. ASSERT(stream);
  646. ASSERT(!Checked<size_t>::multiplication_would_overflow(size, nmemb));
  647. size_t nwritten = stream->write(reinterpret_cast<const u8*>(ptr), size * nmemb);
  648. return nwritten / size;
  649. }
  650. int fseek(FILE* stream, long offset, int whence)
  651. {
  652. ASSERT(stream);
  653. return stream->seek(offset, whence);
  654. }
  655. long ftell(FILE* stream)
  656. {
  657. ASSERT(stream);
  658. return stream->tell();
  659. }
  660. int fgetpos(FILE* stream, fpos_t* pos)
  661. {
  662. ASSERT(stream);
  663. ASSERT(pos);
  664. long val = stream->tell();
  665. if (val == -1L)
  666. return 1;
  667. *pos = val;
  668. return 0;
  669. }
  670. int fsetpos(FILE* stream, const fpos_t* pos)
  671. {
  672. ASSERT(stream);
  673. ASSERT(pos);
  674. return stream->seek((long)*pos, SEEK_SET);
  675. }
  676. void rewind(FILE* stream)
  677. {
  678. ASSERT(stream);
  679. int rc = stream->seek(0, SEEK_SET);
  680. ASSERT(rc == 0);
  681. }
  682. int dbgprintf(const char* fmt, ...)
  683. {
  684. va_list ap;
  685. va_start(ap, fmt);
  686. int ret = printf_internal([](char*&, char ch) { dbgputch(ch); }, nullptr, fmt, ap);
  687. va_end(ap);
  688. return ret;
  689. }
  690. ALWAYS_INLINE void stdout_putch(char*&, char ch)
  691. {
  692. putchar(ch);
  693. }
  694. static FILE* __current_stream = nullptr;
  695. ALWAYS_INLINE static void stream_putch(char*&, char ch)
  696. {
  697. fputc(ch, __current_stream);
  698. }
  699. int vfprintf(FILE* stream, const char* fmt, va_list ap)
  700. {
  701. __current_stream = stream;
  702. return printf_internal(stream_putch, nullptr, fmt, ap);
  703. }
  704. int fprintf(FILE* stream, const char* fmt, ...)
  705. {
  706. va_list ap;
  707. va_start(ap, fmt);
  708. int ret = vfprintf(stream, fmt, ap);
  709. va_end(ap);
  710. return ret;
  711. }
  712. int vprintf(const char* fmt, va_list ap)
  713. {
  714. return printf_internal(stdout_putch, nullptr, fmt, ap);
  715. }
  716. int printf(const char* fmt, ...)
  717. {
  718. va_list ap;
  719. va_start(ap, fmt);
  720. int ret = vprintf(fmt, ap);
  721. va_end(ap);
  722. return ret;
  723. }
  724. static void buffer_putch(char*& bufptr, char ch)
  725. {
  726. *bufptr++ = ch;
  727. }
  728. int vsprintf(char* buffer, const char* fmt, va_list ap)
  729. {
  730. int ret = printf_internal(buffer_putch, buffer, fmt, ap);
  731. buffer[ret] = '\0';
  732. return ret;
  733. }
  734. int sprintf(char* buffer, const char* fmt, ...)
  735. {
  736. va_list ap;
  737. va_start(ap, fmt);
  738. int ret = vsprintf(buffer, fmt, ap);
  739. va_end(ap);
  740. return ret;
  741. }
  742. static size_t __vsnprintf_space_remaining;
  743. ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
  744. {
  745. if (__vsnprintf_space_remaining) {
  746. *bufptr++ = ch;
  747. --__vsnprintf_space_remaining;
  748. }
  749. }
  750. int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
  751. {
  752. __vsnprintf_space_remaining = size;
  753. int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
  754. if (__vsnprintf_space_remaining) {
  755. buffer[ret] = '\0';
  756. }
  757. return ret;
  758. }
  759. int snprintf(char* buffer, size_t size, const char* fmt, ...)
  760. {
  761. va_list ap;
  762. va_start(ap, fmt);
  763. int ret = vsnprintf(buffer, size, fmt, ap);
  764. va_end(ap);
  765. return ret;
  766. }
  767. void perror(const char* s)
  768. {
  769. int saved_errno = errno;
  770. dbg() << "perror(): " << s << ": " << strerror(saved_errno);
  771. fprintf(stderr, "%s: %s\n", s, strerror(saved_errno));
  772. }
  773. static int parse_mode(const char* mode)
  774. {
  775. int flags = 0;
  776. // NOTE: rt is a non-standard mode which opens a file for read, explicitly
  777. // specifying that it's a text file
  778. if (!strcmp(mode, "r") || !strcmp(mode, "rb") || !strcmp(mode, "rt"))
  779. flags = O_RDONLY;
  780. else if (!strcmp(mode, "r+") || !strcmp(mode, "rb+"))
  781. flags = O_RDWR;
  782. else if (!strcmp(mode, "w") || !strcmp(mode, "wb"))
  783. flags = O_WRONLY | O_CREAT | O_TRUNC;
  784. else if (!strcmp(mode, "w+") || !strcmp(mode, "wb+"))
  785. flags = O_RDWR | O_CREAT | O_TRUNC;
  786. else if (!strcmp(mode, "a") || !strcmp(mode, "ab"))
  787. flags = O_WRONLY | O_APPEND | O_CREAT;
  788. else if (!strcmp(mode, "a+") || !strcmp(mode, "ab+"))
  789. flags = O_RDWR | O_APPEND | O_CREAT;
  790. else {
  791. dbg() << "Unexpected mode _" << mode << "_";
  792. ASSERT_NOT_REACHED();
  793. }
  794. return flags;
  795. }
  796. FILE* fopen(const char* pathname, const char* mode)
  797. {
  798. int flags = parse_mode(mode);
  799. int fd = open(pathname, flags, 0666);
  800. if (fd < 0)
  801. return nullptr;
  802. return FILE::create(fd, flags);
  803. }
  804. FILE* freopen(const char* pathname, const char* mode, FILE* stream)
  805. {
  806. (void)pathname;
  807. (void)mode;
  808. (void)stream;
  809. ASSERT_NOT_REACHED();
  810. }
  811. FILE* fdopen(int fd, const char* mode)
  812. {
  813. int flags = parse_mode(mode);
  814. // FIXME: Verify that the mode matches how fd is already open.
  815. if (fd < 0)
  816. return nullptr;
  817. return FILE::create(fd, flags);
  818. }
  819. static inline bool is_default_stream(FILE* stream)
  820. {
  821. return stream == stdin || stream == stdout || stream == stderr;
  822. }
  823. int fclose(FILE* stream)
  824. {
  825. ASSERT(stream);
  826. bool ok = stream->close();
  827. ScopedValueRollback errno_restorer(errno);
  828. stream->~FILE();
  829. if (!is_default_stream(stream))
  830. free(stream);
  831. return ok ? 0 : EOF;
  832. }
  833. int rename(const char* oldpath, const char* newpath)
  834. {
  835. if (!oldpath || !newpath) {
  836. errno = EFAULT;
  837. return -1;
  838. }
  839. Syscall::SC_rename_params params { { oldpath, strlen(oldpath) }, { newpath, strlen(newpath) } };
  840. int rc = syscall(SC_rename, &params);
  841. __RETURN_WITH_ERRNO(rc, rc, -1);
  842. }
  843. void dbgputch(char ch)
  844. {
  845. syscall(SC_dbgputch, ch);
  846. }
  847. ssize_t dbgputstr(const char* characters, ssize_t length)
  848. {
  849. int rc = syscall(SC_dbgputstr, characters, length);
  850. __RETURN_WITH_ERRNO(rc, rc, -1);
  851. }
  852. char* tmpnam(char*)
  853. {
  854. ASSERT_NOT_REACHED();
  855. }
  856. FILE* popen(const char* command, const char* type)
  857. {
  858. if (!type || (*type != 'r' && *type != 'w')) {
  859. errno = EINVAL;
  860. return nullptr;
  861. }
  862. int pipe_fds[2];
  863. int rc = pipe(pipe_fds);
  864. if (rc < 0) {
  865. ScopedValueRollback rollback(errno);
  866. perror("pipe");
  867. return nullptr;
  868. }
  869. pid_t child_pid = fork();
  870. if (child_pid < 0) {
  871. ScopedValueRollback rollback(errno);
  872. perror("fork");
  873. close(pipe_fds[0]);
  874. close(pipe_fds[1]);
  875. return nullptr;
  876. } else if (child_pid == 0) {
  877. if (*type == 'r') {
  878. int rc = dup2(pipe_fds[1], STDOUT_FILENO);
  879. if (rc < 0) {
  880. perror("dup2");
  881. exit(1);
  882. }
  883. close(pipe_fds[0]);
  884. close(pipe_fds[1]);
  885. } else if (*type == 'w') {
  886. int rc = dup2(pipe_fds[0], STDIN_FILENO);
  887. if (rc < 0) {
  888. perror("dup2");
  889. exit(1);
  890. }
  891. close(pipe_fds[0]);
  892. close(pipe_fds[1]);
  893. }
  894. int rc = execl("/bin/sh", "sh", "-c", command, nullptr);
  895. if (rc < 0)
  896. perror("execl");
  897. exit(1);
  898. }
  899. FILE* file = nullptr;
  900. if (*type == 'r') {
  901. file = FILE::create(pipe_fds[0], O_RDONLY);
  902. close(pipe_fds[1]);
  903. } else if (*type == 'w') {
  904. file = FILE::create(pipe_fds[1], O_WRONLY);
  905. close(pipe_fds[0]);
  906. }
  907. file->set_popen_child(child_pid);
  908. return file;
  909. }
  910. int pclose(FILE* stream)
  911. {
  912. ASSERT(stream);
  913. ASSERT(stream->popen_child() != 0);
  914. int wstatus = 0;
  915. int rc = waitpid(stream->popen_child(), &wstatus, 0);
  916. if (rc < 0)
  917. return rc;
  918. return wstatus;
  919. }
  920. int remove(const char* pathname)
  921. {
  922. int rc = unlink(pathname);
  923. if (rc < 0 && errno != EISDIR)
  924. return -1;
  925. return rmdir(pathname);
  926. }
  927. int scanf(const char* fmt, ...)
  928. {
  929. va_list ap;
  930. va_start(ap, fmt);
  931. int count = vfscanf(stdin, fmt, ap);
  932. va_end(ap);
  933. return count;
  934. }
  935. int fscanf(FILE* stream, const char* fmt, ...)
  936. {
  937. va_list ap;
  938. va_start(ap, fmt);
  939. int count = vfscanf(stream, fmt, ap);
  940. va_end(ap);
  941. return count;
  942. }
  943. int sscanf(const char* buffer, const char* fmt, ...)
  944. {
  945. va_list ap;
  946. va_start(ap, fmt);
  947. int count = vsscanf(buffer, fmt, ap);
  948. va_end(ap);
  949. return count;
  950. }
  951. int vfscanf(FILE* stream, const char* fmt, va_list ap)
  952. {
  953. char buffer[BUFSIZ];
  954. if (!fgets(buffer, sizeof(buffer) - 1, stream))
  955. return -1;
  956. return vsscanf(buffer, fmt, ap);
  957. }
  958. void flockfile(FILE* filehandle)
  959. {
  960. (void)filehandle;
  961. dbgprintf("FIXME: Implement flockfile()\n");
  962. }
  963. void funlockfile(FILE* filehandle)
  964. {
  965. (void)filehandle;
  966. dbgprintf("FIXME: Implement funlockfile()\n");
  967. }
  968. FILE* tmpfile()
  969. {
  970. char tmp_path[] = "/tmp/XXXXXX";
  971. if (__generate_unique_filename(tmp_path) < 0)
  972. return nullptr;
  973. int fd = open(tmp_path, O_CREAT | O_EXCL | O_RDWR, S_IWUSR | S_IRUSR);
  974. if (fd < 0)
  975. return nullptr;
  976. // FIXME: instead of using this hack, implement with O_TMPFILE or similar
  977. unlink(tmp_path);
  978. return fdopen(fd, "rw");
  979. }
  980. }