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; }
相关文章推荐
- Linux程序设计---chown命令的简单实现
- Linux(Fedora)下ls命令的简单实现
- 使用linux-c编程实现简单的ls命令
- linux中 wc 命令的简单实现
- linux下实现我自己简单的 cp 命令
- Linux命令简单实现 -- ls
- 在Linux中, 用C语言简单实现chmod命令
- c笔记——linux cat命令的简单实现
- 实现linux touch 命令及 文件权限的由来
- Linux下的 ls 命令的简单实现
- 一个简单的linux命令 touch
- python简单实现linux命令strings
- Linux系统编程_1_目录读取(实现简单ls命令)
- 最近才知道的一款软件,可以实现在windows下使用linux下的命令,甚至可以使用简单的shell
- Linux命令简单实现 -- pwd
- 20155322 2017-2018-1《信息安全系统设计》第九周 Linux命令:pwd命令学习与简单实现
- Linux下之使用简单3种创建文件的命令,并实现一个Html和JavaScript小程序
- Linux命令简单实现 -- whoami
- Linux下ls命令的简单实现
- 最简单的一些linux命令的基本的实现