home *** CD-ROM | disk | FTP | other *** search
/ Total Meltdown / dukenukemtotalmeltdown.img / util / dukenet / global.c < prev    next >
C/C++ Source or Header  |  1995-04-10  |  5KB  |  237 lines

  1. #include "global.h"
  2. #include "commit.h"
  3.  
  4. #include <malloc.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <conio.h>
  8. #include <ctype.h>
  9. #include <io.h>
  10. #include <sys\stat.h>
  11. #include <dir.h>
  12. #include <stdarg.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <dos.h>
  17.  
  18. /*
  19. =================
  20. =
  21. = Error
  22. =
  23. = For abnormal program terminations
  24. =
  25. =================
  26. */
  27.  
  28. void Error (char *error, ...)
  29.    {
  30.     va_list argptr;
  31.  
  32.    Shutdown();
  33.     if (error)
  34.       {
  35.         va_start (argptr,error);
  36.         vprintf (error,argptr);
  37.         va_end (argptr);
  38.         printf ("\n\n");
  39.        }
  40.  
  41.     exit (error != (char *) NULL);
  42.    }
  43.  
  44. /*
  45. =================
  46. =
  47. = CheckParm
  48. =
  49. = Checks for the given parameter in the program's command line arguments
  50. =
  51. = Returns the argument number (1 to argc-1) or 0 if not present
  52. =
  53. =================
  54. */
  55.  
  56. int CheckParm (char *check)
  57.    {
  58.     int             i;
  59.  
  60.     for (i = 1;i<_argc;i++)
  61.         if ( !stricmp(check,_argv[i]) )
  62.             return i;
  63.  
  64.     return 0;
  65.    }
  66.  
  67. //******************************************************************************
  68. //
  69. // SafeOpenWrite ()
  70. //
  71. //******************************************************************************
  72.  
  73. int SafeOpenWrite (char *filename)
  74.    {
  75.     int    handle;
  76.  
  77.     handle = open(filename,O_RDWR | O_BINARY | O_CREAT | O_TRUNC
  78.     , S_IREAD | S_IWRITE);
  79.  
  80.     if (handle == -1)
  81.         Error ("Error opening %s: %s",filename,strerror(errno));
  82.  
  83.     return handle;
  84.    }
  85.  
  86. //******************************************************************************
  87. //
  88. // SafeOpenRead ()
  89. //
  90. //******************************************************************************
  91.  
  92. int SafeOpenRead (char *filename)
  93.    {
  94.     int    handle;
  95.  
  96.     handle = open(filename,O_RDONLY | O_BINARY);
  97.  
  98.     if (handle == -1)
  99.         Error ("Error opening %s: %s",filename,strerror(errno));
  100.  
  101.     return handle;
  102.    }
  103.  
  104.  
  105. //******************************************************************************
  106. //
  107. // SafeRead ()
  108. //
  109. //******************************************************************************
  110.  
  111. void SafeRead (int handle, void *buffer, long count)
  112.    {
  113.     long iocount;
  114.  
  115.     while (count)
  116.        {
  117.         iocount = count > 0x8000 ? 0x8000 : count;
  118.         if (read (handle,buffer,(int)iocount) != iocount)
  119.             Error ("File read failure");
  120.         buffer = (void *)( (BYTE *)buffer + (int)iocount );
  121.         count -= iocount;
  122.         }
  123.     }
  124.  
  125.  
  126. //******************************************************************************
  127. //
  128. // SafeWrite ()
  129. //
  130. //******************************************************************************
  131.  
  132. void SafeWrite (int handle, void *buffer, long count)
  133.     {
  134.     long    iocount;
  135.  
  136.     while (count)
  137.         {
  138.         iocount = count > 0x8000 ? 0x8000 : count;
  139.         if (write (handle,buffer,(int)iocount) != iocount)
  140.             Error ("File write failure");
  141.         buffer = (void *)( (BYTE *)buffer + (int)iocount );
  142.         count -= iocount;
  143.        }
  144.    }
  145.  
  146. //******************************************************************************
  147. //
  148. // SafeMalloc ()
  149. //
  150. //******************************************************************************
  151.  
  152. void *SafeMalloc (long size)
  153.    {
  154.     void *ptr;
  155.  
  156.     ptr = malloc ((short)size);
  157.  
  158.     if (!ptr)
  159.         Error ("Malloc failure for %lu bytes",size);
  160.  
  161.     return ptr;
  162.    }
  163.  
  164.  
  165. //******************************************************************************
  166. //
  167. // LoadFile ()
  168. //
  169. //******************************************************************************
  170.  
  171. long    LoadFile (char *filename, void **bufferptr)
  172.    {
  173.    int  handle;
  174.    long length;
  175.  
  176.    handle = SafeOpenRead (filename);
  177.    length = filelength (handle);
  178.    *bufferptr = SafeMalloc(length);
  179.    SafeRead (handle,*bufferptr, length);
  180.    close (handle);
  181.    return length;
  182.    }
  183.  
  184. //******************************************************************************
  185. //
  186. // ShortSwap ()
  187. //
  188. //******************************************************************************
  189.  
  190. WORD ShortSwap (WORD i)
  191.    {
  192.    return ((i&255)<<8) + ((i>>8)&255);
  193.    }
  194.  
  195. #if 0
  196. /*
  197. =================
  198. =
  199. = PrintXYString
  200. =
  201. =================
  202. */
  203.  
  204. void PrintXYString (int x, int y, char *str)
  205.     {
  206.     char *s;
  207.     BYTE far *screen;
  208.  
  209.     s = str;
  210.     screen = (BYTE far *)MK_FP(0xb800,0) + (y*160) + (x<<1);
  211.  
  212.     while (*s)
  213.       {
  214.       *screen = *s;
  215.       x++;
  216.       s++;
  217.       if (x==80)
  218.          {
  219.          x=0;
  220.          y++;
  221.          if (y==25)
  222.             {
  223.             y=0;
  224.             }
  225.             screen = (BYTE far *)MK_FP(0xb800,0) + (y*160) + (x<<1);
  226.             }
  227.         else
  228.             {
  229.             screen += 2;
  230.             }
  231.  
  232.         while ((*s < 32) && (*s > 0))
  233.             s++;
  234.         }
  235.     }
  236.  
  237. #endif