Linux(erjiwenjianxitong)

123
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MaxUser 1028    //用户最多数目
#define commandAmount 12     //对文件操作的指令数
//用户管理
int userID = -1; //用户登录的ID号,值为-1时表示没有用户登录
char Name[15];     //正在登录用户名
char firstName[5]; //正在被操作的文件
long used = 0;    //全局变量,用户数
long used1 = 0;    //全局变量,用户创建文件数
char q2[20] = "OS/node/";
typedef struct master_file_directory
{
	char userName[15];
	char password[15];
} MFD;
MFD userTable[MaxUser];

typedef struct node {
	char fileName[15];
	char number[5];
//UFD *user;
} MFD1;
MFD1 fileTable[1024];

void userCreate();   //创建用户
int login();   //用户登录
int createDirectory(const char*filename);//创建目录
void fileAttribute(char*fileName);//文件属性
void readDir1();//读用户
void create(char*name); //创建文件
void deletefile(const char*filename);//删除文件
int openfile(char*name);//打开文件
void writefile(char name[], char*buf);//写文件
void readfile(char name[]);//查看文件内容
void closefile(int fd);
void userCreate();//创建用户
int login();//用户登录
void createfile(char name[], char kind[]);//创建文件
void reFileName(char*old, char*new);//文件重命名
void chmodfile(char name[], char kind[]);//修改文件权限
int main() {

	char order[commandAmount][10];
	strcpy(order[0], "create");
	strcpy(order[1], "delete");
	strcpy(order[2], "read");
	strcpy(order[3], "write");
	strcpy(order[4], "arr");
	strcpy(order[5], "chmod");
	strcpy(order[6], "ren");
	strcpy(order[7], "dir");
	strcpy(order[8], "return");
	strcpy(order[9], "exit");
	strcpy(order[10], "diru");
	strcpy(order[11], "delu");
	char command[1028], command_str1[10], command_str2[10], command_str3[1028];
	int i, k, j;
	int length;
	createDirectory("OS");
	createDirectory("OS/node");
	create("OS/user.txt");
	long used;
	struct stat d;
	stat("OS/user.txt", &d);
	d.st_size;
	used = (d.st_size) / 30l;

	FILE *fp;

	if ((fp = fopen("OS/user.txt", "r")) == NULL)

	{
		printf("cannot open this file\n");

		exit(0);

	}
	fread(userTable, sizeof(struct master_file_directory), used, fp);

	fclose(fp);
    if(used==0)
    {
    		strcpy(userTable[0].userName,"root");
       	    strcpy(userTable[0].password,"root");

    			char q[20] = "OS/";
    			char q2[20] = "OS/node/";
    			strcat(q, userTable[0].userName);
    			strcat(q2, userTable[0].userName);
    			createDirectory(q);
    			create(q2);
    			used++;
    			FILE *fp;
    			if ((fp = fopen("OS/user.txt", "w")) == NULL)

    			{
    				printf("cannot open this file\n");

    				exit(0);

    			}
    			fwrite(userTable, sizeof(struct master_file_directory), used, fp);
    			fclose(fp);

    }


	int g = 0;
	while (g == 0) {
		printf("############################################\n");  
		printf("                  1、Login\n");
		printf("                  2、Register\n");
	    printf("                  3、Exit\n");
		printf("############################################\n");
		printf("Please input your choice:>");
		int choice;
		scanf("%d", &choice);
		if (choice == 1) {
			userID = login();
		} else if (choice == 2)
			userCreate();
		else if ((choice == 3))
			exit(0);
		else
			printf("Error,please input right choice\n");
			
		while (userID != -1 && g == 0) {
			fflush(stdin);
			printf("———————————————————————————————————————\n");
			printf(" create--e.g--[create hello rw]--Will create a file named file that can read and write\n");
			printf(" create--e.g--[create hello nrw]--Will create a file named file that can't read or write\n");
			printf(" create--e.g--[create hello r]--Will create a file named file that can read only\n");
			printf(" create--e.g--[create hello w]--Will create a file named file that can write only\n");
			printf(" delete--e.g--[delete hello]--Will delete the file name hello\n");
			printf(" read--e.g[read hello]--Read the file named hello\n");
			printf(" write--[write hello 123]--Write 123 into file hello\n");
			printf(" chmod--[chmod hello r]--Change file hello into read only\n");
			printf(" chmod--[chmod hello w]--Change file hello into write only\n");
			printf(" chmod--[chmod hello rw]--Change file hello into read and write\n");
			printf(" chmod--[chmod hello wr]--Change file hello into write and read\n");
			printf(" dir\n");
			printf(" arr--[arr hello]--Display the attributes of file\n");
			printf(" ren--[ren hello hi]--Rename hello into hi\n");
			printf(" return--return to the Login statue\n");
			printf(" exit\n");
			if(!strcmp(Name,"root"))
						{
				printf(" diru--Display all the users\n");
				printf(" delu--[delu A]--Display user A\n");
  			    }
			printf("————————————————————————————————————————\n");
			printf("please input your command:>");
			gets(command);
			int select;
			for (i = 0; command[i] != ' ' && command[i] != '\0'; i++) //command_str1字符串存储命令的操作类型
				command_str1[i] = command[i];
			k = i;
			command_str1[k] = '\0';
			for (i = 0; i < commandAmount; i++) {
				if (!strcmp(command_str1, order[i])) {
					select = i;
					break;
				}
			}
			if (i == commandAmount) {
				printf("Error,please input again\n");
				continue;
			}
			for (i = k + 1, k = 0; command[i] != ' ' && command[i] != '\0';
					i++, k++) //commmand_str2字符串存储文件名或用户名
				command_str2[k] = command[i];
			command_str2[k] = '\0';
			k = i;
			char q[20] = "OS/";
			strcat(q, Name);
			char p[3] = "/";
			strcat(q, p);
			strcat(q, command_str2);
			strcpy(firstName, command_str2);

			switch (select) {
			case 0:

				for (i = k + 1, k = 0; command[i] != ' ' && command[i] != '\0';
						i++, k++)
					command_str3[k] = command[i];
				command_str3[k] = '\0';

				createfile(q, command_str3);
				break;
			case 1:

				deletefile(q);
				break;
			case 2:
				readfile(q);
				break;
			case 3:
				if (open(q, O_WRONLY | O_APPEND) == -1) {
						printf("The file does not exist or not permissions to write the file\n");
						break;
					}
				int l=0;


				for (i = k + 1, k = 0;command[i] != '\0';
						i++, k++)
					{
					  command_str3[k] = command[i];
					}
				command_str3[k] = '\0';
				k = i;
				writefile(q, command_str3);
				l=1;


				if((strlen(command_str3))>1)
				{
				writefile(q, " ");
				printf("Has written to the file\n");
				}

				break;
			case 4:
				fileAttribute(command_str2);
				break;
			case 5:
				for (i = k + 1, k = 0; command[i] != ' ' && command[i] != '\0';
						i++, k++)
					command_str3[k] = command[i];
				command_str3[k] = '\0';
				chmodfile(q, command_str3);
				break;
			case 6:
				for (i = k + 1, k = 0; command[i] != ' ' && command[i] != '\0';
									i++, k++)
								command_str3[k] = command[i];
							command_str3[k] = '\0';

				reFileName(command_str2, command_str3);
				break;
			case 7:
				readDir();
				break;
			case 8:
				userID = -1;
				break;
			case 9:
				g = 1;
				break;
			case 10:
				readDir1();
				break;
			case 11:
				deldir(command_str2);
				break;
			}
		}
	}

	return 0;
}

