home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / SETEA.ZIP / SETEA.C < prev    next >
Text File  |  1989-12-24  |  5KB  |  111 lines

  1. /*
  2.     SETEA.C     Assigns an ASCII extended attribute to a file.  
  3.                 Demonstrates use of the getEA() and putEA() 
  4.                 subroutines in GETPUTEA.C.
  5.  
  6.                 This program does not require Microsoft or IBM 
  7.                 Toolkit header files, but must be built using LINK
  8.                 and DOSCALLS.LIB from retail OS/2 version 1.2.  
  9.  
  10.                 Warning: EA names and values are case sensitive.
  11.  
  12.     Copyright (C) 1989 Ziff Davis Communications
  13.     PC Magazine * Ray Duncan, December 1989
  14.  
  15.     Compile:    cl -c /Zi setea.c
  16.                 cl -c /Zi getputea.c
  17.                 link setea+getputea,setea,,doscalls,setea.def;
  18.  
  19.     Usage:      setea filename.exe EAname=EAvalue
  20.  
  21.     Examples:   setea myprog.exe .TYPE=Executable
  22.                 setea myfile.txt ".TYPE=Plain Text"
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <memory.h>
  28. #include <malloc.h>
  29. #include <fcntl.h>
  30. #include <sys\types.h>
  31. #include <sys\stat.h>
  32. #include <io.h>
  33.                                             // EA predefined value types
  34. #define EAT_BINARY      0x0fffe             // Length-preceeded binary
  35. #define EAT_ASCII       0x0fffd             // Length-preceeded ASCII
  36. #define EAT_BITMAP      0x0fffb             // Length-preceeded bitmap
  37. #define EAT_METAFILE    0x0fffa             // Metafile
  38. #define EAT_ICON        0x0fff9             // Length-preceeded icon
  39. #define EAT_EA          0x0ffee             // ASCIIZ name of associated EA
  40. #define EAT_MVMT        0x0ffdf             // Multi-value multi-type
  41. #define EAT_MVST        0x0ffde             // Multi-value single-type
  42. #define EAT_ASN1        0x0ffdd             // ASN.1 field
  43.  
  44. #define MAXPATHNAME     260                 // max length of pathname
  45. #define MAXFILENAME     255                 // max length of filename
  46.  
  47. #define API unsigned extern far pascal      // OS/2 API function prototypes 
  48. API DosQPathInfo(void far *, unsigned, char far *, int, unsigned long);
  49.  
  50. struct _EAval * getEA(int, char *);         // GETPUTEA.C prototypes
  51. int putEA(int, char *, void *, unsigned, unsigned);
  52.  
  53. struct _EAval {                             // extended attribute value 
  54.         unsigned type;                      // EA value type
  55.         unsigned size;                      // length of EA variable data
  56.         char data[1]; } ;                   // actual data begins here
  57.  
  58. main(int argc, char *argv[])
  59. {
  60.     char EAnamebuf[80];                     // EA name from user
  61.     char EAvalbuf[80];                      // EA value from user
  62.     char pathnamebuf[MAXPATHNAME];          // fully qualified pathname
  63.     struct _EAval *pEAval;                  // scratch pointer
  64.     int handle;                             // handle for file  
  65.  
  66.     if(argc != 3)                           // check command line
  67.     {
  68.         printf("\nUsage:     setea filename.exe EAname=EAvalue\n");
  69.         printf("\nWarning:   Extended attribute names and values");
  70.         printf("\n           are case sensitive.  Use quotes to");
  71.         printf("\n           embed blanks in EA value.\n");
  72.         printf("\nExamples:  setea myprog.exe .TYPE=Executable");
  73.         printf("\n           setea myfile.txt \".TYPE=Plain Text\"\n");
  74.         exit(1);
  75.     }
  76.                                             // open the file
  77.     if((handle = open(argv[1], O_BINARY | O_RDWR)) == -1)
  78.     {
  79.         printf("\nsetea: file not found or read-only\n");
  80.         exit(2);
  81.     }
  82.                                             // get fully qualified pathname
  83.     if(DosQPathInfo(argv[1], 5, pathnamebuf, MAXPATHNAME, 0L))
  84.     {
  85.         printf("\nsetea: can't qualify pathname\n");
  86.         exit(3);
  87.     }
  88.                                             // parse EA name and value
  89.     strcpy(EAnamebuf, strtok(argv[2], " =\x0a"));
  90.     strcpy(EAvalbuf, strtok(NULL, "\x0a"));
  91.  
  92.                                             // write new EA to disk
  93.     if(putEA(handle, EAnamebuf, EAvalbuf, strlen(EAvalbuf), EAT_ASCII))
  94.     {
  95.         printf("\nsetea: can't set extended attribute!\n");
  96.         exit(4);
  97.     }
  98.                                             // read new EA back again
  99.     if((pEAval = getEA(handle, EAnamebuf)) == NULL)
  100.     {
  101.         printf("\nsetea: can't reread extended attribute!\n");
  102.         exit(5);
  103.     }
  104.                                             // display EA name and value
  105.     printf("\nFile name:\t %s\nEA name:\t %s\nEA value:\t %.*s\n",
  106.            strlwr(pathnamebuf), EAnamebuf, pEAval->size, pEAval->data);
  107.  
  108.     free(pEAval);                           // release heap memory
  109. }
  110.  
  111.