[BusyBox] shell/hush.c: Test for end of file [PATCH]
Shaun Jackman
sjackman at gmail.com
Sat Jun 11 01:00:01 UTC 2005
I ran into a state where an end of file condition was being seen by
the shell as the character (char)0xff instead of EOF, which is usually
(int)-1. I can't see off the top of my head why I would have run into
this while no one else has. From what I can tell though, shell/hush.c
never checks feof(i->file), so it must need to compare the return
value of fgetc(i->file) against EOF before casting it to a char.
Cheers,
Shaun
2005-06-10 Shaun Jackman <sjackman at gmail.com>
* shell/hush.c (get_user_input): Return EOF upon an end-of-file
condition in i->file.
(file_get): Ditto.
--- shell/hush.c
+++ shell/hush.c
@@ -920,10 +920,11 @@
debug_printf("result %s\n",*prompt_str);
}
-static void get_user_input(struct in_str *i)
+static int get_user_input(struct in_str *i)
{
char *prompt_str;
static char the_command[BUFSIZ];
+ int c;
setup_prompt_string(i->promptmode, &prompt_str);
#ifdef CONFIG_FEATURE_COMMAND_EDITING
@@ -937,11 +938,13 @@
#else
fputs(prompt_str, stdout);
fflush(stdout);
- the_command[0]=fgetc(i->file);
+ if( (c = fgetc(i->file)) == EOF ) return EOF;
+ the_command[0]=c;
the_command[1]='\0';
#endif
fflush(stdout);
i->p = the_command;
+ return 0;
}
/* This is the magic location that prints prompts
@@ -959,7 +962,7 @@
* more complicated by now, like sourcing or substituting. */
if (i->__promptme && interactive && i->file == stdin) {
while(! i->p || (interactive && strlen(i->p)==0) ) {
- get_user_input(i);
+ if( get_user_input(i) == EOF ) return EOF;
}
i->promptmode=2;
i->__promptme = 0;
@@ -984,7 +987,9 @@
if (i->p && *i->p) {
return *i->p;
} else {
- i->peek_buf[0] = fgetc(i->file);
+ int c = fgetc(i->file);
+ if( c == EOF ) return EOF;
+ i->peek_buf[0] = c;
i->peek_buf[1] = '\0';
i->p = i->peek_buf;
debug_printf("b_peek: got a %d\n", *i->p);
More information about the busybox
mailing list