home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
COMMENT.ZIP
/
COMMENT.C
next >
Wrap
Text File
|
1991-11-20
|
9KB
|
261 lines
/* This utility will attach a COMMENT EA to a file
Portions of this code were taken from a PC Magazine
program written by Ray Duncan - SETEA.
Those portions are copywrited by PC Magazine */
#define INCL_DOSFILEMGR
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h> /* needed for open, close, read, write, */
/* filelength, and eof */
#include <fcntl.h> /* needed for open mode arguments */
#include <sys\types.h> /* needed for file types */
#include <sys\stat.h> /* needed for status flags */
#include <malloc.h> /* needed for _memmax, malloc. and free */
#include "comment.h"
void main(int argc, char *argv[])
{
char szComment[80];
char szFilename[255];
int iErrCode;
if (argc < 2 || argc > 3)
{
printf("The format of COMMENT is :\n\n");
printf("COMMENT filename \" comment \" \n");
printf("will add/replace a comment \n\n");
printf("COMMENT filename \n");
printf("will retrieve a comment \n");
}
else
{
strcpy (szFilename,argv[1]);
iErrCode = 0;
if (argc ==2)
{
iErrCode = getCommentEA(argv[1],szComment);
switch (iErrCode)
{
case 0:
strcat(szComment,"\n");
printf(szComment);
break;
case 1:
printf("No Comments on this file");
break;
case 2:
printf("Could not open file in read only mode");
break;
default:
break;
}
}
else
{
if (strlen(argv[2]) < 80)
{
iErrCode = addCommentEA(argv[1],argv[2]);
switch (iErrCode)
{
case 0:
printf("Comment added/modified");
break;
case 1:
printf("Could not add the Comment Extended Attribute");
break;
case 2:
printf("Could not open file in read write mode");
break;
default:
break;
}
}
else
printf("Comment must be less than 80 characters");
}
}
}
/*--------------------------------------------------------------
This function gets the COMMENT Extended Attribute and puts
it in szComment
Error Codes:
0 - ok.
1 - No COMMENT EA
2 - Could not open file in read only mode
-------------------------------------------------------*/
int getCommentEA(char *szFilename, char *szComment)
{
int iErrCode;
char EAnamebuf[80]; // EA name from user
struct _EAval *pEAval; // scratch pointer
int handle; // handle for file
if((handle = open(szFilename, O_BINARY | O_RDONLY)) == -1)
iErrCode = 2;
else
{
strcpy(EAnamebuf, "COMMENT");
/* If no COMMENT EA then exit with a 1 */
if((pEAval = getEA(handle, EAnamebuf)) == NULL)
iErrCode = 1;
else
{
memcpy(szComment,pEAval->data,pEAval->size);
szComment[pEAval->size] = '\0';
iErrCode = 0;
}
close(handle);
}
return(iErrCode);
}
/*--------------------------------------------------------------
This function adds a comment to the COMMENT EA
Error Codes:
0 - COMMENT EA modified
1 - Could not add the COMMENT EA
2 - Could not open file in read/write mode
-------------------------------------------------------*/
int addCommentEA(char *szFilename,char *szComment)
{
int iErrCode;
char EAnamebuf[80]; // EA name from user
char EAvalbuf[80]; // EA value from user
int handle; // handle for file
if((handle = open(szFilename, O_BINARY | O_RDWR)) == -1)
iErrCode = 2;
else
{
strcpy(EAnamebuf, "COMMENT");
strcpy(EAvalbuf, szComment);
/* if can't set extended attribute set exit code to 4)*/
if(putEA(handle, EAnamebuf, EAvalbuf, strlen(EAvalbuf), EAT_ASCII))
iErrCode = 1;
else
iErrCode = 0;
close(handle);
}
return(iErrCode);
}
/*
putEA: constructs and writes an extended attribute for a previously
opened file. Called with the handle of the file, the extended
attribute name, the EA value type, and the address and length of
the EA value field data. Returns FALSE if extended attribute was
written successfully. Returns -1 or OS/2 errcode if function failed.
*/
int putEA(int handle, char * eaname,
void * eavalue, unsigned eavalsize, unsigned eatype)
{
struct _FEAList *pFEAList; // misc scratch variables
struct _EA *pEA; // and pointers
struct _EAval *pEAval;
struct _bEAOP EAOP;
int errcode;
pFEAList = malloc(FEALISTSIZE); // allocate buffer for FEAList
if(pFEAList == NULL) return(-1); // bail out if no heap available
EAOP.pFEAList = pFEAList; // initialize contents of
EAOP.pGEAList = NULL; // EAOP pointer structure
EAOP.oError = 0;
(char *) pEA = pFEAList->data; // point to EA header field
pEA->flags = 0; // construct header portion
// pEA->nsize = strlen(eaname); // of extended attribute
pEA->nsize = (unsigned char) strlen(eaname); //bob
pEA->vsize = eavalsize + 4;
strcpy(pEA->name, eaname);
(char *) pEAval = // calculate address of
pEA->name + pEA->nsize + 1; // EA's value field
pEAval->type = eatype; // construct value portion
pEAval->size = eavalsize; // of extended attribute
memcpy(pEAval->data, eavalue, eavalsize);
pFEAList->size = // calculate size of entire
pEA->nsize + pEA->vsize + 9; // FEAList structure
errcode = // set extended attributes
DosSetFileInfo(handle, // file handle
2, // info level 2 = set EAs
(PBYTE)&EAOP, // EAOP structure holds pointer
sizeof(EAOP)); // to FEAList containing EAs
free(pFEAList); // release working buffer
return(errcode); // return DosSetFileInfo status
}
/*
getEA: fetches an extended attribute for a previously opened file.
Returns FALSE if the extended attribute does not exist; otherwise,
returns a pointer to the extended attribute value in a chunk of
heap memory. The EA value is in the form of the EA type indicator,
followed by the length of variable data, followed by the data itself.
The exact format of the EA value is given by the structure _EAval.
The caller must be sure to free the memory holding the extended
attribute value after the value has been used or copied elsewhere.
*/
struct _EAval * getEA(int handle, char * eaname)
{
struct _FEAList *pFEAList = NULL; // misc scratch variables
struct _GEAList *pGEAList = NULL; // and pointers
struct _EA *pEA = NULL;
struct _EAval *pEAval = NULL;
struct _bEAOP EAOP;
pFEAList = malloc(FEALISTSIZE); // allocate buffers
pGEAList = malloc(GEALISTSIZE);
if((pFEAList == NULL) || (pGEAList == NULL))
goto errexit; // bail out if no heap
EAOP.pFEAList = pFEAList; // initialize contents of
EAOP.pGEAList = pGEAList; // EAOP pointer structure
EAOP.oError = 0;
strcpy(pGEAList->GEA.name, eaname); // fill in EA name & length
pGEAList->GEA.size = strlen(eaname);
pFEAList->size = FEALISTSIZE; // fill in structure sizes
pGEAList->size = pGEAList->GEA.size + 6;
if(DosQFileInfo(handle, // get extended attribute
3, // info level 3 = get EA
(PBYTE)&EAOP, // EAOP struct holds pointers
sizeof(EAOP))) // to GEAList and FEAList
goto errexit; // exit if API function failed
(char *) pEA = pFEAList->data; // point to EA header field
if(pEA->vsize == 0) goto errexit; // exit if no EA value present
if((pEAval = malloc(pEA->vsize)) // allocate space for EA value
== NULL) goto errexit; // exit if heap is full
// copy EA value to heap
memcpy(pEAval, pEA->name + pEA->nsize + 1, pEA->vsize);
free(pFEAList); // release working buffers
free(pGEAList);
return(pEAval); // return pointer to EA value
errexit: // common exit point for errors
if(pFEAList != NULL) free(pFEAList);
if(pGEAList != NULL) free(pGEAList);
return(NULL); // NULL pointer indicates error
}