Thread: File Permissions will not change

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    16

    File Permissions will not change

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    chmod(3): change mode of file - Linux man page
    Well it does return a result and an error status, begin with those.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Why would I help you if your code is copyrighted?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    I altered my program to test its success and it does print it so should it have worked?
    Code:
    //Owner - Read
        char r[5] = "r";
        if(strcmp(r, newPerm) == 0)
        {
            printf("r");
            if(chmod(fileName, 400) == 0)
            {
                printf("Success");
            }
        }
    john c I'm guessing you never used xcode... Its put there by default this is code ive written from scratch
    Last edited by KingKush; 07-09-2018 at 02:06 PM.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    I have also implemented status which printed "code should work" so im not sure where my program is going wrong

    Code:
    //Owner - Read
    
    //To see if permissions have changed
        int statusCheck;
        struct stat buff;
    
    
        char r[5] = "r";
        if(strcmp(r, newPerm) == 0)
        {
            printf("r");
            if(chmod(fileName, 400) == 0)
            {
                printf("Success");
            }
        }
        statusCheck = stat(fileName, &buff);
        if(statusCheck == 0)
        {
            printf("Code should work.");
        }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks suspect to me:
    Code:
    chmod(fileName, 700);
    When providing a numeric mode value, I would have expected an octal argument for the mode, e.g.,
    Code:
    chmod(fileName, 0700);
    Quote Originally Posted by KingKush
    Code:
        statusCheck = stat(fileName, &buff);
        if(statusCheck == 0)
        {
            printf("Code should work.");
        }
    You should be checking buff, not merely checking the return value of stat, since the return value of stat only indicates whether the call to stat was successful.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Damn, should've spotted the octal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by john.c View Post
    Why would I help you if your code is copyrighted?
    Every piece of software created is automatically copyrighted by the author, unless it is created under a "Work for Hire Agreement", in which case it is copyrighted by the person / company that hired the programmer.

    Of course, I am referring to US Copyright law. I am not knowledgeable about the copyright laws of other countries, and make no assumption about the OP's nationality.

    The "License" to use the software is different!

    Proprietary License


    Open Source / Free Software License

    It is not unusual to add the line, "All rights reserved." to source code during development while contemplating the license the software will be finally released under.

    There is no reason not to respond to the OP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file permissions
    By witek25 in forum Windows Programming
    Replies: 6
    Last Post: 06-30-2007, 09:36 PM
  2. File permissions
    By Nephiroth in forum Windows Programming
    Replies: 2
    Last Post: 02-05-2006, 05:47 AM
  3. File Permissions
    By KPY79 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2005, 03:45 PM
  4. C File permissions
    By vipers in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 12:00 PM
  5. File Permissions
    By anumandal in forum Linux Programming
    Replies: 2
    Last Post: 03-21-2002, 01:52 AM

Tags for this Thread