Modify a file without changing it’s access and modification times. This is old code, all this was done in school, many years ago, no idea if it still works on modern systems.
/* should open a file, truncate it to length 0 but keep the old acces and modification time */
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<utime.h>
#include<stdio.h>
int main(int argc, char *argv[])
/* In ISO C you can define `main’ either to take no arguments, or to
* take two arguments that represent the command line arguments to the
* program as shown above */
{
int i;
struct stat statbuf; /* defines statbuf as a structure of type stat */
/* struct stat {
* dev_t st_dev; ( device )
* ino_t st_ino; ( inode )
* mode_t st_mode; ( protection )
* nlink_t st_nlink; ( number of hard links )
* uid_t st_uid; ( user ID of owner )
* gid_t st_gid; ( group ID of owner )
* dev_t st_rdev; ( device type (if inode device) )
* off_t st_size; ( total size, in bytes )
* blksize_t st_blksize; ( blocksize for filesystem I/O )
* blkcnt_t st_blocks; ( number of blocks allocated )
* time_t st_atime; ( time of last access )
* time_t st_mtime; ( time of last modification )
* time_t st_ctime; ( time of last change )
}; */
struct utimbuf timebuf; /* defines timebuf as a structure of type utimbuf */
/* struct utimbuf {
time_t actime ( acces time )
time_t modtime ( modification time )
};
time_t is the data type used to represent simple time. Sometimes,
it also represents an elapsed time. When interpreted as a
calendar time value, it represents the number of seconds elapsed
since 00:00:00 on January 1, 1970, Coordinated Universal Time.*/
for(i=1; i<argc; i++) {
if( stat(argv[i],&statbuf) <0) /* return information about the specified file (argv[i]) into statbuf (man stat)
daca stat() returneaza un numar < 0, adica eroare */
printf(“%s:eroare stat\n”, argv[i]);
if (open(argv[i],O_RDWR | O_TRUNC)<0) /* convert a pathname into a file descriptor then open the file read/write and truncate it to 0 (man open) */
printf(“%s:eroare open\n”,argv[i]);
timebuf.actime=statbuf.st_atime;
timebuf.modtime=statbuf.st_mtime;
if(utime(argv[i], &timebuf) <0)
printf(“%s:eroare utime\n”, argv[i]);
};
return 0;
}