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. Java多线程:Semaphore

    Semaphore为并发包中提供用于控制某资源同时可以被几个线程访问的类 实例代码: // 允许2个线程同时访问 final Semaphore semaphore = new Semaphore(2 ...

  2. ExtJs中动态加载机制研究(转)

    觉得写的太好了,怕弄丢了,转一下:http://extjs.org.cn/node/659 昨天我们team对于extjs的动态加载机制做了些深入研究,这里先share下controller加载的结果 ...

  3. ROW_NUMBER() OVER函数的基本用法用法

    ROW_NUMBER() OVER函数的基本用法用法 转自:http://www.cnblogs.com/icebutterfly/archive/2009/08/05/1539657.html 语法 ...

  4. Android 内核基本知识

    Android基本知识 Android基本知识.... 1 1. 各版本系统特性.... 1 2. View绘制流程.... 2 3. 动画体系.... 2 4. 事件分发机制.... 3 输入消息获 ...

  5. nav

    $(document).ready(function() { $(window).resize(function(){ var need=0; var ul_max_width = $(window) ...

  6. WPF学习笔记-自定义窗口

    代码部分 <Style x:Key="for_noresize_window" TargetType="{x:Type Window}"> < ...

  7. linux中VI编辑器使用个人记录

    VI编辑器有三种编辑模式:命令模式.最后行模式.文本编辑模式 启动VI后进入的第一种模式是”命令模式“.从命令模式可进入最后行模式和编辑模式.而后两种模式之间不能直接切换.必须按ESC键退回到命令模式 ...

  8. Reducing the Dimensionality of data with neural networks / A fast learing algorithm for deep belief net

    Deeplearning原文作者Hinton代码注解 Matlab示例代码为两部分,分别对应不同的论文: . Reducing the Dimensionality of data with neur ...

  9. Unity3d项目合作 场景的合并和还原

    Unity3d项目合作  场景的合并和还原 特别声明:转载自Unity3D研究院 如何侵犯版权,请通知我删除! 摘要: 导出Unity场景的所有游戏对象信息,一种是XML一种是JSON.本篇文章我们把 ...

  10. ARM Cortex M3系列GPIO口介绍(工作方式探讨)

    一.Cortex M3的GPIO口特性    在介绍GPIO口功能前,有必要先说明一下M3的结构框图,这样能够更好理解总线结构和GPIO所处的位置. Cortex M3结构框图     从图中可以看出 ...