root/maint/gnulib/tests/test-fopen-gnu.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of opening a file stream.
   2    Copyright (C) 2020-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Bruno Haible <bruno@clisp.org>, 2020.  */
  18 
  19 #include <config.h>
  20 
  21 /* Specification.  */
  22 #include <stdio.h>
  23 
  24 #include <errno.h>
  25 #include <fcntl.h>
  26 #include <unistd.h>
  27 
  28 #include "macros.h"
  29 
  30 #define BASE "test-fopen-gnu.t"
  31 
  32 /* 0x1a is an EOF on Windows.  */
  33 #define DATA "abc\x1axyz"
  34 
  35 int
  36 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38   FILE *f;
  39   int fd;
  40   int flags;
  41   char buf[16];
  42 
  43   /* Remove anything from prior partial run.  */
  44   unlink (BASE "file");
  45   unlink (BASE "binary");
  46 
  47   /* Create the file.  */
  48   f = fopen (BASE "file", "w");
  49   ASSERT (f);
  50   fd = fileno (f);
  51   ASSERT (fd >= 0);
  52   flags = fcntl (fd, F_GETFD);
  53   ASSERT (flags >= 0);
  54   ASSERT ((flags & FD_CLOEXEC) == 0);
  55   ASSERT (fclose (f) == 0);
  56 
  57   /* Create the file and check the 'e' mode.  */
  58   f = fopen (BASE "file", "we");
  59   ASSERT (f);
  60   fd = fileno (f);
  61   ASSERT (fd >= 0);
  62   flags = fcntl (fd, F_GETFD);
  63   ASSERT (flags >= 0);
  64   ASSERT ((flags & FD_CLOEXEC) != 0);
  65   ASSERT (fclose (f) == 0);
  66 
  67   /* Open the file and check the 'x' mode.  */
  68   f = fopen (BASE "file", "ax");
  69   ASSERT (f == NULL);
  70   ASSERT (errno == EEXIST);
  71 
  72   /* Open a binary file and check that the 'e' mode doesn't interfere.  */
  73   f = fopen (BASE "binary", "wbe");
  74   ASSERT (f);
  75   ASSERT (fwrite (DATA, 1, sizeof (DATA)-1, f) == sizeof (DATA)-1);
  76   ASSERT (fclose (f) == 0);
  77 
  78   f = fopen (BASE "binary", "rbe");
  79   ASSERT (f);
  80   ASSERT (fread (buf, 1, sizeof (buf), f) == sizeof (DATA)-1);
  81   ASSERT (fclose (f) == 0);
  82 
  83   /* Cleanup.  */
  84   ASSERT (unlink (BASE "file") == 0);
  85   ASSERT (unlink (BASE "binary") == 0);
  86 
  87   return 0;
  88 }

/* [previous][next][first][last][top][bottom][index][help] */