blind

suckless command-line video editing utility
git clone git://git.suckless.org/blind
Log | Files | Refs | README | LICENSE

io.h (2028B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <fcntl.h>
      3 #include <errno.h>
      4 
      5 #if defined(POSIX_FADV_SEQUENTIAL)
      6 # define fadvise_sequential(...)  posix_fadvise(__VA_ARGS__, POSIX_FADV_SEQUENTIAL)
      7 #else
      8 # define fadvise_sequential(...)
      9 #endif
     10 
     11 #if defined(POSIX_FADV_RANDOM)
     12 # define fadvise_random(...)  posix_fadvise(__VA_ARGS__, POSIX_FADV_RANDOM)
     13 #else
     14 # define fadvise_random(...)
     15 #endif
     16 
     17 #define ewriteall(...)     enwriteall(1, __VA_ARGS__)
     18 #define ereadall(...)      enreadall(1, __VA_ARGS__)
     19 #define epwriteall(...)    enpwriteall(1, __VA_ARGS__)
     20 #define ewritezeroes(...)  enwritezeroes(1, __VA_ARGS__)
     21 #define egetfile(...)      engetfile(1, __VA_ARGS__)
     22 
     23 int writeall(int fd, const void *buf, size_t n);
     24 
     25 static inline void
     26 enwriteall(int status, int fd, const void *buf, size_t n, const char *fname)
     27 {
     28 	if (writeall(fd, buf, n))
     29 		enprintf(status, "write %s:", fname);
     30 }
     31 
     32 ssize_t readall(int fd, void *buf, size_t n);
     33 
     34 static inline size_t
     35 enreadall(int status, int fd, void *buf, size_t n, const char *fname)
     36 {
     37 	ssize_t r = readall(fd, buf, n);
     38 	if (r < 0)
     39 		enprintf(status, "read %s:", fname);
     40 	return (size_t)r;
     41 }
     42 
     43 int pwriteall(int fd, const void *buf, size_t n, off_t ptr);
     44 
     45 static inline void
     46 enpwriteall(int status, int fd, const void *buf, size_t n, off_t ptr, const char *fname)
     47 {
     48 	if (pwriteall(fd, buf, n, ptr))
     49 		enprintf(status, "pwrite %s:", fname);
     50 }
     51 
     52 int writezeroes(int fd, const void *buf, size_t bufsize, size_t n);
     53 
     54 static inline void
     55 enwritezeroes(int status, int fd, const void *buf, size_t bufsize, size_t n, const char *fname)
     56 {
     57 	if (writezeroes(fd, buf, bufsize, n))
     58 		enprintf(status, "write %s:", fname);
     59 }
     60 
     61 int getfile(int fd, void *bufp, size_t *restrict ptrp, size_t *restrict sizep);
     62 
     63 static inline void
     64 engetfile(int status, int fd, void *bufp, size_t *restrict ptrp,
     65 	  size_t *restrict sizep, const char *fname)
     66 {
     67 	if (getfile(fd, bufp, ptrp, sizep)) {
     68 		if (errno == ENOMEM)
     69 			enprintf(status, "realloc:");
     70 		else
     71 			enprintf(status, "read %s:", fname);
     72 	}
     73 }