home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / pax / 1 / mem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-07  |  3.1 KB  |  136 lines

  1. /* $Source: /u/mark/src/pax/RCS/mem.c,v $
  2.  *
  3.  * $Revision: 1.1 $
  4.  *
  5.  * mem.c - memory allocation and manipulation functions
  6.  *
  7.  * DESCRIPTION
  8.  *
  9.  *    These routines are provided for higher level handling of the UNIX
  10.  *    memory allocation functions.
  11.  *
  12.  * AUTHOR
  13.  *
  14.  *     Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  15.  *
  16.  *
  17.  * Sponsored by The USENIX Association for public distribution. 
  18.  *
  19.  * Copyright (c) 1989 Mark H. Colburn.
  20.  * All rights reserved.
  21.  *
  22.  * Redistribution and use in source and binary forms are permitted
  23.  * provided that the above copyright notice is duplicated in all such 
  24.  * forms and that any documentation, advertising materials, and other 
  25.  * materials related to such distribution and use acknowledge that the 
  26.  * software was developed * by Mark H. Colburn and sponsored by The 
  27.  * USENIX Association. 
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  30.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  31.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  32.  *
  33.  * $Log:    mem.c,v $
  34.  * Revision 1.1  88/12/23  18:02:17  mark
  35.  * Initial revision
  36.  * 
  37.  */
  38.  
  39. #ifndef lint
  40. static char *ident = "$Id: mem.c,v 1.1 88/12/23 18:02:17 mark Rel $";
  41. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  42. #endif /* ! lint */
  43.  
  44.  
  45. /* Headers */
  46.  
  47. #include "pax.h"
  48.  
  49.  
  50. /* mem_get - allocate memory
  51.  *
  52.  * DESCRIPTION
  53.  *
  54.  *    Mem_get attempts to allocate a block of memory using the malloc
  55.  *    function call.  In the event that the memory is not available, 
  56.  *    mem_get will display an "Out of memory" message for the user
  57.  *    the first time it encounters the an out of memory situation.
  58.  *    Subsequent calls to mem_get may fail, but no message will be
  59.  *    printed.
  60.  *
  61.  * PARAMETERS
  62.  *
  63.  *    uint len    - The amount of memory to allocate
  64.  *
  65.  * RETURNS
  66.  *
  67.  *    Normally returns the pointer to the newly allocated memory.  If
  68.  *    an error occurss, NULL is returned, and an error message is
  69.  *    printed.
  70.  *
  71.  * ERRORS
  72.  *
  73.  *    ENOMEM    No memory is available 
  74.  */
  75.  
  76. #ifdef __STDC__
  77.  
  78. char *mem_get(uint len)
  79.  
  80. #else
  81.  
  82. char *mem_get(len)
  83. uint            len;        /* amount of memory to get */
  84.  
  85. #endif
  86. {
  87.     char           *mem;
  88.     static short    outofmem = 0;
  89.  
  90.     if ((mem = (char *)malloc(len)) == NULL && !outofmem) {
  91.     outofmem++;
  92.     warn("mem_get()", "Out of memory");
  93.     }
  94.     return (mem);
  95. }
  96.  
  97.  
  98. /* mem_str - duplicate a string into dynamic memory
  99.  *
  100.  * DESCRIPTION
  101.  *
  102.  *    Mem_str attempts to make a copy of string.  It allocates space for
  103.  *    the string, and if the allocation was successfull, copies the old
  104.  *    string into the newly allocated space.
  105.  *
  106.  * PARAMETERS
  107.  *
  108.  *    char *str     - string to make a copy of 
  109.  *
  110.  * RETURNS
  111.  *
  112.  *    Normally returns a pointer to a new string at least as large
  113.  *    as strlen(str) + 1, which contains a copy of the the data 
  114.  *    passed in str, plus a null terminator.  Returns (char *)NULL 
  115.  *    if enough memory to make a copy of str is not available.
  116.  */
  117.  
  118. #ifdef __STDC__
  119.  
  120. char *mem_str(char *str)
  121.  
  122. #else
  123.  
  124. char *mem_str(str)
  125. char           *str;        /* string to make a copy of */
  126.  
  127. #endif
  128. {
  129.     char           *mem;
  130.  
  131.     if (mem = mem_get((uint) strlen(str) + 1)) {
  132.     strcpy(mem, str);
  133.     }
  134.     return (mem);
  135. }
  136.