0
我試圖將一些C/C++代碼轉換爲UNIX shell。我有一些轉換,但有時候,我發現C/C++中的代碼很奇怪,也許是因爲我在上下文中不理解它。更重要的是,客戶端沒有給我輸入/輸出文件或文檔,所以在UNIX中進行編碼是非常困難的。從C到Shell:關於「系統」指令
我得到這個:
int exec_ftpcmd(char *ftpcmd, char *IP_Server, char* icmd)
{
int cmd_st = 0;
FILE *cmd_file;
char *cmd_fname = "ftp_exe.cmd",
*out_fname = "ftp_exe.out",
cmd[100];
printf("ftp_serv: exec_ftpcmd [%s][%s] \n",icmd,ftpcmd);
cmd_file = fopen(cmd_fname,"w");
#ifndef UNIX
fprintf(cmd_file,"user test test1\n"); //just for dos
#endif
if (strlen(icmd)>0)
fprintf(cmd_file,"cd %s\n",icmd);
fprintf(cmd_file,"%s\n",ftpcmd);
fprintf(cmd_file,"quit ");
fclose(cmd_file);
memset(cmd,0,sizeof(cmd));
#ifdef UNIX
sprintf(cmd,"more %s | ftp -v %s >> %s",cmd_fname,IP_Server,out_fname); //unix
#else
sprintf(cmd,"ftp -n -s:%s %s >> %s",cmd_fname,IP_Server,out_fname); //dos
#endif
cmd_st = system(cmd);
return cmd_st;
}
我想我從有很多的命令文件中讀取,並以「系統」測試,看看它們是否OK?我可以在UNIX中使用exec嗎?
我讀here即系統有一些危險。在這種情況下使用它是安全的?
那麼,哪一個很好等價於系統?我應該使用它嗎?有沒有更優雅的方式?
EDIT 我發現的ftp_exe.cmd迷你例如:
[email protected]/narcredit/vcfid>cat ftp_exe.cmd
dir /apps/VB/A01862.txt ftp_dir.cmd
在此示例中,cd將由ftp客戶端執行。一個更適合這個例子的解決方案是:`(echo「cd $ icmd」; echo $ ftpcmd; echo「quit」)| ftp -v $ ipaddress >> $ out_fname` – 2011-12-15 18:04:15