I have written a program to change the permissions for a file however whenever i run the code it doesnt change the permissions when i use the ls -l command in terminal to see if the change has actually been made.
Here is my code:
Code:
//
// main.c
// cm
//
// Created by B sahota on 7/9/18.
// Copyright © 2018 B sahota. All rights reserved.
//
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, const char * argv[]) {
//Print synopsis
if(argc == 1)
{
printf("cm FILE [rwx]");
}
//Check for BadFile
char fileName[150];
strcpy(fileName, argv[1]);
if(access(fileName, F_OK) != -1)
{
printf("File exists.");
}
else
{
printf("File entered does not exist.");
}
//new permission
char newPerm[5];
strcpy(newPerm, argv[2]);
//
//if argc = 2, clear permissions here and exit main()
//
// Owner - Read, Write, Execute
char rwx[5] = "rwx";
if(strcmp(rwx, newPerm) == 0)
{
printf("rwx");
chmod(fileName, 700);
}
//Owner - Read, Write
char rw[5] = "rw";
if(strcmp(rw, newPerm) == 0)
{
printf("rw");
chmod(fileName, 600);
}
//Owner - Read, Execute
char rx[5] = "rx";
if(strcmp(rx, newPerm) == 0)
{
printf("rx");
chmod(fileName, 500);
}
//Owner - Read
char r[5] = "r";
if(strcmp(r, newPerm) == 0)
{
printf("r");
chmod(fileName, 400);
}
}
To clarify the program funs fine and even prints out what is in the if statement so i know its getting into the if block which makes me think im maybe using the chmod function wrong?