linux之utime函数解析

[lingyun@localhost utime]$ ls
hello  utime.c  world
[lingyun@localhost utime]$ cat utime.c 
/*********************************************************************************
 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
 *                  All rights reserved.
 *
 *       Filename:  utime.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/04/2013~)
 *         Author:  fulinux <fulinux@sina.com>
 *      ChangeLog:  1, Release initial version on "08/04/2013 05:49:04 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <utime.h>
#include <sys/stat.h>




/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    int     i, fd;
    struct stat statbuf;
    struct utimbuf timebuf;


    for(i = 1; i < argc; i++)
    {
        if(stat(argv[i], &statbuf) < 0)
        {
            printf ("%s: stat error\n", argv[i]);
            continue;
        }
        if((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0)
        {
            printf ("%s: open error\n", argv[i]);
            continue;
        }
        close(fd);
        timebuf.actime  = statbuf.st_atime;
        timebuf.modtime = statbuf.st_mtime;


        if(utime(argv[i], &timebuf) < 0)
        {
            printf ("%s: utime error\n", argv[i]);
            continue;
        }
    }
    exit(0);
} /* ----- End of main() ----- */




[lingyun@localhost utime]$ gcc utime.c 
[lingyun@localhost utime]$ ls -l hello world 
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:03 hello
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:04 world
[lingyun@localhost utime]$ ls -lu hello world 
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:03 hello
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:04 world
[lingyun@localhost utime]$ date
Sun Aug  4 18:10:44 CST 2013
[lingyun@localhost utime]$ ./a.out hello world 
[lingyun@localhost utime]$ ls -l hello world 
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:03 hello
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:04 world
[lingyun@localhost utime]$ ls -lu hello world 
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:03 hello
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:04 world
[lingyun@localhost utime]$ ls -lc hello world 
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:10 hello
-rw-r--r-- 1 lingyun trainning 0 Aug  4 18:10 world
[lingyun@localhost utime]$ 

posted on 2013-08-04 21:10  you Richer  阅读(587)  评论(0编辑  收藏  举报