glibc/libio/tst-ftell-append.c

170 lines
3.9 KiB
C
Raw Normal View History

/* Verify that ftell returns the correct value after a read and a write on a
file opened in a+ mode.
Copyright (C) 2014-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
<https://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <locale.h>
#include <wchar.h>
/* data points to either char_data or wide_data, depending on whether we're
testing regular file mode or wide mode respectively. Similarly,
fputs_func points to either fputs or fputws. data_len keeps track of the
length of the current data and file_len maintains the current file
length. */
#define BUF_LEN 4
static void *buf;
static char char_buf[BUF_LEN];
static wchar_t wide_buf[BUF_LEN];
static const void *data;
static const char *char_data = "abcdefghijklmnopqrstuvwxyz";
static const wchar_t *wide_data = L"abcdefghijklmnopqrstuvwxyz";
static size_t data_len;
static size_t file_len;
typedef int (*fputs_func_t) (const void *data, FILE *fp);
fputs_func_t fputs_func;
typedef void *(*fgets_func_t) (void *s, int size, FILE *stream);
fgets_func_t fgets_func;
static int do_test (void);
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"
static FILE *
init_file (const char *filename)
{
FILE *fp = fopen (filename, "w");
if (fp == NULL)
{
printf ("fopen: %m\n");
return NULL;
}
int written = fputs_func (data, fp);
if (written == EOF)
{
printf ("fputs failed to write data\n");
fclose (fp);
return NULL;
}
file_len = data_len;
fclose (fp);
fp = fopen (filename, "a+");
if (fp == NULL)
{
printf ("fopen(a+): %m\n");
return NULL;
}
return fp;
}
static int
do_one_test (const char *filename)
{
FILE *fp = init_file (filename);
if (fp == NULL)
return 1;
void *ret = fgets_func (buf, BUF_LEN, fp);
if (ret == NULL)
{
printf ("read failed: %m\n");
fclose (fp);
return 1;
}
int written = fputs_func (data, fp);
if (written == EOF)
{
printf ("fputs failed to write data\n");
fclose (fp);
return 1;
}
file_len += data_len;
long off = ftell (fp);
if (off != file_len)
{
printf ("Incorrect offset %ld, expected %zu\n", off, file_len);
fclose (fp);
return 1;
}
else
printf ("Correct offset %ld after write.\n", off);
return 0;
}
/* Run the tests for regular files and wide mode files. */
static int
do_test (void)
{
int ret = 0;
char *filename;
int fd = create_temp_file ("tst-ftell-append-tmp.", &filename);
if (fd == -1)
{
printf ("create_temp_file: %m\n");
return 1;
}
close (fd);
/* Tests for regular files. */
puts ("Regular mode:");
fputs_func = (fputs_func_t) fputs;
fgets_func = (fgets_func_t) fgets;
data = char_data;
buf = char_buf;
data_len = strlen (char_data);
ret |= do_one_test (filename);
/* Tests for wide files. */
puts ("Wide mode:");
if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
{
printf ("Cannot set en_US.UTF-8 locale.\n");
return 1;
}
fputs_func = (fputs_func_t) fputws;
fgets_func = (fgets_func_t) fgetws;
data = wide_data;
buf = wide_buf;
data_len = wcslen (wide_data);
ret |= do_one_test (filename);
return ret;
}