micro-utils/coreutils/touch.c

42 lines
670 B
C
Raw Permalink Normal View History

#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
int touch(const char *path) {
int fd = open(path, O_CREAT | O_RDONLY, 0666);
if (fd < 0) {
fprintf(stderr, "touch: %s\n", strerror(errno));
return 1;
}
close(fd);
struct utimbuf new_time;
time_t now = time(NULL);
new_time.actime = now;
new_time.modtime = now;
if (utime(path, &new_time) < 0)
return 1;
return 0;
}
int main(const int argc, const char **argv) {
if (argc == 1) {
printf("touch: missing operand\n");
return 1;
}
for (int i = 1; i < argc; i++)
if (touch(argv[i]))
return 1;
return 0;
}