Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the code related about this question Lab#2 talking : #include <stdio.h>

ID: 3865852 • Letter: T

Question

This is the code related about this question Lab#2 talking :

#include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h>
/*The Main Function Start*/ void main(int argc, char *argv[]) { /*Storing The Process Id*/ pid_t pid; int j; int status = 0; //printf("Welcome to Forking and Project Two ”); /* For loop*/ for (j = 1; j < argc; j++) { /*process of forking*/ pid=fork(); /*Child process*/ if (pid == 0) { /*Print file name & the process Id*/ printf("Filename: %s PID: %d ",argv[j],getpid()); return; } else{ } } /*Waiting for the child to finish*/ while( wait(NULL) > 0 ); /*Printing current running process*/ system("ps -H"); printf("The End! "); exit(0); }/*End #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h>
/*The Main Function Start*/ void main(int argc, char *argv[]) { /*Storing The Process Id*/ pid_t pid; int j; int status = 0; //printf("Welcome to Forking and Project Two ”); /* For loop*/ for (j = 1; j < argc; j++) { /*process of forking*/ pid=fork(); /*Child process*/ if (pid == 0) { /*Print file name & the process Id*/ printf("Filename: %s PID: %d ",argv[j],getpid()); return; } else{ } } /*Waiting for the child to finish*/ while( wait(NULL) > 0 ); /*Printing current running process*/ system("ps -H"); printf("The End! "); exit(0); }/*End


The project developed in Lab #2 will be extended by this one. Using stat you will need to open each file within a child process and determine the following: the er ID and the group D and then compare these to the current user. The 3 cases are: if the user is the owner, the user is in the same group as the owner, or if the owner is outside of the user group. After that information is printed, then print the combination of file permissions that apply to this file: read, write, execute. Sample code: man stat, man 2 stat, man getpwuid, man readdir, and fileaccess.c Program requirements: 1. The program should take either filenames or wildcards c for example) as input 2. Create as many child 3. The parent process should wait for all the children to finish and then print "ps-H" 4. For each child d processes as there are files on the command line. process: Print if you have owner, group, or general permissions. Hint: getuid0: & getgid) For the appropriate type, print what type of permissions you have: read, write, execute . 5% Extra Credit: instead of Canvas, submit your assignment via https://education.github.com/ (MUST be private, invite swirsz) I will use Github's last modified date as the submission date. Make sure to put your ful name in the header of your file. (example below) Last Revised: July 12, 2017 File:. You have general p File: Lab3.c You have owner permssions: read write execute : read execute

Explanation / Answer

Given below is the code for the question. Please don't forget to rate the answer if it helped. Thank you.

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>

void printStat(char *filename);
/*The Main Function Start*/
int main(int argc, char *argv[])
{
/*Storing The Process Id*/
pid_t pid;
int j;
  
//printf("Welcome to Forking and Project Two ”);
  
/* For loop*/
for (j = 1; j < argc; j++)
{
/*process of forking*/
pid=fork();
/*Child process*/
if (pid == 0)
{
printStat(argv[j]);
return 0;
}
else{
}
}
  
/*Waiting for the child to finish*/
while( wait(NULL) > 0 );
/*Printing current running process*/
system("ps -H");
printf("The End! ");
exit(0);
}

void printStat(char *filename)
{
struct stat sb;
  
printf("File: %s ", filename);
  
if (stat(filename, &sb) == -1) {
printf("error in stat ");
exit(1);
}


if(sb.st_uid == getuid()) //check if the file's owner is current user
{
printf("You have owner permissions: ");
if(sb.st_mode & S_IRUSR)
printf("read ");
if(sb.st_mode & S_IWUSR)
printf("write ");
if(sb.st_mode & S_IXUSR)
printf("execute ");
}
else if(sb.st_gid == getgid()) //check if the file's group is same current user's group
{
printf("You have group permissions: ");
if(sb.st_mode & S_IRGRP)
printf("read ");
if(sb.st_mode & S_IWGRP)
printf("write ");
if(sb.st_mode & S_IXGRP)
printf("execute ");
}
else
{
printf("You have other permissions: ");
if(sb.st_mode & S_IROTH)
printf("read ");
if(sb.st_mode & S_IWOTH)
printf("write ");
if(sb.st_mode & S_IXOTH)
printf("execute ");
}
  
  
printf(" ");
  
}

output

$ gcc fileperm.c
$ ./a.out list.c /Users/amoeba
File: list.c
You have owner permissions: read write
File: /Users/amoeba
You have group permissions: read execute
The End!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote