home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2MISC / PMODE12S.ZIP / SRC / PMODE / CRT1.C next >
Encoding:
C/C++ Source or Header  |  1997-02-22  |  3.5 KB  |  142 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <crt0.h>
  4. #include <string.h>
  5. #include <libc/internal.h>
  6. #include <stdlib.h>
  7. #include <go32.h>
  8. #include <stubinfo.h>
  9. #include <dpmi.h>
  10. #include <libc/farptrgs.h>
  11. #include <pc.h>
  12. #include <libc/bss.h>
  13.  
  14. /* Global variables */
  15.  
  16. #define ds _my_ds()
  17.  
  18. int __bss_count = 1;
  19.  
  20. char **environ;
  21. int _crt0_startup_flags;    /* default to zero unless app overrides them */
  22.  
  23. int __crt0_argc;
  24. char **__crt0_argv;
  25.  
  26. /* Local variables */
  27.  
  28. static void
  29. setup_core_selector(void)
  30. {
  31.   int c = __dpmi_allocate_ldt_descriptors(1);
  32.   if (c == -1)
  33.   {
  34.     _dos_ds = 0;
  35.     return;
  36.   }
  37.   _dos_ds = c;
  38.  
  39.   __dpmi_set_segment_limit(_dos_ds, 16 * 1024 * 1024 - 1);
  40. }
  41.  
  42. static void
  43. setup_screens(void)
  44. {
  45.   if(_farpeekw(_dos_ds, 0xffff3) == 0xfd80)    /* NEC PC98 ? */
  46.   {
  47.     ScreenPrimary = ScreenSecondary = 0xa0000;
  48.   }
  49.   else if (_farpeekb(_dos_ds, 0x449) == 7)
  50.   {
  51.     ScreenPrimary = 0xb0000;
  52.     ScreenSecondary = 0xb8000;
  53.   }
  54.   else
  55.   {
  56.     ScreenPrimary = 0xb8000;
  57.     ScreenSecondary = 0xb0000;
  58.   }
  59. }
  60.  
  61. static void
  62. setup_go32_info_block(void)
  63. {
  64.   __dpmi_version_ret ver;
  65.  
  66.   __dpmi_get_version(&ver);
  67.  
  68.   _go32_info_block.size_of_this_structure_in_bytes = sizeof(_go32_info_block);
  69.   __tb = _stubinfo->ds_segment * 16;
  70.   _go32_info_block.size_of_transfer_buffer = _stubinfo->minkeep;
  71.   _go32_info_block.pid = _stubinfo->psp_selector;
  72.   _go32_info_block.master_interrupt_controller_base = ver.master_pic;
  73.   _go32_info_block.slave_interrupt_controller_base = ver.slave_pic;
  74.   _go32_info_block.linear_address_of_stub_info_structure = 0xffffffffU; /* force error */
  75.   __dpmi_get_segment_base_address(_stubinfo->psp_selector, &_go32_info_block.linear_address_of_original_psp);
  76.   _go32_info_block.run_mode = _GO32_RUN_MODE_DPMI;
  77.   _go32_info_block.run_mode_info = (ver.major << 8) | (ver.minor);
  78. }
  79.  
  80. char *__dos_argv0;
  81.  
  82. static void
  83. setup_environment(void)
  84. {
  85.   char *dos_environ = alloca(_stubinfo->env_size), *cp;
  86.   short env_selector;
  87.   int env_count=0;
  88.   movedata(_stubinfo->psp_selector, 0x2c, ds, (int)&env_selector, 2);
  89.   movedata(env_selector, 0, ds, (int)dos_environ, _stubinfo->env_size);
  90.   cp = dos_environ;
  91.   do {
  92.     env_count++;
  93.     while (*cp) cp++; /* skip to NUL */
  94.     cp++; /* skip to next character */
  95.   } while (*cp); /* repeat until two NULs */
  96.   environ = (char **)malloc((env_count+1) * sizeof(char *));
  97.   if (environ == 0)
  98.     return;
  99.  
  100.   cp = dos_environ;
  101.   env_count = 0;
  102.   do {
  103.     /* putenv assumes each string is malloc'd */
  104.     environ[env_count] = (char *)malloc(strlen(cp)+1);
  105.     strcpy(environ[env_count], cp);
  106.     env_count++;
  107.     while (*cp) cp++; /* skip to NUL */
  108.     cp++; /* skip to next character */
  109.   } while (*cp); /* repeat until two NULs */
  110.   environ[env_count] = 0;
  111.  
  112.   __dos_argv0 = (char *)malloc(strlen(cp + 3)+1);
  113.   if (__dos_argv0 == 0)
  114.     abort();
  115.   strcpy(__dos_argv0, cp+3);
  116. }
  117.  
  118. extern void __main(void);
  119. extern int  main(int, char **, char **);
  120. extern void _crt0_init_mcount(void);    /* For profiling */
  121.  
  122. void
  123. __crt1_startup(void)
  124. {
  125.   char *pn;
  126.   __bss_count ++;
  127.   __crt0_argv = 0;
  128.   setup_core_selector();
  129.   setup_screens();
  130.   setup_go32_info_block();
  131.   __djgpp_exception_setup();
  132.   setup_environment();
  133.   __crt0_setup_arguments();
  134.   pn = __crt0_argv ? __crt0_argv[0] : __dos_argv0;
  135.   __crt0_load_environment_file(pn);
  136.   _npxsetup(pn);
  137.   _crt0_init_mcount();
  138.   __main();
  139.   exit(main(__crt0_argc, __crt0_argv, environ));
  140. }
  141.  
  142.