程序4-6 utime函数实例

 1 //http://blog.chinaunix.net/uid-24549279-id-71355.html

 2 /*

 3  ============================================================================

 4  Name        : test.c

 5  Author      : blank

 6  Version     :

 7  Copyright   : Your copyright notice

 8  Description : 程序4-6 utime函数实例

 9  ============================================================================

10 */

11 

12 #include <fcntl.h>

13 #include <utime.h>

14 #include <sys/stat.h>

15 #include "ourhdr.h"

16 

17 int main(int argc, char *argv[])

18 {

19     int             i, fd;

20     struct stat     statbuf;

21     struct utimbuf     timebuf;

22 

23     for (i=1; i<argc; i++){

24         printf("argv[%d] = %s\n", i, argv[i]);

25         if (stat(argv[i], &statbuf) < 0){//fetch current times

26             err_ret("%s: stat error", argv[1]);

27             continue;

28         }

29 

30         if ((fd = open(argv[1], O_RDWR|O_TRUNC)) < 0){

31             err_ret("%s: open error", argv[1]);

32             continue;

33         }

34 

35         close(fd);

36         timebuf.actime = statbuf.st_atim.tv_sec;

37         timebuf.modtime = statbuf.st_mtim.tv_sec;

38         if (utime(argv[i], &timebuf) < 0){

39             // retset time

40             err_ret("%s: utime error", argv[i]);

41             continue;

42         }

43     }

44 }

 

你可能感兴趣的:(time)