home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / sc658pch / _iob.c < prev    next >
C/C++ Source or Header  |  1977-12-31  |  4KB  |  146 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. static void *save_ConsoleTask;
  40.  
  41. STDIO_CONSTRUCTOR(stdio_init)
  42. {
  43.     char *window;
  44.     struct FileHandle *handle;
  45.     struct Process *process;
  46.     int x;
  47.  
  48.     __firstfile = stdin;
  49.     __stdinptr = stdin;
  50.     __stdoutptr = stdout;
  51.     
  52.     
  53.     ufbs[0].ufbnxt = &ufbs[1];
  54.     ufbs[1].ufbnxt = &ufbs[2];
  55.     ufbs[2].ufbnxt = NULL;
  56.     __ufbs = &ufbs[0];
  57.     ufbs[0].ufbfn = NULL;
  58.     ufbs[1].ufbfn = NULL;
  59.     ufbs[2].ufbfn = NULL;
  60.     ufbs[0].ufbflg = UFB_RA | O_RAW | UFB_NC;
  61.     ufbs[1].ufbflg = UFB_WA | O_RAW | UFB_NC;
  62.     ufbs[2].ufbflg = UFB_WA | O_RAW | UFB_NC | UFB_CLO;
  63.  
  64.     if (Output() == NULL) 
  65.     {             /* running under workbench      */
  66.         if (_WBenchMsg && _WBenchMsg->sm_ToolWindow)
  67.             ufbs[0].ufbfh = Open(_WBenchMsg->sm_ToolWindow, MODE_NEWFILE);
  68.         else
  69.         {
  70.            int len = 0;
  71.            char *winname = NULL;
  72.            char *p;
  73.            
  74.            len = strlen(__stdiowin);
  75.  
  76.            if (__stdiowin[len-1] == '/')
  77.            {
  78.                winname = "Output";
  79.                if (_WBenchMsg) 
  80.                   winname = _WBenchMsg->sm_ArgList->wa_Name; 
  81.                
  82.                len += strlen(winname);
  83.            }
  84.  
  85.            if (SysBase->LibNode.lib_Version >= 36)
  86.               len += strlen(__stdiov37);
  87.  
  88.            window = malloc(len);
  89.            if (window == NULL) return 1; /* fail the autoinit */
  90.            
  91.            p = stpcpy(window, __stdiowin);
  92.            if (winname)
  93.               p = stpcpy(p, winname);
  94.  
  95.            if (SysBase->LibNode.lib_Version >= 36)
  96.               p = stpcpy(p, __stdiov37);
  97.            
  98.            ufbs[0].ufbfh = Open(window, MODE_NEWFILE);
  99.            free(window);
  100.         }
  101.         
  102.         if (ufbs[0].ufbfh == NULL)
  103.            ufbs[0].ufbfh = Open("NIL:", MODE_NEWFILE);
  104.         
  105.         ufbs[0].ufbflg |= UFB_CLO;
  106.         handle = (struct FileHandle *) (ufbs[0].ufbfh << 2);
  107.         process = (struct Process *) FindTask(0);
  108.         save_ConsoleTask = process->pr_ConsoleTask;
  109.         process->pr_ConsoleTask = (APTR) handle->fh_Type;
  110.         if ((ufbs[1].ufbfh = Open("*", MODE_OLDFILE)) == NULL)
  111.              ufbs[1].ufbfh = Open("NIL:", MODE_OLDFILE);
  112.         ufbs[2].ufbfh = ufbs[1].ufbfh;
  113.     } 
  114.     else 
  115.     {                     /* running under CLI            */
  116.         ufbs[0].ufbfh = Input();
  117.         ufbs[1].ufbfh = Output();
  118.         if ((ufbs[2].ufbfh = Open("*", MODE_OLDFILE)) == NULL)
  119.              ufbs[2].ufbfh = Open("NIL:", MODE_OLDFILE);
  120.     }
  121.  
  122.  
  123.     __nufbs += 3;
  124.  
  125.  
  126.     x = (__fmode) ? 0 : _IOXLAT;
  127.     stdin->_file = 0;
  128.     stdin->_flag = _IOREAD | x;
  129.     stdout->_file = 1;
  130.     stdout->_flag = _IOWRT | _IOLBF | x;
  131.     stderr->_file = 2;
  132.     stderr->_flag = _IOWRT | _IOLBF | x;
  133.     return 0; 
  134.  
  135. }
  136.  
  137.  
  138. STDIO_DESSTRUCTOR(stdio_init)
  139. {
  140.    struct Process *process;
  141.  
  142.    process = (struct Process *) FindTask(0);
  143.    process->pr_ConsoleTask = save_ConsoleTask;
  144. }
  145.     
  146.