home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / source / _iob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  3.3 KB  |  135 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <ios1.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #include <workbench/startup.h>
  8. #include <libraries/dos.h>
  9. #include <libraries/dosextens.h>
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <exec/execbase.h>
  13.  
  14. #include "constructor.h"
  15.  
  16.  
  17. extern char __stdiowin[];
  18. extern char __stdiov37[];
  19. extern struct WBStartup *_WBenchMsg;
  20. extern struct ExecBase *SysBase;
  21. extern FILE *__firstfile;
  22. extern FILE *__stdinptr;
  23. extern FILE *__stdoutptr;
  24.  
  25. /***
  26. *
  27. * The following array contains the control blocks used by the standard
  28. * I/O functions. Any reference to it pulls in the constructor
  29. *
  30. ***/
  31.  
  32. struct __iobuf __iob[3] = {
  33.     { &__iob[1] },
  34.     { &__iob[2] },
  35.     {   NULL   }
  36. };
  37.  
  38. static struct UFB  ufbs[3];
  39.  
  40. STDIO_CONSTRUCTOR(stdio_init)
  41. {
  42.     char *window;
  43.     struct FileHandle *handle;
  44.     struct Process *process;
  45.     int x;
  46.  
  47.     __firstfile = stdin;
  48.     __stdinptr = stdin;
  49.     __stdoutptr = stdout;
  50.     
  51.     
  52.     ufbs[0].ufbnxt = &ufbs[1];
  53.     ufbs[1].ufbnxt = &ufbs[2];
  54.     ufbs[2].ufbnxt = NULL;
  55.     __ufbs = &ufbs[0];
  56.     ufbs[0].ufbfn = NULL;
  57.     ufbs[1].ufbfn = NULL;
  58.     ufbs[2].ufbfn = NULL;
  59.     ufbs[0].ufbflg = UFB_RA | O_RAW | UFB_NC;
  60.     ufbs[1].ufbflg = UFB_WA | O_RAW | UFB_NC;
  61.     ufbs[2].ufbflg = UFB_WA | O_RAW | UFB_NC | UFB_CLO;
  62.  
  63.     if (Output() == NULL) 
  64.     {             /* running under workbench      */
  65.         if (_WBenchMsg && _WBenchMsg->sm_ToolWindow)
  66.             ufbs[0].ufbfh = Open(_WBenchMsg->sm_ToolWindow, MODE_NEWFILE);
  67.         else
  68.         {
  69.            int len = 0;
  70.            char *winname = NULL;
  71.            char *p;
  72.            
  73.            len = strlen(__stdiowin);
  74.  
  75.            if (__stdiowin[len-1] == '/')
  76.            {
  77.                winname = "Output";
  78.                if (_WBenchMsg) 
  79.                   winname = _WBenchMsg->sm_ArgList->wa_Name; 
  80.                
  81.                len += strlen(winname);
  82.            }
  83.  
  84.            if (SysBase->LibNode.lib_Version >= 36)
  85.               len += strlen(__stdiov37);
  86.  
  87.            window = malloc(len);
  88.            if (window == NULL) return 1; /* fail the autoinit */
  89.            
  90.            p = stpcpy(window, __stdiowin);
  91.            if (winname)
  92.               p = stpcpy(p, winname);
  93.  
  94.            if (SysBase->LibNode.lib_Version >= 36)
  95.               p = stpcpy(p, __stdiov37);
  96.            
  97.            ufbs[0].ufbfh = Open(window, MODE_NEWFILE);
  98.            free(window);
  99.         }
  100.         
  101.         if (ufbs[0].ufbfh == NULL)
  102.            ufbs[0].ufbfh = Open("NIL:", MODE_NEWFILE);
  103.         
  104.         ufbs[0].ufbflg |= UFB_CLO;
  105.         handle = (struct FileHandle *) (ufbs[0].ufbfh << 2);
  106.         process = (struct Process *) FindTask(0);
  107.         process->pr_ConsoleTask = (APTR) handle->fh_Type;
  108.         if ((ufbs[1].ufbfh = Open("*", MODE_OLDFILE)) == NULL)
  109.              ufbs[1].ufbfh = Open("NIL:", MODE_OLDFILE);
  110.         ufbs[2].ufbfh = ufbs[1].ufbfh;
  111.     } 
  112.     else 
  113.     {                     /* running under CLI            */
  114.         ufbs[0].ufbfh = Input();
  115.         ufbs[1].ufbfh = Output();
  116.         if ((ufbs[2].ufbfh = Open("*", MODE_OLDFILE)) == NULL)
  117.              ufbs[2].ufbfh = Open("NIL:", MODE_OLDFILE);
  118.     }
  119.  
  120.  
  121.     __nufbs += 3;
  122.  
  123.  
  124.     x = (__fmode) ? 0 : _IOXLAT;
  125.     stdin->_file = 0;
  126.     stdin->_flag = _IOREAD | x;
  127.     stdout->_file = 1;
  128.     stdout->_flag = _IOWRT | _IOLBF | x;
  129.     stderr->_file = 2;
  130.     stderr->_flag = _IOWRT | _IOLBF | x;
  131.     return 0; 
  132.  
  133. }
  134.  
  135.