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

📄 mycrontab.c

📁 Cron的实现 UNIX 下设计编写中小型程序的能力
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#include <string.h>int main(int argc,char* argv[]){	int fd1,fd2,length,pid;	char textfile[200],vi[50]="vi ";	if(argc!=2&&argc!=3)    	{			printf("usage:\n%s <filename>\n%s <filename> {-l|-r|-e}\n",argv[0],argv[0]);        	exit(1);    	}	else if(argc==2)	{		printf("Using the new input task file as crontab file\n");		fd1=open(argv[1],O_RDONLY,S_IROTH|S_IWOTH);        //command of mycrontab file 
		system("touch j03031.cron");		fd2=open(" j03031.cron",O_WRONLY,S_IROTH|S_IWOTH);		length=read(fd1,textfile,200);		if(write(fd2,textfile,length)<0)		{			printf("can't %s %s\n",argv[0],argv[1]);			exit(1);		}		exit(0);	}	else if(argc==3)		//there are 3 parameters	{		if(strcmp(argv[2],"-l")==0)	//display the current crontab file		{			printf("The contant of the crontab file is as following:\n");			system("cat  j03031.cron");			exit(0);		}		else if(strcmp(argv[2],"-r")==0)//remove the current crontab file		{			system("rm  j03031.cron");			printf("The crontab file has been removed\n");			exit(0);		}		else if(strcmp(argv[2],"-e")==0)		{			if((pid=fork())<0)			{				printf("Fork vi process fail\n");				exit(1);			}			else if(pid>0)		//create a child process to execute the editor			{				strcat(vi,argv[1]);				execl("/bin/sh","sh","-c",vi,(char *)0);			}			waitpid(pid,NULL,0);			fd1 = open(argv[1],O_RDONLY,S_IROTH|S_IWOTH);			system("rm  j03031.cron");			system("touch  j03031.cron");			fd2 = open(" j03031.cron",O_WRONLY,S_IROTH|S_IWOTH);			length=read(fd1,textfile,500);			if(write(fd2,textfile,length)<0)			{				fprintf(stderr,"can't %s %s\n",argv[0],argv[1]);				exit(1);			}
 
			exit(0);		}		else		//parameter error		{			printf("usage:\n%s <filename>\n%s <filename> {-l|-r|-e}\n",argv[0],argv[0]);			exit(1);		}	}}

⌨️ 快捷键说明

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