purpose of "open_a_console()" in get_console.c??

Rob Landley rob at landley.net
Sun Mar 26 21:29:39 UTC 2006

On Sunday 26 March 2006 6:43 am, Robert P. J. Day wrote:
>   from libbb/get_concole.c:
> static int open_a_console(const char *fnam)
> {
>     int fd;
>     fd = open(fnam, O_RDWR);           /* first, try read/write */
>     if (fd < 0 && errno == EACCES)     /* else, try readonly */
>         fd = open(fnam, O_RDONLY);
>     if (fd < 0 && errno == EACCES)     /* else, try writeonly */
>         fd = open(fnam, O_WRONLY);
>     return fd;
> }
>   i don't see anything about that routine that is console-specific --
> it just appears to try to open a file three different ways.  is there
> perhaps a more general "open_file" BB routine somewhere that could be
> used just as well so that this one could be deleted?

That's kinda funky behavior.  The open is called with _no_ idea what's going 
to be made of the file, and the last two fallback positions are exclusive.  
(Either you can only read from it, or you can only write to it.)

Attaching a console to a tty is just about the only case where this behavior 
makes sense to me.  Can you think of another?

When you have this kind of question, look at the callsites to see what use is 
being made of it...

Never bet against the cheap plastic solution.

More information about the busybox mailing list