2018-12-19 20:14:55 +00:00
|
|
|
#include <stdio.h>
|
2019-01-21 23:58:13 +00:00
|
|
|
#include <errno.h>
|
2018-12-19 20:14:55 +00:00
|
|
|
#include <utime.h>
|
2019-01-21 23:58:13 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2018-12-19 20:14:55 +00:00
|
|
|
#include <sys/types.h>
|
2019-01-21 23:58:13 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
static bool file_exists(const char* path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int rc = stat(path, &st);
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (rc == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
perror("stat");
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-12-19 20:14:55 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "usage: touch <path>\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-01-21 23:58:13 +00:00
|
|
|
if (file_exists(argv[1])) {
|
|
|
|
int rc = utime(argv[1], nullptr);
|
|
|
|
if (rc < 0)
|
|
|
|
perror("utime");
|
|
|
|
} else {
|
2019-04-23 15:04:12 +00:00
|
|
|
int fd = open(argv[1], O_CREAT, 0100644);
|
2019-01-21 23:58:13 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int rc = close(fd);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("close");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 20:14:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|