mkrundir.c (677B)
1 #include <errno.h> 2 #include <limits.h> 3 #include <stdint.h> 4 #include <stdio.h> 5 #include <sys/stat.h> 6 #include <unistd.h> 7 8 #define PARENT "/run/user/" // Must end with '/'. 9 10 int 11 main(void) { 12 uintmax_t uid = getuid(); 13 uintmax_t gid = getgid(); 14 15 // This should always be big enough. 16 char path[sizeof PARENT + sizeof(uintmax_t)*CHAR_BIT]; 17 int n = snprintf(path, sizeof path, PARENT"%ju", uid); 18 if (n < 0 || n >= sizeof path) return 1; 19 20 if (mkdir(path, S_IRWXU) < 0) { 21 if (errno != EEXIST) return 1; 22 // If it already exists, make sure the mode is correct. 23 if (chmod(path, S_IRWXU) < 0) return 1; 24 } 25 if (chown(path, uid, gid) < 0) return 1; 26 27 puts(path); 28 29 return 0; 30 }