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

The syntaxe should be like this: $ mychmod nnn file. where nnn is an octal numbe

ID: 3634988 • Letter: T

Question

The syntaxe should be like this: $ mychmod nnn file. where nnn is an octal number having its usual meaning. e.g 640

Attention: The predefined function atoi () only works on decimal integers, the conversion is the responsibility of the program (you can write a function a2oct () for example)!

Write the command mychmod thus presented. Emphasis will be placed on
- Checking that there are at least two parameters (the new rights and a file) on the command line - the maximum number of parameters is not fixed;
- Check that the first parameter is an integer;
- Check the other parameters are files and which has the rights necessary for the implementation of chmod () on these files. We stop the execution of the program at the first error detected.

This is my code, but its is not working well. Please i need help. Thanks in advance


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

int isFile(const char *ct){
int test=0;
test=open(ct,O_RDONLY) ;
if(test==-1)
return 1;
else
return 0;
close(test);
}

int main( int nb , char ** args){

if(nb<3)
exit(1);
int nbs=(int)(atoi(args[1]));


int i=2;
while(i<nb)
{

if(isFile(args[i])==0){
chmod(args[i],nbs);
if(chmod(args[i],nbs)==0)
printf("success ");
else
printf("failed ");
}
else
exit(1);



i++;
}

return 0;

}

Explanation / Answer

The atoi() function only translates decimal, not octal. For octal conversion, use strtol() (or, as Chris Jester-Young points out, strtoul() - though the valid sizes of file permission modes for Unix all fit within 16 bits, and so will never produce a negative long anyway) with either 0 or 8 as the base. Actually, in this context, specifying 8 is best. It allows people to write 777 and get the correct octal value. With a base of 0 specified, the string 777 is decimal (again). Additionally: Do not use 'implicit int' return type for main(); be explicit as required by C99 and use int main(void) or int main(int argc, char **argv). Do not play with chopping trailing nulls off your string. char mode[4] = "0777"; This prevents C from storing a terminal null - bad! Use: char mode[] = "0777"; This allocates the 5 bytes needed to store the string with a null terminator. Report errors on stderr, not stdout. Report errors with a newline at the end. It is good practice to include the program name and file name in the error message, and also (as CJY pointed out) to include the system error number and the corresponding string in the output. That requires the header (for strerror()) and for errno. Additionally, the exit status of the program should indicate failure when the chmod() operation fails. Putting all the changes together yields: #include #include #include #include #include int main(int argc, char **argv) { char mode[] = "0777"; char buf[100] = "/home/hello.t"; int i; i = strtol(mode, 0, 8); if (chmod (buf,i) < 0) { fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s) ", argv[0], buf, mode, errno, strerror(errno)); exit(1); } return(0); } Be careful with errno; it can change when functions are called. It is safe enough here, but in many scenarios, it is a good idea to capture errno into a local variable and use the local variable in printing operations, etc. Note too that the code does no error checking on the result of strtol(). In this context, it is safe enough; if the user supplied the value, it would be a bad idea to trust them to get it right. One last comment: generally, you should not use 777 permission on files (or directories). For files, it means that you don't mind who gets to modify your executable program, or how. This is usually not the case; you do care (or should care) who modifies your programs. Generally, don't make data files executable at all; when files are executable, do not give public write access and look askance at group write access. For directories, public write permission means you do not mind who removes any of the files in the directory (or adds files). Again, occasionally, this may be the correct permission setting to use, but it is very seldom correct. (For directories, it is usually a good idea to use the 'sticky bit' too: 1777 permission is what is typically used on /tmp, for example - but not on MacOS X.)
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