This is an archive of the discontinued LLVM Phabricator instance.

[libc] add fgets
ClosedPublic

Authored by michaelrj on Oct 26 2022, 1:43 PM.

Details

Summary

This adds the fgets function and its unit tests.

Diff Detail

Event Timeline

michaelrj created this revision.Oct 26 2022, 1:43 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptOct 26 2022, 1:43 PM
michaelrj requested review of this revision.Oct 26 2022, 1:43 PM
sivachandra accepted this revision.Oct 26 2022, 2:01 PM
sivachandra added inline comments.
libc/src/stdio/fgets.cpp
25

Nit: Because reinterpret_cast already shows the type, you use auto:

auto *stream = ...;
27

Use FileLock instead:

File::FileLock lock(stream);

You can use a nested block to reduce the scope of the lock.

libc/test/src/stdio/fgets_test.cpp
72

Add tests for error.

81

Reading more should still be an EOF and not an error you mean?

This revision is now accepted and ready to land.Oct 26 2022, 2:01 PM
michaelrj updated this revision to Diff 470950.Oct 26 2022, 3:06 PM
michaelrj marked 2 inline comments as done.

address comments

libc/src/stdio/fgets.cpp
27

I tried using FileLock but it's a private part of File.

libc/test/src/stdio/fgets_test.cpp
72

The only true error state for fgets is a read error, which is tested above when I try to read from the write-only file. If there are other error states you see that I don't test then I'll add cases for them, but I don't see any.

sivachandra accepted this revision.Oct 26 2022, 3:16 PM
sivachandra added inline comments.
libc/src/stdio/fgets.cpp
27

Ok, never mind then.

libc/test/src/stdio/fgets_test.cpp
72

What I mean is to add:

ASSERT_EQ(__llvm_libc::ferror(file), 0);

inside of the for block above.

michaelrj updated this revision to Diff 470957.Oct 26 2022, 3:22 PM
michaelrj marked 4 inline comments as done.

add ferror checking.

michaelrj updated this revision to Diff 470958.Oct 26 2022, 3:23 PM

fix a few style issues

This revision was automatically updated to reflect the committed changes.