home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / msdos / programm / 8811 < prev    next >
Encoding:
Text File  |  1992-08-25  |  31.6 KB  |  706 lines

  1. Xref: sparky comp.os.msdos.programmer:8811 news.answers:2627
  2. Newsgroups: comp.os.msdos.programmer,news.answers
  3. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!ncoast!brown
  4. From: brown@NCoast.ORG (Stan Brown)
  5. Subject: comp.os.msdos.programmer FAQ part 3 of 4
  6. Expires: Sat, 10 Oct 1992 00:25:46 GMT
  7. Organization: Oak Road Systems, Cleveland Ohio USA
  8. Date: Wed, 26 Aug 1992 00:25:46 GMT
  9. Approved: news-answers-request@MIT.Edu
  10. Message-ID: <msdos-faq.920825.3@NCoast.ORG>
  11. Followup-To: comp.os.msdos.programmer
  12. References: <msdos-faq.920825.1@NCoast.ORG>
  13. Supersedes: <1992Jul25.161111.7026@NCoast.ORG>
  14. Lines: 690
  15.  
  16. Archive-name: msdos-programmer-faq/part3
  17. Last-modified: 22 August 1992
  18.  
  19.  
  20. (continued from part 2)         (no warranty on the code or information)
  21.  
  22. If the posting date is more than six weeks in the past, see instructions
  23. in the last part of this list for how to get an updated copy.
  24.  
  25.             Copyright (C) 1992  Stan Brown, Oak Road Systems
  26.  
  27.  
  28. section 4.  Disks and files
  29. ===========================
  30.  
  31. Q401. What drive was the PC booted from?
  32.  
  33.     Under DOS 4.0 or later, load 3305 hex into AX; do an INT 21.  DL is
  34.     returned with an integer indicating the boot drive (1=A:, etc.).
  35.  
  36. Q402. Which real and virtual disk drives are valid?
  37.  
  38.     Use INT 21 function 29 (parse filename).  Point DS:SI at a null-
  39.     terminated ASCII string that contains the drive letter and a colon,
  40.     point ES:DI at a 37-byte dummy FCB buffer, set AX to 2900h, and do
  41.     an INT 21.  On return, AL is FF if the drive is invalid, something
  42.     else if the drive is valid.  RAM disks and SUBSTed drives are
  43.     considered valid.
  44.  
  45.     Unfortunately, the b: drive is considered valid even on a single-
  46.     diskette system.  You can check that special case by interrogating
  47.     the BIOS equipment byte at 0040:0010.  Bits 7-6 contain the one less
  48.     than the number of diskette drives, so if those bits are zero you
  49.     know that b: is an invalid drive even though function 29 says it's
  50.     valid.
  51.  
  52.     Following is some code originally posted by Doug Dougherty, with my
  53.     fix for the b: special case, tested in Borland C++ 2.0 small model:
  54.  
  55.         #include <dos.h>
  56.         void drvlist(void)  {
  57.             char *s = "A:", fcb_buff[37];
  58.             int valid;
  59.             for (   ;  *s<='Z';  (*s)++) {
  60.                 _SI = (unsigned) s;
  61.                 _DI = (unsigned) fcb_buff;
  62.                 _ES = _DS;
  63.                 _AX = 0x2900;
  64.                 geninterrupt(0x21);
  65.                 valid = _AL != 0xFF;
  66.                 if (*s == 'B'  &&  valid) {
  67.                     char far *equipbyte = (char far *)0x00400010UL;
  68.                     valid = (*equipbyte & (3 << 6)) != 0;
  69.                 }
  70.                 printf("Drive '%s' is %sa valid drive.\n",
  71.                         s, valid ? "" : "not ");
  72.             }
  73.         }
  74.  
  75. Q403. How can I make my single floppy drive both a: and b:?
  76.  
  77.     Under any DOS since DOS 2.0, you can put the command
  78.  
  79.         assign b=a
  80.  
  81.     into your AUTOEXEC.BAT file.  Then, when you type "DIR B:" you'll no
  82.     longer get the annoying prompt to insert diskette B (and the even
  83.     more annoying prompt to insert A the next time you type "DIR A:").
  84.  
  85.     You may be wondering why anybody would want to do this.  Suppose you
  86.     use two different machines, maybe one at home and one at work.  One
  87.     of them has only a 3.5" diskette drive; the other machine has two
  88.     drives, and b: is the 3.5" one.  You're bound to type "dir b:" on
  89.     the first one, and get the nuisance message
  90.  
  91.         Insert diskette for drive B: and press any key when ready.
  92.  
  93.     But if you assign drive b: to point to a:, you avoid this problem.
  94.  
  95.     Caution:  there are a few commands, such as DISKCOPY, that will not
  96.     work right on ASSIGNed or SUBSTed drives.  See the DOS manual for
  97.     the full list.  Before typing one of those commands, be sure to turn
  98.     off the mapping by typing "assign" without arguments.
  99.  
  100.     The DOS 5.0 manual says that ASSIGN is obsolete, and recommends the
  101.     equivalent form of SUBST: "subst b: a:\".  Unfortunately, if this
  102.     command is executed when a: doesn't hold a diskette, the command
  103.     fails.  ASSIGN doesn't have this problem.
  104.  
  105. Q404. Why won't my C program open a file with a path?
  106.  
  107.     You've probably got something like the following code:
  108.  
  109.         char *filename = "c:\foo\bar\mumble.dat";
  110.         . . .  fopen(filename, "r");
  111.  
  112.     The problem is that \f is a form feed, \b is a backspace, and \m is
  113.     m.  Whenever you want a backslash in a string constant in C, you
  114.     must use two backslashes:
  115.  
  116.         char *filename = "c:\\foo\\bar\\mumble.dat";
  117.  
  118.     This is a feature of every C compiler, because Dennis Ritchie
  119.     designed C this way.  It's a problem only on MS-DOS systems, because
  120.     only DOS (and Atari ST/TT running TOS, I'm told) uses the backslash
  121.     in directory paths.  But even in DOS this backslash convention
  122.     applies _only_ to string constants in your source code.  For file
  123.     and keyboard input at run time, \ is just a normal character, so
  124.     users of your program would type in file specs at run time the same
  125.     way as in DOS commands, with single backslashes.
  126.  
  127.     Another possibility is to code all paths in source programs with /
  128.     rather than \ characters:
  129.  
  130.         char *filename = "c:/foo/bar/mumble.dat";
  131.  
  132.     Ralf Brown writes that "All versions of the DOS kernel accept either
  133.     forward or backslashes as directory separators.  I tend to use this
  134.     form more frequently than backslashes since it is easier to type and
  135.     read."  This applies to DOS function calls (and therefore to calls
  136.     to the file library of every programming language), but not to DOS
  137.     commands.
  138.  
  139. Q405. How can I redirect printer output to a file?
  140.  
  141.     My personal favorite utility for this purpose is PRN2FILE from {PC
  142.     Magazine}, available from Simtel as PD1:<MSDOS.PRINTER>PRN2FILE.ARC,
  143.     14451 bytes including .ASM source, or from garbo as prn2file.zip in
  144.     /pc/printer.  ({PC Magazine} has given copies away as part of its
  145.     utilities disks, so you may already have a copy.)
  146.  
  147.     The other listings that I found in Simtel's index as of mid-January
  148.     1992 are, from newest to oldest:
  149.  
  150.     PD1:<MSDOS.PRINTER>
  151.     PRIND25.ZIP   17008  900420  Redirect printer to file/screen/printer
  152.     RPRN.ARC       2631  900120  Redirect printer output to disk file
  153.     PRINDIR.ARC    6836  891210  Redirects printer output to disk file
  154.     VPRNT301.ARC  15259  891030  Printer redirection to disk file, v3.01
  155.     PSTASH20.ARC   2830  890216  Redirects lpt output to file
  156.     PRNDSK.ARC    27172  871224  Redirect Printer Output To Disk
  157.     LPTX600.ARC   31596  870319  Redirect printer output to a file
  158.  
  159.     At garbo, in the /pc/printer directory, you can find
  160.  
  161.     lamneth.zip     PrintScreen redirect to a disk file v09-Mar-88
  162.     lptx.zip        Redirect printer output v6.0
  163.     lptx700.zip     Redirect printer output v7.0 (not update, just
  164.                     different)
  165.     prindir8.zip    Printer and port Redirection from Allen Creations
  166.  
  167. Q406. What's the format of an .EXE header?
  168.  
  169.     See pages 349-350 of {PC Magazine}'s June 30, 1992 issue (xi:12) for
  170.     the old and new formats.  For a more detailed layout, look under INT
  171.     21 function 4B in Ralf Brown's interrupt list.
  172.  
  173. Q407. How can my program open more files than DOS's limit of 20?
  174.  
  175.     (This is a summary of an article Ralf Brown posted on 8 August 1992.)
  176.  
  177.     There are separate limits on files and file handles.  For example,
  178.     DOS opens three files but five file handles:  CON (stdin, stdout,
  179.     and stderr), AUX (stdaux), and PRN (stdprn).
  180.  
  181.     The limit in FILES= in CONFIG.SYS is a system-wide limit on files
  182.     opened by all programs (including the three that DOS opens and any
  183.     opened by TSRs); each process has a limit of 20 handles (including
  184.     the five that DOS opens).  Example:  CONFIG.SYS has FILES=40.  Then
  185.     program #1 will be able to open 15 file handles.  Assuming that the
  186.     program actually does open 15 handles pointing to 15 different
  187.     files, other programs could still open a total of 22 files (40-3-15
  188.     = 22), though no one program could open more than 15 file handles.
  189.  
  190.     If you're running DOS 3.3 or later, you can increase the per-process
  191.     limit of 20 file handles by a call to INT 21 function 67, Set Handle
  192.     Count.  Your program is still limited by the system-wide limit on
  193.     open files, so you may also need to increase the FILES= value in
  194.     your CONFIG.SYS file (and reboot).  The run-time library that you're
  195.     using may have a fixed-size table of file handles, so you may also
  196.     need to get source code for the module that contains the table,
  197.     increase the table size, and recompile it.
  198.  
  199.  
  200. section 5. Serial ports (COM ports)
  201. ===================================
  202.  
  203. Q501. How do I set my machine up to use COM3 and COM4?
  204.  
  205.     Unless your machine is fairly old, it's probably already set up.
  206.     After installing the board that contains the extra COM port(s),
  207.     check the I/O addresses in word 0040:0004 or 0040:0006.  (In DEBUG,
  208.     type "D 40:4 L4" and remember that every word is displayed low
  209.     byte first, so if you see "03 56" the word is 5603.)  If those
  210.     addresses are nonzero, your PC is ready to use the ports and you
  211.     don't need the rest of this answer.
  212.  
  213.     If the I/O address words in the 0040 segment are zero after you've
  214.     installed the I/O board, you need some code to store these values
  215.     into the BIOS data segment:
  216.  
  217.         0040:0004  word  I/O address of COM3
  218.         0040:0006  word  I/O address of COM4
  219.         0040:0011  byte (bits 3-1): number of serial ports installed
  220.  
  221.     The documentation with your I/O board should tell you the port
  222.     addresses.  When you know the proper port addresses, you can add
  223.     code to your program to store them and the number of serial ports
  224.     into the BIOS data area before you open communications.  Or you can
  225.     use DEBUG to create a little program to include in your AUTOEXEC.BAT
  226.     file, using this script:
  227.  
  228.             n SET_ADDR.COM      <--- or a different name ending in .COM
  229.             a 100
  230.             mov  AX,0040
  231.             mov  DS,AX
  232.             mov  wo [0004],aaaa <--- replace aaaa with COM3 address or 0
  233.             mov  wo [0006],ffff <--- replace ffff with COM4 address or 0
  234.             and  by [0011],f1
  235.             or   by [0011],8    <--- use number of serial ports times 2
  236.             mov  AH,0
  237.             int  21
  238.                                 <--- this line must be blank
  239.             rCX
  240.             1f
  241.             rBX
  242.             0
  243.             w
  244.             q
  245.  
  246. Q502. How do I find the I/O address of a COM port?
  247.  
  248.     Look in the four words beginning at 0040:0000 for COM1 through COM4.
  249.     (The DEBUG command "D 40:0 L8" will do this.  Remember that words
  250.     are stored and displayed low byte first, so a word value of 03F8
  251.     will be displayed as F8 03.)  If the value is zero, that COM port is
  252.     not installed (or you've got an old BIOS; see the preceding Q).  If
  253.     the value is nonzero, it is the I/O address of the transmit/receive
  254.     register for the COM port.  Each COM port occupies eight consecutive
  255.     I/O addresses (though only seven are used by many chips).
  256.  
  257.     Here's some C code (tested under MSC 5.0 and BC++ 2.0) to find the
  258.     I/O address:
  259.  
  260.         unsigned ptSel(unsigned comport) {
  261.             unsigned io_addr;
  262.             if (comport >= 1  &&  comport <= 4) {
  263.                 unsigned far *com_addr = (unsigned far *)0x00400000UL;
  264.                 io_addr = com_addr[comport-1];
  265.             }
  266.             else
  267.                 io_addr = 0;
  268.             return io_addr;
  269.         }
  270.  
  271. Q503. But aren't the COM ports always at I/O addresses 3F8, 2F8, 3E8,
  272.       and 2E8?
  273.  
  274.     The first two are usually right (though not always); the last two
  275.     are different on many machines.
  276.  
  277. Q504. How do I configure a COM port and use it to transmit data?
  278.  
  279.     After hearing several recommendations, I looked at Joe Campbell's {C
  280.     Programmer's Guide to Serial Communications}, ISBN 0-672-22584-0,
  281.     and agree that it is excellent.  He gives complete details on how
  282.     serial ports work, along with complete programs for doing polled or
  283.     interrupt-driver I/O.  The book is quite thick, and none of it looks
  284.     like filler.
  285.  
  286.     If Campbell's book is overkill for you, you'll find a good short
  287.     description of serial I/O in {DOS 5: A Developer's Guide}, ISBN
  288.     1-55851-177-6, by Al Williams.
  289.  
  290.  
  291. section 6. Other hardware questions and problems
  292. ================================================
  293.  
  294. Q601. Which 80x86 CPU is running my program?
  295.  
  296.     According to an article posted by Michael Davidson, Intel's approved
  297.     code for distinguishing among 8086, 80286, 80386, and 80486 and for
  298.     detecting the presence of an 80287 or 80387 is published in the
  299.     Intel's 486SX processor manual (order number 240950-001).  You can
  300.     download David Kirschbaum's improved version of this from Simtel as
  301.     PD1:<MSDOS.SYSUTL>CPUID593.ZIP.
  302.  
  303.     According to an article posted by its author, WCPU041.ZIP knows the
  304.     differences between DX and SX varieties of 386 and 486 chips, and
  305.     can also detect a math coprocessor.  It's in PD1:<MSDOS.SYSUTL> at
  306.     Simtel.
  307.  
  308. Q602. How can a C program send control codes to my printer?
  309.  
  310.     If you just fprintf(stdprn, ...), C will translate some of your
  311.     control codes.  The way around this is to reopen the printer in
  312.     binary mode:
  313.  
  314.         prn = fopen("PRN", "wb");
  315.  
  316.     You must use a different file handle because stdprn isn't an lvalue.
  317.     By the way, PRN or LPT1 must not be followed by a colon in DOS 5.0.
  318.  
  319.     There's one special case, Ctrl-Z (ASCII 26), the DOS end-of-file
  320.     character.  If you try to send an ASCII 26 to your printer, DOS
  321.     simply ignores it.  To get around this, you need to reset the
  322.     printer from "cooked" to "raw" mode.  Microsoft C users must use int
  323.     21 function 44, "get/set device information".  Turbo C and Borland
  324.     C++ users can use ioctl to accomplish the same thing:
  325.  
  326.         ioctl(fileno(prn), 1, ioctl(fileno(prn),0) & 0xFF | 0x20, 0);
  327.  
  328.     An alternative approach is simply to write the printer output into a
  329.     disk file, then copy the file to the printer with the /B switch.
  330.  
  331.     A third approach is to bypass DOS functions entirely and use the
  332.     BIOS printer functions at INT 17.  If you also fprintf(stdprn,...)
  333.     in the same program, you'll need to use fflush( ) to synchronize
  334.     fprintf( )'s buffered output with the BIOS's unbuffered.
  335.  
  336.     By the way, if you've opened the printer in binary mode from a C
  337.     program, remember that outgoing \n won't be translated to carriage
  338.     return/line feed.  Depending on your printer, you may need to send
  339.     explicit \n\r sequences.
  340.  
  341. Q603. How can I redirect printer output to a file?
  342.  
  343.     Please see section 4, "Disks and files", for the answer.
  344.  
  345. Q604. Which video adapter is installed?
  346.  
  347.     This technique should work if your BIOS is not too old.  It uses
  348.     three functions from INT 10, the BIOS video interrupt.
  349.  
  350.     Set AH=12h, AL=0, BL=32h; INT 10h.  If AL is 12h, you have a VGA.
  351.     If not, set AH=12h, BL=10h; INT 10h.  If BL is 0,1,2,3, you have an
  352.     EGA with 64,128,192,256K memory.  If not, set AH=0Fh; INT 10h.  If
  353.     AL is 7, you have an MDA (original monochrome adapter) or Hercules;
  354.     if not, you have a CGA.
  355.  
  356.     I've tested this for my VGA and got the right answer; but I can't
  357.     test it for the other equipment types.  Please let me know by email
  358.     at brown@ncoast.org if your results vary.
  359.  
  360. Q605. How do I switch to 43- or 50-line mode?
  361.  
  362.     Download PD1:<MSDOS.SCREEN>VIDMODE.ZIP, 4412 bytes, from Simtel or
  363.     one of the mirror sites.  It contains .COM utilities and .ASM
  364.     source code.
  365.  
  366. Q606. How can I find the Microsoft mouse position and button status?
  367.  
  368.     Use INT 33 function 3, described in Ralf Brown's interrupt list.
  369.  
  370.     The Windows manual says that the Logitech mouse is compatible with
  371.     the Microsoft one, so I assume the interrupt will work the same.
  372.  
  373.     Also, see the directory PD1:<MSDOS.MOUSE> at Simtel.
  374.  
  375. Q607. How can I access a specific address in the PC's memory?
  376.  
  377.     First check the library that came with your compiler.  Many vendors
  378.     have some variant of peek and poke functions; in Turbo Pascal use
  379.     the pseudo-arrays Mem, MemW, and MemL.  As an alternative, you can
  380.     construct a far pointer:  use Ptr in Turbo Pascal, MK_FP in the
  381.     Turbo C family, and FP_OFF and FP_SEG in Microsoft C.
  382.  
  383.     Caution:  Turbo C and Turbo C++ also have FP_OFF and FP_SEG macros,
  384.     but they can't be used to construct a pointer.  In Borland C++ those
  385.     macros work the same as in Microsoft C, but MK_FP is easier to use.
  386.  
  387.     By the way, it's not useful to talk about "portable" ways to do
  388.     this.  Any operation that is tied to a specific memory address is
  389.     not likely to work on another kind of machine.
  390.  
  391. Q608. How can I read or write my PC's CMOS memory?
  392.  
  393.     There are a great many public-domain utilities that do this.  These
  394.     were available for download from Simtel as of 31 March 1992:
  395.  
  396.     PD1:<MSDOS.AT>
  397.     CMOS13.ZIP     7258  920330  Backup and restore damaged CMOS memory
  398.     CMOSER11.ZIP  28323  910721  386/286 enhanced CMOS setup program
  399.     CMOSRAM.ZIP   76096  920214  Save AT/386/486 CMOS data to file and restore
  400.     ROM2.ARC      20497  900131  Save AT and 386 CMOS data to file and restore
  401.     SETUP21.ARC   24888  880613  Setup program which modifies CMOS RAM
  402.     VIEWCMOS.ARC  15374  900225  Display contents of AT CMOS RAM, w/C source
  403.  
  404.     At garbo, /pc/ts/tsutle17.zip contains a CMOS program to check and
  405.     display CMOS memory, but not to write to it.
  406.  
  407.     I have heard good reports of CMOS299.ZIP, available in the pc.dir
  408.     directory of cantva.canterbury.ac.nz [132.181.30.3].
  409.  
  410.     Of the above, my only experience is with CMOSRAM, which seems to
  411.     work fine.  It contains an excellent (and witty) .DOC file that
  412.     explains the hardware involved and gives specific recommendations
  413.     for preventing disaster or recovering from it.  It's $5 shareware.
  414.  
  415.     Robert Jourdain's {Programmer's Problem Solver for the IBM PC, XT,
  416.     and AT} has code for accessing the CMOS RAM, according to an article
  417.     posted in this newsgroup.
  418.  
  419.  
  420. section 7. Other software questions and problems
  421. ================================================
  422.  
  423. Q701. How can a program reboot my PC?
  424.  
  425.     You can generate a "cold" boot or a "warm" boot.  A cold boot is
  426.     the same as turning the power off and on; a warm boot is the same as
  427.     Ctrl-Alt-Del and skips the power-on self test.
  428.  
  429.     For a warm boot, store the hex value 1234 in the word at 0040:0072.
  430.     For a cold boot, store 0 in that word.  Then, if you want to live
  431.     dangerously, jump to address FFFF:0000.  Here's C code to do it
  432.     (tested under MSC 5.0 and BC++ 2.0):
  433.  
  434.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  435.             void (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  436.             unsigned far* type = (unsigned far*)0x00400072UL;
  437.             *type = (want_warm ? 0x1234 : 0);
  438.             (*boot)( );
  439.         }
  440.  
  441.     What's wrong with that method?  It will boot right away, without
  442.     closing files, flushing disk caches, etc.  If you boot without
  443.     flushing a write-behind disk cache (if one is running), you could
  444.     lose data or even trash your hard drive.
  445.  
  446.     There are two methods of signaling the cache to flush its buffers:
  447.     (1) simulate a keyboard Ctrl-Alt-Del in the keystroke translation
  448.     function of the BIOS (INT 15 function 4F), and (2) issue a disk
  449.     reset (DOS function 0D).  Most disk-cache programs hook one or both
  450.     of those interrupts, so if you use both methods you'll probably be
  451.     safe.
  452.  
  453.     When user code simulates a Ctrl-Alt-Del, one or more of the programs
  454.     that have hooked INT 15 function 4F can ask that the key be ignored by
  455.     clearing the carry flag.  For example, HyperDisk does this when it
  456.     has started but not finished a cache flush.  So if the carry flag
  457.     comes back cleared, the boot code has to wait a couple of cluck
  458.     ticks and then try again.  (None of this matters on older machines
  459.     whose BIOS can't support 101- or 102-key keyboards; see "What is the
  460.     SysRq key for?" in section 3, "Keyboard".)
  461.  
  462.     Here's C code that tries to signal the disk cache (if any) to
  463.     flush (compiles under MSC 5.0 and BC++ 2.0):
  464.  
  465.         #include <dos.h>
  466.         void bootme(int want_warm)  /* arg 0 = cold boot, 1 = warm */ {
  467.             union REGS reg;
  468.             void    (far* boot)(void) = (void (far*)(void))0xFFFF0000UL;
  469.             unsigned far* boottype    =     (unsigned far*)0x00400072UL;
  470.             char     far* shiftstate  =         (char far*)0x00400017UL;
  471.             unsigned      ticks;
  472.             int           time_to_waste;
  473.             /* Simulate reception of Ctrl-Alt-Del: */
  474.             for (;;) {
  475.                 *shiftstate |= 0x0C;    /* turn on Ctrl & Alt */
  476.                 reg.x.ax = 0x4F53;      /* 0x53 = Del's scan code */
  477.                 reg.x.cflag = 1;        /* sentinel for ignoring key */
  478.                 int86(0x15, ®, ®);
  479.                 /* If carry flag is still set, we've finished. */
  480.                 if (reg.x.cflag)
  481.                     break;
  482.                 /* Else waste some time before trying again: */
  483.                 reg.h.ah = 0;
  484.                 int86(0x1A, ®, ®);/* system time into CX:DX */
  485.                 ticks = reg.x.dx;
  486.                 for (time_to_waste = 3;  time_to_waste > 0;  ) {
  487.                     reg.h.ah = 0;
  488.                     int86(0x1A, ®, ®);
  489.                     if (ticks != reg.x.dx)
  490.                         ticks = reg.x.dx , --time_to_waste;
  491.                 }
  492.             }
  493.             /* Issue a DOS disk reset request: */
  494.             reg.h.ah = 0x0D;
  495.             int86(0x21, ®, ®);
  496.             /* Set boot type and boot: */
  497.             *boottype = (want_warm ? 0x1234 : 0);
  498.             (*boot)( );
  499.         }
  500.  
  501. Q702. How can I time events with finer resolution than the system
  502.       clock's 55 ms (about 18 ticks a second)?
  503.  
  504.     The following files can be downloaded from Simtel:
  505.  
  506.     PD1:<MSDOS.AT>
  507.     ATIM.ARC       5946  881126  Precision program timing for AT
  508.  
  509.     PD1:<MSDOS.C>
  510.     MILLISEC.ZIP  37734  911205  MSC/asm src for millisecond res timing
  511.     MSCHRT3.ZIP   53708  910605  High-res timer toolbox for MSC 5.1
  512.     MSEC_12.ZIP    8484  920320  High-def millisec timer v1.2 (C,ASM)
  513.  
  514.     PD1:<MSDOS.TURBO-C>
  515.     TCHRT3.ZIP    53436  910606  High-res timer toolbox for Turbo C 2.0
  516.     TCTIMER.ARC   20087  891030  High-res timing of events for Turbo C
  517.     ZTIMER11.ZIP  74477  920428  Time C/C++/Pascal/ASM-microsec accuracy
  518.  
  519.     PD1:<MSDOS.TURBOPAS>
  520.     BONUS507.ARC 150435  900205  [Turbo Pascal source: high-res timing]
  521.  
  522.     Pascal users can download source code in /pc/turbopas/bonus507.zip
  523.     at garbo.
  524.  
  525. Q703. How can I find the error level of the previous program?
  526.  
  527.     First, which previous program are you talking about?  If your
  528.     current program ran another one, when the child program ends its
  529.     error level is available to the program that spawned it.  Most
  530.     high-level languages provide a way to do this; for instance, in
  531.     Turbo Pascal it's Lo(DosExitCode) and the high byte gives the way
  532.     in which the child terminated.  In Microsoft C, the exit code of a
  533.     synchronous child process is the return value of the spawn-typoe
  534.     function that creates the process.
  535.  
  536.     If your language doesn't have a function to return the error code
  537.     of a child process, you can use INT 21 function 4D (get return
  538.     code).  By the way, this will tell you the child's exit code and the
  539.     manner of its ending (normal, Ctrl-C, critical error, or TSR).
  540.  
  541.     It's much trickier if the current program wants to get the error
  542.     level of the program that ran and finished before this one started.
  543.     G.A.Theall has published source and compiled code to do this; you
  544.     can download it from Simtel as PD1:<MSDOS.BATUTL>ERRLVL12.ZIP.  (The
  545.     code uses undocumented features in DOS 3.3 through 5.0.  Theall says
  546.     in the .DOC file that the values returned under 4DOS or other
  547.     replacements won't be right.)
  548.  
  549. Q704. How can a program set DOS environment variables?
  550.  
  551.     Program functions that read or write "the environment" typically
  552.     access only the program's copy of the environment.  What this Q
  553.     really wants to do is to modify the active environment, the one that
  554.     is affected by SET commands in batch files or at the DOS prompt.
  555.     You need to do some programming to find the active environment, and
  556.     that programming varies for different versions of DOS.
  557.  
  558.     A fairly well-written article in {PC Magazine} volume 8 number 20
  559.     (1989 Nov 28), pages 309-314, explains how to find the active
  560.     environment, and includes Pascal source code.  The article hints at
  561.     how to change the environment, and suggests creating paths longer
  562.     than 128 characters as one application.
  563.  
  564.     In searching Simtel for source code, I found many possibilities.  I
  565.     liked PD1:<MSDOS.SYSUTL>RBSETNV1.ZIP of the ones I looked at (not
  566.     all of them).  It includes some utilities to manipulate the environ-
  567.     ment, with source code in C.
  568.  
  569. Q705. How can I change the switch character to - from /?
  570.  
  571.     Under DOS 5.0, you can't.
  572.  
  573.     Earlier DOS versions let you change the "switch character" that
  574.     introduces command-line options by using SWITCHAR= in CONFIG.SYS or
  575.     by calling a DOS function.  Then you would type "DIR -P" rather than
  576.     "DIR /P".  Not only did this make DOS look more like Unix (oooo!),
  577.     but you could type / rather than \ in directory names.
  578.  
  579.     Under DOS 5.0, all this has changed.  The DOS 5.0 commands hard-code
  580.     / as the switch character.  I've seen several reports that programs
  581.     to change the switch character stopped working under DOS 5.0.
  582.  
  583.     Programs like SLASH.ZIP claim to change the switch character from /
  584.     to something else.  They do, but the change affects only the
  585.     third-party programs that query the switch character via INT 21
  586.     function 3700.  DOS internal commands and most MS-DOS external
  587.     commands will still use /.  (DOS replacements like 4DOS may honor
  588.     the switch character for internal commands.)
  589.  
  590. Q706. Why does my interrupt function behave strangely?
  591.  
  592.     Interrupt service routines can be tricky, because you have to do
  593.     some things differently from "normal" programs.  If you make a
  594.     mistake, debugging is a pain because the symptoms may not point at
  595.     what's wrong.  Your machine may lock up or behave erratically, or
  596.     just about anything else can happen.  Here are some things to look
  597.     for.  (See the next Q for general help before you have a problem.)
  598.  
  599.     First, did you fail to set up the registers at the start of your
  600.     routine?  When your routine begins executing, you can count on
  601.     having CS point to your code segment and SS:SP point to some valid
  602.     stack (of unknown length), and that's it.  In particular, an
  603.     interrupt service routine must set DS to DGROUP before accessing any
  604.     data in its data segments.  (If you're writing in a high-level
  605.     language, the compiler may generate this code for you automatically;
  606.     check your compiler manual.  For instance, in Borland and Microsoft
  607.     C, give your function the "interrupt" attribute.)
  608.  
  609.     Did you remember to turn off stack checking when compiling your
  610.     interrupt server and any functions it calls?  The stack during the
  611.     interrupt is not where the stack-checking code expects it to be.
  612.     (Caution:  Some third-party libraries have stack checking compiled
  613.     in, so you can't call them from your interrupt service routine.)
  614.  
  615.     Next, are you calling any DOS functions (INT 21, 25, or 26) in your
  616.     routine?  DOS is not re-entrant.  This means that if your interrupt
  617.     happens to be triggered while the CPU is executing a DOS function,
  618.     calling another DOS function will wreak havoc.  (Some DOS functions
  619.     are fully re-entrant, as noted in Ralf Brown's interrupt list.
  620.     Also, your program can test, in a way too complicated to present
  621.     here, when it's safe to call non-re-entrant DOS functions.  See INT
  622.     28 and functions 34, 5D06, 5D0B of INT 21.  Your program must read
  623.     both the "InDOS flag" and the "critical error flag".)
  624.  
  625.     Is a function in your language library causing trouble?  Does it
  626.     depend on some initializations done at program startup that is no
  627.     longer available when the interrupt executes?  Does it call DOS (see
  628.     preceding paragraph)?  For example, in both Borland and Microsoft C
  629.     the memory-allocation functions (malloc, etc..) and standard I/O
  630.     functions (scanf, printf) call DOS functions and also depend on
  631.     setups that they can't get at from inside an interrupt.  Many other
  632.     library functions have the same problem, so you can't use them
  633.     inside an interrupt function without special precautions.
  634.  
  635.     Is your routine simply taking too long?  This can be a problem if
  636.     you're hooking on to the timer interrupt, INT 1C or INT 8.  Since
  637.     that interrupt expects to be called 18.2 times a second, your
  638.     routine -- plus any others hooked to the same interrupts -- must
  639.     execute in less than 55 ms.  If they use even a substantial fraction
  640.     of that time, you'll see significant slowdowns of your foreground
  641.     program.  For a good writeup, download INTSHARE (from ni.funet.fi
  642.     in pub/msdos/simtel20/info or from Simtel in PD1:<MSDOS.INFO>).
  643.  
  644.     Did you forget to restore all registers at the end of your routine?
  645.  
  646.     Did you chain improperly to the original interrupt?  You need to
  647.     restore the stack to the way it was upon entry to your routine, then
  648.     do a far jump (not call) to the original interrupt service routine.
  649.     (The process is a little different in high-level languages.)
  650.  
  651. Q707. How can I write a TSR (terminate-stay-resident) utility?
  652.  
  653.     Several books can help you with this.
  654.  
  655.     - Ray Duncan's {Advanced MS-DOS}, ISBN 1-55615-157-8, gives a brief
  656.       checklist intended for experienced programmers.  The ISBN is for
  657.       the second edition, through DOS 4; but check to see whether the
  658.       DOS 5 version is available yet.
  659.  
  660.     - {DOS 5:  A Developer's Guide} by Al Williams, ISBN 1-55851-177-6,
  661.       goes into a little more detail, 90 pages worth!
  662.  
  663.     - Pascal programmers might look at {The Ultimate DOS Programmer's
  664.       Manual} by John Mueller and Wallace Wang, ISBN 0-8306-3534-3, for
  665.       an extended example in mixed Pascal and assembler.
  666.  
  667.     - For a pure assembler treatment, check Steven Holzner's {Advanced
  668.       Assembly Language}, ISBN 0-13-663014-6.  He has a book with the
  669.       same title out from Brady Press, but it's about half as long as
  670.       this one.
  671.  
  672.     - For C programmers, there's a chapter in Herbert Schildt's {The Art
  673.       of C:  Elegant Programming Solutions}.  I haven't seen the book,
  674.       but a posted article recommended it.
  675.  
  676.     You might want to download PD1:<MSDOS.ASMUTL>TEMPLATE.ZIP, 13161
  677.     bytes, from Simtel.  It's Douglas Boling's MASM template for a TSR.
  678.     In the same directory, AMISL083.ZIP, 45230 bytes, contains Ralf
  679.     Brown's assembly-language implementation of the Alternate Multiplex
  680.     Interrupt Specification, with utilities in C.
  681.  
  682.     Finally, there are commercial products, of which TesSeRact (for
  683.     C-language TSRs) is one of the best known.
  684.  
  685. Q708. How can I write a device driver?
  686.  
  687.     Many books answer this in detail.  Among them are {Advanced MS-DOS}
  688.     and {DOS 5: A Developer's Guide}, cited in the preceding Q.
  689.     Michael Tischer's {PC System Programming}, ISBN 1-55755-036-0, has
  690.     an extensive treatment, as does Dettman and Kyle's {DOS Programmer's
  691.     Reference: 2d Edition}, ISBN 0-88022-458-4.  For a really in-depth
  692.     treatment, look for a specialized book like Robert Lai's {Writing
  693.     MS-DOS Device Drivers}, ISBN 0-201-13185-4.
  694.  
  695. Q709. What can I use to manage versions of software?
  696.  
  697.     In PD1:<MSDOS.PGMUTL> at Simtel you'll find RCS55.ZIP, 388K dated 18
  698.     Dec 1991.  I haven't used it myself, but I understand this is a port
  699.     of the Unix RCS utility, but is limited to one-character extensions
  700.     on filenames (no .CPP).
  701.  
  702. (continued in part 4)
  703. -- 
  704.  
  705. Stan Brown, Oak Road Systems                      brown@Ncoast.ORG
  706.