Follow upstream changes -- Bytestring updates
[git-darcs-import.git] / src / c_compat.c
blobc4d36074b22cbc7d80dca15c4f6bcda894a8c10b
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <sys/time.h>
12 #include <time.h>
14 #ifdef _WIN32
15 #include <windows.h>
16 #include <io.h>
17 #endif
19 #ifndef _WIN32
21 int open_read(const char *fname) {
22 return open(fname, O_RDONLY);
25 int open_write(const char *fname) {
26 return open(fname, O_WRONLY | O_TRUNC | O_CREAT,
27 S_IWUSR | S_IRUSR | S_IROTH | S_IRGRP);
30 int get_errno() {
31 return errno;
34 #include <sys/wait.h>
35 #include <signal.h>
36 #if HAVE_SIGINFO_H
37 #include <siginfo.h>
38 #endif
40 /* This waits and then returns the exit status of the process that was
41 waited for. */
42 int smart_wait(int pid) {
43 int stat;
44 pid_t rc;
46 do {
47 rc = waitpid(pid, &stat, 0);
48 } while(rc < 0 && errno == EINTR);
50 if(rc < 0) {
51 perror("waitpid");
52 return -138;
53 } else if (WIFEXITED(stat)) {
54 return WEXITSTATUS(stat);
55 } else if (WIFSIGNALED(stat)) {
56 /* Fixme: psignal isn't portable (not in POSIX). */
57 psignal(WTERMSIG(stat), "Error in subprocess");
58 return - WTERMSIG(stat);
59 } else {
60 return -137;
64 #include <unistd.h>
65 #include <sys/time.h>
66 #include <errno.h>
67 #include <stdio.h>
69 int execvp_no_vtalarm(const char *file, char *const argv[]) {
70 /* Reset the itimers in the child, so it doesn't get plagued
71 * by SIGVTALRM interrupts.
73 struct timeval tv_null = { 0, 0 };
74 struct itimerval itv;
75 itv.it_interval = tv_null;
76 itv.it_value = tv_null;
77 setitimer(ITIMER_REAL, &itv, NULL);
78 setitimer(ITIMER_VIRTUAL, &itv, NULL);
79 setitimer(ITIMER_PROF, &itv, NULL);
80 execvp(file, argv);
81 perror("Error in execvp");
82 return errno;
85 #include <sys/types.h>
86 #include <sys/stat.h>
87 #endif