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. ios开发之通知事件

    每天学习一点点,总结一点点,成功从良好的习惯开始! 昨天学习了ios开发中的关于通知事件的一些东西,在这里简单总结下,仅供初学者学习,更多的是怕我自己忘了,咩哈哈~~~~ 通知(notificatio ...

  2. extend简单用法

    eg:var obj1=[{a:1,b:2},{a:2,b:3}] var obj2=[{c:3,d:2},{c:4,d:3}] var resultArray=[]; for (var i = 0; ...

  3. 基于Jquery+Ajax+Json实现分页显示

    1.后台action产生json数据. List blackList = blackService.getBlackInfoList(mobileNum, gatewayid, startDate, ...

  4. adb设备,根据serial获取vid pid

    使用adb devices命令,可以轻松获取到所有连接到PC的adb设备的serial值. 但是adb命令无法获取adb usb设备的vendor id和product id. 本程序根据adb协议, ...

  5. HTML中如何添加日历插件(JQUERY)

    日期插件的包为:mydate.js1:首先将日期插件的包引入到页面中 1 <script language="JavaScript" src="../Inc/Js/ ...

  6. Unity3d场景合并

    Unity3d场景合并 一.Unity3d场景合并,有一次的情况是这样的,就是我们是每个人都在开发,每个人有不同的场景,那么合并的时候,有些会出问题,那么我有一个好的方案,就是首先弄一个公共的资源库, ...

  7. Knight Tournament

    Codeforces Round #207 (Div. 1) A:http://codeforces.com/problemset/problem/356/A 题意:给你n匹马,然后有m场比赛.每场比 ...

  8. KVM如何以HADOOP作共享存储?

    看到西部数码的作法. 回想IBM的SMARTCLOUD作法,这主要就是应用了HDFS? 外加上HBASE和ZOOKEEPER保驾的? 然后,再想到,这HDFS和OPENSTATCK的SWIFT...纠 ...

  9. Linux 配置多IP

    这里以红帽Linux为例.假定原系统已配置一个IP,地址为:192.168.20.140,配置文件路径/etc/sysconfig/network-script/ifcfg-eth0.现在需要配置一个 ...

  10. Instant Complexity(模拟,递归)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1535   Accepted: 529 Description Analyz ...