home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ealib.zip / ealib.c next >
C/C++ Source or Header  |  1995-02-02  |  10KB  |  341 lines

  1. /* -*-C-*-
  2.  
  3. $Id$
  4.  
  5. Copyright (c) 1995 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #define INCL_DOS
  36. #define INCL_DOSERRORS
  37. #include <os2.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41.  
  42. #include "ealib.h"
  43.  
  44. static ea_list *    read_eas (ULONG, PVOID);
  45. static ea_binding * read_ea_by_index (ULONG, PVOID, ULONG);
  46. static ea_binding * read_ea_by_name (ULONG, PVOID, PSZ);
  47. static PDENA2       read_dena_by_index (ULONG, PVOID, ULONG);
  48. static ea_binding * get_ea_value (ULONG, PVOID, PDENA2);
  49. static void         setup_query_ea_info (PDENA2, PEAOP2);
  50. static ea_binding * convert_feal_to_binding (PFEA2LIST);
  51. static void         write_eas (ULONG, PVOID, ea_list *);
  52. static void         write_ea (ULONG, PVOID, ea_binding *);
  53. static PFEA2LIST    convert_binding_to_feal (ea_binding *);
  54. static void         fatal_api_call_error (APIRET, const char *);
  55. static void *       xmalloc (unsigned int);
  56.  
  57. #define API_CALL(proc, args) do                        \
  58. {                                    \
  59.   APIRET API_CALL_rc = (proc args);                    \
  60.   if (API_CALL_rc != NO_ERROR)                        \
  61.     fatal_api_call_error (API_CALL_rc, #proc);                \
  62. } while (0)
  63.  
  64. #define MALLOC xmalloc
  65. #define FREE(ptr) free ((void *) (ptr))
  66.  
  67. ea_list *
  68. read_file_eas (HFILE file_handle)
  69. {
  70.   return (read_eas (ENUMEA_REFTYPE_FHANDLE, (& file_handle)));
  71. }
  72.  
  73. ea_list *
  74. read_path_eas (PSZ path)
  75. {
  76.   return (read_eas (ENUMEA_REFTYPE_PATH, path));
  77. }
  78.  
  79. ea_binding *
  80. read_file_ea_by_index (HFILE file_handle, ULONG index)
  81. {
  82.   return (read_ea_by_index (ENUMEA_REFTYPE_FHANDLE, (& file_handle), index));
  83. }
  84.  
  85. ea_binding *
  86. read_path_ea_by_index (PSZ path, ULONG index)
  87. {
  88.   return (read_ea_by_index (ENUMEA_REFTYPE_PATH, path, index));
  89. }
  90.  
  91. ea_binding *
  92. read_file_ea_by_name (HFILE file_handle, PSZ name)
  93. {
  94.   return (read_ea_by_name (ENUMEA_REFTYPE_FHANDLE, (& file_handle), name));
  95. }
  96.  
  97. ea_binding *
  98. read_path_ea_by_name (PSZ path, PSZ name)
  99. {
  100.   return (read_ea_by_name (ENUMEA_REFTYPE_PATH, path, name));
  101. }
  102.  
  103. void
  104. write_file_eas (HFILE file_handle, ea_list * list)
  105. {
  106.   write_eas (ENUMEA_REFTYPE_FHANDLE, (& file_handle), list);
  107. }
  108.  
  109. void
  110. write_path_eas (PSZ path, ea_list * list)
  111. {
  112.   write_eas (ENUMEA_REFTYPE_PATH, path, list);
  113. }
  114.  
  115. void
  116. write_file_ea (HFILE file_handle, ea_binding * binding)
  117. {
  118.   write_ea (ENUMEA_REFTYPE_FHANDLE, (& file_handle), binding);
  119. }
  120.  
  121. void
  122. write_path_ea (PSZ path, ea_binding * binding)
  123. {
  124.   write_ea (ENUMEA_REFTYPE_PATH, path, binding);
  125. }
  126.  
  127. void
  128. free_ea_list (ea_list * list)
  129. {
  130.   while (list != 0)
  131.     {
  132.       ea_list * next = (EA_LIST_NEXT (list));
  133.       free_ea_binding (EA_LIST_BINDING (list));
  134.       FREE (list);
  135.       list = next;
  136.     }
  137. }
  138.  
  139. void
  140. free_ea_binding (ea_binding * binding)
  141. {
  142.   FREE (EA_BINDING_NAME (binding));
  143.   if ((EA_BINDING_VALUE (binding)) != 0)
  144.     FREE (EA_BINDING_VALUE (binding));
  145.   FREE (binding);
  146. }
  147.  
  148. static ea_list *
  149. read_eas (ULONG type, PVOID pfile)
  150. {
  151.   ULONG index = 1;
  152.   ea_list * head = 0;
  153.   ea_list * tail = 0;
  154.   while (1)
  155.     {
  156.       ea_binding * binding = (read_ea_by_index (type, pfile, index));
  157.       if (binding == 0)
  158.     break;
  159.       {
  160.     ea_list * list = (MALLOC (sizeof (ea_list)));
  161.     (EA_LIST_BINDING (list)) = binding;
  162.     (EA_LIST_NEXT (list)) = 0;
  163.     if (head == 0)
  164.       head = list;
  165.     else
  166.       (EA_LIST_NEXT (tail)) = list;
  167.     tail = list;
  168.       }
  169.       index += 1;
  170.     }
  171.   return (head);
  172. }
  173.  
  174. static ea_binding *
  175. read_ea_by_index (ULONG type, PVOID pfile, ULONG index)
  176. {
  177.   PDENA2 dena = (read_dena_by_index (type, pfile, index));
  178.   return ((dena == 0) ? 0 : (get_ea_value (type, pfile, dena)));
  179. }
  180.  
  181. static ea_binding *
  182. read_ea_by_name (ULONG type, PVOID pfile, PSZ name)
  183. {
  184.   ULONG index = 1;
  185.   while (1)
  186.     {
  187.       PDENA2 dena = (read_dena_by_index (type, pfile, index));
  188.       if (dena == 0)
  189.     return (0);
  190.       if ((strcmp (name, (dena -> szName))) == 0)
  191.     return (get_ea_value (type, pfile, dena));
  192.       FREE (dena);
  193.       index += 1;
  194.     }
  195. }
  196.  
  197. static PDENA2
  198. read_dena_by_index (ULONG type, PVOID pfile, ULONG index)
  199. {
  200.   ULONG count = 1;
  201.   PDENA2 dena = (MALLOC (500));    /* 500 is magic -- IBM doesn't explain. */
  202.   API_CALL (DosEnumAttribute, (type, pfile, index, dena, 500, (& count),
  203.                    ENUMEA_LEVEL_NO_VALUE));
  204.   if (count == 0)
  205.     {
  206.       FREE (dena);
  207.       return (0);
  208.     }
  209.   else
  210.     return (dena);
  211. }
  212.  
  213. static ea_binding *
  214. get_ea_value (ULONG type, PVOID pfile, PDENA2 dena)
  215. {
  216.   ULONG level = FIL_QUERYEASFROMLIST;
  217.   EAOP2 eaop;
  218.   ULONG size = (sizeof (eaop));
  219.   setup_query_ea_info (dena, (& eaop));
  220.   if (type == ENUMEA_REFTYPE_FHANDLE)
  221.     API_CALL (DosQueryFileInfo, ((* ((PHFILE) pfile)), level, (& eaop), size));
  222.   else
  223.     API_CALL (DosQueryPathInfo, (pfile, level, (& eaop), size));
  224.   FREE (eaop . fpGEA2List);
  225.   return (convert_feal_to_binding (eaop . fpFEA2List));
  226. }
  227.  
  228. static void
  229. setup_query_ea_info (PDENA2 dena, PEAOP2 eaop)
  230. {
  231.   unsigned int geal_size = ((sizeof (GEA2LIST)) + (dena -> cbName));
  232.   unsigned int feal_size
  233.     = ((sizeof (FEA2LIST)) + (dena -> cbName) + (dena -> cbValue));
  234.   (eaop -> fpGEA2List) = (MALLOC (geal_size));
  235.   ((eaop -> fpGEA2List) -> cbList) = geal_size;
  236.   (eaop -> fpFEA2List) = (MALLOC (feal_size));
  237.   ((eaop -> fpFEA2List) -> cbList) = feal_size;
  238.   (eaop -> oError) = 0;
  239.   {
  240.     PGEA2 gea = (& (((eaop -> fpGEA2List) -> list) [0]));
  241.     (gea -> oNextEntryOffset) = 0;
  242.     (gea -> cbName) = (dena -> cbName);
  243.     strcpy ((gea -> szName), (dena -> szName));
  244.   }
  245.   FREE (dena);
  246. }
  247.  
  248. static ea_binding *
  249. convert_feal_to_binding (PFEA2LIST feal)
  250. {
  251.   PFEA2 fea = (& ((feal -> list) [0]));
  252.   ea_binding * binding = (MALLOC (sizeof (ea_binding)));
  253.   (EA_BINDING_FLAGS (binding)) = (fea -> fEA);
  254.   (EA_BINDING_NAME_LENGTH (binding)) = (fea -> cbName);
  255.   (EA_BINDING_VALUE_LENGTH (binding)) = (fea -> cbValue);
  256.   (EA_BINDING_NAME (binding)) = (MALLOC ((fea -> cbName) + 1));
  257.   strcpy ((EA_BINDING_NAME (binding)), (fea -> szName));
  258.   (EA_BINDING_VALUE (binding)) = (MALLOC (fea -> cbValue));
  259.   memcpy ((EA_BINDING_VALUE (binding)),
  260.       (& ((fea -> szName) [(fea -> cbName) + 1])),
  261.       (fea -> cbValue));
  262.   FREE (feal);
  263.   return (binding);
  264. }
  265.  
  266. static void
  267. write_eas (ULONG type, PVOID pfile, ea_list * list)
  268. {
  269.   while (list != 0)
  270.     {
  271.       write_ea (type, pfile, (EA_LIST_BINDING (list)));
  272.       list = (EA_LIST_NEXT (list));
  273.     }
  274. }
  275.  
  276. static void
  277. write_ea (ULONG type, PVOID pfile, ea_binding * binding)
  278. {
  279.   ULONG level = FIL_QUERYEASIZE;
  280.   EAOP2 eaop;
  281.   ULONG size = (sizeof (eaop));
  282.   (eaop . fpGEA2List) = 0;
  283.   (eaop . fpFEA2List) = (convert_binding_to_feal (binding));
  284.   (eaop . oError) = 0;
  285.   if (type == ENUMEA_REFTYPE_FHANDLE)
  286.     API_CALL (DosSetFileInfo, ((* ((PHFILE) pfile)), level, (& eaop), size));
  287.   else
  288.     API_CALL (DosSetPathInfo, (pfile, level, (& eaop), size, DSPI_WRTTHRU));
  289.   FREE (eaop . fpFEA2List);
  290. }
  291.  
  292. static PFEA2LIST
  293. convert_binding_to_feal (ea_binding * binding)
  294. {
  295.   unsigned int feal_size
  296.     = ((sizeof (FEA2LIST))
  297.        + (EA_BINDING_NAME_LENGTH (binding))
  298.        + (EA_BINDING_VALUE_LENGTH (binding)));
  299.   PFEA2LIST feal = (MALLOC (feal_size));
  300.   PFEA2 fea = (& ((feal -> list) [0]));
  301.   (feal -> cbList) = feal_size;
  302.   (fea -> oNextEntryOffset) = 0;
  303.   (fea -> fEA) = (EA_BINDING_FLAGS (binding));
  304.   (fea -> cbName) = (EA_BINDING_NAME_LENGTH (binding));
  305.   (fea -> cbValue) = (EA_BINDING_VALUE_LENGTH (binding));
  306.   strcpy ((fea -> szName), (EA_BINDING_NAME (binding)));
  307.   if ((EA_BINDING_VALUE (binding)) != 0)
  308.     memcpy ((& ((fea -> szName) [(fea -> cbName) + 1])),
  309.         (EA_BINDING_VALUE (binding)),
  310.         (fea -> cbValue));
  311.   return (feal);
  312. }
  313.  
  314. void (* ea_error_hook) (APIRET, const char *);
  315.  
  316. static void
  317. fatal_api_call_error (APIRET rc, const char * name)
  318. {
  319.   if (ea_error_hook != 0)
  320.     (* ea_error_hook) (rc, name);
  321.   else
  322.     {
  323.       fprintf (stderr, "Fatal error %d in procedure %s.\n", rc, name);
  324.       fflush (stderr);
  325.     }
  326.   exit (EXIT_FAILURE);
  327. }
  328.  
  329. static void *
  330. xmalloc (unsigned int nbytes)
  331. {
  332.   void * result = (malloc (nbytes));
  333.   if (result == 0)
  334.     {
  335.       fprintf (stderr, "Unable to allocate %d bytes of memory.\n", nbytes);
  336.       fflush (stderr);
  337.       exit (EXIT_FAILURE);
  338.     }
  339.   return (result);
  340. }
  341.