/*---------创建目录--------*/
int createDirectory(const char*filename) {
	return mkdir(filename, S_IRWXU);
}

/*---------文件属性--------*/
void fileAttribute(char*fileName) {
	char q2[20] = "OS/node/";
	strcat(q2, Name);
	struct stat d;
	stat(q2, &d);
	used1 = (d.st_size) / 20l;
	FILE *fp;

	if ((fp = fopen(q2, "r")) == NULL) {
		printf("error1\n");
		exit(0);
	}
	fread(fileTable, sizeof(struct node), used1, fp);
	fclose(fp);
	int i;
	char e[30] = "OS/";
	strcat(e, Name);
	strcat(e, "/");
	strcat(e, fileName);
	stat(e, &d);

	char b[5] = "";
	int l = 0;
	if (!access(e, 4)) {
		strcpy(b, "r");
		l = 1;
	}
	if (!access(e, 2)) {
		strcat(b, "w");
		l = 1;
	}
	if (l == 0) {
		strcat(b, "nrw");
	}
	for (i = 0; i < used1; i++) {
		if (!strcmp(fileTable[i].fileName, fileName)) {
			printf("FileName  Type  Length  PhyAddr \n");
			printf("%s          %s      %ld        %s",fileTable[i].fileName,
						b,d.st_size,fileTable[i].number);
			break;
		}

	}
	if (i == used1) {
		printf("Error,without this file\n");
	}

}


