私はCコードを持っています..私はUNIX用の次のコードを持っています:
l_iRet = system( "/bin/cp -p g_acOutputLogName g_acOutPutFilePath");
生成されたバイナリを実行すると、次のエラーが表示されます。
cp: cannot access g_acOutputLogName
誰でも私を助けることができますか?
一般に、システム関数よりも関数の exec ファミリを優先する必要があります。システム関数はコマンドをシェルに渡します。つまり、コマンド インジェクションと偶発的なパラメーター展開について心配する必要があります。exec を使用してサブプロセスを呼び出す方法は次のとおりです。
pid_t child;
child = fork();
if (child == -1) {
perror("Could not fork");
exit(EXIT_FAILURE);
} else if (child == 0) {
execlp("/bin/cp", g_acOutputLogName, g_acOutPutFilePath, NULL);
perror("Could not exec");
exit(EXIT_FAILURE);
} else {
int childstatus;
if (waitpid(child, &childstatus, 0) == -1) {
perror("Wait failed");
}
if (!(WIFEXITED(childstatus) && WEXITSTATUS(childstatus) == EXIT_SUCCESS)) {
printf("Copy failed\n");
}
}
おそらくg_acOutputLogNameandg_acOutPutFilePathは、実際のパスではなく、プログラム内のchar[](または) 変数です。char*
変数名ではなく、そこに格納されている値を使用する必要があります。次に例を示します。
char command[512];
snprintf( command, sizeof command, "/bin/cp -p %s %s",
g_acOutputLogName, g_acOutPutFilePath );
l_iRet = system( command );