home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / main.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  7KB  |  279 lines

  1. /* from Dale Schumacher's dLibs */
  2. /* heavily modified by ers and jrb */
  3.  
  4. #include <stddef.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <osbind.h>
  9. #include <mintbind.h>
  10. #include <memory.h>
  11. #include <time.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <ctype.h>
  15. #include <param.h>
  16. #include "lib.h"
  17.  
  18. int    errno;
  19. int    __mint;        /* 0 for TOS, MiNT version number otherwise */
  20.  
  21. char    _rootdir;    /* user's preferred root directory */
  22.  
  23. clock_t _starttime;    /* 200 HZ tick when we started the program */
  24. clock_t _childtime;    /* time consumed so far by our children */
  25. FILE    _iob[_NFILE];    /* stream buffers initialized below */
  26.  
  27. /* functions registered by user for calling at exit */
  28. #ifdef __STDC__
  29. typedef void (*ExitFn)(void);
  30. #else
  31. typedef void (*ExitFn)();
  32. #endif
  33. ExitFn *_at_exit;
  34. int _num_at_exit;    /* number of functions registered - 1 */
  35.  
  36. /*
  37.  * get MiNT version number. Since this has to be done in supervisor mode,
  38.  * we might as well set the start-up time of the system here, too.
  39.  */
  40.  
  41. static void
  42. getMiNT()
  43. {
  44.     long *cookie;
  45.  
  46. /* get the system time in 200HZ ticks from the BIOS _hz_200 variable */
  47.     _starttime = *((unsigned long *) 0x4baL);
  48.     _childtime = 0;
  49.  
  50.     cookie = *((long **) 0x5a0L);
  51.     if (!cookie)
  52.         __mint = 0;
  53.     else {
  54.         while (*cookie) {
  55.             if (*cookie == 0x4d694e54L) {    /* MiNT */
  56.                 __mint = (int) cookie[1];
  57.                 return;
  58.             }
  59.             cookie += 2;
  60.         }
  61.     }
  62.     __mint = 0;
  63. }
  64.  
  65. /* supplied by the user */
  66. __EXTERN int main __PROTO((int, char **, char **));
  67.  
  68. /* in getbuf.c */
  69. __EXTERN void _getbuf __PROTO((FILE *));
  70.  
  71. #if __GNUC__ > 1
  72. /* in libgcc2.c */
  73. __EXTERN void __do_global_dtors __PROTO ((void));
  74. #endif
  75.  
  76. #if defined(__TURBOC__) && !defined(__NO_FLOAT__)
  77. void _fpuinit(void); /* in PCFLTLIB.LIB */
  78.  
  79. long _fpuvect[10];
  80. long _pfumode;
  81. long _fpuctrl;
  82. #endif
  83.  
  84. void
  85. _main(_argc, _argv, _envp)
  86.     long _argc;
  87.     char **_argv, **_envp;
  88. {
  89.     register FILE *f;
  90.     register int i;
  91.     char *s, *pconv;
  92.     extern int __default_mode__;    /* in defmode.c or defined by user */
  93.     extern short _app;    /* tells if we're an application or acc */
  94.  
  95.     char *p, *tmp;
  96.     size_t len, cnt;
  97.     
  98.     _num_at_exit = 0;
  99.     errno = 0;
  100.  
  101. #if defined(__TURBOC__) && !defined(__NO_FLOAT__)
  102.     _fpuinit();
  103. #endif
  104.  
  105. /*
  106.  * check for MiNT
  107.  */
  108.     (void)Supexec(getMiNT);
  109.     if (__mint && _app)
  110.         (void)Pdomain(1);    /* set MiNT domain */
  111.  
  112. /*
  113.  * initialize UNIXMODE stuff. Note that this library supports only
  114.  * a few of the UNIXMODE variables, namely "b" (binary mode default)
  115.  * and "r<c>" (default root directory).
  116.  */
  117.     if ((s = getenv("UNIXMODE")) != 0) {
  118.         while (*s) {
  119.             if (*s == 'b')
  120.                 __default_mode__ = _IOBIN;
  121.             else if (*s == 'r' && s[1])
  122.                 _rootdir = *++s;
  123.             else if (*s == '.' && s[1])
  124.                 s++;    /* ignore */
  125.             s++;
  126.         }
  127.     }
  128.  
  129.     if (_rootdir >= 'A' && _rootdir <= 'Z')
  130.         _rootdir = _rootdir - 'A' + 'a';
  131.  
  132. /*
  133.  * if we're running under MiNT, and the current drive is U:, then this
  134.  * must be our preferred drive
  135.  */
  136.     if (!_rootdir && __mint >= 9) {
  137.         if (Dgetdrv() == 'U'-'A')
  138.             _rootdir = 'u';
  139.     }
  140.  
  141.     /* clear isatty status for dumped programs */
  142.     for (i = 0; i < __NHANDLES; i++)
  143.       __open_stat[i].status = FH_UNKNOWN;
  144.  
  145. /* if stderr is not re-directed to a file, force 2 to console
  146.  * (UNLESS we've been run from a shell we trust, i.e. one that supports
  147.  *  the official ARGV scheme, in which case we leave stderr be).
  148.  */
  149.     if(!*_argv[0] && isatty(2))
  150.         (void)Fforce(2, -1);
  151.  
  152.     stdin->_flag = _IOREAD|_IOFBF|__default_mode__;
  153.     stdout->_flag = _IOWRT|_IOLBF|__default_mode__;
  154.     stderr->_flag = _IORW|_IONBF|__default_mode__;
  155.              /* some brain-dead people read from stderr */
  156.  
  157.     for(i = 0, f = _iob; i < 3; ++i, ++f) {    /* flag device streams */
  158.         if(isatty(f->_file = i))
  159.             f->_flag |= _IODEV;
  160.         else
  161.             if(f == stdout)    { /* stderr is NEVER buffered */
  162.             /* if stdout re-directed, make it full buffered */
  163.                 f->_flag &= ~(_IOLBF | _IONBF);
  164.                 f->_flag |=  _IOFBF;
  165.             }
  166.         _getbuf(f);    /* get a buffer */
  167.     }
  168.      for(i = 3; i < _NFILE; i++, f++) {
  169.       f->_flag = 0;        /* clear flags, if this is a dumped program */
  170.     }
  171.  
  172.     /* Fix up environment, if necessary. All variables listed in PCONVERT
  173.      * are affected (by default, only PATH will be converted).
  174.      * The "standard" path separators for PATH are
  175.      * ',' and ';' in the Atari world, but POSIX mandates ':'. This
  176.      * conflicts with the use of ':' as a drive separator, so we
  177.      * also convert names like A:\foo to /dev/A/foo
  178.      * NOTE: this conversion must be undone in spawn.c so that
  179.      * old fashioned programs will understand us!
  180.      */
  181.  
  182.     for (i = 0; (pconv = _envp[i]) != 0; i++) {
  183.         if (! strncmp(pconv, "PCONVERT=", 9)) {
  184.             pconv += 9;
  185.             break;
  186.         }
  187.     }
  188.  
  189.     for (i = 0; (s = _envp[i]) != 0; i++) {
  190.  
  191.         if (pconv) {
  192.  
  193.             p = pconv;
  194.             while (*p) {
  195.                 
  196.                 tmp = p; len = 0;
  197.                 while (*tmp && *tmp != ',') {
  198.                     tmp++; len++;
  199.                 }
  200.                 
  201.                 if (!strncmp(s, p, len) && s[len] == '=') {
  202.                     len++;
  203.                     tmp = s + len;        /* tmp now after '=' */    
  204.                     cnt = 1;
  205.                     while (*tmp) {        /* count words */
  206.                         if (*tmp == ';' || *tmp == ',')
  207.                             cnt++;
  208.                         tmp++;
  209.                     }
  210.                     _envp[i] = (char *) malloc(cnt * MAXPATHLEN);
  211.                     strncpy(_envp[i], s, len);
  212.                     _path_dos2unx(s + len, _envp[i] + len);
  213.                     _envp[i] = (char *) realloc(_envp[i], 
  214.                         strlen(_envp[i]) + 1);
  215.  
  216.                     break;        
  217.                 }
  218.                 
  219.                 if (! *tmp) break;
  220.                 p = tmp + 1;
  221.             }
  222.         }    
  223.         else    /* ! pconv */
  224.         {
  225.         /* PATH is always converted */
  226.             if (s[0] == 'P' && s[1] == 'A' && s[2] == 'T' &&
  227.                 s[3] == 'H' && s[4] == '=')
  228.             {
  229.                 _envp[i] = (char *) malloc(5 + MAXPATHLEN);
  230.                 strncpy(_envp[i], s, 5);
  231.                 _path_dos2unx(s + 5, _envp[i] + 5);
  232.                 _envp[i] = (char *) realloc(_envp[i], 
  233.                     strlen(_envp[i]) + 1);
  234.                 break;
  235.             }
  236.         }
  237.     }
  238.  
  239.     /* ANSI-Draft: A return from the initial call to the main 
  240.      * function is equivalent to calling the exit function with
  241.      * the value returned by the main function as its argument. If
  242.      * the main function executes a return that specifies no
  243.      * value, the termination status returned to the host
  244.      * environment is undefined. [section 2.1.2.2]
  245.      */
  246.     exit(main((int) _argc, _argv, _envp));
  247. }
  248.  
  249. /* For debuggers: non-macro versions of std{in,out,err}. */
  250. #undef stdin
  251. #undef stdout
  252. #undef stderr
  253. FILE *stdin = &_iob[0];
  254. FILE *stdout = &_iob[1];
  255. FILE *stderr = &_iob[2];
  256.  
  257. __EXITING exit(status)
  258.     int status;
  259. {
  260.     register int i, f;
  261.  
  262.     for(i = _num_at_exit - 1; i >= 0; --i)
  263.         (*_at_exit[i])();
  264.  
  265. #if __GNUC__ > 1
  266.     __do_global_dtors ();
  267. #endif
  268.  
  269.     for(i=0; i<_NFILE; ++i) {
  270.         f = _iob[i]._flag;
  271.         if(f & (_IORW | _IOREAD | _IOWRT))
  272.             if (_iob[i]._file <= 2) /* only flush std. streams */
  273.                 fflush(&_iob[i]);
  274.             else
  275.                 fclose(&_iob[i]);
  276.     }
  277.     _exit(status);
  278. }
  279.