/*---------读用户--------*/
void readDir1()
{
	long used;
	struct stat d;
	stat("OS/user.txt", &d);
	d.st_size;
	used = (d.st_size) / 30l;
	FILE *fp;
	if ((fp = fopen("OS/user.txt", "r")) == NULL)

	{
		printf("cannot open this file\n");

		exit(0);

	}
	fread(userTable, sizeof(struct master_file_directory), used, fp);
	int i;int f=0;
	for(i=0;i<used;i++)
	{
		if(strcmp(userTable[i].userName,"root"))
		{
		  printf("%s\n",userTable[i].userName);
		  f=1;
		}

	}
	if(f==0)
	{
		printf("Hasn't create the user\n");
	}

}


/*---------删除用户--------*/
void deldir(const char*filename)
{   char v[20]="OS/";

	strcat(v,filename);
	remove(v);

	long used;
	struct stat d;
	stat("OS/user.txt", &d);
	d.st_size;
	used = (d.st_size) / 30l;
	FILE *fp;
	if ((fp = fopen("OS/user.txt", "r")) == NULL)

	{
		printf("cannot open this file\n");

		exit(0);

	}
	fread(userTable, sizeof(struct master_file_directory), used, fp);
	int i, m = 0;
	for (i = 0; i < used; i++) {
		if (!strcmp(userTable[i].userName, filename)) {
			m = 1;
			for (; i < used - 1; i++)
				{
				 strcpy(userTable[i].userName, userTable[i+1].userName);
			     strcpy(userTable[i].password, userTable[i+1].password);
				}
			used--;
			if ((fp = fopen("OS/user.txt", "w")) == NULL) {
				printf("error1\n");
				exit(0);
			}
			fwrite(userTable, sizeof(struct master_file_directory), used, fp);
			fclose(fp);
			printf("Delete the user successfully\n");
			break;

		}
	}

	if(m==0)
	{
		printf("The user does't exist\n");
	}
     }

/*---------读用户文件属性--------*/
void readDir() {

	char q2[20] = "OS/node/";
	strcat(q2, Name);
	struct stat d;
	stat(q2, &d);
	used1 = (d.st_size) / 20l;
	FILE *fp;
	if ((fp = fopen(q2, "r")) == NULL) {
		printf("error1\n");
		exit(0);
	}
	fread(fileTable, sizeof(struct node), used1, fp);
	fclose(fp);
	int i;
	printf("FileName  Type  Length  PhyAddr \n    \n");
	for (i = 0; i < used1; i++) {

		struct stat d;
		char e[30] = "OS/";
		strcat(e, Name);
		strcat(e, "/");
		strcat(e, fileTable[i].fileName);
		stat(e, &d);

		char b[5] = "";
		int l = 0;
		if (!access(e, 4)) {
			strcpy(b, "r");
			l = 1;
		}
		if (!access(e, 2)) {
			strcat(b, "w");
			l = 1;
		}
		if (l == 0) {
			strcat(b, "nrw");
		}
		printf("%s          %s      %ld        %s",fileTable[i].fileName,
				b,d.st_size,fileTable[i].number);
		printf("\n");

	}

}

/*---------创建文件--------*/
void create(char*name) {
	int i;
	if ((i = open(name, O_CREAT, 00700)) == -1) {
		
	} else
		
		close(i);

}

