This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: fgetpos/ftello without system call
- From: Florian Weimer <fweimer at redhat dot com>
- To: Carlos O'Donell <carlos at redhat dot com>, libc-help <libc-help at sourceware dot org>
- Date: Fri, 1 Sep 2017 15:12:13 +0200
- Subject: Re: fgetpos/ftello without system call
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=fweimer at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 157034629B
- References: <6ae5f3c9-58e3-d080-6103-a3bd3965bdfe@redhat.com> <af4a6442-c06f-7e95-f6f5-5ad8de70eed4@redhat.com>
On 09/01/2017 02:59 PM, Carlos O'Donell wrote:
> You should be able to do that, and it shouldn't trigger a seek.
It does not work as intended.
$ cat
#include <err.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char **argv)
{
if (argc != 3)
errx (1, "usage: %s PATH BUFFER\n", argv[0]);
FILE *fp = fopen64 (argv[1], "rce");
if (fp == NULL)
err (1, "fopen64");
int length = atoi (argv[2]);
if (length <= 1)
errx (1, "invalid length");
char *buffer = malloc (length);
int lineno = 0;
while (true)
{
off64_t offset = ftello64 (fp);
if (offset < 0)
err (1, "ftello64");
fgets (buffer, length, fp);
if (ferror (fp))
err (1, "fgets");
if (feof (fp))
break;
++lineno;
printf ("line %d at offset %lld: [[%s]]\n",
lineno, (long long) offset, buffer);
}
if (fclose (fp) != 0)
err (1, "fclose");
free (buffer);
return 0;
}
$ gcc -D_GNU_SOURCE -O2 -Wall -Werror=implicit-function-declaration
-Werror=implicit-int -o ftello-fgets -g ftello-fgets.c
$ strace -c -e lseek ./ftello-fgets /usr/share/misc/magic 500 > /dev/null
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00 0.051526 2 25709 lseek
------ ----------- ----------- --------- --------- ----------------
100.00 0.051526 25709 total
$ wc -l /usr/share/misc/magic
25708 /usr/share/misc/magic
Note sure what I'm doing wrong.
Thanks,
Florian