chmod、chown函数的使用
生活随笔
收集整理的這篇文章主要介紹了
chmod、chown函数的使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
作用:改變指定文件的權(quán)限。第二個(gè)參數(shù):mode必須為一個(gè)8進(jìn)制數(shù);返回值為0表示成功,-1表示失敗。
//代碼
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h>int main(int argc, char* argv[]) {if(argc < 2){printf("a.out filename\n");exit(1);}int ret = chmod(argv[1], 0755); //0開(kāi)頭,8進(jìn)制數(shù),該方式僅僅只是修改了所屬主、所有者和其它用戶的權(quán)限。if(ret == -1){perror("chmod");exit(1);}return 0; }#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
作用:改變指定文件的所屬主和所屬組,注意所屬主和所屬組必須用身份號(hào)碼ID指定,不能用名字。
0成功? -1失敗?? 用戶ID和組ID 可以使用id命令或者在/etc/passwd文件中查看,/etc/passwd文件:用戶名:加密后的密碼:用戶ID:組ID:用戶名和注釋字段:用戶家目錄:終端類型
//代碼
#include <stdio.h> #include <stdlib.h>int main(int argc, char* argv[]) {if(argc < 2){printf("a.out filename!\n");exit(1);}// user->ftp group->ftpint ret = chown(argv[1], 116, 125);if(ret == -1){perror("chown");exit(1);}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的chmod、chown函数的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: access、strtol函数的使用(后
- 下一篇: 妖姬和武器哪个打排位强些