实验内容及要求
实验内容:编写一个文件工具filetools,使其具有以下功能:
- 退出;
- 创建新文件;
- 写文件;
- 读文件;
- 修改文件权限;
- 查看当前文件权限并退出。
- …
提示用户输入功能号(0,1,2,3,4,5……),并根据用户输入的功能选择相应的功能。
实验要求:请仔细阅读实验准备的内容,对程序示例中确实的代码进行补充,然后使用GCC进行编译,并给出结果。
Shell版
#!/bin/bash
# filetools
while true ;
do
echo *e ""
echo -e ""
echo -e "**********************************************************"
echo -e "0:quit"
echo -e "1:create new files"
echo -e "2:write files"
echo -e "3:read files"
echo -e "4:chmod files"
echo -e "5:check permissino and quit"
echo -e "Please Enter 0 to 5:"
echo -e "----------------------------------------------------------"
folderpath="/home/joker2020/ddd"
read ANS
case $ANS in
0) exit;
;;
1) touch file1.txt
;;
2) read file
echo $file > ./file1.txt
echo "You have write a file in $PWD"
;;
3) while read line
do
echo $line
done < file1.txt
;;
4) while true;
do
echo -e "enter a number:"
echo -e "0: null"
echo -e "1: x"
echo -e "2: w"
echo -e "3: wx"
echo -e "4: r"
echo -e "5: rx"
echo -e "6: rw"
echo -e "7: rwx"
read NUM
case $NUM in
0)chmod 000 file1.txt
echo "You cancal the pomission of file1.txt"
break;
;;
1)chmod 100 file1.txt
echo "You change the pomission of file1.txt as x"
break;
;;
2)chmod 200 file1.txt
echo "You change the pomission of file1.txt as w"
break;
;;
3)chmod 300 file1.txt
echo "You change the pomission of file1.txt as wx"
break;
;;
4)chmod 400 file1.txt
echo "You change the pomission of file1.txt as r"
break;
;;
5)chmod 500 file1.txt
echo "You change the pomission of file1.txt as rx"
break;
;;
6)chmod 600 file1.txt
echo "You change the pomission of file1.txt as rw"
break;
;;
7)chmod 700 file1.txt
echo "You change the pomission of file1.txt as rwx"
break;
;;
*)echo "Wrong Number!Again"
;;
esac
done;;
5)ls -l file1.txt
exit;
;;
*) echo "You have choose a wrong number"
;;
esac
done
C语言版本
#include<stdio.h> //标准输入输出函数
#include<stdlib.h> //C、C++语言的最常用的系统函数
#include<sys/types.h>
#include<sys/stat.h>
#include<syslog.h>
#include<string.h>
//补充缺失的头文件
#define MAX 128
int chmd()
{
int c;
mode_t mode=S_IWUSR;
printf("0.0700\n 1.0400\n 2.0200\n 3.0100\n");
printf("Please input your choice of change the mode of the file :");
scanf("%d", &c);
switch(c)
{
case 0:chmod("file1", S_IRWXU); break;
// Mode flag: Read, write, execute by user.
//依据上面数字的提示定义其他case中文件权限的情况。
case 1:
chmod("file1",S_IRUSR);
break;
case 2:
chmod("file1",S_IWUSR);
break;
case 3:
chmod("file1",S_IXUSR);
break;
default:printf("You have a wrong choice! \n");
}
return 0;
}
void main()
{
int fd;
int num;
int choice;
char buffer[MAX];
struct stat st;
char* path="/bin/ls";
char* argv[4]={"ls","-l","file1",NULL};
while(1)
{
printf("*************************\n");
printf("0. 退出\n");
printf("1. 创建新文件\n");
printf("2. 写文件\n");
printf("3. 读文件\n");
printf("4. 修改文件权限\n");
printf("5. 查看当前文件的权限并退出\n");
printf("*************************\n");
printf("Please input your choice(0-6):");
scanf("%d", &choice);
switch(choice)
{
case 0: close(fd);
exit(0);
case 1:
fd=open("file1",O_RDWR|O_TRUNC|O_CREAT,0750);
/*
O_RDWR : file open mode: Read/Write
O_TRUNC : file open mode: Truncate file to length 0
O_CREAT : file open mode: Create if file does not yet exist.
0750: file access permission bits -rwxr-x---当前用户rwx;同组用户r-x;其他用户无权限
*/
if(fd==-1)
printf("File Create Failed!\n");
else
printf("fd=%d\n",fd);
break;
case 2:
fd = open("file1",O_RDWR|O_TRUNC|O_CREAT,0750);
scanf("%s",buffer);
while(num = read(fd,buffer,MAX)>0){
write("file2",buffer,num);
}
break;
case 3:
fd = open("file1",O_RDWR|O_TRUNC|O_CREAT,0750);
while(num = read(fd,buffer,MAX)>0){
write("STDOUT_FILENO",buffer,fd);
}
break;
case 4:
chmd();
printf("Change mode success!\n");
break;
case 5:
execv(path, argv); //-ls -l 显示文件完整的信息,包括权限,拥有者,拥有组,文件名,大小等
break;
default:
printf("You have a wrong choice!\n");
}
}
}