X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f X-Trace-PostClient-IP: 68.147.131.211 From: Brian Inglis Newsgroups: comp.os.msdos.djgpp Subject: Re: Problem using system( ) Organization: Systematic Software Message-ID: References: <414c1225$0$177$cc7c7865 AT news DOT luth DOT se> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 103 Date: Sat, 18 Sep 2004 15:50:52 GMT NNTP-Posting-Host: 24.71.223.147 X-Complaints-To: abuse AT shaw DOT ca X-Trace: pd7tw2no 1095522652 24.71.223.147 (Sat, 18 Sep 2004 09:50:52 MDT) NNTP-Posting-Date: Sat, 18 Sep 2004 09:50:52 MDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On 18 Sep 2004 10:47:01 GMT in comp.os.msdos.djgpp, Martin Str|mberg wrote: >anonymous wrote: >> I read the documentation for the system() function and I could understand >> that if the COMMAND.COM is used then it will always return 0, even if >> the command did not execute. > >> I want to know whether the command passed to the system() >> was successfully run or not. > >You can't if COMMAND.COM is invoked as you seem to understand from >above. You might be helped by "info libc a spawn". > >> I am not able to understand >> the following: >> >> "The behavior of `system' can be customized at run time by defining the >> variable `DJSYSFLAGS' in the environment. The value of that variable >> should be the numerical value of `__system_flags' that you'd like to >> set; it will override the value of `__system_flags' specified when the >> program was compiled." > >> Does it mean that the `DJSYSFLAGS' should be defined something like > >> #define DJSYSFLAGS ( __system_emulate_command | __system_use_shell \ >> |__system_call_cmdproc) > >No. It means you should set the environment variable "DJSYSFLAGS" to >the numerical value of the flags you want enabled (0x20, 0x4, 0x2) >ored together. I. e. you want to do "set DJSYSFLAGS 0x26". (I've never >used this so I might be wrong, but this is what I conclude from the >manual. ) Or: #include __system_flags |= __system_emulate_command | __system_use_shell | __system_call_cmdproc; But I don't think this is what you want; if you have a Unix shell (e.g. SHELL=bash), you should use: __system_flags |= __system_use_shell | __system_call_cmdproc; /* use shell */ __system_flags &= ~__system_emulate_command; /* clear emulation */ but if you want system() to avoid the shell, you should use: __system_flags |= __system_emulate_command; /* emulation */ __system_flags &= ~(__system_use_shell |__system_call_cmdproc); /* don't use shell */ >> Also the return value is in the lower 8 bits, >> so I'm checking it like > >> struct retVal >> { >> char r[2]; >> }*ret; > >> ... > >> int temp = system(cmd); >> ret = (struct retVal *) &temp; >> printf ("\n %d %d", ret->r[0], ret->r[1]); > >IMHO, that casting is pretty bad C. Try: > >int child_exit, child_signal; >int temp = system(cmd); if (temp == -1) { perror( cmd ); exit( # ); } >child_exit = temp&0xff; >child_signal = (temp&0xff000) >> 8; ITYM: child_signal = (temp & 0x3ff00) >> 8; /* bits 8-17 */ >printf ("\n %d %d", child_exit, child_signal); > >instead. The above is UNTESTED. > >> But, the vales are always zero even for system() called with >> an invalid command. > >You might be better servered by spawn(). Note that unzip32 (unzip from info-zip) does not document any return codes, so you might have to download and check out the source to see if any meaning can be inferred from any of the return codes. -- Thanks. Take care, Brian Inglis Calgary, Alberta, Canada Brian DOT Inglis AT CSi DOT com (Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]ca) fake address use address above to reply