Blame gl/tests/test-fopen-gnu.c

Packit Service 991b93
/* Test of opening a file stream.
Packit Service 991b93
   Copyright (C) 2020 Free Software Foundation, Inc.
Packit Service 991b93
Packit Service 991b93
   This program is free software: you can redistribute it and/or modify
Packit Service 991b93
   it under the terms of the GNU General Public License as published by
Packit Service 991b93
   the Free Software Foundation; either version 3 of the License, or
Packit Service 991b93
   (at your option) any later version.
Packit Service 991b93
Packit Service 991b93
   This program is distributed in the hope that it will be useful,
Packit Service 991b93
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 991b93
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 991b93
   GNU General Public License for more details.
Packit Service 991b93
Packit Service 991b93
   You should have received a copy of the GNU General Public License
Packit Service 991b93
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service 991b93
Packit Service 991b93
/* Written by Bruno Haible <bruno@clisp.org>, 2020.  */
Packit Service 991b93
Packit Service 991b93
#include <config.h>
Packit Service 991b93
Packit Service 991b93
/* Specification.  */
Packit Service 991b93
#include <stdio.h>
Packit Service 991b93
Packit Service 991b93
#include <errno.h>
Packit Service 991b93
#include <fcntl.h>
Packit Service 991b93
#include <unistd.h>
Packit Service 991b93
Packit Service 991b93
#include "macros.h"
Packit Service 991b93
Packit Service 991b93
#define BASE "test-fopen-gnu.t"
Packit Service 991b93
Packit Service 991b93
/* 0x1a is an EOF on Windows.  */
Packit Service 991b93
#define DATA "abc\x1axyz"
Packit Service 991b93
Packit Service 991b93
int
Packit Service 991b93
main (void)
Packit Service 991b93
{
Packit Service 991b93
  FILE *f;
Packit Service 991b93
  int fd;
Packit Service 991b93
  int flags;
Packit Service 991b93
  char buf[16];
Packit Service 991b93
Packit Service 991b93
  /* Remove anything from prior partial run.  */
Packit Service 991b93
  unlink (BASE "file");
Packit Service 991b93
  unlink (BASE "binary");
Packit Service 991b93
Packit Service 991b93
  /* Create the file.  */
Packit Service 991b93
  f = fopen (BASE "file", "w");
Packit Service 991b93
  ASSERT (f);
Packit Service 991b93
  fd = fileno (f);
Packit Service 991b93
  ASSERT (fd >= 0);
Packit Service 991b93
  flags = fcntl (fd, F_GETFD);
Packit Service 991b93
  ASSERT (flags >= 0);
Packit Service 991b93
  ASSERT ((flags & FD_CLOEXEC) == 0);
Packit Service 991b93
  ASSERT (fclose (f) == 0);
Packit Service 991b93
Packit Service 991b93
  /* Create the file and check the 'e' mode.  */
Packit Service 991b93
  f = fopen (BASE "file", "we");
Packit Service 991b93
  ASSERT (f);
Packit Service 991b93
  fd = fileno (f);
Packit Service 991b93
  ASSERT (fd >= 0);
Packit Service 991b93
  flags = fcntl (fd, F_GETFD);
Packit Service 991b93
  ASSERT (flags >= 0);
Packit Service 991b93
  ASSERT ((flags & FD_CLOEXEC) != 0);
Packit Service 991b93
  ASSERT (fclose (f) == 0);
Packit Service 991b93
Packit Service 991b93
  /* Open the file and check the 'x' mode.  */
Packit Service 991b93
  f = fopen (BASE "file", "ax");
Packit Service 991b93
  ASSERT (f == NULL);
Packit Service 991b93
  ASSERT (errno == EEXIST);
Packit Service 991b93
Packit Service 991b93
  /* Open a binary file and check that the 'e' mode doesn't interfere.  */
Packit Service 991b93
  f = fopen (BASE "binary", "wbe");
Packit Service 991b93
  ASSERT (f);
Packit Service 991b93
  ASSERT (fwrite (DATA, 1, sizeof (DATA)-1, f) == sizeof (DATA)-1);
Packit Service 991b93
  ASSERT (fclose (f) == 0);
Packit Service 991b93
Packit Service 991b93
  f = fopen (BASE "binary", "rbe");
Packit Service 991b93
  ASSERT (f);
Packit Service 991b93
  ASSERT (fread (buf, 1, sizeof (buf), f) == sizeof (DATA)-1);
Packit Service 991b93
  ASSERT (fclose (f) == 0);
Packit Service 991b93
Packit Service 991b93
  /* Cleanup.  */
Packit Service 991b93
  ASSERT (unlink (BASE "file") == 0);
Packit Service 991b93
  ASSERT (unlink (BASE "binary") == 0);
Packit Service 991b93
Packit Service 991b93
  return 0;
Packit Service 991b93
}