自己动手写shell之chgrp,chown,chmod
来源:互联网 发布:韩顺平 php泰牛 编辑:程序博客网 时间:2024/06/08 19:36
1.chgrp实现
#include <grp.h>#include <unistd.h>void chgrp(char * groupname,char * filename){ struct group * groupinfo = NULL; if((groupinfo = getgrnam(groupname)) == NULL) { printf("groupname does not exist\n"); return; } if(access(filename,F_OK) != 0) { printf("file %s does not exist\n",filename); return; } int gid = groupinfo->gr_gid; chown(filename,-1,gid); printf("the group id of the file has been changed successfully\n");}
2.chown实现
void chowner(char * ownername,char * filename){ struct passwd *password_info = NULL; if((password_info = getpwnam(ownername)) == NULL) { printf("username does not exist\n"); return; } if(access(filename,F_OK) != 0) { printf("file %s does not exist\n",filename); return; } int uid = password_info->pw_uid; chown(filename,uid,-1); printf("the user id of the file has been changed successfully\n");}
3.chmod实现
void _chmod(char * mod,char * filename){ if(access(filename,F_OK) != 0) { printf("the file %s does not exist",filename); return; } int len = strlen(mod); switch(len) { case 1: { int a; a = mod[0] - '0'; mode_t mode = 0; if(a < 0 || a > 7) { printf("octal number out of range\n"); return ; } if(a & 0x04) mode = mode | S_IROTH; if(a & 0x02) mode = mode | S_IWOTH; if(a & 0x01) mode = mode | S_IXOTH; chmod(filename,mode); printf("the mode has been changed successfully\n"); break; } case 2: { int a,b; mode_t mode = 0; a = mod[0] - '0'; b = mod[1] - '0'; if(a < 0 || a > 7 || b < 0 || b > 7) { printf("octal number out of range\n"); return ; } if(b & 0x04) mode = mode | S_IROTH; if(b & 0x02) mode = mode | S_IWOTH; if(b & 0x01) mode = mode | S_IXOTH; if(a & 0x04) mode = mode | S_IRGRP; if(a & 0x02) mode = mode | S_IWGRP; if(a & 0x01) mode = mode | S_IXGRP; chmod(filename,mode); printf("the mode has been changed successfully\n"); break; } case 3: { int a,b,c; mode_t mode = 0; a = mod[0] - '0'; b = mod[1] - '0'; c = mod[2] - '0'; if(a < 0 || a > 7 || b < 0 || b > 7 || c < 0 || c > 7) { printf("octal number out of range\n"); return ; } if(a & 0x04) mode = mode | S_IRUSR; if(a & 0x02) mode = mode | S_IWUSR; if(a & 0x01) mode = mode | S_IXUSR; if(b & 0x04) mode = mode | S_IRGRP; if(b & 0x02) mode = mode | S_IWGRP; if(b & 0x01) mode = mode | S_IXGRP; if(c & 0x04) mode = mode | S_IROTH; if(c & 0x02) mode = mode | S_IWOTH; if(c & 0x01) mode = mode | S_IXOTH; chmod(filename,mode); printf("the mode has been changed successfully\n"); break; } default: { int a,b,c,d; mode_t mode = 0; a = mod[len-4] - '0'; b = mod[len-3] - '0'; c = mod[len-2] - '0'; d = mod[len-1] - '0'; if(a != 0 || b < 0 || b > 7 || c < 0 || c > 7 || d < 0 || d >7) { printf("octal number out of range\n"); return ; } if(b & 0x04) mode = mode | S_IRUSR; if(b & 0x02) mode = mode | S_IWUSR; if(b & 0x01) mode = mode | S_IXUSR; if(c & 0x04) mode = mode | S_IRGRP; if(c & 0x02) mode = mode | S_IWGRP; if(c & 0x01) mode = mode | S_IXGRP; if(d & 0x04) mode = mode | S_IROTH; if(d & 0x02) mode = mode | S_IWOTH; if(d & 0x01) mode = mode | S_IXOTH; chmod(filename,mode); printf("the mode has been changed successfully\n"); break; } }}
0 0
- 自己动手写shell之chgrp,chown,chmod
- Liunx 学习之chmod,chown,chgrp命令
- Linux命令之 chgrp,chmod,chown
- Linux常用命令之usermod chgrp chown chmod
- linux之chown, chgrp, chmod命令
- chmod,chgrp,chown
- chown chgrp chmod
- chmod & chown & chgrp
- chmod chgrp chown
- chmod、chgrp、chown
- linux-shell-chmod,chown,chgrp-文件安全与权限
- chgrp chown chmod的使用方法
- chmod,chgrp,chown的使用
- ls+chmod+chown(chgrp)+umask
- chmod,chgrp,chown的使用
- linux常用命令chmod,chown,chgrp
- linux chmod chgrp chown rwx
- Linux学习之七(chown、chgrp、chmod)
- 海纳百川,有容乃大
- C++修改本地组策略
- Android 框架推荐
- hdu 5015 233 Matrix(构造矩阵)
- Ananagrams uva156
- 自己动手写shell之chgrp,chown,chmod
- cocos2d 定时器
- Ural 1961 Cantonese Dialect
- linux驱动调试方法
- Android MemInfo 各项的意义(转)
- JVM垃圾收集算法
- 谁控制了浏览器/
- 【算法导论】插入排序
- DBLINK