diff options
author | Matthias-Christian Ott <ott@enolink.de> | 2008-07-04 18:05:08 +0200 |
---|---|---|
committer | Matthias-Christian Ott <ott@enolink.de> | 2008-07-04 18:05:08 +0200 |
commit | f982c1c37ccc4adc38260a132aa64ee29d1a1a8c (patch) | |
tree | 5d2ca4b666b91eed75ce2b16c3cd7ab785e66838 | |
parent | d83cbc27b99427d00846832a73810f285d8f0d05 (diff) | |
download | st-f982c1c37ccc4adc38260a132aa64ee29d1a1a8c.tar.gz st-f982c1c37ccc4adc38260a132aa64ee29d1a1a8c.zip |
replace eprint() functions with BSD error functions
-rw-r--r-- | pty.c | 12 | ||||
-rw-r--r-- | st.c | 14 | ||||
-rw-r--r-- | std.c | 25 | ||||
-rw-r--r-- | util.c | 23 | ||||
-rw-r--r-- | util.h | 2 |
5 files changed, 31 insertions, 45 deletions
@@ -22,22 +22,22 @@ getpty(void) { | |||
22 | ptm = open("/dev/ptmx", O_RDWR); | 22 | ptm = open("/dev/ptmx", O_RDWR); |
23 | if(ptm == -1) | 23 | if(ptm == -1) |
24 | if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1) | 24 | if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1) |
25 | eprintn("error, cannot open pty"); | 25 | err(EXIT_FAILURE, "cannot open pty"); |
26 | #endif | 26 | #endif |
27 | #if defined(_XOPEN_SOURCE) | 27 | #if defined(_XOPEN_SOURCE) |
28 | if(ptm != -1) { | 28 | if(ptm != -1) { |
29 | if(grantpt(ptm) == -1) | 29 | if(grantpt(ptm) == -1) |
30 | eprintn("error, cannot grant access to pty"); | 30 | err(EXIT_FAILURE, "cannot grant access to pty"); |
31 | if(unlockpt(ptm) == -1) | 31 | if(unlockpt(ptm) == -1) |
32 | eprintn("error, cannot unlock pty"); | 32 | err(EXIT_FAILURE, "cannot unlock pty"); |
33 | ptsdev = ptsname(ptm); | 33 | ptsdev = ptsname(ptm); |
34 | if(!ptsdev) | 34 | if(!ptsdev) |
35 | eprintn("error, slave pty name undefined"); | 35 | err(EXIT_FAILURE, "slave pty name undefined"); |
36 | pts = open(ptsdev, O_RDWR); | 36 | pts = open(ptsdev, O_RDWR); |
37 | if(pts == -1) | 37 | if(pts == -1) |
38 | eprintn("error, cannot open slave pty"); | 38 | err(EXIT_FAILURE, "cannot open slave pty"); |
39 | } | 39 | } |
40 | else | 40 | else |
41 | eprintn("error, cannot open pty"); | 41 | err(EXIT_FAILURE, "cannot open pty"); |
42 | #endif | 42 | #endif |
43 | } | 43 | } |
@@ -1,13 +1,17 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | 1 | /* See LICENSE file for copyright and license details. */ |
2 | #include "util.h" | ||
3 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <stdlib.h> | ||
4 | #include <string.h> | 4 | #include <string.h> |
5 | 5 | ||
6 | int | 6 | int |
7 | main(int argc, char *argv[]) { | 7 | main(int argc, char *argv[]) { |
8 | if(argc == 2 && !strcmp("-v", argv[1])) | 8 | if(argc == 2 && !strcmp("-v", argv[1])) { |
9 | eprint("st-"VERSION", © 2007-2008 st engineers, see LICENSE for details\n"); | 9 | fprintf(stderr, "st-"VERSION", © 2007-2008 st engineers, see LICENSE for details\n"); |
10 | else if(argc != 1) | 10 | exit(EXIT_SUCCESS); |
11 | eprint("usage: st [-v]\n"); | 11 | } |
12 | else if(argc != 1) { | ||
13 | fprintf(stderr, "usage: st [-v]\n"); | ||
14 | exit(EXIT_FAILURE); | ||
15 | } | ||
12 | return 0; | 16 | return 0; |
13 | } | 17 | } |
@@ -3,6 +3,7 @@ | |||
3 | #include <sys/types.h> | 3 | #include <sys/types.h> |
4 | #include <sys/wait.h> | 4 | #include <sys/wait.h> |
5 | #include <ctype.h> | 5 | #include <ctype.h> |
6 | #include <err.h> | ||
6 | #include <signal.h> | 7 | #include <signal.h> |
7 | #include <stdarg.h> | 8 | #include <stdarg.h> |
8 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -73,7 +74,7 @@ getch() { | |||
73 | if(rbuf.i++ >= rbuf.n) { | 74 | if(rbuf.i++ >= rbuf.n) { |
74 | rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data)); | 75 | rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data)); |
75 | if(rbuf.n == -1) | 76 | if(rbuf.n == -1) |
76 | eprintn("error, cannot read from slave pty"); | 77 | err(EXIT_FAILURE, "cannot read from slave pty"); |
77 | rbuf.i = 0; | 78 | rbuf.i = 0; |
78 | } | 79 | } |
79 | return rbuf.data[rbuf.i]; | 80 | return rbuf.data[rbuf.i]; |
@@ -113,7 +114,7 @@ parseesc(void) { | |||
113 | qmark = 1; | 114 | qmark = 1; |
114 | else if(c == ';') { | 115 | else if(c == ';') { |
115 | if(!digit) | 116 | if(!digit) |
116 | eprint("syntax error\n"); | 117 | errx(EXIT_FAILURE, "syntax error"); |
117 | digit = 0; | 118 | digit = 0; |
118 | j++; | 119 | j++; |
119 | } | 120 | } |
@@ -216,7 +217,7 @@ shell(void) { | |||
216 | pid = fork(); | 217 | pid = fork(); |
217 | switch(pid) { | 218 | switch(pid) { |
218 | case -1: | 219 | case -1: |
219 | eprint("error, cannot fork\n"); | 220 | err(EXIT_FAILURE, "cannot fork"); |
220 | case 0: | 221 | case 0: |
221 | setsid(); | 222 | setsid(); |
222 | dup2(pts, STDIN_FILENO); | 223 | dup2(pts, STDIN_FILENO); |
@@ -237,7 +238,7 @@ sigchld(int n) { | |||
237 | int ret; | 238 | int ret; |
238 | 239 | ||
239 | if(waitpid(pid, &ret, 0) == -1) | 240 | if(waitpid(pid, &ret, 0) == -1) |
240 | eprintn("error, waiting for child failed"); | 241 | err(EXIT_FAILURE, "waiting for child failed"); |
241 | if(WIFEXITED(ret)) | 242 | if(WIFEXITED(ret)) |
242 | exit(WEXITSTATUS(ret)); | 243 | exit(WEXITSTATUS(ret)); |
243 | else | 244 | else |
@@ -257,7 +258,7 @@ unbuffer(void) { | |||
257 | void | 258 | void |
258 | ungetch(int c) { | 259 | ungetch(int c) { |
259 | if(rbuf.i + 1 >= rbuf.n) | 260 | if(rbuf.i + 1 >= rbuf.n) |
260 | eprint("error, read buffer full\n"); | 261 | errx(EXIT_FAILURE, "read buffer full"); |
261 | rbuf.data[rbuf.i++] = c; | 262 | rbuf.data[rbuf.i++] = c; |
262 | } | 263 | } |
263 | 264 | ||
@@ -266,10 +267,14 @@ main(int argc, char *argv[]) { | |||
266 | fd_set rfds; | 267 | fd_set rfds; |
267 | int r; | 268 | int r; |
268 | 269 | ||
269 | if(argc == 2 && !strcmp("-v", argv[1])) | 270 | if(argc == 2 && !strcmp("-v", argv[1])) { |
270 | eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n"); | 271 | fprintf(stderr, "std-"VERSION", © 2008 Matthias-Christian Ott\n"); |
271 | else if(argc == 1) | 272 | exit(EXIT_SUCCESS); |
272 | eprint("usage: st [-v]\n"); | 273 | } |
274 | else if(argc == 1) { | ||
275 | fprintf(stderr, "usage: st [-v]\n"); | ||
276 | exit(EXIT_FAILURE); | ||
277 | } | ||
273 | getpty(); | 278 | getpty(); |
274 | shell(); | 279 | shell(); |
275 | FD_ZERO(&rfds); | 280 | FD_ZERO(&rfds); |
@@ -278,7 +283,7 @@ main(int argc, char *argv[]) { | |||
278 | for(;;) { | 283 | for(;;) { |
279 | r = select(ptm + 1, &rfds, NULL, NULL, NULL); | 284 | r = select(ptm + 1, &rfds, NULL, NULL, NULL); |
280 | if(r == -1) | 285 | if(r == -1) |
281 | eprintn("error, cannot select"); | 286 | err(EXIT_FAILURE, "cannot select"); |
282 | if(FD_ISSET(ptm, &rfds)) { | 287 | if(FD_ISSET(ptm, &rfds)) { |
283 | do { | 288 | do { |
284 | c = getch(); | 289 | c = getch(); |
@@ -11,27 +11,6 @@ emallocz(unsigned int size) { | |||
11 | void *res = calloc(1, size); | 11 | void *res = calloc(1, size); |
12 | 12 | ||
13 | if(!res) | 13 | if(!res) |
14 | eprint("fatal: could not malloc() %u bytes\n", size); | 14 | err(EXIT_FAILURE, "could not malloc() %u bytes\n", size); |
15 | return res; | 15 | return res; |
16 | } | 16 | } |
17 | |||
18 | void | ||
19 | eprint(const char *errstr, ...) { | ||
20 | va_list ap; | ||
21 | |||
22 | va_start(ap, errstr); | ||
23 | vfprintf(stderr, errstr, ap); | ||
24 | va_end(ap); | ||
25 | exit(EXIT_FAILURE); | ||
26 | } | ||
27 | |||
28 | void | ||
29 | eprintn(const char *errstr, ...) { | ||
30 | va_list ap; | ||
31 | |||
32 | va_start(ap, errstr); | ||
33 | vfprintf(stderr, errstr, ap); | ||
34 | va_end(ap); | ||
35 | fprintf(stderr, ": %s\n", strerror(errno)); | ||
36 | exit(EXIT_FAILURE); | ||
37 | } | ||
@@ -1,5 +1,3 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | 1 | /* See LICENSE file for copyright and license details. */ |
2 | 2 | ||
3 | void *emallocz(unsigned int size); | 3 | void *emallocz(unsigned int size); |
4 | void eprint(const char *errstr, ...); | ||
5 | void eprintn(const char *errstr, ...); | ||