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的更多相关文章

  1. ls, chgrp, chown, chmod

    ls命令 [root@client ~]# ls -la 总用量 dr-xr-x---. root root 2月 : . dr-xr-xr-x. root root 2月 : .. -rwxrwxr ...

  2. 15、Linux 文件属性和测试( chgrp,chown,chmod和-e -f -d -s

    一.更改文件属性 1.chgrp:更改文件属组 语法: chgrp [-R] 属组名文件名 参数选项 -R:递归更改文件属组,就是在更改某个目录文件的属组时,如果加上-R的参数,那么该目录下的所有文件 ...

  3. linux常见命令chgrp/chown/chmod

    linux文件权限有读(r-4)写(w-2)执行(x-1) linux文件的所有方式有拥有者(user),属组(group),其他人(others) 改变文件属组命令 -- chgrp 修改/data ...

  4. 每天一个linux命令(21):chgrp,chown,chmod

    这三个命令都是改变文件属性与权限的,就放一起写了 charp:改变文件所属用户组 chown:改变文件所属者 chmod:改变文件的权限 一个文件对于owner,group ,others有不同的权限 ...

  5. 自己动手写shell命令之write

    Linux下write命令同意用户跟其它终端上的用户对话.用c语言实现shell命令write.代码例如以下: #include <stdio.h> #include <fcntl. ...

  6. 自己动手写shell命令之ls -R1fF

    ls命令的R參数代表递归的列出全部子目录中的全部文件,1表示每一行仅仅显示一个文件或目录,f表示不排序即输出.F表示在每项的输出的最后依据其文件类型对应的加上*/=>@|字符.通过c语言实现ls ...

  7. 自己动手写shell命令之more

    unix下more命令的简单实现: #include <stdio.h> #define PAGELEN 24 #define LINELEN 512 int do_more(FILE * ...

  8. 自己动手写shell命令之ls

    linux下ls命令(支持-R參数)的c语言实现: #include <stdio.h> #include <sys/types.h> #include <dirent. ...

  9. linux 中更改权限命令chown,chmod,chgrp

    写在前面,关于chown,chmod的区别 chown用法 用来更改某个目录或文件的用户名和用户组的 chown 用户名:组名 文件路径(可以是就对路径也可以是相对路径) 例1:chown root: ...

随机推荐

  1. python 中文乱码解决

    # -*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf8') physicsPath = u"D: ...

  2. Title of live Writer

    Test From Windows Live Writer **markdown bold**

  3. 转 四大Java EE容器(Tomcat、JBoss、Resin、Glassfish)之简单比较

                                      现在流行的Java EE容器有很多:Tomcat.JBoss.Resin.Glassfish等等.下面对这四种Java EE容器进行 ...

  4. 2. SharePoint Online 开发,请联系qq512800530。加好备注。(不要发站内信。。。)

    ///(不要发站内信...) <meta name="keywords" content="SharePoint Online, SP Online, SPO, S ...

  5. 【Java】Java 序列化的高级认识

    如果你只知道实现 Serializable 接口的对象,可以序列化为本地文件.那你最好再阅读该篇文章,文章对序列化进行了更深一步的讨论,用实际的例子代码讲述了序列化的高级认识,包括父类序列化的问题.静 ...

  6. JVM上的随机数与熵池策略

    在apache-tomcat官方文档:如何让tomcat启动更快里面提到了一些启动时的优化项,其中一项是关于随机数生成时,采用的“熵源”(entropy source)的策略. 他提到tomcat7的 ...

  7. 【POJ】2278 DNA Sequence

    各种wa后,各种TLE.注意若AC非法,则ACT等一定非法.而且尽量少MOD. #include <iostream> #include <cstdio> #include & ...

  8. Linux Kernel 空指针逆向引用拒绝服务漏洞

    漏洞名称: Linux Kernel 空指针逆向引用拒绝服务漏洞 CNNVD编号: CNNVD-201306-449 发布时间: 2013-07-01 更新时间: 2013-07-01 危害等级:   ...

  9. i++和++i的老问题

    对于++j,该式是指先将j的值自加1,然后再取j的值.自增过后参与计算 i的值也为10:对于i++,该表达式是指先取i的值做运算,再将i加1.参见过计算后再自增

  10. sql server 2008有关SQL的模糊查询

    执行 数据库查询时,有完整查询和模糊查询之分. 一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,%:表示任意0个 ...