home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / unpost / part06 / utils.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-18  |  3.7 KB  |  129 lines

  1. /******************************************************************************
  2. * Module    :   Utility Routines --- Replacement functions for compiler
  3. *               library functions.  This module exists to avoid having to
  4. *               if def the hell out of the code for systems that, for
  5. *               example, have bcopy, but not memmove.
  6. *
  7. * Author    :   John W. M. Stevens
  8. ******************************************************************************/
  9.  
  10. #include    "compiler.h"
  11.  
  12. #include    "utils.h"
  13.  
  14. /*-----------------------------------------------------------------------------
  15. | Routine   :   StrDup() --- String duplication function.
  16. |
  17. | Inputs    :   Str - A pointer to the string to duplicate.
  18. -----------------------------------------------------------------------------*/
  19.  
  20. char    *StrDup(char    *Str)
  21. {
  22.     register    int     StrLen;
  23.     auto        char    *Tmp;
  24.     extern      FILE    *ErrFile;
  25.  
  26.     /*  Determine lenght of string to duplicate.    */
  27.     StrLen = strlen( Str ) + 1;
  28.  
  29.     /*  Allocate the memory.    */
  30.     if ((Tmp = calloc(1, StrLen)) == NULL)
  31.     {
  32.         fprintf(ErrFile,
  33.                 "%s %d : Error - out of memory.\n",
  34.                 __FILE__,
  35.                 __LINE__);
  36.         exit( 1 );
  37.     }
  38.  
  39.     /*  Copy string contents in.    */
  40.     strcpy(Tmp, Str);
  41.     return( Tmp );
  42. }
  43.  
  44. /*-----------------------------------------------------------------------------
  45. | Routine   :   FileExists() --- Check to see if a file already exists.
  46. |
  47. | Inputs    :   FlName  - Name of file.
  48. |
  49. | Returns   :   Returns zero for file does not exist, non-zero for it does.
  50. -----------------------------------------------------------------------------*/
  51.  
  52. int     FileExists(char     *FlName)
  53. {
  54.     auto    FILE    *fp;
  55.  
  56.     /*  Attempt to open the file as read only to determine if it exists.
  57.     *   Don't you wish that ANSI C had a standard library function for
  58.     *   this?
  59.     */
  60.     if ((fp = fopen(FlName, TXT_READ)) != NULL)
  61.     {
  62.         /*  It exists, so close it and return.  */
  63.         fclose( fp );
  64.         return( 1 );
  65.     }
  66.     return( 0 );
  67. }
  68.  
  69. /*-----------------------------------------------------------------------------
  70. | Routine   :   MemCopy() --- Copy memory.
  71. |
  72. | Inputs    :   Dest    - Destination address.
  73. |               Source  - Source address.
  74. |               NoBytes - Number of bytes to copy.
  75. -----------------------------------------------------------------------------*/
  76.  
  77. void    MemCopy(void    *Dest,
  78.                 void    *Source,
  79.                 int     NoBytes)
  80. {
  81.     register    int     i;
  82.     auto        char    *d;
  83.     auto        char    *s;
  84.  
  85.     /*  Copy bytes. */
  86.     for (d = (char *) Dest, s = (char *) Source, i = 0;
  87.          i < NoBytes;
  88.          i++)
  89.         *d++ = *s++;
  90. }
  91.  
  92. /*-----------------------------------------------------------------------------
  93. | Routine   :   MemMove() --- Move memory, taking into account possible
  94. |               overlap.
  95. |
  96. | Inputs    :   Dest    - Destination address.
  97. |               Source  - Source address.
  98. |               NoBytes - Number of bytes to copy.
  99. -----------------------------------------------------------------------------*/
  100.  
  101. void    MemMove(void    *Dest,
  102.                 void    *Source,
  103.                 int     NoBytes)
  104. {
  105.     register    int     i;
  106.     auto        char    *d;
  107.     auto        char    *s;
  108.  
  109.     /*  Pointer conversion. */
  110.     d = (char *) Dest;
  111.     s = (char *) Source;
  112.  
  113.     /*  Check copy direction.   */
  114.     if (d < s)
  115.     {
  116.         /*  Copy up.    */
  117.         for (i = 0; i < NoBytes; i++)
  118.             *d++ = *s++;
  119.     }
  120.     else if (d > s)
  121.     {
  122.         /*  Copy down.  */
  123.         for (d += NoBytes - 1, s += NoBytes - 1, i = 0;
  124.              i < NoBytes;
  125.              i++)
  126.             *d-- = *s--;
  127.     }
  128. }
  129.