C语言DOS函数

来源:百度知道 编辑:UC知道 时间:2024/05/24 07:37:07
我想请问下,就是C里DOS函数system是不是每次执行,返回的值都是-1,要是执行的时候命令没有成功,是不是还是-1.

system 函数调用送返 整数。
#include <stdlib.h>
int system(const char *command);

IEEE 国际标准 1003.1, 2004 Edition 规定见
http://www.opengroup.org/onlinepubs/000095399/functions/system.html

但不同的编译器未必按国际标准设计。

MS VC++ 编译器, 命令成功 送返 0, 失败 送返 1。

int system(const char * cmdstring)
{
pid_t pid;
int status;

if(cmdstring == NULL){

return (1);
}

if((pid = fork())<0){

status = -1;
}
else if(pid == 0){
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
-exit(127);
}
else{
while(waitpid(pid, &status, 0) < 0){
if(errno != EINTER){
status = -1;