home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ERRABORT.ZIP / TEST.C < prev    next >
C/C++ Source or Header  |  1989-09-11  |  3KB  |  80 lines

  1. /* test.c - test my 'errabort ()' DLL routine. */
  2.  
  3. #define INCL_DOS
  4.  
  5. #include <os2.h>
  6. #include <stdio.h>
  7. #include <os2local.h>
  8.  
  9. /* The following variable must be this data type and this name.  In
  10.  * addition, it must be global in scope -- although for this small
  11.  * program, with only a single source file and a single function
  12.  * (main) it does not matter, it is necessary for more typical programs.
  13.  */
  14. static PSZ pszProgramName = "test.c" ;
  15.  
  16.  
  17. void main (int argc, char **argv)
  18. {
  19.     /* These first two variables must be declared as shown, with the
  20.      * same data type and same name.  In practice, I usually declare
  21.      * 'usRC' globally and let every function use the same one; however,
  22.      * if you do that, be sure you declare it locally for any threads
  23.      * you create.
  24.      */
  25.     static PSZ pszFunctionName = "main" ;
  26.     USHORT usRC ;
  27.  
  28.     /* The rest of these variables are unrelated to 'errabort ()'; they
  29.      * are simply what this particular program requires.
  30.      */
  31.     HDIR hdir ;
  32.     FILEFINDBUF findbuf ;
  33.     USHORT usSearchCount ;
  34.     USHORT cbBytes ;
  35.     static UCHAR szHello [] = "\r\nTest routine for 'errabort ()' has just begun.\r\n" ;
  36.     static UCHAR szHuh [] = "\r\nHuh?  'errabort ()' should have worked.\r\n" ;
  37.  
  38.     /* Even though we initialized 'pszProgramName' above to what we believe
  39.      * is the program name at the time we are compiling it, we can get a
  40.      * more up-to-date name now, at least with OS/2 1.1 and Microsoft C
  41.      * 5.1; other combinations may need to omit the next statement and
  42.      * rely on the initialization above.
  43.      */
  44.     pszProgramName = argv [0] ;
  45.  
  46.     /* We are ready to make our first OS/2 call.  It is important that
  47.      * the return code from the call be assigned to 'usRC'.
  48.      */
  49.     usRC = DosWrite (STDERR, szHello, sizeof (szHello) - 1, &cbBytes) ;
  50.  
  51.     /* Now we invoke the 'ERRCHK' macro.  Note that we can code this
  52.      * macro as if it were a function (as has been done here), with a
  53.      * terminating semicolon.  Purists may prefer to omit the semicolon
  54.      * to emphasize that 'ERRCHK' is a macro and not a function; that
  55.      * works too.
  56.      */
  57.     ERRCHK (DosWrite) ;
  58.  
  59.     /* Now create an error that 'errabort ()' should handle. */
  60.     usSearchCount = 1 ;            /* This is OK. */
  61.     hdir = HDIR_CREATE ;        /* So is this. */
  62.  
  63.     /* However, we are relying on your directory not having a file
  64.      * named "abc.xyz".  If it does, then we will find it, and you
  65.      * will be left wondering whether 'errabort ()' actually works
  66.      * or not.  Ask someone who uses more likely names for their files.
  67.      */
  68.     usRC = DosFindFirst ("abc.xyz", &hdir, FILE_NORMAL, &findbuf,
  69.       sizeof (findbuf), &usSearchCount, 0L) ;
  70.     ERRCHK (DosFindFirst) ;
  71.  
  72.     /* We shouldn't get to this statement, because the preceeding one
  73.      * should have resulted in a call to 'errabort ()', which in turn
  74.      * should have done 'DosExit (EXIT_PROCESS, 1)'.  If you see the
  75.      * following message, you may be programming in the Twilight Zone...
  76.      */
  77.     usRC = DosWrite (STDERR, szHuh, sizeof (szHuh) - 1, &cbBytes) ;
  78.     DosExit (EXIT_PROCESS, 1) ;
  79. }
  80.