Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using chmod in a C program

Tags:

I have a program where I need to set the permissions of a file (say /home/hello.t) using chmod and I have to read the permissions to be set from a file. For this I first read the permissions into a character array and then try to modify the permissions of the file. But I see that permissions are set in a weird manner.

A sample program I have written:

main()
{
    char mode[4]="0777";
    char buf[100]="/home/hello.t";
    int i;
    i = atoi(mode);
    if (chmod (buf,i) < 0)
        printf("error in chmod");
}

I see that the permissions of the file are not set to 777. Can you please help me out on how to set the permissions of the file after reading the same from a character array.

like image 586
Raghav Avatar asked Dec 31 '10 07:12

Raghav


People also ask

What does chmod do in C?

chmod() automatically clears the S_ISGID bit in the file's mode bits if all these conditions are true: The calling process does not have appropriate privileges, that is, superuser authority (UID=0). The group ID of the file does not match the group ID or supplementary group IDs of the calling process.

How do I change permissions on a file in C?

To change the permission of a file, create (form) the command using the chmod command and pass it into the system() function.

How do I use chmod?

To add world read and execute permission to a file using the symbolic mode you would type chmod o+rx [filename]. To remove world read permission from a file you would type chmod o-r [filename].

How do I give permission to C file in Linux?

From man 3p chmod: SYNOPSIS #include <sys/stat. h> int chmod(const char *path, mode_t mode); ... If you want to read the permissions, you'd use stat.


1 Answers

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 <string.h> header (for strerror()) and <errno.h> for errno. Additionally, the exit status of the program should indicate failure when the chmod() operation fails.

Putting all the changes together yields:

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

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)\n",
                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.)

like image 66
Jonathan Leffler Avatar answered Sep 20 '22 08:09

Jonathan Leffler