diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-21 15:29:29 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-09-21 15:29:29 +0000 |
commit | 30cfdf90ceecad82a378396fad517cb4d3fe7b30 (patch) | |
tree | 419fcca48293f604278c31b65f3d5fe07c967e20 | |
parent | 006e8628fc0a25c08df99f3b86d0c47f44aacc2b (diff) | |
download | busybox-w32-30cfdf90ceecad82a378396fad517cb4d3fe7b30.tar.gz busybox-w32-30cfdf90ceecad82a378396fad517cb4d3fe7b30.tar.bz2 busybox-w32-30cfdf90ceecad82a378396fad517cb4d3fe7b30.zip |
crontab: do not destroy STDIN_FILENO, editor may need it (crontab -e)
vi: deal with EOF/error on stdin and with input NULs
function old new delta
crontab_main 623 642 +19
edit_file 901 906 +5
readit 331 318 -13
-rw-r--r-- | editors/vi.c | 48 | ||||
-rw-r--r-- | miscutils/crontab.c | 19 |
2 files changed, 35 insertions, 32 deletions
diff --git a/editors/vi.c b/editors/vi.c index 27f2a3abd..02bdbb37b 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -147,10 +147,10 @@ struct globals { | |||
147 | #endif | 147 | #endif |
148 | 148 | ||
149 | smallint editing; // >0 while we are editing a file | 149 | smallint editing; // >0 while we are editing a file |
150 | // [code audit says "can be 0 or 1 only"] | 150 | // [code audit says "can be 0, 1 or 2 only"] |
151 | smallint cmd_mode; // 0=command 1=insert 2=replace | 151 | smallint cmd_mode; // 0=command 1=insert 2=replace |
152 | int file_modified; // buffer contents changed (counter, not flag!) | 152 | int file_modified; // buffer contents changed (counter, not flag!) |
153 | int last_file_modified; // = -1; | 153 | int last_file_modified; // = -1; |
154 | int fn_start; // index of first cmd line file name | 154 | int fn_start; // index of first cmd line file name |
155 | int save_argc; // how many file names on cmd line | 155 | int save_argc; // how many file names on cmd line |
156 | int cmdcnt; // repetition count | 156 | int cmdcnt; // repetition count |
@@ -623,7 +623,7 @@ static void edit_file(char *fn) | |||
623 | // These are commands that change text[]. | 623 | // These are commands that change text[]. |
624 | // Remember the input for the "." command | 624 | // Remember the input for the "." command |
625 | if (!adding2q && ioq_start == NULL | 625 | if (!adding2q && ioq_start == NULL |
626 | && strchr(modifying_cmds, c) | 626 | && c != '\0' && strchr(modifying_cmds, c) |
627 | ) { | 627 | ) { |
628 | start_new_cmd_q(c); | 628 | start_new_cmd_q(c); |
629 | } | 629 | } |
@@ -645,8 +645,8 @@ static void edit_file(char *fn) | |||
645 | } | 645 | } |
646 | //------------------------------------------------------------------- | 646 | //------------------------------------------------------------------- |
647 | 647 | ||
648 | place_cursor(rows, 0, FALSE); // go to bottom of screen | 648 | place_cursor(rows - 1, 0, FALSE); // go to bottom of screen |
649 | clear_to_eol(); // Erase to end of line | 649 | clear_to_eol(); // erase to end of line |
650 | cookmode(); | 650 | cookmode(); |
651 | #undef cur_line | 651 | #undef cur_line |
652 | } | 652 | } |
@@ -2009,9 +2009,9 @@ static void start_new_cmd_q(char c) | |||
2009 | { | 2009 | { |
2010 | // get buffer for new cmd | 2010 | // get buffer for new cmd |
2011 | // if there is a current cmd count put it in the buffer first | 2011 | // if there is a current cmd count put it in the buffer first |
2012 | if (cmdcnt > 0) | 2012 | if (cmdcnt > 0) { |
2013 | lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c); | 2013 | lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c); |
2014 | else { // just save char c onto queue | 2014 | } else { // just save char c onto queue |
2015 | last_modifying_cmd[0] = c; | 2015 | last_modifying_cmd[0] = c; |
2016 | lmc_len = 1; | 2016 | lmc_len = 1; |
2017 | } | 2017 | } |
@@ -2157,21 +2157,21 @@ static void winch_sig(int sig UNUSED_PARAM) | |||
2157 | //----- Come here when we get a continue signal ------------------- | 2157 | //----- Come here when we get a continue signal ------------------- |
2158 | static void cont_sig(int sig UNUSED_PARAM) | 2158 | static void cont_sig(int sig UNUSED_PARAM) |
2159 | { | 2159 | { |
2160 | rawmode(); // terminal to "raw" | 2160 | rawmode(); // terminal to "raw" |
2161 | last_status_cksum = 0; // force status update | 2161 | last_status_cksum = 0; // force status update |
2162 | redraw(TRUE); // re-draw the screen | 2162 | redraw(TRUE); // re-draw the screen |
2163 | 2163 | ||
2164 | signal(SIGTSTP, suspend_sig); | 2164 | signal(SIGTSTP, suspend_sig); |
2165 | signal(SIGCONT, SIG_DFL); | 2165 | signal(SIGCONT, SIG_DFL); |
2166 | kill(my_pid, SIGCONT); | 2166 | kill(my_pid, SIGCONT); // huh? why? we are already "continued"... |
2167 | } | 2167 | } |
2168 | 2168 | ||
2169 | //----- Come here when we get a Suspend signal ------------------- | 2169 | //----- Come here when we get a Suspend signal ------------------- |
2170 | static void suspend_sig(int sig UNUSED_PARAM) | 2170 | static void suspend_sig(int sig UNUSED_PARAM) |
2171 | { | 2171 | { |
2172 | place_cursor(rows - 1, 0, FALSE); // go to bottom of screen | 2172 | place_cursor(rows - 1, 0, FALSE); // go to bottom of screen |
2173 | clear_to_eol(); // Erase to end of line | 2173 | clear_to_eol(); // erase to end of line |
2174 | cookmode(); // terminal to "cooked" | 2174 | cookmode(); // terminal to "cooked" |
2175 | 2175 | ||
2176 | signal(SIGCONT, cont_sig); | 2176 | signal(SIGCONT, cont_sig); |
2177 | signal(SIGTSTP, SIG_DFL); | 2177 | signal(SIGTSTP, SIG_DFL); |
@@ -2247,18 +2247,20 @@ static char readit(void) // read (maybe cursor) key from stdin | |||
2247 | 2247 | ||
2248 | fflush(stdout); | 2248 | fflush(stdout); |
2249 | n = chars_to_parse; | 2249 | n = chars_to_parse; |
2250 | // get input from User- are there already input chars in Q? | 2250 | // get input from User - are there already input chars in Q? |
2251 | if (n <= 0) { | 2251 | if (n <= 0) { |
2252 | // the Q is empty, wait for a typed char | 2252 | // the Q is empty, wait for a typed char |
2253 | again: | ||
2253 | n = safe_read(STDIN_FILENO, readbuffer, sizeof(readbuffer)); | 2254 | n = safe_read(STDIN_FILENO, readbuffer, sizeof(readbuffer)); |
2254 | if (n < 0) { | 2255 | if (n <= 0) { |
2255 | if (errno == EBADF || errno == EFAULT || errno == EINVAL | 2256 | place_cursor(rows - 1, 0, FALSE); // go to bottom of screen |
2256 | || errno == EIO) | 2257 | clear_to_eol(); // erase to end of line |
2257 | editing = 0; // want to exit | 2258 | cookmode(); // terminal to "cooked" |
2258 | errno = 0; | 2259 | bb_error_msg_and_die("can't read user input"); |
2259 | } | 2260 | } |
2260 | if (n <= 0) | 2261 | /* elsewhere we can get very confused by NULs */ |
2261 | return 0; // error | 2262 | if (readbuffer[0] == '\0') |
2263 | goto again; | ||
2262 | if (readbuffer[0] == 27) { | 2264 | if (readbuffer[0] == 27) { |
2263 | // This is an ESC char. Is this Esc sequence? | 2265 | // This is an ESC char. Is this Esc sequence? |
2264 | // Could be bare Esc key. See if there are any | 2266 | // Could be bare Esc key. See if there are any |
diff --git a/miscutils/crontab.c b/miscutils/crontab.c index ef6d94375..673b558c3 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c | |||
@@ -93,6 +93,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
93 | char *new_fname; | 93 | char *new_fname; |
94 | char *user_name; /* -u USER */ | 94 | char *user_name; /* -u USER */ |
95 | int fd; | 95 | int fd; |
96 | int src_fd; | ||
96 | int opt_ler; | 97 | int opt_ler; |
97 | 98 | ||
98 | /* file [opts] Replace crontab from file | 99 | /* file [opts] Replace crontab from file |
@@ -144,15 +145,15 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
144 | bb_show_usage(); | 145 | bb_show_usage(); |
145 | 146 | ||
146 | /* Read replacement file under user's UID/GID/group vector */ | 147 | /* Read replacement file under user's UID/GID/group vector */ |
148 | src_fd = STDIN_FILENO; | ||
147 | if (!opt_ler) { /* Replace? */ | 149 | if (!opt_ler) { /* Replace? */ |
148 | if (!argv[0]) | 150 | if (!argv[0]) |
149 | bb_show_usage(); | 151 | bb_show_usage(); |
150 | if (NOT_LONE_DASH(argv[0])) { | 152 | if (NOT_LONE_DASH(argv[0])) { |
151 | fd = open_as_user(pas, argv[0]); | 153 | src_fd = open_as_user(pas, argv[0]); |
152 | if (fd < 0) | 154 | if (src_fd < 0) |
153 | bb_error_msg_and_die("user %s cannot read %s", | 155 | bb_error_msg_and_die("user %s cannot read %s", |
154 | pas->pw_name, argv[0]); | 156 | pas->pw_name, argv[0]); |
155 | xmove_fd(fd, STDIN_FILENO); | ||
156 | } | 157 | } |
157 | } | 158 | } |
158 | 159 | ||
@@ -180,23 +181,23 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
180 | tmp_fname = xasprintf("%s.%u", crontab_dir, (unsigned)getpid()); | 181 | tmp_fname = xasprintf("%s.%u", crontab_dir, (unsigned)getpid()); |
181 | /* No O_EXCL: we don't want to be stuck if earlier crontabs | 182 | /* No O_EXCL: we don't want to be stuck if earlier crontabs |
182 | * were killed, leaving stale temp file behind */ | 183 | * were killed, leaving stale temp file behind */ |
183 | fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC, 0600); | 184 | src_fd = xopen3(tmp_fname, O_RDWR|O_CREAT|O_TRUNC, 0600); |
184 | xmove_fd(fd, STDIN_FILENO); | 185 | fchown(src_fd, pas->pw_uid, pas->pw_gid); |
185 | fchown(STDIN_FILENO, pas->pw_uid, pas->pw_gid); | ||
186 | fd = open(pas->pw_name, O_RDONLY); | 186 | fd = open(pas->pw_name, O_RDONLY); |
187 | if (fd >= 0) { | 187 | if (fd >= 0) { |
188 | bb_copyfd_eof(fd, STDIN_FILENO); | 188 | bb_copyfd_eof(fd, src_fd); |
189 | close(fd); | 189 | close(fd); |
190 | xlseek(src_fd, 0, SEEK_SET); | ||
190 | } | 191 | } |
192 | close_on_exec_on(src_fd); /* don't want editor to see this fd */ | ||
191 | edit_file(pas, tmp_fname); | 193 | edit_file(pas, tmp_fname); |
192 | xlseek(STDIN_FILENO, 0, SEEK_SET); | ||
193 | /* fall through */ | 194 | /* fall through */ |
194 | 195 | ||
195 | case 0: /* Replace (no -l, -e, or -r were given) */ | 196 | case 0: /* Replace (no -l, -e, or -r were given) */ |
196 | new_fname = xasprintf("%s.new", pas->pw_name); | 197 | new_fname = xasprintf("%s.new", pas->pw_name); |
197 | fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600); | 198 | fd = open(new_fname, O_WRONLY|O_CREAT|O_TRUNC|O_APPEND, 0600); |
198 | if (fd >= 0) { | 199 | if (fd >= 0) { |
199 | bb_copyfd_eof(STDIN_FILENO, fd); | 200 | bb_copyfd_eof(src_fd, fd); |
200 | close(fd); | 201 | close(fd); |
201 | xrename(new_fname, pas->pw_name); | 202 | xrename(new_fname, pas->pw_name); |
202 | } else { | 203 | } else { |