[PATCH] Move {read,write}_all functions to a dedicated header
Siddhesh Poyarekar
siddhesh@gotplt.org
Fri Jun 2 10:01:32 GMT 2023
On 2023-06-02 02:10, Frederic Berat wrote:
>>
>> This needs a copyright header.
>>
>
> What's the rule for that ? It's a bit confusing since there isn't a
> copyright header in most of the headers in "include", so I assumed it
> didn't need it.
I don't know if there's a rule for it, but AFAICT, most recently added
files appear to have the copyright header.
>
>>> @@ -0,0 +1,48 @@
>>> +#ifndef _UNISTD_EXT_H
>>> +#define _UNISTD_EXT_H
>>> +
>>> +#include <error.h>
>>> +#include <errno.h>
>>> +#include <libintl.h>
>>> +#include <unistd.h>
>>> +
>>> +/* Helpers used in catgets/gencat.c and malloc/memusage*.c */
>>> +static inline void
>>> +write_all (int fd, const void *buffer, size_t length)
>>> +{
>>> + const char *p = buffer;
>>> + const char *end = p + length;
>>> + while (p < end)
>>> + {
>>> + ssize_t ret = write (fd, p, end - p);
>>> + if (ret < 0)
>>> + error (EXIT_FAILURE, errno,
>>> + gettext ("write of %zu bytes failed after %td: %m"),
>>> + length, p - (const char *) buffer);
>>> +
>>> + if (ret == 0)
>>> + error (EXIT_FAILURE, 0,
>>> + gettext ("write returned 0 after writing %td bytes of %zu"),
>>> + p - (const char *) buffer, length);
>>> + p += ret;
>>> + }
>>> +}
>>> +
>>> +static inline void
>>> +read_all (int fd, void *buffer, size_t length)
>>> +{
>>> + char *p = buffer;
>>> + char *end = p + length;
>>> + while (p < end)
>>> + {
>>> + ssize_t ret = read (fd, p, end - p);
>>> + if (ret < 0)
>>> + error (EXIT_FAILURE, errno,
>>> + gettext ("read of %zu bytes failed after %td: %m"),
>>> + length, p - (char *) buffer);
>>> +
>>> + p += ret;
>>> + }
>>> +}
>>> +
>>> +#endif
>>> diff --git a/malloc/memusage.c b/malloc/memusage.c
>>> index 53a3630651..1c6930188c 100644
>>> --- a/malloc/memusage.c
>>> +++ b/malloc/memusage.c
>>> @@ -18,10 +18,7 @@
>>>
>>> #include <assert.h>
>>> #include <dlfcn.h>
>>> -#include <errno.h>
>>> -#include <error.h>
>>> #include <fcntl.h>
>>> -#include <libintl.h>
>>
>> You shouldn't remove these headers because gettext gets used despite the
>> unistd_ext.h usage, resulting in an implicit, transitive dependency.
>> Does it break anything if you don't remove these includes?
>>
>
> Nothing breaks if I keep these headers.
> I actually removed them because they were added in the patch that
> introduced write_all/read_all.
> Since I moved write/read_all in the header, I removed the headers that
> were introduced with them, but I admit I didn't check if they were
> actually missing before that.
OK, then it makes sense to just leave them in I think, since other code
in this file needs them.
Thanks,
Sid
More information about the Libc-alpha
mailing list