[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]$
linux之utime函数解析
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
18-utime 函数
utime 函数可以用来更改文件的最近访问时间和最近修改时间,即 atime 和 mtime。如果你不知道这两个时间是什么含义
linux utime 修改时间 #include 文件名