From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11981 Path: news.gmane.org!.POSTED!not-for-mail From: William Pitcock Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] stdio: implement fopencookie(3) Date: Thu, 5 Oct 2017 06:48:24 +0000 Message-ID: <20171005064824.27894-1-nenolod@dereferenced.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1507186362 32308 195.159.176.226 (5 Oct 2017 06:52:42 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 5 Oct 2017 06:52:42 +0000 (UTC) Cc: William Pitcock To: musl@lists.openwall.com Original-X-From: musl-return-11994-gllmg-musl=m.gmane.org@lists.openwall.com Thu Oct 05 08:52:38 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 1e001N-0007rV-VZ for gllmg-musl@m.gmane.org; Thu, 05 Oct 2017 08:52:38 +0200 Original-Received: (qmail 17500 invoked by uid 550); 5 Oct 2017 06:52:41 -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 17451 invoked from network); 5 Oct 2017 06:52:40 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dereferenced-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=Ov03OuPjvMr1UK6eRIQCfFCp3q/7ZAwNdwFBNWfnlSo=; b=su0qlEmfFsy5eP1cGGm0PTOPGSfifRMkmMZzbJCUfFxfuz6h03UlGJ8MS/Obyp0aq7 d/jnYeVTEpJbfBsS9fqY211S28rcqk0iuGorxjI+zh6EjOiqE0xWhPceUoNja5h4vUh4 eJNBDIUKOMD09Rf+6LqwDiBxd4vG4aF/Dz7T6Oru84z42NZoKE51N6BcaOdqrGFdwDey 8vk/+XHb9wQ3XbWj8rY/7Epgxjw5rxw74Vhc7wL+Iu1gC2Yms27v+V5j4vc+ToPlxJmY o0yPGHMdpGCala9aorzE1GpaKTpV8qf7uzuwetrM1QYEdNryQ/ROYgaYy+58DDqjufXE 3LgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Ov03OuPjvMr1UK6eRIQCfFCp3q/7ZAwNdwFBNWfnlSo=; b=Q+DNfVjxa7OGChtbtdA3JQIOdwQvAcE0AnGYAmmezivWK5YOfHG9nSRmOCCjdOVNQD C8MkkcTo3oFYkwXuiVikdFz91G77odlXBb7qi3XhZ96BsfXgdkWQ8Ff2yDzd9rSpbvaD vNBrD3N7t1ilahA7m2iECibIYERHwDIAXbeQcYmn4Ou8T3q5LxHV4E7oPJ1df/LNDJw/ ltj4P1LtUnuMZSgNZPyf71y4CraWCANseLeqkHaMgTOqFhRSQEcv1ZJluT0KbIHRyYWT CmSs7XquhJMasu7TXoZYHcCgSndtORlCYU45JGG2lIXKFNJ2NCkIPGqHInh6ZRgDHht3 ga4w== X-Gm-Message-State: AHPjjUi3/pEcbS9dTjpnr7NzcLX9QYDRPib9SDGTi6iNnE9O3omDqE6J g+RG3MhAg69s8swQ1ovGiw0WDs3R X-Google-Smtp-Source: AOwi7QALSjTP+b3MJufpGVN7aSwg56TaSf2xhukL88V+DqwG5cgywRNWzjoNh/xjJVGSOVdOVj54Zg== X-Received: by 10.84.133.99 with SMTP id 90mr22494573plf.148.1507186344524; Wed, 04 Oct 2017 23:52:24 -0700 (PDT) X-Mailer: git-send-email 2.13.3 Xref: news.gmane.org gmane.linux.lib.musl.general:11981 Archived-At: --- include/stdio.h | 9 +++++ src/stdio/fopencookie.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/stdio/fopencookie.c diff --git a/include/stdio.h b/include/stdio.h index 884d2e6a..998883e5 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -182,6 +182,15 @@ int vasprintf(char **, const char *, __isoc_va_list); #ifdef _GNU_SOURCE char *fgets_unlocked(char *, int, FILE *); int fputs_unlocked(const char *, FILE *); + +typedef struct { + ssize_t (*read)(void *cookie, char *buf, size_t size); + ssize_t (*write)(void *cookie, const char *buf, size_t size); + int (*seek)(void *cookie, off_t *offset, int whence); + int (*close)(void *cookie); +} cookie_io_functions_t; + +FILE *fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs); #endif #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) diff --git a/src/stdio/fopencookie.c b/src/stdio/fopencookie.c new file mode 100644 index 00000000..e0b5a119 --- /dev/null +++ b/src/stdio/fopencookie.c @@ -0,0 +1,98 @@ +#define _GNU_SOURCE +#include "stdio_impl.h" +#include +#include +#include +#include +#include + +struct fcookie { + void *cookie; + cookie_io_functions_t iofuncs; +}; + +static size_t cookieread(FILE *f, unsigned char *buf, size_t len) +{ + struct fcookie *fc = f->cookie; + size_t ret; + if (fc->iofuncs.read == NULL) return -1; + ret = fc->iofuncs.read(fc->cookie, (char *) buf, len); + if (ret == 0) f->flags |= F_EOF; + f->rpos = f->buf; + f->rend = f->buf + ret; + return ret; +} + +static size_t cookiewrite(FILE *f, const unsigned char *buf, size_t len) +{ + struct fcookie *fc = f->cookie; + size_t ret; + size_t len2 = f->wpos - f->wbase; + if (fc->iofuncs.write == NULL) return -1; + if (len2) { + f->wpos = f->wbase; + if (cookiewrite(f, f->wpos, len2) < len2) return 0; + } + return fc->iofuncs.write(fc->cookie, (const char *) buf, len); +} + +static off_t cookieseek(FILE *f, off_t off, int whence) +{ + struct fcookie *fc = f->cookie; + if (fc->iofuncs.seek) return fc->iofuncs.seek(fc->cookie, &off, whence); + return -1; +} + +static int cookieclose(FILE *f) +{ + struct fcookie *fc = f->cookie; + if (fc->iofuncs.close) return fc->iofuncs.close(fc->cookie); + return 0; +} + +FILE *fopencookie(void *cookie, const char *mode, cookie_io_functions_t iofuncs) +{ + FILE *f; + struct winsize wsz; + struct fcookie *fc; + + /* Check for valid initial mode character */ + if (!strchr("rwa", *mode)) { + errno = EINVAL; + return 0; + } + + /* Allocate FILE+fcookie+buffer or fail */ + if (!(f=malloc(sizeof *f + sizeof *fc + UNGET + BUFSIZ))) return 0; + + /* Zero-fill only the struct, not the buffer */ + memset(f, 0, sizeof *f); + + /* Impose mode restrictions */ + if (!strchr(mode, '+')) f->flags = (*mode == 'r') ? F_NOWR : F_NORD; + + /* Set up our fcookie */ + fc = (void *)(f + 1); + fc->cookie = cookie; + fc->iofuncs.read = iofuncs.read; + fc->iofuncs.write = iofuncs.write; + fc->iofuncs.seek = iofuncs.seek; + fc->iofuncs.close = iofuncs.close; + + f->fd = -1; + f->cookie = fc; + f->buf = (unsigned char *)f + sizeof *f + sizeof *fc + UNGET; + f->buf_size = BUFSIZ; + f->lbf = EOF; + + /* Initialize op ptrs. No problem if some are unneeded. */ + f->read = cookieread; + f->write = cookiewrite; + f->seek = cookieseek; + f->close = cookieclose; + + if (!libc.threaded) f->lock = -1; + + /* Add new FILE to open file list */ + return __ofl_add(f); +} -- 2.13.3