GETS(3POSIX)

πŸ’– GETS(3POSIX) πŸ’–

okay bestie, sit DOWN. this one is from the POSIX Programmer's Manual and it's lowkey one of the messiest functions in the whole C library, and i need you to hear me out before you go copy-pasting it into your code, fr fr. 🫢


πŸ“œ PROLOG πŸ“œ

so this manual page is part of the POSIX Programmer's Manual. but here's the tea: the Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the actual details), or honestly the interface might not even be implemented on Linux at all. πŸ’€


✨ NAME ✨

gets β€” get a string from a stdin stream. that's it, that's the whole job description.


πŸŽ€ SYNOPSIS πŸŽ€

#include <stdio.h>

char *gets(char *s);

🌸 DESCRIPTION 🌸

everything described on this reference page is aligned with the ISO C standard, and any conflict between the requirements here and the ISO C standard is unintentional (nobody's trying to start drama). this volume of POSIX.1‐2017 defers to the ISO C standard, periodt.

so what does gets() actually do? it shall read bytes from the standard input stream, stdin, into the array pointed to by s, until a <newline> is read or an end-of-file condition is encountered. any <newline> shall be discarded (thrown away, gone, not your problem) and a null byte shall be placed immediately after the last byte read into the array. πŸ’…

and okay, small detail but we love detail: the gets() function may mark the last data access timestamp of the file associated with stream for update. that last data access timestamp shall be marked for update by the first successful execution of fgetc(), fgets(), fread(), fscanf(), getc(), getchar(), getdelim(), getline(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc(). iykyk. πŸ•βœ¨


πŸ”₯ RETURN VALUE πŸ”₯

upon successful completion, gets() shall return s. iconic. but listen:

  • if the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end-of-file indicator for the stream shall be set and gets() shall return a null pointer.
  • if a read error occurs, the error indicator for the stream shall be set, gets() shall return a null pointer, and it sets errno to indicate the error.

πŸ’€ ERRORS πŸ’€

refer to fgetc(). that's the whole vibe. go read that page bestie.

the following sections are informative. (translation: they're the commentary track, not the main feature ✨)

πŸ™ˆ EXAMPLES πŸ™ˆ

none. literally none. no examples, no demo, nothing. it's giving "we didn't want to encourage you." 😭


⚠️ APPLICATION USAGE ⚠️

okay this is the part where i need you to actually pay attention, because this is serious:

reading a line that overflows the array pointed to by s results in undefined behavior. and because of that, the use of fgets() is recommended. 🫢

and since the user cannot specify the length of the buffer passed to gets(), use of this function is discouraged. the length of the string read is unlimited. it is possible to overflow this buffer in such a way as to cause applications to fail, or possible system security violations. like, actual security holes. not a drill. 🚨

so yeah β€” applications should use the fgets() function instead of the obsolescent gets() function. do not pass go, do not collect your unvalidated input. πŸ’…


🧠 RATIONALE 🧠

the standard developers decided to mark the gets() function as obsolescent even though it is in the ISO C standard, and the reason is literally just the possibility of buffer overflow. they knew. they always knew. 😭


πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

the gets() function may be removed in a future version. it's on the chopping block, bestie. main character energy but for all the wrong reasons. πŸ‘‘


🌈 SEE ALSO 🌈

  • Section 2.5, Standard I/O Streams
  • feof()
  • ferror()
  • fgets()

also, the Base Definitions volume of POSIX.1‐2017, <stdio.h> β€” go give it a read, it's the blueprint. ⭐


portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document (it's the final boss, the original wins). the original Standard can be obtained online at http://www.opengroup.org/unix/online.html . 🌐

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . πŸ›


that's the whole page, bestie!! courtesy of IEEE/The Open Group, 2017 β€” mwah, thanks for the lore, now please go use fgets(), i am begging you πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά