O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
write.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
3/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
4/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
5/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
6/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
7/* */
14/* ************************************************************************** */
15
16#include "o2s/write.h"
17
18#include "o2s/log.h" // log_*
19
20#include <sys/types.h> // size_t, ssize_t
21
22#include <errno.h> // errno
23#include <iso646.h> // and
24#include <stdbool.h> // bool
25#include <string.h> // strerror
26#include <unistd.h> // write
27
29bool write_all(int file_descriptor, const char* memory, size_t length)
30{
31 ssize_t returned;
32 size_t written = 0;
33
34 while (written < length and (returned = write(file_descriptor, memory + written, length - written)) > 0)
35 written += returned;
36 if (written == length)
37 return true;
38 log_error("Unable to write the last %zu bytes out of %zu to file descriptor %i: %s",
39 length - written, length, file_descriptor, strerror(errno));
40 return false;
41}
Simplistic logging utilities.
#define log_error(...)
Report a condition causing the current operation to abort.
Definition log.h:83
Dynamic string implementation.
bool write_all(int file_descriptor, const char *memory, size_t length)
Keep calling write, untill all bytes are written, or write returns an error.
Definition write.c:29
Wrapper arounf the write system call.