您的位置:首页 > 运维架构 > Linux

Linux命令简单实现 -- touch

2011-05-02 19:41 459 查看
/*
* touch.c
*
*  Created on: May 2, 2011
*      Author: bertrand
*/
#include <sys/time.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
char *program_name;
void usage()
{
printf("Usage: %s FILENAME/n", program_name);
}
static int touch(const char *file)
{
int fd = -1;
int status;
status = access(file, F_OK);	/* test if file existed */
if(status < 0)
{
/* create the file */
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(fd == -1)
{
printf("Can't create the file : Permission Denied!/n");
return -1;
}
}
else
{
/* test if file is writable */
status = access(file, W_OK);
if(status < 0)
{
printf("Can't modify the timestamp : Permission Denied!/n");
return -1;
}
else
{
status = utime(file, NULL);
if(status < 0)
{
printf("Can't modify the timestamp of %s/n", file);
return -1;
}
}
}
}
int main(int argc, char *argv[])
{
program_name = argv[0];
if(argc != 2)
{
usage();
return -1;
}
touch(argv[1]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: