home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / LIBX / os2ea_op.c < prev    next >
C/C++ Source or Header  |  1992-09-24  |  4KB  |  170 lines

  1. /*
  2.  * $Id: os2ea_op.c,v 1.8 1992/07/24 11:49:52 ak Exp $
  3.  *
  4.  * Extended Attribute Management for OS/2.
  5.  * - Operations on EAs.
  6.  *
  7.  * A.Kaiser    Mai 91
  8.  *
  9.  * $Log: os2ea_op.c,v $
  10.  * Revision 1.8  1992/07/24  11:49:52  ak
  11.  * Fixed memory leak: lists were not freed when load_ea returned 0.
  12.  *
  13.  * OS/2 2.0 ea buffer not contains a magic number field.
  14.  * Type of EA buffer changed from FEAList to EABuf.
  15.  *
  16.  * Revision 1.7  1992/03/05  20:28:38  ak
  17.  * Bugfix.
  18.  *
  19.  * Revision 1.6  1992/02/26  20:53:31  ak
  20.  * OS/2 2.0.
  21.  *
  22.  * Revision 1.5  1992/02/14  18:40:44  ak
  23.  * *** empty log message ***
  24.  *
  25.  * Revision 1.4  1992/01/03  14:37:29  ak
  26.  * $Header: h:/SRC.SSB/lib/libx/os2ea_op.c,v 1.8 1992/07/24 11:49:52 ak Exp $Id:
  27.  *
  28.  * Revision 1.3  1992/01/03  14:03:53  ak
  29.  * Zortech fixes & updates.
  30.  *
  31.  * Revision 1.2  1991/12/12  20:21:20  ak
  32.  * RCSID fixes.
  33.  * mk(s)temp fixed.
  34.  *
  35.  * Revision 1.1.1.1  1991/12/12  17:15:31  ak
  36.  * Initial checkin of server source, modified to contain RCS IDs.
  37.  *
  38.  * Revision 1.1  1991/12/12  17:15:26  ak
  39.  * Initial revision
  40.  *
  41.  */
  42. #ifdef OS2
  43.  
  44. static char *rcsid = "$Id: os2ea_op.c,v 1.8 1992/07/24 11:49:52 ak Exp $";
  45.  
  46. #include <string.h>
  47. #include <stdlib.h>
  48. #define INCL_DOSFILEMGR
  49. #include <os2.h>
  50. #include <os2eattr.h>
  51.  
  52. #if OS2 >= 2
  53.  #ifndef _fmalloc
  54.   #define _fmalloc    malloc
  55.   #define _frealloc    realloc
  56.   #define _ffree    free
  57.   #define _fmemcpy    memcpy
  58.   #define _fmemcmp    memcmp
  59.  #endif
  60. #elif defined(__ZTC__)
  61.  #include <ztc.h>
  62. #else
  63.  #include <malloc.h>
  64.  #include <memory.h>
  65. #endif
  66.  
  67. #if OS2 >= 2
  68.   struct EABuf {
  69.       long    magic;
  70.       FEAList    ea;
  71.   };
  72. #else
  73.   struct EABuf {
  74.       FEAList    ea;
  75.   };
  76. #endif
  77.  
  78. static pFEA
  79. find_ea(pFEAList feaList, char *name)
  80. {
  81.     register pFEA    p;
  82.     pFEA        end;
  83.     register int    i, len;
  84.  
  85.     len = strlen(name);
  86.     WalkFEA(p, feaList)
  87.         if (p->cbName == len) {
  88.             if (_fmemcmp(szNameFEA(p), name, len) != 0)
  89.                 continue;
  90.             return p;
  91.         }
  92.     WalkFEAEnd(p, feaList)
  93.     return 0;
  94. }
  95.  
  96. int
  97. ea_get(pEABuf pea, char *name, void *value, int *length)
  98. {
  99.     pFEA p;
  100.  
  101.     if ((p = find_ea(&pea->ea, name)) == 0)
  102.         return 0;
  103.     if (p->cbName > *length)
  104.         return -1;
  105.     *length = p->cbValue;
  106.     _fmemcpy(value, aValueFEA(p), *length);
  107.     return 0x100 + p->fEA;
  108. }
  109.  
  110. #if OS2 < 2    /* too complicated in OS/2 2.0 */
  111.  
  112. pEABuf
  113. ea_set(pEABuf pea, char *name, void *value, unsigned length, unsigned flag)
  114. {
  115.     pFEA    p, q;
  116.     int    size, namlen, diff;
  117.  
  118.     namlen = strlen(name) + 1;
  119.     size = sizeof(FEAData) + namlen + length;
  120.     if (pea == 0) {
  121.         if ((pea = _fmalloc((size_t)(size + sizeof(ULONG)))) == 0)
  122.             return 0;
  123.         p = pea->ea.list;
  124.     } else {
  125.         if ((p = find_ea(&pea->ea, name)) == 0) {
  126.             diff = size;
  127.             p = (pFEA) EndOfEA(&pea->ea);
  128.         } else {
  129.             diff = length - p->cbValue;
  130.             if (diff == 0) {
  131.                 p->fEA = flag;
  132.                 _fmemcpy(aValueFEA(p), value, length);
  133.                 return pea;
  134.             }
  135.         }
  136.         if ((pea = _frealloc(pea, (size_t)(pea->ea.cbList + diff))) == 0)
  137.             return 0;
  138.         _fmemmove((PBYTE)p + diff, p, (PBYTE) EndOfEA(&pea->ea) - (PBYTE)p);
  139.     }
  140.     p->fEA = flag;
  141.     p->cbName = namlen;
  142.     p->cbValue = length;
  143.     _fmemcpy(szNameFEA(p), name, namlen);
  144.     _fmemcpy(aValueFEA(p), value, length);
  145.     pea->ea.cbList += diff;
  146.     return pea;
  147. }
  148.  
  149. pEABuf
  150. ea_remove(pEABuf pea, char *name)
  151. {
  152.     pFEA    p, q;
  153.     int    size;
  154.  
  155.     if ((p = find_ea(&pea->ea, name)) == 0)
  156.         return pea;
  157.     size = SizeFEA(p);
  158.     if ((pea = _frealloc(pea, (size_t)(pea->ea.cbList - size))) == 0)
  159.         return 0;
  160.     q = NextFEA(p);
  161.     if (q > p)
  162.         _fmemmove(p, q, (size_t)((PBYTE)EndOfEA(&pea->ea) - (PBYTE)q));
  163.     pea->ea.cbList -= size;
  164.     return pea;
  165. }
  166.  
  167. #endif    /* 2.0 */
  168. #endif
  169.  
  170.