Chapter Contents

Previous

Next
utime

utime



Specify Access and Modification Times for a File

Portability: POSIX.1 conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
SEE ALSO


SYNOPSIS

#include <utime.h>

int utime(char *name, struct utimbuf *time)


DESCRIPTION

utime updates the access and modification times for a file. name is the filename. time is the pointer to a utimbuf structure, which contains the new access and modification times. If time is NULL , the access and modification times for name are changed to the current time.

utimbuf contains the following:
time_t actime is the access time.
time_t modtime is the modification time.


RETURN VALUE

utime returns 0 if it is successful and -1 if it is not successful.


EXAMPLE

This example sets the modification time for a file to be the same as the time of last access:

#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>
#include <stdlib.h>

main(int argc, char *argv[]) {
   struct stat fdata;
   struct utimbuf tdata;
   int rc;

   if (argc != 2) {
      puts("Incorrect number of arguments");
      exit(EXIT_FAILURE);
   }
   rc = stat(argv[1], &fdata);
   if (rc != 0) {
      perror("stat failure");
      exit(EXIT_FAILURE);
   }
   tdata.modtime = tdata.actime = fdata.st_atime;
      /* Set modification time to access time.         */
   rc = utime(argv[1], &tdata);  /* Update file times. */
   if (rc != 0) {
      perror("utime failure");
      exit(EXIT_FAILURE);
   }
   exit(EXIT_SUCCESS);
}


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.