##// END OF EJS Templates
chg: add fchdirx as a utility function...
Jun Wu -
r28854:ddef1446 default
parent child Browse files
Show More
@@ -1,173 +1,180 b''
1 /*
1 /*
2 * Utility functions
2 * Utility functions
3 *
3 *
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
5 *
5 *
6 * This software may be used and distributed according to the terms of the
6 * This software may be used and distributed according to the terms of the
7 * GNU General Public License version 2 or any later version.
7 * GNU General Public License version 2 or any later version.
8 */
8 */
9
9
10 #include <errno.h>
10 #include <errno.h>
11 #include <signal.h>
11 #include <signal.h>
12 #include <stdarg.h>
12 #include <stdarg.h>
13 #include <stdio.h>
13 #include <stdio.h>
14 #include <stdlib.h>
14 #include <stdlib.h>
15 #include <string.h>
15 #include <string.h>
16 #include <sys/types.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
18 #include <unistd.h>
19
19
20 #include "util.h"
20 #include "util.h"
21
21
22 static int colorenabled = 0;
22 static int colorenabled = 0;
23
23
24 static inline void fsetcolor(FILE *fp, const char *code)
24 static inline void fsetcolor(FILE *fp, const char *code)
25 {
25 {
26 if (!colorenabled)
26 if (!colorenabled)
27 return;
27 return;
28 fprintf(fp, "\033[%sm", code);
28 fprintf(fp, "\033[%sm", code);
29 }
29 }
30
30
31 static void vabortmsgerrno(int no, const char *fmt, va_list args)
31 static void vabortmsgerrno(int no, const char *fmt, va_list args)
32 {
32 {
33 fsetcolor(stderr, "1;31");
33 fsetcolor(stderr, "1;31");
34 fputs("chg: abort: ", stderr);
34 fputs("chg: abort: ", stderr);
35 vfprintf(stderr, fmt, args);
35 vfprintf(stderr, fmt, args);
36 if (no != 0)
36 if (no != 0)
37 fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
37 fprintf(stderr, " (errno = %d, %s)", no, strerror(no));
38 fsetcolor(stderr, "");
38 fsetcolor(stderr, "");
39 fputc('\n', stderr);
39 fputc('\n', stderr);
40 exit(255);
40 exit(255);
41 }
41 }
42
42
43 void abortmsg(const char *fmt, ...)
43 void abortmsg(const char *fmt, ...)
44 {
44 {
45 va_list args;
45 va_list args;
46 va_start(args, fmt);
46 va_start(args, fmt);
47 vabortmsgerrno(0, fmt, args);
47 vabortmsgerrno(0, fmt, args);
48 va_end(args);
48 va_end(args);
49 }
49 }
50
50
51 void abortmsgerrno(const char *fmt, ...)
51 void abortmsgerrno(const char *fmt, ...)
52 {
52 {
53 int no = errno;
53 int no = errno;
54 va_list args;
54 va_list args;
55 va_start(args, fmt);
55 va_start(args, fmt);
56 vabortmsgerrno(no, fmt, args);
56 vabortmsgerrno(no, fmt, args);
57 va_end(args);
57 va_end(args);
58 }
58 }
59
59
60 static int debugmsgenabled = 0;
60 static int debugmsgenabled = 0;
61
61
62 void enablecolor(void)
62 void enablecolor(void)
63 {
63 {
64 colorenabled = 1;
64 colorenabled = 1;
65 }
65 }
66
66
67 void enabledebugmsg(void)
67 void enabledebugmsg(void)
68 {
68 {
69 debugmsgenabled = 1;
69 debugmsgenabled = 1;
70 }
70 }
71
71
72 void debugmsg(const char *fmt, ...)
72 void debugmsg(const char *fmt, ...)
73 {
73 {
74 if (!debugmsgenabled)
74 if (!debugmsgenabled)
75 return;
75 return;
76
76
77 va_list args;
77 va_list args;
78 va_start(args, fmt);
78 va_start(args, fmt);
79 fsetcolor(stderr, "1;30");
79 fsetcolor(stderr, "1;30");
80 fputs("chg: debug: ", stderr);
80 fputs("chg: debug: ", stderr);
81 vfprintf(stderr, fmt, args);
81 vfprintf(stderr, fmt, args);
82 fsetcolor(stderr, "");
82 fsetcolor(stderr, "");
83 fputc('\n', stderr);
83 fputc('\n', stderr);
84 va_end(args);
84 va_end(args);
85 }
85 }
86
86
87 void fchdirx(int dirfd)
88 {
89 int r = fchdir(dirfd);
90 if (r == -1)
91 abortmsgerrno("failed to fchdir");
92 }
93
87 void *mallocx(size_t size)
94 void *mallocx(size_t size)
88 {
95 {
89 void *result = malloc(size);
96 void *result = malloc(size);
90 if (!result)
97 if (!result)
91 abortmsg("failed to malloc");
98 abortmsg("failed to malloc");
92 return result;
99 return result;
93 }
100 }
94
101
95 void *reallocx(void *ptr, size_t size)
102 void *reallocx(void *ptr, size_t size)
96 {
103 {
97 void *result = realloc(ptr, size);
104 void *result = realloc(ptr, size);
98 if (!result)
105 if (!result)
99 abortmsg("failed to realloc");
106 abortmsg("failed to realloc");
100 return result;
107 return result;
101 }
108 }
102
109
103 /*
110 /*
104 * Execute a shell command in mostly the same manner as system(), with the
111 * Execute a shell command in mostly the same manner as system(), with the
105 * give environment variables, after chdir to the given cwd. Returns a status
112 * give environment variables, after chdir to the given cwd. Returns a status
106 * code compatible with the Python subprocess module.
113 * code compatible with the Python subprocess module.
107 */
114 */
108 int runshellcmd(const char *cmd, const char *envp[], const char *cwd)
115 int runshellcmd(const char *cmd, const char *envp[], const char *cwd)
109 {
116 {
110 enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
117 enum { F_SIGINT = 1, F_SIGQUIT = 2, F_SIGMASK = 4, F_WAITPID = 8 };
111 unsigned int doneflags = 0;
118 unsigned int doneflags = 0;
112 int status = 0;
119 int status = 0;
113 struct sigaction newsa, oldsaint, oldsaquit;
120 struct sigaction newsa, oldsaint, oldsaquit;
114 sigset_t oldmask;
121 sigset_t oldmask;
115
122
116 /* block or mask signals just as system() does */
123 /* block or mask signals just as system() does */
117 memset(&newsa, 0, sizeof(newsa));
124 memset(&newsa, 0, sizeof(newsa));
118 newsa.sa_handler = SIG_IGN;
125 newsa.sa_handler = SIG_IGN;
119 newsa.sa_flags = 0;
126 newsa.sa_flags = 0;
120 if (sigemptyset(&newsa.sa_mask) < 0)
127 if (sigemptyset(&newsa.sa_mask) < 0)
121 goto done;
128 goto done;
122 if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
129 if (sigaction(SIGINT, &newsa, &oldsaint) < 0)
123 goto done;
130 goto done;
124 doneflags |= F_SIGINT;
131 doneflags |= F_SIGINT;
125 if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
132 if (sigaction(SIGQUIT, &newsa, &oldsaquit) < 0)
126 goto done;
133 goto done;
127 doneflags |= F_SIGQUIT;
134 doneflags |= F_SIGQUIT;
128
135
129 if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
136 if (sigaddset(&newsa.sa_mask, SIGCHLD) < 0)
130 goto done;
137 goto done;
131 if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
138 if (sigprocmask(SIG_BLOCK, &newsa.sa_mask, &oldmask) < 0)
132 goto done;
139 goto done;
133 doneflags |= F_SIGMASK;
140 doneflags |= F_SIGMASK;
134
141
135 pid_t pid = fork();
142 pid_t pid = fork();
136 if (pid < 0)
143 if (pid < 0)
137 goto done;
144 goto done;
138 if (pid == 0) {
145 if (pid == 0) {
139 sigaction(SIGINT, &oldsaint, NULL);
146 sigaction(SIGINT, &oldsaint, NULL);
140 sigaction(SIGQUIT, &oldsaquit, NULL);
147 sigaction(SIGQUIT, &oldsaquit, NULL);
141 sigprocmask(SIG_SETMASK, &oldmask, NULL);
148 sigprocmask(SIG_SETMASK, &oldmask, NULL);
142 if (cwd && chdir(cwd) < 0)
149 if (cwd && chdir(cwd) < 0)
143 _exit(127);
150 _exit(127);
144 const char *argv[] = {"sh", "-c", cmd, NULL};
151 const char *argv[] = {"sh", "-c", cmd, NULL};
145 if (envp) {
152 if (envp) {
146 execve("/bin/sh", (char **)argv, (char **)envp);
153 execve("/bin/sh", (char **)argv, (char **)envp);
147 } else {
154 } else {
148 execv("/bin/sh", (char **)argv);
155 execv("/bin/sh", (char **)argv);
149 }
156 }
150 _exit(127);
157 _exit(127);
151 } else {
158 } else {
152 if (waitpid(pid, &status, 0) < 0)
159 if (waitpid(pid, &status, 0) < 0)
153 goto done;
160 goto done;
154 doneflags |= F_WAITPID;
161 doneflags |= F_WAITPID;
155 }
162 }
156
163
157 done:
164 done:
158 if (doneflags & F_SIGINT)
165 if (doneflags & F_SIGINT)
159 sigaction(SIGINT, &oldsaint, NULL);
166 sigaction(SIGINT, &oldsaint, NULL);
160 if (doneflags & F_SIGQUIT)
167 if (doneflags & F_SIGQUIT)
161 sigaction(SIGQUIT, &oldsaquit, NULL);
168 sigaction(SIGQUIT, &oldsaquit, NULL);
162 if (doneflags & F_SIGMASK)
169 if (doneflags & F_SIGMASK)
163 sigprocmask(SIG_SETMASK, &oldmask, NULL);
170 sigprocmask(SIG_SETMASK, &oldmask, NULL);
164
171
165 /* no way to report other errors, use 127 (= shell termination) */
172 /* no way to report other errors, use 127 (= shell termination) */
166 if (!(doneflags & F_WAITPID))
173 if (!(doneflags & F_WAITPID))
167 return 127;
174 return 127;
168 if (WIFEXITED(status))
175 if (WIFEXITED(status))
169 return WEXITSTATUS(status);
176 return WEXITSTATUS(status);
170 if (WIFSIGNALED(status))
177 if (WIFSIGNALED(status))
171 return -WTERMSIG(status);
178 return -WTERMSIG(status);
172 return 127;
179 return 127;
173 }
180 }
@@ -1,31 +1,32 b''
1 /*
1 /*
2 * Utility functions
2 * Utility functions
3 *
3 *
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
4 * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org>
5 *
5 *
6 * This software may be used and distributed according to the terms of the
6 * This software may be used and distributed according to the terms of the
7 * GNU General Public License version 2 or any later version.
7 * GNU General Public License version 2 or any later version.
8 */
8 */
9
9
10 #ifndef UTIL_H_
10 #ifndef UTIL_H_
11 #define UTIL_H_
11 #define UTIL_H_
12
12
13 #ifdef __GNUC__
13 #ifdef __GNUC__
14 #define PRINTF_FORMAT_ __attribute__((format(printf, 1, 2)))
14 #define PRINTF_FORMAT_ __attribute__((format(printf, 1, 2)))
15 #else
15 #else
16 #define PRINTF_FORMAT_
16 #define PRINTF_FORMAT_
17 #endif
17 #endif
18
18
19 void abortmsg(const char *fmt, ...) PRINTF_FORMAT_;
19 void abortmsg(const char *fmt, ...) PRINTF_FORMAT_;
20 void abortmsgerrno(const char *fmt, ...) PRINTF_FORMAT_;
20 void abortmsgerrno(const char *fmt, ...) PRINTF_FORMAT_;
21
21
22 void enablecolor(void);
22 void enablecolor(void);
23 void enabledebugmsg(void);
23 void enabledebugmsg(void);
24 void debugmsg(const char *fmt, ...) PRINTF_FORMAT_;
24 void debugmsg(const char *fmt, ...) PRINTF_FORMAT_;
25
25
26 void fchdirx(int dirfd);
26 void *mallocx(size_t size);
27 void *mallocx(size_t size);
27 void *reallocx(void *ptr, size_t size);
28 void *reallocx(void *ptr, size_t size);
28
29
29 int runshellcmd(const char *cmd, const char *envp[], const char *cwd);
30 int runshellcmd(const char *cmd, const char *envp[], const char *cwd);
30
31
31 #endif /* UTIL_H_ */
32 #endif /* UTIL_H_ */
General Comments 0
You need to be logged in to leave comments. Login now