Back to index

glibc  2.9
test_rdwr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1992,1996,1997,1998,2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 
00024 
00025 int
00026 main (int argc, char **argv)
00027 {
00028   static const char hello[] = "Hello, world.\n";
00029   static const char replace[] = "Hewwo, world.\n";
00030   static const size_t replace_from = 2, replace_to = 4;
00031   char filename[FILENAME_MAX];
00032   char *name = strrchr (*argv, '/');
00033   char buf[BUFSIZ];
00034   FILE *f;
00035   int lose = 0;
00036 
00037   if (name != NULL)
00038     ++name;
00039   else
00040     name = *argv;
00041 
00042   (void) sprintf (filename, "/tmp/%s.test", name);
00043 
00044   f = fopen (filename, "w+");
00045   if (f == NULL)
00046     {
00047       perror (filename);
00048       exit (1);
00049     }
00050 
00051   (void) fputs (hello, f);
00052   rewind (f);
00053   (void) fgets (buf, sizeof (buf), f);
00054   rewind (f);
00055   (void) fputs (buf, f);
00056   rewind (f);
00057   {
00058     size_t i;
00059     for (i = 0; i < replace_from; ++i)
00060       {
00061        int c = getc (f);
00062        if (c == EOF)
00063          {
00064            printf ("EOF at %Zu.\n", i);
00065            lose = 1;
00066            break;
00067          }
00068        else if (c != hello[i])
00069          {
00070            printf ("Got '%c' instead of '%c' at %Zu.\n",
00071                   (unsigned char) c, hello[i], i);
00072            lose = 1;
00073            break;
00074          }
00075       }
00076   }
00077 
00078   {
00079     long int where = ftell (f);
00080     if (where == (long int) replace_from)
00081       {
00082        register size_t i;
00083        for (i = replace_from; i < replace_to; ++i)
00084          if (putc(replace[i], f) == EOF)
00085            {
00086              printf ("putc('%c') got %s at %Zu.\n",
00087                     replace[i], strerror (errno), i);
00088              lose = 1;
00089              break;
00090            }
00091       }
00092     else if (where == -1L)
00093       {
00094        printf ("ftell got %s (should be at %Zu).\n",
00095               strerror (errno), replace_from);
00096        lose = 1;
00097       }
00098     else
00099       {
00100        printf ("ftell returns %lu; should be %Zu.\n", where, replace_from);
00101        lose = 1;
00102       }
00103   }
00104 
00105   if (!lose)
00106     {
00107       rewind (f);
00108       if (fgets (buf, sizeof (buf), f) == NULL)
00109        {
00110          printf ("fgets got %s.\n", strerror(errno));
00111          lose = 1;
00112        }
00113       else if (strcmp (buf, replace))
00114        {
00115          printf ("Read \"%s\" instead of \"%s\".\n", buf, replace);
00116          lose = 1;
00117        }
00118     }
00119 
00120   if (lose)
00121     printf ("Test FAILED!  Losing file is \"%s\".\n", filename);
00122   else
00123     {
00124       (void) remove (filename);
00125       puts ("Test succeeded.");
00126     }
00127 
00128   return lose ? EXIT_FAILURE : EXIT_SUCCESS;
00129 }