网站开发为什么需要团队完成,怎么搭建源码网站,襄阳做网站的公司有哪些,辽宁建设工程信息网中标公示几天一、chown 命令
下面以实例简单讲解下 chown 的使用方法。当前登录的账号是 sunbin
创建测试文件当前 test.txt 文件所有者是sunbin#xff0c;所属组也是sunbin。
利用 chown 命令修改 test.txt 的所有者和所属组.可以看到#xff0c;test.txt 的拥有者变成了 root#…一、chown 命令
下面以实例简单讲解下 chown 的使用方法。当前登录的账号是 sunbin
创建测试文件当前 test.txt 文件所有者是sunbin所属组也是sunbin。
利用 chown 命令修改 test.txt 的所有者和所属组.可以看到test.txt 的拥有者变成了 root所属组变为了root。 二、chown函数
1. chown函数原型
#include unistd.h
int chown(const char *pathname, uid_t owner, gid_t group);若成功返回0若出错返回-1
参数
pathname要更改的文件名owner拥有者 uidgropu所属组 gid需要注意的是这个函数接受的是 uid 和 gid而不是以字符串表示的用户名和用户组。所以需要另一个函数getpwnam根据字符串名称来获取 uid 和 gid. 它的原型如下 1. 测试代码
struct passwd
{char *pw_name; // usernamechar *pw_passwd; // user passworduid_t pw_uid; // user IDgid_t pw_gid; // group IDchar *pw_gecos; // user informationchar *pw_dir; // home directorychar *pw_shell; // shell program
}struct passwd *getpwnam(const char *name);1. 测试代码
#include pwd.h
#include stdio.h
#include stdlib.h
#include unistd.hint main(int argc, char *argv[])
{uid_t uid;struct passwd *pwd;char *endptr;if (argc ! 3 || argv[1][0] \0) {fprintf(stderr, %s owner file\n, argv[0]);exit(EXIT_FAILURE);}uid strtol(argv[1], endptr, 10); /* Allow a numeric string */if (*endptr ! \0) { /* Was not pure numeric string */pwd getpwnam(argv[1]); /* Try getting UID for username */if (pwd NULL) {perror(getpwnam);exit(EXIT_FAILURE);}uid pwd-pw_uid;}if (chown(argv[2], uid, -1) -1) {perror(chown);exit(EXIT_FAILURE);}exit(EXIT_SUCCESS);
}
输出结果 strtol用法链接
#include stdlib.h
long int strtol(const char *nptr, char **endptr, int base);
测试代码
#include stdio.h
#include stdlib.hint main()
{char buffer[20] 10379cend$3;char *stop;printf(%d\n, strtol(buffer, stop, 2)); //2printf(%s\n, stop);char a[] 100;char b[] 100;char c[] ffff;printf(a %d\n, strtol(a, NULL, 10)); //100printf(b %d\n, strtol(b, NULL, 2)); //4printf(c %d\n, strtol(c, NULL, 16)); //65535
}
输出结果 参考资料
1. 17-chown 函数