/*---------删除文件--------*/
void deletefile(const char*filename) {
	unlink(filename);
	char q2[20] = "OS/node/";
	strcat(q2, Name);
	struct stat d;
	stat(q2, &d);
	d.st_size;
	used1 = (d.st_size) / 20l;
	FILE *fp;

	if ((fp = fopen(q2, "r")) == NULL) {
		printf("error1\n");
		exit(0);
	}
	fread(fileTable, sizeof(struct node), used1, fp);

	fclose(fp);
	int i, m = 0;
	for (i = 0; i < used1; i++) {
		if (!strcmp(fileTable[i].fileName, firstName)) {
			m = 1;
			for (; i < used1 - 1; i++)
			{
				strcpy(fileTable[i].fileName, fileTable[i + 1].fileName);
			     strcpy(fileTable[i].number, fileTable[i + 1].number);
			}
			     used1--;
			if ((fp = fopen(q2, "w")) == NULL) {
				printf("error1\n");
				exit(0);
			}
			fwrite(fileTable, sizeof(struct node), used1, fp);
			fclose(fp);
			break;

		}
	}
	if (m == 0) {
		printf("\nThe file you input doesn't exist,delete failed\n");
	}

	printf("The file has been deleted\n");
}

/*---------打开文件--------*/
int openfile(char*name) {
	int i;
	if ((i = open(name, O_RDWR | O_APPEND)) == -1) {
		printf("could'nt open the file %s", name);
		return 0;
	} else {

		return i;
	}
}
/*---------写文件--------*/
void writefile(char name[], char*buf) {
	int i;
	i = open(name, O_WRONLY | O_APPEND);

		write(i, buf, strlen(buf));

		close(i);


}

/*---------查看文件内容--------*/
void readfile(char name[]) {
	char buf;
	int i;
	FILE *fp;
	if ((fp = fopen(name, "r")) == NULL)

	{
		printf("cannot open this file\n");

	}
	else
	{
	buf = fgetc(fp);
	  printf("The file content is as follows:");
	while (buf != EOF)

	{
		putchar(buf);
		buf = fgetc(fp);
	}

	printf("\n");
	}

}
void closefile(int fd) {
	close(fd);
}
/*---------创建用户--------*/
void userCreate() {
	long used;
	struct stat d;
	stat("OS/user.txt", &d);
	d.st_size;
	used = (d.st_size) / 30l;
	char a;
	a = getchar();
	char c;
	char userName[10];
	int i;
	if (used < MaxUser) {
		printf("请输入用户名:");
		for (i = 0; (c = getchar()) != '\n'; i++) {
			if (c == 13)
				break;
			else
				userName[i] = c;

		}
		userName[i] = '\0';
		for (i = 0; i < used; i++) {
			if (!strcmp(userTable[i].userName, userName)) {

				printf("The user name already exists, failed to create\n");
				return;
			}
		}
		strcpy(userTable[used].userName, userName);

		printf("Please enter the password:");
		for (i = 0; (c = getchar()) != '\n'; i++) {
			if (c == 13)
				break;
			else
				userTable[used].password[i] = c;
		}
		userTable[userID].password[i] = '\0';

		printf("Create successfully\n");

		char q[20] = "OS/";
		char q2[20] = "OS/node/";
		strcat(q, userName);
		strcat(q2, userName);
		createDirectory(q);
		create(q2);
		used++;
		FILE *fp;
		if ((fp = fopen("OS/user.txt", "w")) == NULL)

		{
			printf("cannot open this file\n");

			exit(0);

		}
		fwrite(userTable, sizeof(struct master_file_directory), used, fp);
		fclose(fp);

	} else {
		printf("Failed to create a user, the user has reached the upper limit\n");

	}
	fflush(stdin);

}
/*---------用户登录--------*/
int login() {
	long used;
	struct stat d;
	stat("OS/user.txt", &d);
	d.st_size;
	used = (d.st_size) / 30l;
	char name[15], psw[15];
	char c;
	int i, times;
	char a;
	a = getchar();
	printf("Please enter the user name:");

	for (i = 0; (c = getchar()) != '\n'; i++) {
		if (c == 13)
			break;
		else
			name[i] = c;

	}
	name[i] = '\0';
	for (i = 0; i < used; i++) {
		if (!strcmp(userTable[i].userName, name)) {
			strcpy(Name, userTable[i].userName);

			break;
		}
	}
	if (i == used) {
		printf("The user name you entered doesn't exist\n");

		return -1;
	}
	for (times = 0; times < 3; times++) {
		memset(psw, '\0', sizeof(psw));
		printf("Please enter the password:");
		for (i = 0; (c = getchar()) != '\n'; i++) {
			if (c == 13)
				break;
			else
				psw[i] = c;

		}

		for (i = 0; i < used; i++) {
			if (!strcmp(psw, userTable[i].password)) {
				printf("Login successful\n");

				break;
			}
		}
		if (i == used) {
			printf("You enter the wrong password, you have  % d chance\n", 2 - times);
			if (times == 2)
				exit(0);
		} else
			break;
	}

	return i;
}

