Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <utime.h>
int utime(const char *pathname, const struct utimbuf *newtimes);
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 effective 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 effective UID of the calling process must match the owner UID of the file, or the calling process must have write permission to the file or appropriate privileges.
If successful, utime() returns 0 and updates the access and modification times of the file to those specified.
⁄* CELEBU07 *⁄
#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>
main() {
int fd;
char fn[]="utime.file";
struct utimbuf ubuf;
if ((fd = creat(fn, S_IWUSR)) < 0)
perror("creat() error");
else {
close(fd);
puts("before utime()");
system("ls -l utime.file");
ubuf.modtime = 0;
time(&ubuf.actime);
if (utime(fn, &ubuf) != 0)
perror("utime() error");
else {
puts("after utime()");
system("ls -l utime.file");
}
unlink(fn);
}
}
before utime()
--w------- 1 WELLIE SYS1 0 Apr 19 15:23 utime.file
after utime()
--w------- 1 WELLIE SYS1 0 Dec 31 1969 utime.file