文件IO-chmod-fchown-lchown

第一版代码

/*
	Linux API:chown
	function:C 实现chmod函数功能,修改文件权限
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
	mode_t mode = S_IXUSR | S_IRUSR | S_IWUSR;
	char buf[100] = "";
	
	//printf("size is %ld\n", sizeof(buf));	// sizeof 可以计算数组的长度
	
	// {}初始化数组所有元素为0
	// for (int i = 0; i < sizeof(buf); i++)
	// {
		// printf("%d,", buf[i]);
	// }
	// printf("\nbuf[] is %d", buf[1]);
	
	scanf("%s", buf);
	getchar();
	system("ls -al");
	//int num = atoi(mode);	// 字符串转数字,但是权限还会用宏定义好
	//printf("mode is %d\n", mode);
	
	if (chmod(buf, mode) < 0)
	{
		perror("Error chmod");
		exit(EXIT_FAILURE);
	}
	system("ls -al");
	
	exit(EXIT_SUCCESS);
}
posted @ 2022-11-02 00:27  starc的miao  阅读(25)  评论(0)    收藏  举报