自己动手写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: ...
随机推荐
- iOS把两张图片合成一张图片
0x00 步骤 先读取两张图片把创建出CGImageRef 创建上下文画布 把图片依次画在画布指定位置上 从上下文中获得合并后的图片 关闭上下文 释放内存 0x01 代码实现 - (void)comp ...
- 《find技巧》-“linux命令五分系列”之一
一天一个命令,做个记录, 我要成大神,哈哈哈 本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linu ...
- photoshop cc 版本安装失败解决办法
好久没有碰ps,看了下在ps版本都到cc了.忍不住也想尝试最新版本,但是安装出现了很多问题,导致我花了很多时间才搞定,现在分享给大家几点经验吧. Exit Code: Please see speci ...
- Bootstrap_Javascript_弹出框
HTML: <button type="button" class="btn btn-default" data-container="body ...
- 谈谈 jQuery 中的防冲突(noConflict)机制
许多的 JS 框架类库都选择使用 $ 符号作为函数或变量名,jQuery 是其中最为典型的一个.在 jQuery 中,$ 符号只是 window.jQuery 对象的一个引用,因此即使 $ 被删除,w ...
- TatukGIS-TGIS_Editor.CreateShape
procedure CreateShape(const _layer: TObject; const _ptg: TGIS_Point3D; const _type: TGIS_ShapeType; ...
- Http和Socket连接的区别
Http和Socket连接区别 相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过自己的浅显理解能对初学者有所帮助. 1.TCP连接 要想明白Socket连接,先 ...
- DotNet 资源
DotNet 资源 目录 API 应用框架(Application Frameworks) 应用模板(Application Templates) 人工智能(Artificial Intelligen ...
- iOS类别(Category)与扩展(Extension)-b
苹果的官方文档 Category在iOS开发中使用非常频繁.尤其是在为系统类进行拓展的时候,我们可以不用继承系统类,直接给系统类添加方法,最大程度的体现了Objective-C的动态语言特性. #im ...
- iOS的launch image --备用
当我们打开一款应用程序的时候,首先映入眼帘的往往并不是程序的主界面,而是经过精心设计的欢迎界面,这个界面通常会停留几秒钟,然后消失.看似很平常的一个小小的欢迎界面,其实还大有讲究. 一.为什么会出现欢 ...