chown.c (1191B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/stat.h> 5 #include <unistd.h> 6 #include <fcntl.h> 7 #include <errno.h> 8 9 int main(void) 10 { 11 char *template = "/tmp/sfakeroot.test.XXXXXX"; 12 char path[strlen(template) + 1]; 13 struct stat st_before; 14 struct stat st_after; 15 16 strcpy(path, template); 17 printf("test making path %s\n", mktemp(path)); 18 19 if (creat(path, 777) == -1) { 20 fprintf(stderr, "creat '%s' failed: %s\n", path, strerror(errno)); 21 } 22 23 if (stat(path, &st_before) == -1) 24 { 25 fprintf(stderr, "stat failed\n"); 26 return 1; 27 } 28 if (st_before.st_uid != 0 || st_before.st_gid != 0) { 29 fprintf(stderr, "uid/gid not 0\n"); 30 return 1; 31 } 32 33 if (chown(path, 42, 43) == -1) { 34 fprintf(stderr, "chown '%s' failed: %s\n", path, strerror(errno)); 35 return 1; 36 } 37 38 if (stat(path, &st_after) == -1) 39 { 40 fprintf(stderr, "stat failed\n"); 41 return 1; 42 } 43 if (st_after.st_uid != 42) { 44 fprintf(stderr, "uid not 42\n"); 45 return 1; 46 } 47 if (st_after.st_gid != 43) { 48 fprintf(stderr, "gid not 43\n"); 49 return 1; 50 } 51 52 return 0; 53 }