Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libc: implement fread() #66940

Open
ycsin opened this issue Dec 24, 2023 · 5 comments
Open

libc: implement fread() #66940

ycsin opened this issue Dec 24, 2023 · 5 comments
Assignees
Labels
area: C Library C Standard Library Enhancement Changes/Updates/Additions to existing features Good first issue Good for a first time contributor to take

Comments

@ycsin
Copy link
Member

ycsin commented Dec 24, 2023

This was originally added as a ticket under POSIX, but fread() is not a POSIX function. It is from C89.

https://en.cppreference.com/w/c/io/fread

The expectation from the POSIX API is that this function is implemented as part of whatever C library is in use.

@ycsin ycsin added Feature Request A request for a new feature Good first issue Good for a first time contributor to take area: POSIX POSIX API Library labels Dec 24, 2023
@nashif nashif added Enhancement Changes/Updates/Additions to existing features and removed Feature Request A request for a new feature labels Jan 19, 2024
@RAJAGOPALAN-GANGADHARAN
Copy link
Contributor

Hi @ycsin and @cfriedt , I would like to give this a shot. Can you please assign me this issue? I'm thinking of using this pr - #68516 as a base. Thanks!

@cfriedt
Copy link
Member

cfriedt commented May 1, 2024

@RAJAGOPALAN-GANGADHARAN - done!

However, something to keep in mind is that fread() is actually just a part of ISO C, all the way back to C89. That means, that the implementation of this function should likely go into the common libc, and tests should be added to the common libc test suite.

https://en.cppreference.com/w/c/io/fread

Additionally, a fairly fundamental problem needs to be solved for this to be done The Right Way (TM). Namely, we need a way to easily associate FILE * objects to (and from) integer file descriptors.

That would ideally be done in ZVFS (which is itself a kind of relocation of code from various libraries and subsystems to avoid dependency cycles at the API level.

@RAJAGOPALAN-GANGADHARAN
Copy link
Contributor

Thanks @cfriedt - Looking at how fwrite has been implemented in minimal libc currently, seems like it only supports write to stdout and stderr. No fs support seems to be added (the use of zephyr fs apis like fs_open etc - https://docs.zephyrproject.org/latest/services/file_system/index.html#c.fs_dir_t.), is this what is expected from the fread (read from stdin) as well? Also in the future will the support be expanded to fs operations for these apis? Thanks!

@cfriedt
Copy link
Member

cfriedt commented May 4, 2024

@RAJAGOPALAN-GANGADHARAN Currently stdin / stdout / stderr should work, but we do want to support operations on other files as well.

@de-nordic
Copy link
Collaborator

@RAJAGOPALAN-GANGADHARAN - done!

However, something to keep in mind is that fread() is actually just a part of ISO C, all the way back to C89. That means, that the implementation of this function should likely go into the common libc, and tests should be added to the common libc test suite.

https://en.cppreference.com/w/c/io/fread

Additionally, a fairly fundamental problem needs to be solved for this to be done The Right Way (TM). Namely, we need a way to easily associate FILE * objects to (and from) integer file descriptors.

That would ideally be done in ZVFS (which is itself a kind of relocation of code from various libraries and subsystems to avoid dependency cycles at the API level.

@cfriedt Is correct.

C standard is subset of POSIX standard (or POSIX standard is superset over C).
This can be seen when comparing read and fread functions using the OpenGroup documentation:
read -> https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
fread -> https://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html

Look also at the C standard, draft, here https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf - you will notice that the fread is part of stdlib definition (Chapter 7).

Note also that C standard wins with POSIX when differs:
image

@cfriedt cfriedt added area: C Library C Standard Library and removed area: POSIX POSIX API Library labels Jun 11, 2024
@cfriedt cfriedt changed the title posix: implement fread() libc: implement fread() Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library Enhancement Changes/Updates/Additions to existing features Good first issue Good for a first time contributor to take
Projects
None yet
Development

No branches or pull requests

6 participants