From owner-freebsd-hackers@freebsd.org Sun Aug 25 22:51:35 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2C082C7DEF for ; Sun, 25 Aug 2019 22:51:35 +0000 (UTC) (envelope-from farhan@farhan.codes) Received: from mail.farhan.codes (mail.farhan.codes [155.138.165.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 46Gr3V1qSCz4Zwb; Sun, 25 Aug 2019 22:51:33 +0000 (UTC) (envelope-from farhan@farhan.codes) Received: from mail.farhan.codes (rainloop [172.16.0.4]) by mail.farhan.codes (Postfix) with ESMTPSA id 3C3F9166AD; Sun, 25 Aug 2019 18:50:55 -0400 (EDT) MIME-Version: 1.0 Date: Sun, 25 Aug 2019 22:50:55 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailer: RainLoop/1.12.1 From: "Farhan Khan" Message-ID: <0562c72f444c42097b5af75733686ff5@farhan.codes> Subject: Re: Trouble using and understanding funopen(3) To: cem@freebsd.org Cc: freebsd-hackers@freebsd.org In-Reply-To: References: <519c2fce85fe0db1cd189d2060f09a0f@farhan.codes> X-Rspamd-Queue-Id: 46Gr3V1qSCz4Zwb X-Spamd-Bar: ------- X-Spamd-Result: default: False [-7.05 / 15.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; R_DKIM_ALLOW(-0.20)[farhan.codes:s=mail]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(-0.20)[+mx]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; DKIM_TRACE(0.00)[farhan.codes:+]; RCPT_COUNT_TWO(0.00)[2]; DMARC_POLICY_ALLOW(-0.50)[farhan.codes,reject]; NEURAL_HAM_SHORT(-0.98)[-0.979,0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; IP_SCORE(-3.07)[ip: (-9.86), ipnet: 155.138.160.0/20(-4.93), asn: 20473(-0.52), country: US(-0.05)]; ASN(0.00)[asn:20473, ipnet:155.138.160.0/20, country:US]; MID_RHS_MATCH_FROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Aug 2019 22:51:35 -0000 August 22, 2019 4:28 PM, "Conrad Meyer" wrote:=0A=0A> H= i Farhan,=0A> =0A> First, I'd suggest using the more portable fopencookie= (3) interface,=0A> which is similar to funopen(3).=0A> =0A> Second, read = functions return 0 to indicate end of file.=0A> =0A> Finally, the file co= okie routines are stateful. If you want to create=0A> a pseudo-FILE that = only has 10 bytes in it, you have to track the=0A> current file offset by= creating a cookie. Here is a minimal example=0A> of using a cookie.=0A> = =0A> struct my_file {=0A> off_t offset;=0A> };=0A> =0A> my_read(void *v, = buf, len)=0A> {=0A> struct my_file *f =3D v;=0A> size_t rlen;=0A> =0A> /*= Indicate EOF for reads past EOF. */=0A> if (f->offset >=3D 10)=0A> retur= n (0);=0A> =0A> rlen =3D MIN(len, 10 - f->offset);=0A> memcpy(buf, "AAAAA= AAAAA", rlen);=0A> f->offset +=3D rlen;=0A> =0A> return ((int)rlen);=0A> = }=0A> =0A> main()=0A> {=0A> struct my_file *cookie;=0A> FILE *f;=0A> char= buf[100];=0A> size_t x;=0A> =0A> cookie =3D calloc(1, sizeof(*cookie));= =0A> f =3D fopencookie(cookie, "rb", { .read =3D my_read, });=0A> =0A> x = =3D fread(buf, 1, sizeof(buf), f);=0A> ...=0A> }=0A> =0A> Conrad=0A> =0A>= On Thu, Aug 22, 2019 at 9:24 AM Farhan Khan via freebsd-hackers=0A> wrote:=0A> =0A=0AConrad,=0A=0ATook me a while t= o massage that into the format I wanted, but I got it working. Thanks!=0A= =0ATo be honest, I had trouble understanding the manual. I was mistakenly= under the impression that the readfn function was literally just a pass-= through to whatever read mechanism you implement. It also was not obvious= how the return value is interpreted. Do you think it might be useful to = get an update to the documentation? Or was this me failing to understand = the way funopen worked?=0A=0AAlso, Linux's fopencookie(3) has a code samp= le. That might be good to have as well.=0A=0AThanks=0A=0A---=0AFarhan Kha= n=0APGP Fingerprint: 1312 89CE 663E 1EB2 179C 1C83 C41D 2281 F8DA C0DE