root/maint/gnulib/tests/test-pwrite.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test the pwrite function.
   2    Copyright (C) 2009-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 #include <config.h>
  18 
  19 #include <unistd.h>
  20 
  21 #include "signature.h"
  22 SIGNATURE_CHECK (pwrite, ssize_t, (int, const void *, size_t, off_t));
  23 
  24 #include <sys/types.h>
  25 #include <fcntl.h>
  26 #include <errno.h>
  27 #include <string.h>
  28 
  29 #include "macros.h"
  30 
  31 #define N (sizeof buf - 1)
  32 
  33 int
  34 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36   char const *file = "out";
  37   int fd;
  38   char buf[] = "0123456789";
  39   off_t pos = 2;
  40   size_t i;
  41   off_t init_pos;
  42 
  43   ASSERT (file);
  44 
  45   fd = open (file, O_CREAT | O_WRONLY, 0600);
  46   ASSERT (0 <= fd);
  47   ASSERT (write (fd, buf, N) == N);
  48   ASSERT (close (fd) == 0);
  49 
  50   fd = open (file, O_WRONLY, 0600);
  51   ASSERT (0 <= fd);
  52 
  53   init_pos = lseek (fd, pos, SEEK_SET);
  54   ASSERT (init_pos == pos);
  55 
  56   for (i = 0; i < N; i+=2)
  57     {
  58       const char byte = 'W';
  59       ASSERT (pwrite (fd, &byte, 1, i) == 1);
  60       ASSERT (lseek (fd, 0, SEEK_CUR) == init_pos);
  61     }
  62 
  63   {
  64     /* Invalid offset must evoke failure with EINVAL.  */
  65     const char byte = 'b';
  66     ASSERT (pwrite (fd, &byte, 1, (off_t) -1) == -1);
  67     ASSERT (errno == EINVAL);
  68   }
  69 
  70   ASSERT (close (fd) == 0);
  71 
  72   {
  73     fd = open (file, O_RDONLY);
  74     ASSERT (0 <= fd);
  75     ASSERT (read (fd, buf, N) == N);
  76     ASSERT (close (fd) == 0);
  77     ASSERT (strcmp ("W1W3W5W7W9",buf) == 0);
  78   }
  79 
  80   /* Test behaviour for invalid file descriptors.  */
  81   {
  82     char byte = 'x';
  83     errno = 0;
  84     ASSERT (pwrite (-1, &byte, 1, 0) == -1);
  85     ASSERT (errno == EBADF);
  86   }
  87   {
  88     char byte = 'x';
  89     close (99);
  90     errno = 0;
  91     ASSERT (pwrite (99, &byte, 1, 0) == -1);
  92     ASSERT (errno == EBADF);
  93   }
  94 
  95   return 0;
  96 }

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