Write a program in C for a UNIX system that meets the following conditions progr
ID: 3830075 • Letter: W
Question
Write a program in C for a UNIX system that meets the following conditions program requirements. The program must create parent and child processes with fork() and use the stat() function to determine file permissions.
Program requirements: The program should take either filenames or wildcards (*.c for example) as input. Create as many child processes as there are files on the command line. For each child process: a. Print if you have owner, group, or general permissions. b. For the appropriate type, print what type of permissions you have: read, write, execute c. If no filename is specified on the command prompt, use direct to retrieve all files in the current folder. The parent process should wait for all the children to finish and then print "ps -H"Explanation / Answer
#include #include #include #include #define MAX_COUNT 200 #define BUF_SIZE 100 pid_t pid; int i; char buf[BUF_SIZE]; int main(int argc, char **argv) { if(argc != 2) return 1; struct stat fileStat; if(stat(argv[1],&fileStat) < 0) return 1; printf("Information for %s ",argv[1]); printf("--------------------------- "); printf("File Size: %d bytes ",fileStat.st_size); printf("Number of Links: %d ",fileStat.st_nlink); printf("File inode: %d ",fileStat.st_ino); printf("File Permissions: "); printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-"); printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-"); printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-"); printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-"); printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-"); printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-"); printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-"); printf( (fileStat.st_mode & S_IROTH) ? "r" : "-"); printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-"); printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-"); printf(" "); printf("The file %s a symbolic link ", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not"); return 0; } fork(); pid = getpid(); for (i = 1; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.