/* $ dcc chmod.c $ touch myfile $ ls -l myfile -rw------- 1 z5555555 z5555555 0 Oct 4 08:20 myfile $ ./a.out 755 myfile $ ls -l myfile -rwxr-xr-x 1 z5555555 z5555555 0 Oct 4 08:22 myfile $ */ #include #include #include // change permissions of the specified files int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } //Could have written a function to validate octal values in the argv[1] char *end; // first argument is mode in octal mode_t mode = strtol(argv[1], &end, 8); //printf("%s\n",end); // check first argument was a valid octal number if (argv[1][0] == '\0' || end[0] != '\0') { fprintf(stderr, "%s: invalid mode: %s\n", argv[0], argv[1]); return 1; } //works for multiple files supplied thorugh command line arguments for (int arg = 2; arg < argc; arg++) { if (chmod(argv[arg], mode) != 0) { perror(argv[arg]); // prints why the chmod failed return 1; } } return 0; }