Date: Mon, 17 Nov 1997 20:20:28 +0100 (GMT) From: "X DOT PONS AT UAB DOT ES" Subject: system calls generate Segm.Viol. on NT To: djgpp AT delorie DOT com Message-id: <01IQ4NN88GG200LLZ6@cc.uab.es> Organization: Universitat Autonoma de Barcelona MIME-version: 1.0 Content-type: TEXT/PLAIN; CHARSET=ISO-8859-1 Precedence: bulk Dear programmers, This code perfectly runs on DOS, Windows 3.1x DOS-box and Windows 95 DOS-box, but it crashes on a NT 4.0 (NTFS). It simply runs a "dir c:\\" command froma a system() call and stores the output to a file in the c: root. === START OF CODE === #include #include int main(void) { FILE *pf; int c; if (system("dir c:\\ > c:\\somefile.txt")) { printf("\n\asystem() error"); return 1; } if ((pf=fopen("c:\\somefile.txt", "rt"))==NULL) { puts ("\n\aCannot open file..."); return 1; } while ((c=getc(pf))!=EOF) putchar(c); fclose(pf); return 0; } === END OF CODE === If the command line is "dir c:\\" instead of "dir c:\\ > c:\\somefile.txt" it also crashes, so the redirector '>' does not seem the responsible. On the other hand, putting this line on a BAT and running it from a system also crashes. More details: 1/ Sometimes it does not crash the first time it is executed, but if you simply rerun the program, it crashes every time until the DOS box is closed. 2/ When this code is compiled in BC 3.x (16-bit) or in Borland C++ 5.02 (32-bit WIN32 application), it runs perfectly and does not produce any change in the free memory. 3/ The symify help for the output is as follows. Exiting due to signal SIGSEGV General Protection Fault at eip=00003b94 eax=fff40000 ebx=00000000 ecx=000001f9 edx=fff40000 esi=00004000 edi=0000000b ebp=00053070 esp=00053060 cs=01cf ds=01d7 es=01d7 fs=019f gs=01df ss=01d7 Call frame traceback EIPs: 0x00003b94 _malloc+308 0x00003b11 _malloc+177 0x000079b7 __flsbuf+159 0x00002a4d _putchar+25 0x000015fc _main+108 0x00001b47 ___crt1_startup+115 4/ The "mem /c" command reports that 16 bytes have been loosed after a DJ program runs on our NT console (but NOT on other O.S.), but it happens on any program, even the "Hello world". ** Normal situation: ******** Memoria convencional : Nombre Tamano en decimal Tamano en Hex ------------- --------------------- ------------- MSDOS 11344 ( 11.1K) 2C50 KBD 3280 ( 3.2K) CD0 HIMEM 1248 ( 1.2K) 4E0 COMMAND 3440 ( 3.4K) D70 KB16 6096 ( 6.0K) 17D0 FREE 112 ( 0.1K) 70 FREE 544 ( 0.5K) 220 FREE 629088 (614.3K) 99960 Total libre : 629744 (615.0K) Memoria superior : Nombre Tamano en decimal Tamano en Hex ------------- --------------------- ------------- SYSTEM 163824 (160.0K) 27FF0 MOUSE 12528 ( 12.2K) 30F0 MSCDEXNT 464 ( 0.5K) 1D0 REDIR 2672 ( 2.6K) A70 DOSX 38864 ( 38.0K) 97D0 >> ! FREE 544 ( 0.5K) 220 FREE 108640 (106.1K) 1A860 Total libre : 109184 (106.6K) Bytes disponibles para programas (Convencional+Sup.): 738928 (721.6K) Tamano del mayor programa: 629088 (614.3K) Mayor bloque de memoria superior disponible: 108640 (106.1K) 1048576 bytes totales de memoria extendida contigua 0 bytes disponibles de memoria extendida contigua 931840 bytes de memoria XMS disponible MS-DOS est  residente en HMA ** After "Hello world" execution: ******** Memoria convencional : Nombre Tamano en decimal Tamano en Hex ------------- --------------------- ------------- MSDOS 11344 ( 11.1K) 2C50 KBD 3280 ( 3.2K) CD0 HIMEM 1248 ( 1.2K) 4E0 COMMAND 3440 ( 3.4K) D70 KB16 6096 ( 6.0K) 17D0 FREE 112 ( 0.1K) 70 FREE 544 ( 0.5K) 220 FREE 629088 (614.3K) 99960 Total libre : 629744 (615.0K) Memoria superior : Nombre Tamano en decimal Tamano en Hex ------------- --------------------- ------------- SYSTEM 163824 (160.0K) 27FF0 MOUSE 12528 ( 12.2K) 30F0 MSCDEXNT 464 ( 0.5K) 1D0 REDIR 2672 ( 2.6K) A70 DOSX 38864 ( 38.0K) 97D0 >> ! FREE 48 ( 0.0K) 30 >> ! FREE 480 ( 0.5K) 1E0 FREE 108640 (106.1K) 1A860 Total libre : 109168 (106.6K) Bytes disponibles para programas (Convencional+Sup.): 738912 (721.6K) Tamano del mayor programa: 629088 (614.3K) Mayor bloque de memoria superior disponible: 108640 (106.1K) 1048576 bytes totales de memoria extendida contigua 0 bytes disponibles de memoria extendida contigua 931840 bytes de memoria XMS disponible MS-DOS est  residente en HMA Note that the first FREE block has been broken in two pieces but they lack 16 bytes. Sorry if this is not relevant, it is only a symptom thas is strange for us (running DJ applications on DOS, Windows 3 or 95 leaves the memory in the same "distribution"). Any idea on how to run other programs or internal commands from a DJGPP program? We have already tried: - system("command /C dir c:\\ > somefile.txt"); - to put the "dir c:\\ > somefile.txt" string on a file.BAT and then call system(file.BAT); - spawnl(P_WAIT) instead of system(). - system("start /W command /C dir c:\\ > somefile.txt"); without success. :-(((( Thanks for any help! Joan Maso and Xavier Pons