zoukankan      html  css  js  c++  java
  • 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 ", argv[i]);
                continue;
            }
            if((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0)
            {
                printf ("%s: open error ", 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 ", 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]$ 

  • 相关阅读:
    20172302 201720182 《程序设计与数据结构》实验二报告
    20172302 201720182 《程序设计与数据结构》实验一报告
    20172302 《程序设计与数据结构》第五周学习总结
    POJ 1061 青蛙的约会
    hdu 2485 Highways
    UVA 10608
    hdu 1213 how many tables
    java类static成员加载顺寻
    C# virtual,override或者new
    vs无法在WEB服务器上启动调试
  • 原文地址:https://www.cnblogs.com/riskyer/p/3236954.html
Copyright © 2011-2022 走看看