From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11762 Path: news.gmane.org!.POSTED!not-for-mail From: Denys Vlasenko Newsgroups: gmane.linux.lib.musl.general Subject: shell needs to change fd in a FILE Date: Mon, 31 Jul 2017 17:05:24 +0200 Message-ID: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: blaine.gmane.org 1501513581 9828 195.159.176.226 (31 Jul 2017 15:06:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 31 Jul 2017 15:06:21 +0000 (UTC) To: musl , Rich Felker Original-X-From: musl-return-11775-gllmg-musl=m.gmane.org@lists.openwall.com Mon Jul 31 17:06:14 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1dcCGn-000223-PL for gllmg-musl@m.gmane.org; Mon, 31 Jul 2017 17:06:09 +0200 Original-Received: (qmail 18430 invoked by uid 550); 31 Jul 2017 15:05:58 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 18323 invoked from network); 31 Jul 2017 15:05:56 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=vsWC13rDYt17OVioaTDHy4QZ8ar/WCKqCEZFmpLUcKo=; b=orLPEzve8VN/h8CB1yMXzl1rH4vl8CXg7W1ALOY7Bo3v1gfFnDRA0ciSMANHSrMjAt +KBLmPXpxk4IhFTtdheBOuBs7kS4vfd0OqIjuBuaM8l9eTqsLuYWAhobSBpPYqELpG5M ViDNoAf7i7yKrCiALlJX97TScszki2qWOJjfWQqwD5sRCipUnqJLJ66rSEUcCcouPMlN huknqLyMeJRbgTDk3TaTwmz3CDpMdNcYcSHMlOHLr0zNuqESkp/u0g0GFPJMJ7QxCG9S s9NxkUiUIf6PVPaliIVPWG9nnbRx2nTvaNKlVZ36BPQCI7qqVdW1Lda4lZhzWriOlrpy +SwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=vsWC13rDYt17OVioaTDHy4QZ8ar/WCKqCEZFmpLUcKo=; b=Y6qaYJUDo3IHhM80Aqoo5dnFI9ocKzaQu05i2YgLYtX5hi1aeDlo/6m8i6GljdHmNo l9aXaVov2RItz94LLrIWq/oM+1ng8Ys18VypAHRIok8est8Fh0k9LkwLE35WVoVUL7DA 27x50gFQyzfEf00BgEILKd78vhRyX5MI/F8YQfBiANTN7x67+kpX3purB5W/U7hHbjc5 3DEZfP/baMuSabjmSc/wNO0qtov7qH6ap5myf72N9FzUTMcNfyC5vlSOhKnojhe3av77 2RohxZZnwEKAG75hQ8XkiHMRsOLja0ZVLZVDhs/EueNvGum/TGQQ2fTDpt6Dll1Eb05R f3uQ== X-Gm-Message-State: AIVw113A+eJ26l+3z4A9/L4SFb2jlXki/ZX7LFlwrTmRXqqSmUmHG9OB e8R6CRgqjWowODCAMmmuCPdzy1no9hSB X-Received: by 10.80.146.5 with SMTP id i5mr14348961eda.48.1501513545166; Mon, 31 Jul 2017 08:05:45 -0700 (PDT) Xref: news.gmane.org gmane.linux.lib.musl.general:11762 Archived-At: 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?