micro-utils/coreutils/touch.c
Your Name 7eed71d9b2 mk
2023-10-13 15:34:16 +03:00

43 lines
671 B
C

#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;
}