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

Hello I need help on writing and fixing my mistakes on a program using system ca

ID: 3638471 • Letter: H

Question

Hello I need help on writing and fixing my mistakes on a program using system calls that changes the permissions on a file. The program can be written in C or C++.

The executable should be called mychmod and should be executed by:

mychmod -u rwx -g rwx -o rwx -U rwx -G rwx -O rwx file1 file2 ...

The lowercase options add permissions and the uppercase options will remove permissions. Each of the switches is optional and should be interpreted as the ones in the Unix command chmod(1).The permissions can be speci?ed as any permutation of the string rwx, and can be split over multiple switches such as:


mychmod -u r -u wx -g rx -G w -O wrx file1 file2 ...

If the command cannot be properly parsed, or if there is no ?le speci?ed, it should print a help message to show usage of the command and exit with a status value of 1. As well as send the help message to stderr.


A successful execution of command is indicated by a return status of 0.


This is my code i have so far:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc, char *argv[])

// Reads

{
char char_read;
int fd;
if (argc < 2)
{
fprintf(stderr, "readfile: Filename must be specified ");
exit(EXIT_FAILURE);
}
if ((fd = open(argv[1], O_RDONLY)) < 0) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
while(read(fd, &char_read, 1) > 0)
printf("%c", char_read);
exit(EXIT_SUCCESS);
}

// fgetc()

char char_read;
FILE *fileptr;
if (argc < 2)
{
fprintf(stderr, "readfile: Filename must be specified ");
exit(EXIT_FAILURE);
}
if ((fileptr = fopen(argv[1], "r")) == NULL)
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
while((char_read = fgetc(fileptr)) != EOF)
printf("%c", char_read);
exit(EXIT_SUCCESS);
}

char mode[] = "";
char buf[] = "";
int i;
i = strtol(mode, 0, 8);
if (chmod (buf,i) < 0)
{
fprint(stderr, "%s: error in chmod(%s, %s) - %d (%s) ",
argv[0], buf, mode, errno, strerror(errno));
exit(1);
}
return(0);
}


// I have been working on this and still wont compile. Please let me know my mistakes and what i am doing wrong and solutions for them. Thanks.

Explanation / Answer

#include #include #include #include #include int main(int argc, char *argv[]) // Reads { char char_read; int fd; if (argc < 2) { fprintf(stderr, "readfile: Filename must be specified "); exit(EXIT_FAILURE); } if ((fd = open(argv[1], O_RDONLY)) < 0) { perror(argv[1]); exit(EXIT_FAILURE); } while(read(fd, &char_read, 1) > 0) printf("%c", char_read); exit(EXIT_SUCCESS); } // fgetc() char char_read; FILE *fileptr; if (argc < 2) { fprintf(stderr, "readfile: Filename must be specified "); exit(EXIT_FAILURE); } if ((fileptr = fopen(argv[1], "r")) == NULL) { perror(argv[1]); exit(EXIT_FAILURE); } while((char_read = fgetc(fileptr)) != EOF) printf("%c", char_read); exit(EXIT_SUCCESS); } char mode[] = ""; char buf[] = ""; int i; i = strtol(mode, 0, 8); if (chmod (buf,i) < 0) { fprint(stderr, "%s: error in chmod(%s, %s) - %d (%s) ", argv[0], buf, mode, errno, strerror(errno)); exit(1); } return(0); }
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