home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / dlibssrc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-03  |  2.3 KB  |  78 lines

  1. #include <osbind.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4.  
  5. extern char    *_base;
  6. extern int    _argc;
  7. extern char    **_argv;
  8. extern char    *_envp;
  9.  
  10. FILE    _iob[MAXFILES] =            /* stream buffers */
  11. {
  12. /*in*/    {0, F_READ|F_DEVICE, NULL, NULL, 0, 0, '\0', '\0'},
  13. /*out*/    {1, F_WRITE|F_DEVICE, NULL, NULL, 0, 0, '\0', '\0'},
  14. /*err*/    {-1, F_WRITE|F_DEVICE, NULL, NULL, 0, 0, '\0', '\0'},
  15. /*prn*/    {-3, F_WRITE|F_DEVICE, NULL, NULL, 0, 0, '\0', '\0'},
  16. /*aux*/    {-2, F_READ|F_WRITE|F_DEVICE, NULL, NULL, 0, 0, '\0', '\0'},
  17.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  18.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  19.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  20.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  21.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  22.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  23.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  24.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  25.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  26.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  27.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  28.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  29.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  30.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'},
  31.     {0, 0, NULL, NULL, 0, 0, '\0', '\0'}
  32. };
  33.  
  34. void _main(cmdline, cmdlen)
  35. char *cmdline;
  36. int cmdlen;
  37. /*
  38.  *    Parses command line, opens standard files, plus other assorted
  39.  *    general setup. Then calls user's main() function.  If main()
  40.  *    returns, exit() is called with a 0 return value.  The following
  41.  *    standard streams are initialized by _main():
  42.  *        stdin        Standard input, may be redirected
  43.  *        stdout        Standard output, may be redirected
  44.  *        stderr        Usually the system console
  45.  *        stdprn        The standard printer (output only)
  46.  *        stdaux        The communication port (input/output)
  47.  *    The _initargs() function is called to process the command line.
  48.  *    The global variables "_argc", "_argv" and "_envp" are used to
  49.  *    store the values to be passed to the user's main().
  50.  */
  51. {
  52.     void exit();
  53.  
  54.     _argc = 0;
  55.     _argv = NULL;
  56.     _envp = *((char **) (_base + 0x2C));
  57.     _initargs(cmdline, cmdlen);
  58.     main(_argc, _argv, _envp);
  59.     exit(0);
  60. }
  61.  
  62. void exit(status)
  63. int status;
  64. /*
  65.  *    Flushes and closes all open streams.  Returns <status> value to
  66.  *    the operating system.
  67.  */
  68. {
  69.     register int i, f;
  70.  
  71.     for(i=0; i<MAXFILES; ++i) {
  72.         f = _iob[i].F_stat;
  73.         if((f & (F_READ | F_WRITE)) && !(f & F_DEVICE))
  74.             fclose(&_iob[i]);
  75.     }
  76.     _exit(status);
  77. }
  78.