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

📄 change.c

📁 Linux下实现shell的部分功能
💻 C
字号:
#include "ysh.h"
#include "extern.h"

void ctrl_z()
{
	NODE *p;
	int i=1;
	if (pid1==0)
		goto out; /*前台没有工作*/
	/*改变链表中的相应节点的状态*/
	if (head!=NULL) { /*链表不为空*/
		p=head;
		/*遍历链表,看该工作是否已经在链表中*/
		while (p->pid!=pid1&&p->link!=NULL)
			p=p->link;
		if (p->pid==pid1) /*工作已在链表中*/
		{
			strcpy(p->state,"stopped");
		}
		else
		{ /*工作不在链表中*/
			add_node(input,pid1); /*增加新节点*/
			strcpy(end->state,"stopped"); /*设置节点状态*/
		}
	}
	else
	{ /*连标为空*/
		add_node(input,pid1); /*增加新节点*/
		strcpy(end->state,"stopped"); /*设置节点状态*/
	}
	sig_z=1; /*sig_z标志置一,del_node()函数中不用删除节点*/
	kill(pid1,SIGSTOP); /*发送SIGSTOP信号给正在前台运行的工作,将其停止*/
	for (p=head;p->pid!=pid1;p=p->link) /*显示提示信息*/
		i++;
	printf("[%d]\t%s\t%s\n",i,end->state,end->cmd);
	pid1=0;
	out: return;
}

void bg_cmd(int job_num)
{
	NODE *p;
	int i=0;
	p=head;
	/*根据作业号,遍历链表,找到指定节点*/
	for (i=1;i<job_num;i++)
		p=p->link;
	kill(p->pid,SIGCONT); /*向对应工作发送SIGCONT信号,使其在后台运行*/
	strcpy(p->state,"running"); /*修改对应节点状态*/
}

void fg_cmd(int job_num)
{
	NODE *p;
	int i=0;
	p=head;
	/*根据作业号,遍历链表,找到指定节点*/
	for (i=1;i<job_num;i++)
		p=p->link;
	strcpy(p->state,"running"); /*修改对应节点状态*/
	strcpy(input,p->cmd); /*将命令名复制到input中,为下一次按下ctrl-z作准备*/
	pid1=p->pid; /*获取该节点对应工作的进程号*/
	signal(SIGTSTP,ctrl_z); /*设置signal()信号,为下一次按下ctrl-z作准备*/
	kill(p->pid,SIGCONT); /*向对应工作发送SIGCONT信号,使其运行*/
	waitpid(p->pid,NULL,0); /*父进程等待前台进程的运行*/
}

void up_history()
{
	if (envhis.start==envhis.end) { /*循环数组为空*/
		printf("No history command!\nysh@/root/ysh> ");
	} else if (envhis.start<envhis.end) { /*start<end时*/
		if (history_number > envhis.start)
			printf("%s\nysh@/root/ysh> ",envhis.his_cmd[history_number]);
		else
			printf("That's all!\nysh@/root/ysh> ");
	}else { /*start>end时*/
		if (history_number >= 0)
			printf("%s\nysh@/root/ysh> ",envhis.his_cmd[history_number]);
		else {
			if ((history_number + HISNUM) > envhis.start)
				printf("%s\nysh@/root/ysh> ",envhis.his_cmd[history_number + HISNUM]);
			else
				printf("That's all!\nysh@/root/ysh> ");
		}
	}
	history_number--;
}

⌨️ 快捷键说明

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