/* Example for the stat() syscall */ #include #include #include #include #include #include #define FILENAME "../test.txt" int main(int argc, char **argv){ /* int stat(const char *file_name, struct stat *buf); */ struct stat fileInfo; int result; struct passwd * usrInfo; char * usrname; result = stat(FILENAME, &fileInfo); /* alternative using file descriptor: fstat(2) */ if (result) { if (errno == ENOENT) { printf("Error: File does not exist.\n"); /* do specific error handling since file doesn't exist ... */ } else { /* an error occured */ printf("Error when calling stat(): %s\n", strerror(errno)); } } else { printf("\n"); printf("Call to stat() succeeded.\n"); /* use fileInfo... */ printf("File permissions: %04o\n", fileInfo.st_mode&07777); usrInfo = getpwuid(fileInfo.st_uid); if (usrInfo != NULL) { printf("Owner of file: %s\n", usrInfo->pw_name); } printf("\n"); } return result; }