⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 more2.c

📁 在linux环境下的ls功能的实现
💻 C
字号:
#include<stdio.h>
#include<termios.h>
#include<fcntl.h>
#include<string.h>
#include<signal.h>

#define PAGELEN 24
#define LINELEN 512
#define ASK "Do you want anohter try"
#define SLEEPTIME 4
#define BEEP putchar('\a')


char *is_codedfile(char *files);
char *search(char *searfile);
void do_more(FILE *fp);
int see_more();
char *get_response(char *question);
void set_cr_noecho_mode();
void set_nodelay_mode();
void tty_mode(int how);
void ctrl_c_handler(int signum);

char line3[LINELEN];


char *get_response(char *question)
{
	char *input;
	while(1)
	{
		sleep(SLEEPTIME);		
		scanf("%s",input);
		return input;
		BEEP;
		
	}
}


char* is_codedfile(char *files)
{
	char *repwd;
	if((repwd=search(files))==NULL)
	{
		return NULL;
	}
	else
	{
		return repwd;
	}
	
}


char *search(char *searfile)
{
	FILE *fp;
	if((fp=fopen("filepwd","r"))!=NULL)
	{
		char line[LINELEN];
		while(fgets(line,LINELEN,fp))
		{
		
			int i=0;
			char line2[LINELEN];
			while(line[i]!=' ')
				line2[i]=line[i++];
			line2[i]='\0';
			if(strcmp(searfile,line2)==0)
			{
			        int h=0;
			        while(line[++i]!='\n')
			             line3[h++]=line[i];
			        line3[h]='\0';
			        fclose(fp);			
				return(line3);				
			}
		}
		fclose(fp);
	}
	else
		exit(1);
	return NULL;

}

int see_more()
{
        char c;
        printf("\033[7m more? \033[m");
	while((scanf("%c",&c))!=EOF)
	{
		if(c=='q')
			return 0;
		if(c==' ')
			return PAGELEN;
		if(c=='\n')
			return 1;
		return 0;   		
	}
	return 0;
}

void do_more(FILE *fp)
{
	char line[LINELEN];
	char c;
	int num_of_lines=0;
	int reply;
	scanf("%c",&c);
	while(fgets(line,LINELEN,fp))
	{
		if(num_of_lines==PAGELEN)
		{
		        reply=see_more();
		        if(reply==0)
		        break;
			num_of_lines-=reply;
		}
		num_of_lines++;	
		printf("%s",line);	
	}
	
}



void set_cr_noecho_mode()
{
	struct termios ttystate;
	tcgetattr(0,&ttystate);
	ttystate.c_lflag &=~ICANON;
	ttystate.c_lflag &=~ECHO;
	ttystate.c_cc[VMIN]=1;
	tcsetattr(0,TCSANOW,&ttystate);

}

void set_nodelay_mode()
{
	int termflags;
	termflags=fcntl(0,F_GETFL);
	termflags |=O_NDELAY;
	fcntl(0,F_SETFL,termflags);
}

void tty_mode(int how)
{
	static struct termios original_mode;
	static int            original_flags;
	static int            stored=0;
	if(how==0)
	{
		tcgetattr(0,&original_mode);
		original_flags=fcntl(0,F_GETFL);
		stored=1;
		
	}
	else if(stored)
	{
		tcsetattr(0,TCSANOW,&original_mode);
		fcntl(0,F_SETFL,original_flags);
	}

}

void ctrl_c_handler(int signum)
{
	tty_mode(1);
	exit(1);
}


int main(int ac,char *av[])
{
	FILE *fp;
	char* filepwd;
	int left_try=3;
	if(ac==1)
	
		printf("Error,more input.\n");
	else
		while(--ac)
		{
			char k[128];
			strcpy(k,av[1]);
			if((filepwd=is_codedfile(k))!=NULL)
			{
				char pwd[128];
				char* response;	
				char temp[128];			
				printf("The %s file is coded file, needs password:\n",k);
				strcpy(pwd,filepwd);
				tty_mode(0);
				set_cr_noecho_mode();
				set_nodelay_mode();
				signal(SIGINT,ctrl_c_handler);
				signal(SIGQUIT,SIG_IGN);
				while(left_try>=0)
				{
					response=get_response(ASK); 					
					strcpy(temp,response);			
					if(strcmp(pwd,temp)==0)
					{
					
				                if((fp=fopen(k,"r"))!=NULL)
				                {
				                  tty_mode(1);			
				                  do_more(fp);
				                  fclose(fp);
				
				                }
				                else
				                    exit(1);
				                return 0;						
					}
					else
					{
						left_try--;	
						printf("Wrong password,try again:\n");
						fflush(stdout);								
					}
					
				}
				
			}
			else
			{
			  if((fp=fopen(k,"r"))!=NULL)
			  {
			     do_more(fp);
			     fclose(fp);
			  }
			  else
			  {
			        printf("no such file\n");
			  	exit(1);
			  }
			}
		}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -