RAD Studio
ContentsIndex
PreviousUpNext
chmod, _wchmod

Header File 

io.h  

Category 

Input/output Routines 

Prototype 

int chmod(const char *path, int amode); 

int _wchmod(const wchar_t *path, int amode); 

Description 

Changes file access mode. 

chmod sets the file-access permissions of the file given by path according to the mask given by amode. path points to a string. 

amode can contain one or both of the symbolic constants S_IWRITE and S_IREAD (defined in sys\stat.h).

S_IWRITE 
Permission to write 
S_IREAD 
Permission to read 
S_IREAD | S_IWRITE 
Permission to read and write (write permission implies read permission) 

Return Value 

Upon successfully changing the file access mode, chmod returns 0. Otherwise, chmod returns a value of -1. 

In the event of an error, the global variable errno is set to one of the following values:

EACCES 
Permission denied 
ENOENT 
Path or file name not found 

Example

#include <errno.h>
#include <stdio.h>
#include <io.h>
#include <process.h>
#include <sys\stat.h>
void main(void)
{
  char filename[64];
  struct stat stbuf;
  int amode;
  printf("Enter name of file: ");
  scanf("%s", filename);
  if (stat(filename, &stbuf) != 0)
  {
    perror("Unable to get file information");
    exit(1);
  }
  if (stbuf.st_mode & S_IWRITE)
  {
    printf("Changing to read-only\n");
    amode = S_IREAD;
  }
  else
  {
    printf("Changing to read-write\n");
    amode = S_IREAD|S_IWRITE;
  }
  if (chmod(filename, amode) != 0)
  {
    perror("Unable to change file mode");
    exit(1);
  }
  exit(0);
}

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
chmod 
 
 
_wchmod 
 
NT only 
 
 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!