## chmod ##
改變文件的權限
### 相關函數 ###
fchmod,stat,open,chown
### 表頭文件 ###
```
#include<sys/types.h>
#include<sys/stat.h>
```
### 定義函數 ###
```
int chmod(const char *path, mode_t mode);
```
### 函數說明 ###
chmod()會依參數mode 權限來更改參數path 指定文件的權限。
### 參數 ###
mode 有下列數種組合
S_ISUID 04000 文件的(set user-id on execution)位
S_ISGID 02000 文件的(set group-id on execution)位
S_ISVTX 01000 文件的sticky位
S_IRUSR(S_IREAD) 00400 文件所有者具可讀取權限
S_IWUSR(S_IWRITE)00200 文件所有者具可寫入權限
S_IXUSR(S_IEXEC) 00100 文件所有者具可執行權限
S_IRGRP 00040 用戶組具可讀取權限
S_IWGRP 00020 用戶組具可寫入權限
S_IXGRP 00010 用戶組具可執行權限
S_IROTH 00004 其他用戶具可讀取權限
S_IWOTH 00002 其他用戶具可寫入權限
S_IXOTH 00001 其他用戶具可執行權限
只有該文件的所有者或有效用戶識別碼為0,才可以修改該文件權限。基于系統安全,如果欲將數據寫入一執行文件,而該執行文件具有S_ISUID 或S_ISGID 權限,則這兩個位會被清除。如果一目錄具有S_ISUID 位權限,表示在此目錄下只有該文件的所有者或root可以刪除該文件。
### 返回值 ###
權限改變成功返回0,失敗返回-1,錯誤原因存于errno。
### 錯誤代碼 ###
EPERM 進程的有效用戶識別碼與欲修改權限的文件擁有者不同,而且也不具root權限。
EACCESS 參數path所指定的文件無法存取。
EROFS 欲寫入權限的文件存在于只讀文件系統內。
EFAULT 參數path指針超出可存取內存空間。
EINVAL 參數mode不正確
ENAMETOOLONG 參數path太長
ENOENT 指定的文件不存在
ENOTDIR 參數path路徑并非一目錄
ENOMEM 核心內存不足
ELOOP 參數path有過多符號連接問題。
EIO I/O 存取錯誤
### 范例 ###
```
/* 將/etc/passwd 文件權限設成S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH */
#include<sys/types.h>
#include<sys/stat.h>
main()
{
chmod(“/etc/passwd”,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
}
```
- 字符測試篇
- isalnum
- isalpha
- isascii
- iscntrl
- isdigit
- isgraphis
- islower
- isprint
- isspace
- ispunct
- isupper
- isxdigit
- 字符串轉換篇
- atof
- atoi
- atol
- gcvt
- strtod
- strtol
- strtoul
- toascii
- tolower
- toupper
- 內存控制篇
- calloc
- free
- getpagesize
- malloc
- mmap
- munmap
- 日期時間篇
- asctime
- ctime
- gettimeofday
- gmtime
- localtime
- mktime
- settimeofday
- time
- 內存及字符串操作篇
- bcmp
- bcopy
- bzero
- index
- memccpy
- memchr
- memcmp
- memcpy
- memmove
- memset
- rindex
- strcasecmp
- strcat
- strchr
- strcmp
- strcoll
- strcpy
- strcspn
- strdup
- strlen
- strncasecmp
- strncat
- strncpy
- strpbrk
- strrchr
- strspn
- strstr
- strtok
- 常用數學函數篇
- abs
- acos
- asin
- atan
- atan2
- ceil
- cos
- cosh
- exp
- frexp
- ldexp
- log
- log10
- pow
- sin
- sinh
- sqrt
- tan
- tanh
- 用戶組篇
- endgrent
- endpwent
- endutent
- fgetgrent
- fgetpwent
- getegid
- geteuid
- getgid
- getgrent
- getgrgid
- getgrnam
- getgroups
- getpw
- getpwent
- getpwnam
- getpwuid
- getuid
- getutent
- getutid
- getutline
- initgroups
- pututline
- seteuid
- setfsgid
- setfsuid
- setgid
- setgrent
- setgroups
- setpwent
- setregid
- setreuid
- setuid
- setutent
- utmpname
- 數據結構及算法篇
- crypt
- bsearch
- lfind
- lsearch
- qsort
- rand
- srand
- 文件操作篇
- close
- creat
- dup
- dup2
- fcntl
- flock
- fsync
- lseek
- mkstemp
- open
- read
- sync
- write
- 文件內容操作篇
- clearerr
- fclose
- fdopen
- feof
- fflush
- fgetc
- fgets
- fileno
- fopen
- fputc
- fputs
- fread
- freopen
- fseek
- ftell
- fwrite
- getc
- getchar
- gets
- mktemp
- putc
- putchar
- rewind
- setbuf
- setbuffer
- setlinebuf
- setvbuf
- ungetc
- 進程操作篇
- atexit
- execl
- execlp
- execv
- execve
- execvp
- exit
- _exit
- vfork
- getpgid
- getpgrp
- getpid
- getppid
- getpriority
- nice
- on_exit
- setpgid
- setpgrp
- setpriority
- system
- wait
- waitpid
- 格式化輸入輸出篇
- fprintf
- fscanf
- printf
- scanf
- sprintf
- sscanf
- vfprintf
- vfscanf
- vprintf
- vscanf
- vsprintf
- vsscanf
- 文件權限控制篇
- access
- alphasort
- chdir
- chmod
- chown
- chroot
- closedir
- fchdir
- fchmod
- fchown
- fstat
- ftruncate
- getcwd
- link
- lstat
- opendir
- readdir
- readlink
- remove
- rename
- rewinddir
- seekdir
- stat
- symlink
- telldir
- truncate
- umask
- unlink
- utime
- utimes
- 信號處理篇
- alarm
- kill
- pause
- sigaction
- sigaddset
- sigdelset
- sigemptyset
- sigfillset
- sigismember
- signal
- sigpending
- sigprocmask
- sleep
- ferror
- perror
- strerror
- mkfifo
- pclose
- pipe
- popen
- 接口處理篇
- accept
- bind
- connect
- endprotoent
- endservent
- getsockopt
- htonl
- htons
- inet_addr
- inet_aton
- inet_ntoa
- listen
- ntohl
- ntohs
- recv
- recvfrom
- recvmsg
- send
- sendmsg
- sendto
- setprotoent
- setservent
- setsockopt
- shutdown
- socket
- 環境變量篇
- getenv
- putenv
- setenv
- 終端控制篇
- getopt
- isatty
- select
- ttyname