Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #390329
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Buffer contents well-defined after fgets() reaches EOF ? |
| Date | 2025-02-15 08:37 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <864j0vyxj3.fsf@linuxsc.com> (permalink) |
| References | (4 earlier) <vobjdt$t5ka$1@dont-email.me> <vobkd5$t7np$1@dont-email.me> <20250210124911.00006b31@yahoo.com> <86ldu9zxkb.fsf@linuxsc.com> <20250214165108.00002984@yahoo.com> |
Michael S <already5chosen@yahoo.com> writes: > On Thu, 13 Feb 2025 07:14:28 -0800 > Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: > >> Michael S <already5chosen@yahoo.com> writes: >> >>> On Sun, 9 Feb 2025 17:22:43 -0800 >>> Andrey Tarasevich <noone@noone.net> wrote: >>> >>>> On Sun 2/9/2025 5:06 PM, Andrey Tarasevich wrote: >>>> >>>>> On Sun 2/9/2025 3:52 PM, Lawrence D'Oliveiro wrote: >>>>> >>>>>> On Sat, 8 Feb 2025 23:12:44 -0800, Andrey Tarasevich wrote: >>>>>> >>>>>>> If `fgets` reads nothing (instant end-of-file), the entire >>>>>>> buffer remains untouched. >>>>>> >>>>>> You mean, only a single null byte gets written. >>>>> >>>>> No. The buffer is not changed at all in such case. >>>> >>>> ... which actually raises an interesting quiz/puzzle/question: >>>> >>>> Under what circumstances `fgets` is expected to return an empty >>>> string? (I.e. set the [0] entry of the buffer to '\0' and return >>>> non-null)? >>>> >>>> The only answer I can see right away is: >>>> >>>> When one calls it as `fgets(buffer, 1, file)`, i.e. asks it to >>>> read 0 characters. >>>> >>>> This is under assumption that asking `fgets` to read 0 characters >>>> is supposed to prevent it from detecting end-of-file condition or >>>> I/O error condition. One can probably do some nitpicking at the >>>> current wording... but I believe the above is the intent. >>> >>> fgets() is one of many poorly defined standard library functions >>> inherited from early UNIX days. [...] >> >> What about the fgets() function do you think is poorly defined? >> >> Second question: by "poorly defined" do you mean "defined >> wrongly" or "defined ambiguously" (or both)? > > For starter, it looks like designers of fgets() did not believe in > their own motto about files being just streams of bytes. > I don't know the history, so, may be, the function was defined this way > for portability with systems where text files have special record-based > structure? > > Then, everything about it feels inelegant. > A return value carries just 1 bit of information, success or failure. > So why did they encode this information in baroque way instead of > something obvious, 0 and 1? > Appending zero at the end also feels like a hack, but it is necessary > because of the main problem. And the main problem is: how the user is > supposed to figure out how many bytes were read? > In well-designed API this question should be answered in O(1) time. > With fgets(), it can be answered in O(N) time when input is trusted to > contain no zeros. When input is arbitrary, finding out the answer is > even harder and requires quirks. If I understand you correctly your complaint is that the existing semantics are not as useful as you would like them to be, even though the current definition does make the behavior well defined. Is that right? Clearly using fgets() is problematic when the input stream might contain null characters. To me it seems obvious that the original implementors expected that fgets() would not be used in such cases, perhaps with the less severe restriction that the presence of embedded nulls could be detected and simply rejected as bad input, much the same as overly long lines or a final line without a terminating newline character.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 06:59 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-09 06:23 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-08 22:23 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-08 23:12 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-09 23:52 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 17:06 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 17:22 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-10 12:49 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-13 07:14 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-14 16:51 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 15:10 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-14 17:23 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 16:46 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 17:28 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 17:22 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-14 11:03 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 19:34 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:06 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 20:01 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 20:51 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:02 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:29 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:29 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-16 01:04 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-16 07:37 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 18:59 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-16 10:48 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:14 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-17 11:54 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-16 07:32 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-16 11:05 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:25 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:21 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-16 20:26 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:41 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:29 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:33 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 20:23 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-14 14:38 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 21:02 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:53 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:48 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-15 08:37 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:08 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-18 20:17 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-21 05:58 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-15 08:12 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:44 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-10 03:28 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 20:11 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-10 07:21 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-10 16:39 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-10 13:58 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 01:03 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-10 22:33 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 03:42 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 04:54 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 13:07 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 21:47 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 17:44 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-12 06:16 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-11 13:59 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 17:58 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-11 15:40 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-17 21:11 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-10 22:32 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-10 22:38 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 12:04 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-13 07:29 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 08:13 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-09 12:50 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 18:29 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 21:57 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 07:27 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 18:34 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 16:57 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:35 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 20:37 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 07:08 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 22:41 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 07:54 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 23:22 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 00:59 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-17 02:50 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 22:57 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 23:24 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Ben Bacarisse <ben@bsb.me.uk> - 2025-02-10 01:32 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:40 +0100
csiph-web