Back to index

glibc  2.9
test-utime.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994,1996,1997,2000,2002,2003 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 <fcntl.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <sys/stat.h>
00023 #include <unistd.h>
00024 #include <utime.h>
00025 #include <time.h>
00026 
00027 int
00028 main (int argc, char *argv[])
00029 {
00030   char file[L_tmpnam];
00031   struct utimbuf ut;
00032   struct stat st;
00033   struct stat stnow;
00034   time_t now1, now2;
00035   int fd;
00036 
00037   if (tmpnam (file) == 0)
00038     {
00039       perror ("tmpnam");
00040       return 1;
00041     }
00042 
00043   fd = creat (file, 0666);
00044   if (fd < 0)
00045     {
00046       perror ("creat");
00047       return 1;
00048     }
00049   close (fd);
00050 
00051   /* Test utime with arg */
00052   ut.actime = 500000000;
00053   ut.modtime = 500000001;
00054   if (utime (file, &ut))
00055     {
00056       perror ("utime");
00057       remove (file);
00058       return 1;
00059     }
00060 
00061   if (stat (file, &st))
00062     {
00063       perror ("stat");
00064       remove (file);
00065       return 1;
00066     }
00067 
00068   /* Test utime with NULL.
00069      Since there's a race condition possible here, we check
00070      the time before and after the call to utime.  */
00071   now1 = time (NULL);
00072   if (now1 == (time_t)-1)
00073     {
00074       perror ("time");
00075       remove (file);
00076       return 1;
00077     }
00078 
00079   /* The clocks used to set the modification time and that used in the
00080      time() call need not be the same.  They need not have the same
00081      precision.  Therefore we delay the following operation by one
00082      second which makes sure we can compare with second precision.  */
00083   sleep (1);
00084 
00085   if (utime (file, NULL))
00086     {
00087       perror ("utime NULL");
00088       remove (file);
00089       return 1;
00090     }
00091 
00092   sleep (1);
00093 
00094   now2 = time (NULL);
00095   if (now2 == (time_t)-1)
00096     {
00097       perror ("time");
00098       remove (file);
00099       return 1;
00100     }
00101 
00102   if (stat (file, &stnow))
00103     {
00104       perror ("stat");
00105       remove (file);
00106       return 1;
00107     }
00108 
00109   remove (file);
00110 
00111   if (st.st_mtime != ut.modtime)
00112     {
00113       printf ("modtime %ld != %ld\n", st.st_mtime, ut.modtime);
00114       return 1;
00115     }
00116 
00117   if (st.st_atime != ut.actime)
00118     {
00119       printf ("actime %ld != %ld\n", st.st_atime, ut.actime);
00120       return 1;
00121     }
00122 
00123   if (stnow.st_mtime < now1 || stnow.st_mtime > now2)
00124     {
00125       printf ("modtime %ld <%ld >%ld\n", stnow.st_mtime, now1, now2);
00126       return 1;
00127     }
00128 
00129   if (stnow.st_atime < now1 || stnow.st_atime > now2)
00130     {
00131       printf ("actime %ld <%ld >%ld\n", stnow.st_atime, now1, now2);
00132       return 1;
00133     }
00134 
00135   puts ("Test succeeded.");
00136   return 0;
00137 }