[摳頂] /cmds/wiz/_crontab.c

作者: laechan (揮淚斬馬雲)   2018-01-08 21:50:46
今天忙死了。總之,新的工作室 day2,今天晚上看底下的東西
能不能 coding 完。
// _crontab.c
// [email protected] add in 2018/01/08
// * * * * * 要做啥工作 格式大概長這樣
// 目前已知有 a-b 及 */n 這兩種
// n 若給錯導致不能被整除的情況發生時就不會 match
inherit DAEMON;
mixed crontabs;
void create()
{
::create();
seteuid(getuid(this_object()));
crontabs=({});
if(file_exists("/open/cmds/crontab.o"))
restore_object("/open/cmds/crontab");
}
int save_room()
{
save_object("/open/cmds/crontab");
return 1;
}
// 到以上為止的寫法都很固定
// 暫時不開放 a,b,c * * * * 這種給法, 事實上它還能混合
// 例如 a,b,*/10 * * * * 這樣會沒完沒了, 逗號先不開放
int illegel_check(string tmp,string kind,int min,int max)
{
int t,t1,t2;
t=atoi(tmp);
if(tmp=="*") return 0;
else if(sscanf(min,"%d-%d",t1,t2)==2)
{
if(t1<min || t1>max || t2<min || t2>max || t1>=t2)
{
write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n");
return 1;
}
return 0;
}
// 週不能有 / 號
else if(kind!="週" && sscanf(tmp,"*/%d",t1)==1)
{
// 要能整除
if(t1<min || t1>max || (t1!=0 && t%t1!=0))
{
write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n");
return 1;
}
return 0;
}
else if(sscanf(tmp,"%d",t)==1)
{
if(t<min || t>max)
{
write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n");
return 1;
}
return 0;
}
write("crontab -add: 你所給的"+kind+" '"+tmp+"' 格式不符喔.\n");
return 1;
}
int cmd_crontab(string str,object me)
{
string what,files,funs,t1,t2,t3,t4,t5;
int n,n1,n2;
object ob;
mixed tmps=({});
if(!str || str=="")
return notify_fail(@LONG
例行排程(crontab)指令說明:
======================================================
crontab -list 例行排程列表
crontab -add xxx 將 xxx 加進排程中
crontab -del xxx 刪掉 xxx 這個排程
每一個排程都是一行,前五個分別代表 分 時 日 月 週,
每一個都可以是 數字、a-b、或 a/b 或 * 的形式。再之後
所接的東西固定為 什麼物件->呼叫什麼函數
例子
* * * * * /d/event/control->newyear 每分鐘的呼叫
0 * * * * /d/event/control->newyear 每小時整點呼叫
*/10 * * * * /d/event/control->newyear 每10分鐘的呼叫
======================================================
LONG
);
else if(str=="-l" || str=="-list")
return notify_fail(implode(crontabs,"\n")+"\n");
else if(sscanf(str,"-add %s",what)==1)
{
if(sscanf(what,"%s %s %s %s %s %s->%s",
t1,t2,t3,t4,t5,files,funs)!=7)
return notify_fail("格式: 分 時 日 月 週 物件檔->函數名\n"+
" 前五個可給 數字,a-b,a/b 及 * 等.\n");
// 不能有完全一模一樣的
if(member_array(what,crontabs)!=-1)
return notify_fail("crontab -add: 這條例行排程已經有了喔!\n");
if(illegel_check(t1,"分",0,59)>0) return notify_fail("");
if(illegel_check(t2,"時",0,23)>0) return notify_fail("");
if(illegel_check(t3,"日",1,31)>0) return notify_fail("");
if(illegel_check(t4,"月",1,12)>0) return notify_fail("");
// 週不能用 / 號
if(illegel_check(t5,"週",1,7)>0) return notify_fail("");
// 通過上面的判斷就可以加進去
crontabs+=({what});
save_room();
return notify_fail("crontab -add: 這個例行排程已加入.\n");
}
else if(sscanf(str,"-del %s",what)==1 ||
sscanf(str,"-delete %s",what)==1)
{
if(member_array(what,crontabs)==-1)
return notify_fail("crontab -del: 沒有這個例行排程喔.\n");
crontabs-=({what});
save_room();
return notify_fail("crontab -del: 這個例行排程已刪除.\n");
}
return notify_fail("沒有 crontab "+str+" 這個語法喔.\n");
}
大概是上面的感覺吧,明天再放到 sanc 跑看看。
Laechan

Links booklink

Contact Us: admin [ a t ] ucptt.com