/******************************************************************************
*   "Irit" - the 3d (not only polygonal) solid modeller.		      *
*									      *
* Written by:  Gershon Elber				 Ver 0.2, Mar. 1990   *
*******************************************************************************
* Procedures to handle the dos interface - print/change the current dir. etc. *
******************************************************************************/

#include <sys/types.h>
#ifdef DJGCC
#include <dir.h>
#endif /* DJGCC */
#ifdef __WINNT__
#include <direct.h>
#endif /* __WINNT__ */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "program.h"
#include "dosintr.h"
#include "ctrl-brk.h"
#include "windows.h"
  
#ifdef AMIGA
#undef DOUBLE
#ifdef __SASC
#include <proto/dos.h>
#endif /* __SASC */
#ifdef __GNUC__
#include <dos/dos.h>
#include <inline/dos.h>
#endif
#endif /* AMIGA */

/*****************************************************************************
*   Routine to change current directory					     *
*****************************************************************************/
void DosChangeDir(char *s)
{
#ifdef DJGCC
    char cwd[LINE_LEN], *sptr;

    getcwd(cwd, LINE_LEN-1);			   /* Save current position. */

    if (s[1] == ':')
        sptr = &s[2];			    /* Sptr points on the path only. */
    else
	sptr = s;

    if (strlen(sptr) != 0 && chdir(s))
	WndwInputWindowPutStr("CHDIR: No Such Dir!");

    if (getcwd(s, LINE_LEN-1) == NULL) {      /* Test if directory is valid! */
	WndwInputWindowPutStr("CHDIR: hardware (!?) error - ignored");
	/* Restore old working directory: */
	if (strlen(&cwd[2]) != 0)
	    chdir(cwd);			      /* If directory is not root... */
    }
#else
    chdir(s);
#endif /* DJGCC */
}

/******************************************************************************
* Procedure to return current time from last time, time was reset.	      *
* Time is reset if the given parameter is non zero. Time is returned in       *
* seconds.								      *
******************************************************************************/
double DosGetTime(double ResetTime)
{
    return IritCPUTime(!APX_EQ(ResetTime, 0.0));
}

/******************************************************************************
* Procedure to execute a command.					      *
******************************************************************************/
void DosSystem(char *Str)
{
    char Buffer[LINE_LEN_LONG];
#   ifdef __UNIX__
        int pid = getpid();
#   else
        int pid = (int) IritRandom(0.0, 10000.0);
#   endif /* __UNIX__ */

    sprintf(Buffer, "%s > irit_tmp.%d", Str, pid);
#ifdef AMIGA
    if (SystemTagList(Buffer, NULL))
#else
    if (system(Buffer))
#endif
	WndwInputWindowPutStr("Undefined error in attempt to run command");
    else {
	FILE *f;

	sprintf(Buffer, "irit_tmp.%d", pid);
	if ((f = fopen(Buffer, "r")) != NULL) {
	    while (fgets(Buffer, LINE_LEN_LONG - 1, f))
		WndwInputWindowPutStr(Buffer);
	    fclose(f);

	    sprintf(Buffer, "irit_tmp.%d", pid);
	    unlink(Buffer);
	}
	else
	    WndwInputWindowPutStr("Failed to read redirected output");
    }
}