/*---------创建文件--------*/
void createfile(char name[], char kind[]) {
	FILE *fp;

	if ((fp = fopen(name, "w+")) == NULL)

	{
		printf("The file already exists\n");

	} else {
		char q2[20] = "OS/node/";
		strcat(q2, Name);
		struct stat d;
		stat(q2, &d);
		d.st_size;
		used1 = (d.st_size) / 20l;
		FILE *fp;

		if ((fp = fopen(q2, "r")) == NULL) {
			printf("error1\n");
			exit(0);
		}
		fread(fileTable, sizeof(struct node), used1, fp);

		fclose(fp);
		strcpy(fileTable[used1].fileName, firstName);
		char j[5];
		sprintf(j, "%ld\n", used1);

		strcpy(fileTable[used1].number, j);
		used1++;

		if ((fp = fopen(q2, "w")) == NULL) {
			printf("error2\n");
			exit(0);

		}
		fwrite(fileTable, sizeof(struct node), used1, fp);
		fclose(fp);

		if (!strcmp(kind, "r")) {
			chmod(name, S_IREAD);
		}
		if (!strcmp(kind, "w")) {
			chmod(name, S_IWRITE);
		}
		if (!strcmp(kind, "nrw")) {
			chmod(name, S_IFREG);
		}
		printf("Create %s ssuccessfully\n", firstName);
		
	}
}

/*---------文件重命名--------*/

void reFileName(char*old, char*new) {

	char e[30] = "OS/";
	strcat(e, Name);
	strcat(e, "/");
	char e1[30];
	strcpy(e1, e);
	strcat(e1, old);
	strcat(e, new);
	rename(e1, e);

	char q2[20] = "OS/node/";
			strcat(q2, Name);
			struct stat d;
			stat(q2, &d);
			d.st_size;
			used1 = (d.st_size) / 20l;
			FILE *fp;

			if ((fp = fopen(q2, "r")) == NULL) {
				printf("error1\n");
				exit(0);
			}
			fread(fileTable, sizeof(struct node), used1, fp);

			fclose(fp);
			int i;
		for(i=0;i<used1;i++){
			if(!strcmp(fileTable[i].fileName,old))
			{

				strcpy(fileTable[i].fileName,new);

				printf("Rename successfully\n");
				if ((fp = fopen(q2, "w")) == NULL) {
									printf("Error1\n");
									exit(0);
								}
			fwrite(fileTable, sizeof(struct node), used1, fp);

				fclose(fp);
				break;
			}

		}
		if(i==used1)
		{
			printf("Input error, there is no this file\n");
		}



}
/*---------修改文件权限--------*/
void chmodfile(char name[], char kind[]) {

	int r = 0;

	if (!strcmp(kind, "r")) {
		if (!chmod(name, S_IREAD)) {
			printf("File permissions has been amended to read only\n");
			r = 1;
		} else
			r = 2;
	}
	if (!strcmp(kind, "w")) {
		if (!chmod(name, S_IWRITE)) {
			printf("File permissions has been amended to write only\n");
			r = 1;
		} else
			r = 2;
	}
	if (!strcmp(kind, "nrw")) {
		if (chmod(name, S_IFREG)) {
			printf("File permissions has been amended to no-read and no-write\n");
			r = 1;
		} else
			r = 2;
	}
	if (!strcmp(kind, "rw") || !strcmp(kind, "wr")) {
		if (!chmod(name, S_IREAD | S_IWRITE)) {
			printf("File permissions has been amended to read and write\n");
			r = 1;
		} else
			r = 2;
	}
	if (r == 0) {
		printf("Access directive input errors, modify permissions to fail\n");
	}
	if (r == 2) {
		printf("Input error, there is no this file\n");
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值