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 setserrnoto 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. β
π COPYRIGHT π
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 πβ¨ππ¦πΈπ
π«Ά
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms