自己动手写shell之chgrp,chown,chmod
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;
}
}
}
自己动手写shell之chgrp,chown,chmod的更多相关文章
- ls, chgrp, chown, chmod
ls命令 [root@client ~]# ls -la 总用量 dr-xr-x---. root root 2月 : . dr-xr-xr-x. root root 2月 : .. -rwxrwxr ...
- 15、Linux 文件属性和测试( chgrp,chown,chmod和-e -f -d -s
一.更改文件属性 1.chgrp:更改文件属组 语法: chgrp [-R] 属组名文件名 参数选项 -R:递归更改文件属组,就是在更改某个目录文件的属组时,如果加上-R的参数,那么该目录下的所有文件 ...
- linux常见命令chgrp/chown/chmod
linux文件权限有读(r-4)写(w-2)执行(x-1) linux文件的所有方式有拥有者(user),属组(group),其他人(others) 改变文件属组命令 -- chgrp 修改/data ...
- 每天一个linux命令(21):chgrp,chown,chmod
这三个命令都是改变文件属性与权限的,就放一起写了 charp:改变文件所属用户组 chown:改变文件所属者 chmod:改变文件的权限 一个文件对于owner,group ,others有不同的权限 ...
- 自己动手写shell命令之write
Linux下write命令同意用户跟其它终端上的用户对话.用c语言实现shell命令write.代码例如以下: #include <stdio.h> #include <fcntl. ...
- 自己动手写shell命令之ls -R1fF
ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...
- 自己动手写shell命令之more
unix下more命令的简单实现: #include <stdio.h> #define PAGELEN 24 #define LINELEN 512 int do_more(FILE * ...
- 自己动手写shell命令之ls
linux下ls命令(支持-R參数)的c语言实现: #include <stdio.h> #include <sys/types.h> #include <dirent. ...
- linux 中更改权限命令chown,chmod,chgrp
写在前面,关于chown,chmod的区别 chown用法 用来更改某个目录或文件的用户名和用户组的 chown 用户名:组名 文件路径(可以是就对路径也可以是相对路径) 例1:chown root: ...
随机推荐
- C++最后课程项目总结
第一次独立完成的C++小项目,40小时 + 5小时Update + 8小时Linux移植. 过程: 过程非常认真,一个星期主要就是忙这个,为了完成某个部分,有时饭都推迟吃,连续对着电脑10几个小时很累 ...
- AppiumDriver 运行app启动基本参数
记录一下 DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(Mobile ...
- workerman需要的php模块posix、pcntl、sysvshm、sysvmsg缺少,怎么办
如果您的php是源码编译,那么请进到php的源码目录,再进入ext目录下,分别找到相应的php模块目录,进行编译 1. 假设php目录为/usr/local/php, 进到相应的php模块目录,执行 ...
- 【技术宅11】php入门运算
//1.空bool $a=''; $b=NULL; $c=false; $d=0; $e='0'; $f=array(); $g=array(array()); $h='NULL'; var_dump ...
- js+dom开发第十六天
一.css常用标签及页面布局 1.常用标签 position(定位) z-index(定位多层顺序) background(背景) text-align(针对字符自动左右居中) margin(外边距) ...
- How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code = 0) ?
How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code ...
- Asp.Net Mvc - 在OnResultExecut* 拦截Action返回的HTML
在Asp.Net MVC项目中通过重写ActionFilterAttribute中的方法,我们就可以在轻松的在Action方法执行前后做一些特殊的操作如:[身份认证.日志记录.内容截取等]. 但是我们 ...
- [151116 记录] 使用Python3.5爬取豆瓣电影Top250
这一段时间,一直在折腾Python爬虫.已有的文件记录显示,折腾爬虫大概个把月了吧.但是断断续续,一会儿鼓捣python.一会学习sql儿.一会调试OpenCV,结果什么都没学好.前几天,终于耐下心来 ...
- Javascript学习之函数(function)
在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函 ...
- AST抽象语法树
抽象语法树简介 (一)简介 抽象语法树(abstract syntax code,AST)是源代码的抽象语法结构的树状表示,树上的每个节点都表示源代码中的一种结构,这所以说是抽象的,是因为抽象语法树并 ...