FORM  PTCHVERS   $   SAS Institute, Inc. Patch V6.50 INPF    O{  /_main.cOUTF    p  S/_iob.c PSEQ  S"I*#include <stdio.h>
#include <fcntl.h>
#include <ios1.h>
#include <string.h>
#include <stdlib.h>

#include <workbench/startup.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <exec/execbase.h>

#include "constructor.h"


extern char __stdiowin[];
extern char __stdiov37[];
extern struct WBStartup *_WBenchMsg;
extern struct ExecBase *SysBase;
extern FILE *__firstfile;
extern FILE *__stdinptr;
extern FILE *__stdoutptr;

/***
*
* The following array contains the control blocks used bysI
* I/O functions. Any reference to it pulls in the constructor
*
***/

struct __iobuf __iob[3] = {
    { &__iob[1] },
    { &__iob[2] },
    {   NULL   }
};

static struct UFB  ufbs[3];

STDIO_CONSTRUCTOR(stdio_init)
{
    char *window;
    struct FileHandle *handle;
    struct Process *process;
    int x;

    __firstfile = stdin;
    __stdinptr = stdin;
    __stdoutptr = stdout;
    
    
    ufbs[0].ufbnxt = &ufbs[1];
    ufbs[1].ufbnxt = &ufbs[2];
    ufbs[2].ufbnxt = NULL;
    __ufbs = &ufbs[0];
    ufbs[0].ufbfn = NULL;
    ufbs[1].ufbfn = NULL;
    ufbs[2].ufbfn = NULL;
    ufbs[0].ufbflg = UFB_RA | O_RAW | UFB_NC;
    ufbs[1].ufbflg = UFB_WA | O_RAW | UFB_NC;
    ufbs[2].ufbflg = UFB_WA | O_RAW | UFB_NC | UFB_CLO;

    if (Output() == NULL) 
    {s<iX/* running under workbench      */
        if (_WBenchMsg && _WBenchMsg->sm_ToolWindow)
siVufbs[0].ufbfh = Open(_WBenchMsg->sm_ToolWindow, MODE_NEWFILE);
        else
        {
iint len = 0;
sichar *winname = NULL;
sCi	char *p;
sDi
sBilen = strlen(__stdiowin);

SgIif (__stdiowin[len-1] == '/')
           {
               winname = "Output";
               if (_WBenchMsg) 
                  winname = _WBenchMsg->sm_ArgList->wa_Name; 
               
               len += strlen(winname);
           }

           if (SysBase->LibNode.lib_Version >= 36)
              len += strlen(__stdiov37);

           window = malloc(len);
           if (window == NULL) return 1; /* fail the autoinit */
           
           p = stpcpy(window,sli);
iif (winname)
S&isp = stpcpy(p, winname);

           if (SysBase->LibNode.lib_Version >= 36)
              p = stpcpy(p, __stdiov37)si9   
           ufbs[0].ufbfh = Open(window, MODE_NEWFILE)si   free(window)si,}
        
        if (ufbs[0].ufbfh == NULLsi-   ufbs[0].ufbfh = Open("NIL:", MODE_NEWFILE)spi"
        ufbs[0].ufbflg |= UFB_CLOsi3handle = (struct FileHandle *) (ufbs[0].ufbfh << 2)s>i(process = (struct Process *) FindTask(0)si0process->pr_ConsoleTask = (APTR) handle->fh_Typesi6if ((ufbs[1].ufbfh = Open("*", MODE_OLDFILE)) == NULL)si*ufbs[1].ufbfh = Open("NIL:", MODE_OLDFILE)r5ufbs[2].ufbfh = ufbs[1].ufbfh;
    } 
    else 
    {si/* running under CLIs i"*/
        ufbs[0].ufbfh = Input()sRiufbs[1].ufbfh = Output()si6if ((ufbs[2].ufbfh = Open("*", MODE_OLDFILE)) == NULL)S4I3 ufbs[2].ufbfh = Open("NIL:", MODE_OLDFILE);
    }


    __nufbs += 3;


    x = (__fmode) ? 0 : _IOXLAT;
    stdin->_file = 0;
    stdin->_flag = _IOREAD | x;
    stdout->_file = 1;
    stdout->_flag = _IOWRT | _IOLBF | x;
    stderr->_file = 2;
    stderr->_flag = _IOWRT | _IOLBF | x;
    return 0; 

}

   