apue实验之:utime

/*! \file main.cpp
 *  \brief 使用utime设置文件时间
 *  \date 05  6 2006 15:39:35
 *  \author "cyper - sun" <me@sunchangming.com>
 */

#if HAVE_CONFIG_H
# include <config.h>
#endif


#include <iostream>
#include <argtable2.h>
#include <ace/Log_Msg.h>
#include <ace/OS.h>
#include <ace/SString.h>

#include <sys/types.h>
#include <utime.h>

//#include "main.h"

#ifndef VERSION
#define VERSION "0.0.1"
#endif 


struct arg_file *files;
struct arg_lit *help, *version;
struct arg_end *end;

void utime_main(const char* filename);

int ACE_TMAIN( int argc, ACE_TCHAR* argv[]){
    ///将日志输出到STDERR和SYSLOG
  ACE_LOG_MSG->open(argv[0],ACE_Log_Msg::STDERR|ACE_Log_Msg::SYSLOG);

  setlocale(LC_ALL, ""); 

  ///分析options
  void* argtable[]={

    help = arg_lit0(NULL,"help",                "display this help and exit"),
    version = arg_lit0(NULL,"version",             "display version information and exit"),

    files = arg_filen(NULL, NULL, "FILE", 0, argc+2, NULL),
    end = arg_end(20),
  };
  const char *progname = "mkdir";
  int exitcode=0;

  /* verify the argtable[] entries were allocated sucessfully */
  if (arg_nullcheck(argtable) != 0) {
    /* NULL entries were detected, some allocations must have failed */
    printf("%s: insufficient memory\n",progname);
    exitcode=1;
    //goto exit;
  }
  else {
    /* Parse the command line as defined by argtable[] */
    int    nerrors = arg_parse(argc,argv,argtable);

    /* special case: '--help' takes precedence over error reporting */
    if (help->count > 0) {
      printf("Usage: %s", progname);
      arg_print_syntax(stdout,argtable,"\n");
      arg_print_glossary(stdout,argtable,"  %-25s %s\n");
      exitcode=0;
    }
    else{
      /* special case: '--version' takes precedence error reporting */
      if (version->count > 0) {
        std::cout<<VERSION<<std::endl;      
        exitcode=0;        
      }
      else {
        /* If the parser returned any errors then display them and exit */
        if (nerrors > 0) {
          /* Display the error details contained in the arg_end struct.*/
          arg_print_errors(stderr,end,progname);
          std::cout<<"Try '"<<progname<<" --help' for more information."
                   <<std::endl;
          exitcode=1;
        }
        else {
          /// 开始工作了……
          for(int i=0;i!=files->count;++i)
            utime_main(files->filename[i]);
        }
      }
    }
  }
  arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
  return exitcode;

  };


///utime这个函数已经过时了,应该使用utimes代替
  void utime_main(const char* filename){
    struct stat st;
    utimbuf buf;
    ///获得当前时间
    if (ACE_OS::stat(filename,&st)<0){
      ACE_ERROR((LM_ERROR,ACE_TEXT("%p\n"),ACE_TEXT("获取文件属性失败")));
      return;
    }
    ///清除现有文件内容
    if (ACE_OS::open(filename,O_RDWR| O_TRUNC)<0){
      ACE_ERROR((LM_ERROR,ACE_TEXT("%p\n"),ACE_TEXT("打开文件失败")));
      return;
    }

    buf.actime=st.st_atime;
    buf.modtime=st.st_mtime;

    ///重置时间
    if(utime(filename,&buf) <0)
      ACE_ERROR((LM_ERROR,ACE_TEXT("%p\n"),ACE_TEXT("设置文件时间失败")));
  }

$ echo "aaaa" > changemod
$ echo "bbbb" > times
$ ./utime changemod times
$ ls -lc changemod times 更改状态时间
-rw-r--r-- 1 snnn wheel 0 6 5 15:53 changemod
-rw-r--r-- 1 snnn wheel 0 6 5 15:53 times
$ ls -lu changemod times 最后存取时间
-rw-r--r-- 1 snnn wheel 0 6 5 15:51 changemod
-rw-r--r-- 1 snnn wheel 0 6 5 15:51 times
$ ls -l changemod times 最后修改时间
-rw-r--r-- 1 snnn wheel 0 6 5 15:52 changemod
-rw-r--r-- 1 snnn wheel 0 6 5 15:52 times

最后修改时间和最后存取时间都没有变。用open的trunc方式打开的文件的
时候不改变这两个值,尽管文件内容已经被清空了。
最后更改状态时间是我们手动用utime强行设置的。

此博客中的热门博文

少写代码,多读别人写的代码

在windows下使用llvm+clang

tensorflow distributed runtime初窥