gtpc2m9c | C/C++ Language Support User's Guide |
This function sets file access and modification times.
Format
#define _POSIX_SOURCE #include <utime.h> int utime(const char *pathname, const struct utimbuf *newtimes);
This function sets the access and modification times of pathname to the values in the utimbuf structure. If newtimes is a NULL pointer, the access and modification times are set to the current time.
Normally, the user ID (UID) of the calling process must match the owner UID of the file, or the calling process must have appropriate privileges. However, if newtimes is a NULL pointer, the UID of the calling process must match the owner UID of the file, or the calling proess must have write permission to the file or appropriate privileges.
The contents of a utimbuf structure are:
Normal Return
If successful, the utime function returns the value 0 and updates the access and modification times of the file to those specified.
Error Return
If unsuccessful, the utime functions returns the value -1, does not update the file times and sets errno to one of the following:
Programming Considerations
None.
Examples
The following example creates a file, utime.file, and then uses the utime function to modify the timestamp of the file.
#define _POSIX_SOURCE #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <time.h> #include <unistd.h> #include <utime.h> int main(void) { int fd; char fn[]="utime.file"; struct utimbuf ubuf; if ((fd = creat(fn, S_IWUSR)) < 0) perror("creat() error"); else { close(fd); ubuf.modtime = 0; time(ubuf.actime); if (utime(fn, ubuf) != 0 perror("utime() error"); unlink(fn); } return 0; }
Related Information
See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.