coreutils

custom mini coreutils
git clone git://git.evenfri.xyz/coreutils.git
Log | Files | Refs | README

touch.c (439B)


      1 #include <libc.h>
      2 
      3 #define eexist 17
      4 
      5 int main(int argc, char *argv[]) {
      6   if (argc < 2) {
      7     return 1;
      8   }
      9   
     10   for (int i = 1; i < argc; i++) {
     11     char *p = argv[i];
     12     int fd = open(p, o_creat | o_excl | o_wronly, 0666);
     13     if (fd == -1) {
     14       if (errno == eexist) {
     15         if (utime(p, NULL) == -1) {
     16           write(stderr, "touch: utime: error\n", 20);
     17         }
     18       }
     19     } else {
     20       close(fd);
     21     }
     22   }
     23 
     24   return 0;
     25 }