add a newline when appending to an mbox
[mailx.git] / util.c
blob921878ba14977bd48cbc731e38da12b97794cce6
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/wait.h>
8 #include "util.h"
10 int file_size(int fd)
12 struct stat stat;
13 fstat(fd, &stat);
14 return stat.st_size;
17 int xread(int fd, char *buf, int len)
19 int nr = 0;
20 while (nr < len) {
21 int ret = read(fd, buf + nr, len - nr);
22 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
23 continue;
24 if (ret <= 0)
25 break;
26 nr += ret;
28 return nr;
31 int xwrite(int fd, char *buf, int len)
33 int nw = 0;
34 while (nw < len) {
35 int ret = write(fd, buf + nw, len - nw);
36 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
37 continue;
38 if (ret < 0)
39 break;
40 nw += ret;
42 return nw;
45 void exec_file(char *filename, char **args)
47 pid_t pid;
48 if (!(pid = fork())) {
49 execvp(filename, args);
50 exit(1);
52 waitpid(pid, NULL, 0);
55 void exec_pipe(char *file, char **argv, char *s, int len)
57 pid_t pid;
58 int fds[2];
59 pipe(fds);
60 if (!(pid = fork())) {
61 close(fds[1]);
62 dup2(fds[0], STDIN_FILENO);
63 execvp(file, argv);
64 exit(1);
66 close(fds[0]);
67 write(fds[1], s, len);
68 close(fds[1]);
69 waitpid(pid, NULL, 0);