home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / eatest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-23  |  4.2 KB  |  191 lines

  1. /* eatest.c (emx+gcc) */
  2.  
  3. #define INCL_DOSFILEMGR
  4. #include <os2.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <getopt.h>
  9. #include <io.h>
  10. #include <sys/ea.h>
  11.  
  12. #define FALSE 0
  13. #define TRUE  1
  14.  
  15. enum actions
  16. {
  17.   ACTION_NONE,
  18.   ACTION_ADD,
  19.   ACTION_SHOW,
  20.   ACTION_REMOVE
  21. };
  22.  
  23. static enum actions action = ACTION_NONE;
  24.  
  25.  
  26. static void do_add (const char *path, const char *name, const char *value)
  27. {
  28.   int i, size;
  29.   struct _ea ea;
  30.   char *buf;
  31.  
  32.   size = strlen (value);
  33.   buf = malloc (size + 4);
  34.   if (buf == NULL)
  35.     {
  36.       fprintf (stderr, "Out of memory\n");
  37.       exit (2);
  38.     }
  39.   ((USHORT *)buf)[0] = EAT_ASCII;
  40.   ((USHORT *)buf)[1] = size;
  41.   memcpy (buf+4, value, size);
  42.   ea.flags = 0;
  43.   ea.size = size + 4;
  44.   ea.value = buf;
  45.   if (strcmp (path, "-") == 0)
  46.     i = _ea_put (&ea, NULL, fileno (stdout), name);
  47.   else
  48.     i = _ea_put (&ea, path, 0, name);
  49.   if (i < 0)
  50.     {
  51.       perror (path);
  52.       exit (2);
  53.     }
  54.   free (buf);
  55. }
  56.  
  57.  
  58. static void do_remove (const char *path, const char *name)
  59. {
  60.   int i;
  61.  
  62.   if (strcmp (path, "-") == 0)
  63.     i = _ea_remove (NULL, fileno (stdout), name);
  64.   else
  65.     i = _ea_remove (path, 0, name);
  66.   if (i < 0)
  67.     perror (path);
  68. }
  69.  
  70.  
  71. static void do_show (const char *path, const char *name)
  72. {
  73.   struct _ea ea;
  74.   int i;
  75.  
  76.   if (strcmp (path, "-") == 0)
  77.     i = _ea_get (&ea, NULL, fileno (stdin), name);
  78.   else
  79.     i = _ea_get (&ea, path, 0, name);
  80.   if (i < 0)
  81.     perror (path);
  82.   else
  83.     {
  84.       printf ("%s: %s: ", path, name);
  85.       if (ea.size >= 4)
  86.         {
  87.           switch (*(USHORT *)ea.value)
  88.             {
  89.             case EAT_ASCII:
  90.               printf ("\"");
  91.               fwrite (ea.value+4, ea.size-4, 1, stdout);
  92.               printf ("\"\n");
  93.               break;
  94.             case EAT_BINARY:
  95.               printf ("%d bytes of binary data\n", ea.size);
  96.               break;
  97.             case EAT_BITMAP:
  98.               printf ("%d bytes of bitmap data\n", ea.size);
  99.               break;
  100.             case EAT_METAFILE:
  101.               printf ("%d bytes of metafile data\n", ea.size);
  102.               break;
  103.             case EAT_ICON:
  104.               printf ("%d bytes of icon data\n", ea.size);
  105.               break;
  106.             case EAT_EA:
  107.               printf ("%d bytes of associated data\n", ea.size);
  108.               break;
  109.             case EAT_MVMT:
  110.             case EAT_MVST:
  111.             case EAT_ASN1:
  112.               printf ("%d bytes of multivalue data\n", ea.size);
  113.               break;
  114.             default:
  115.               printf ("%d bytes\n", ea.size);
  116.               break;
  117.             }
  118.         }
  119.       else
  120.         printf ("%d bytes\n", ea.size);
  121.       _ea_free (&ea);
  122.     }
  123. }
  124.  
  125.  
  126. static void usage (void)
  127. {
  128.   fputs ("Usage: eatool -s <file> <name>\n", stderr);
  129.   fputs ("       eatool -a <file> <name> <value>\n", stderr);
  130.   fputs ("       eatool -r <file> <name>\n", stderr);
  131.   fputs ("Options:\n", stderr);
  132.   fputs ("  -s   Show extended attribute\n", stderr);
  133.   fputs ("  -a   Add extended attribute\n", stderr);
  134.   fputs ("  -r   Remove extended attribute\n", stderr);
  135.   exit (1);
  136. }
  137.  
  138.  
  139. static void set_action (enum actions a)
  140. {
  141.   if (action != ACTION_NONE)
  142.     usage ();
  143.   action = a;
  144. }
  145.  
  146.  
  147. int main (int argc, char *argv[])
  148. {
  149.   int c;
  150.  
  151.   opterr = FALSE;
  152.   while ((c = getopt (argc, argv, "ars")) != EOF)
  153.     {
  154.       switch (c)
  155.         {
  156.         case 'a':
  157.           set_action (ACTION_ADD);
  158.           break;
  159.         case 'r':
  160.           set_action (ACTION_REMOVE);
  161.           break;
  162.         case 's':
  163.           set_action (ACTION_SHOW);
  164.           break;
  165.         default:
  166.           usage ();
  167.         }
  168.     }
  169.   switch (action)
  170.     {
  171.     case ACTION_ADD:
  172.       if (argc - optind != 3)
  173.         usage ();
  174.       do_add (argv[optind+0], argv[optind+1], argv[optind+2]);
  175.       break;
  176.     case ACTION_REMOVE:
  177.       if (argc - optind != 2)
  178.         usage ();
  179.       do_remove (argv[optind+0], argv[optind+1]);
  180.       break;
  181.     case ACTION_SHOW:
  182.       if (argc - optind != 2)
  183.         usage ();
  184.       do_show (argv[optind+0], argv[optind+1]);
  185.       break;
  186.     default:
  187.       usage ();
  188.     }
  189.   return (0);
  190. }
  191.