spoon

set dwm status
git clone git://git.2f30.org/spoon
Log | Files | Refs | LICENSE

file.c (604B)


      1 #include <err.h>
      2 #include <fcntl.h>
      3 #include <unistd.h>
      4 
      5 ssize_t
      6 readn(int fd, void *buf, size_t nbytes)
      7 {
      8 	size_t nleft = nbytes;
      9 	ssize_t n;
     10 
     11 	do {
     12 		n = read(fd, buf, nleft);
     13 		if (n == 0)
     14 			break;
     15 		else if (n == -1)
     16 			return -1;
     17 		nleft -= n;
     18 		buf += n;
     19 	} while (nleft > 0);
     20 	return (nbytes - nleft);
     21 }
     22 
     23 int
     24 fileread(void *arg, char *buf, size_t len)
     25 {
     26 	char *path = arg;
     27 	ssize_t n;
     28 	int fd;
     29 
     30 	fd = open(path, O_RDONLY);
     31 	if (fd == -1) {
     32 		warn("open %s", path);
     33 		return -1;
     34 	}
     35 	n = readn(fd, buf, len);
     36 	close(fd);
     37 	if (n == -1 || n == 0)
     38 		return -1;
     39 	else
     40 		buf[n - 1] = '\0';
     41 	return 0;
     42 }