mailing list of musl libc
 help / color / mirror / code / Atom feed
* shell needs to change fd in a FILE
@ 2017-07-31 15:05 Denys Vlasenko
  2017-07-31 20:18 ` Rich Felker
  0 siblings, 1 reply; 5+ messages in thread
From: Denys Vlasenko @ 2017-07-31 15:05 UTC (permalink / raw)
  To: musl, Rich Felker

Hi,

I'm using ordinary FILE i/o for reading scripts in hush shell,
instead of rolling my own implementation, so that I can reuse
buffering code in libc, through the use of fgetc().

This works for almost all cases, except this one: if in script
I have a redirect which says shell wants to open a fd which happens
to be equal to the fd (say, 10) shell already used for script FILE:

    exec 10>FILE

What other shells do in this situation is they simply
dup and close script fd [in real code, they use fcntl(F_DUPFD)
instead of dup() since they want to avoid getting low fds],
so that fd is "moved" and no longer collides with the redirect.

I can do this trick, but since I use FILE interface, then
I need to inform libc that it needs to use new fd for this FILE.

"fileno(fp) = new_fd;" is non-portable and does not work in either
musl or glibc: it's a function, not a macro referencing
(fp)->field_holding_fd.

"fclose(fp); fp = fdopen(new_fd);" is not good since fp may have
some buffered input, which will be lost by such code.

How about adding a "set_fileno(fp, fd)" extension to musl,
with some easy define to probe for to conditionally use it?


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-08-02 16:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-31 15:05 shell needs to change fd in a FILE Denys Vlasenko
2017-07-31 20:18 ` Rich Felker
2017-08-02 12:08   ` Denys Vlasenko
2017-08-02 15:38     ` Rich Felker
2017-08-02 16:02       ` Denys Vlasenko

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).