home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
ATTRIB.CMM
< prev
next >
Wrap
Text File
|
1996-01-30
|
8KB
|
249 lines
/*
* Attrib.cmm
*
* CMM Attrib. This program allows you to view or set file attribute bits.
*
* Runs on any version of CEnvi
*/
#include "netware.lib"
/* ---------------------------------------------------------------------
The following variables allow us to keep track of which bits the user
wants us to twiddle.
--------------------------------------------------------------------- */
#define BITS "RHSEDA"
flags = 0; // 1 if user wants to change the values
flagor = 0; // stores the +A, etc options
flagand = 0; // stores the -A, etc options
/* ---------------------------------------------------------------------
These variables tell us what command line options the user selected
--------------------------------------------------------------------- */
filename = ""; // filename to be worked on - can have wildcards
recursive = 0; // should we do recursive stuff, like *.cmm in all
// subdirectories
help = 0; // If so, we print the usage display
/*
* A standard usage help display.
*/
usage()
{
printf("Displays or changes file attributes.\n\n");
printf("ATTRIB [+ | -] bit ... [[drive:][path]filename] [/S]\n");
printf("\n + Sets an attribute.\n");
printf(" - Clears an attribute.\n");
printf(" R Read-only file attribute.\n");
printf(" A Archive file attribute.\n");
printf(" S System file attribute.\n");
printf(" H Hidden file attribute.\n");
printf(" E Executable file attribute.\n");
printf(" /S Processes files in all directories in the specified path.\n");
}
/*
* Examine the command line and set appropriate flags
*/
parse_args(argc,argv)
{
for( i=1;i<argc;i++ )
{
if( argv[i][0]=='/' )
{
switch( argv[i][1] )
{
case 's': recursive = 1; break;
case '?': help = 1; break;
default: usage();
}
continue;
}
if( argv[i][0]=='-' || argv[i][0]=='+' )
{
chr = toupper(argv[i][1]); search = "1"; search[0] = chr;
if( chr=='D' )
{
printf("You may not set or clear the directory bit.\n");
exit(EXIT_FAILURE);
}
if( (bit = strcspn(BITS,search))==strlen(BITS) )
{
printf("Unrecognized atribute: %c\n",argv[i][1]);
exit(EXIT_FAILURE);
}
if( argv[i][0]=='+' )
{
flagor |= (1<<bit);
} else {
// If you turn on read-only, netware automatically sets these bits. So,
// if you turn it off, we should remove them
if( defined(_NWNLM_) && (chr=='R') )
flagand |= _A_NODELET | _A_NORENAM;
flagand |= (1<<bit);
}
flags = 1;
continue;
}
if( strlen(filename)>0 )
{
printf("Too many files on command line.\n");
exit(EXIT_FAILURE);
}
filename = argv[i];
}
}
/*
* This is a Netware function. It won't work on the other systems.
*/
DOSTimeFromCalendar(time)
{
undefine(field1);
undefine(field2);
NLMLink("_ConvertTimeToDOS",time,field1,field2);
return (field2<<16) | field1;
}
/*
* This is a DOS/Windows only function
*/
SetFileAttributes(pFileName,pAttributes)
{
lReg.ah = 0x43;
lReg.al = 1;
lReg.cx = pAttributes;
if( !defined(_DOS32_) ) {
lReg.ds = segment(pFileName); lReg.dx = offset(pFileName);
} else {
lReg.dx = pointer(pFileName);
}
return interrupt(0x21,lReg);
}
/*
* We process the arguments, then do the work. This should act as an exact
* clone of the DOS attrib command.
*/
main(argc,argv)
{
parse_args(argc,argv);
if( help ) usage();
// Get an array of the files we are to work with.
if( filename[0]!='\0' )
{
files = Directory(filename);
if( !strcmp(filename,".") || !stricmp(filename,"..") ||
(files && GetarraySpan(files)==0 && (files[0].attrib & _A_SUBDIR))
)
strcat(filename,defined(_NWNLM_)?"/":"\\");
}
s = strlen(filename);
if( s && filename[s-1]==':' )
{
strcat(filename,defined(_NWNLM_)?"/":"\\"); s++;
}
if( s && (filename[s-1]=='/' || filename[s-1]=='\\') )
strcat(filename,"*.*");
if( s==0 ) filename = "*.*";
files = Directory(filename,recursive,~_A_SUBDIR);
if( files==NULL ) {
printf("File not found - %s\n",filename);
exit(EXIT_FAILURE);
}
number = GetArraySpan(files)+1;
// In this case, the user just wants us to list the atributes for the
// specified filespec.
if( flags==0 )
{
for( i=0;i<number;i++ )
if( defined(_NWNLM_) )
printf(" %s %s %s %s %s %s %s %s %s %s %s %s\n",
(files[i].attrib & _A_SUBDIR)?"D":"-",
(files[i].attrib & _A_RDONLY)?"RO":"RW",
(files[i].attrib & _A_SHARE)?"SH":"--",
(files[i].attrib & _A_HIDDEN)?"H":"-",
(files[i].attrib & _A_SYSTEM)?"SY":"--",
(files[i].attrib & _A_TRANS)?"T":"-",
(files[i].attrib & _A_IMMPURG)?"P":"-",
(files[i].attrib & _A_ARCH)?"A":"-",
(files[i].attrib & _A_NOCOPY)?"CI":"--",
(files[i].attrib & _A_NODELET)?"DI":"--",
(files[i].attrib & _A_NORENAM)?"RI":"--",
FullPath(files[i].name));
else
printf(" %c%c%c%c%c%c %s\n",
(files[i].attrib & _A_ARCH)?'A':' ',
(files[i].attrib & _A_SUBDIR)?'D':' ',
(files[i].attrib & _A_EXECUTE)?'E':' ',
(files[i].attrib & _A_SYSTEM)?'S':' ',
(files[i].attrib & _A_HIDDEN)?'H':' ',
(files[i].attrib & _A_RDONLY)?'R':' ',
FullPath(files[i].name));
exit(EXIT_SUCCESS);
}
// At this point, he doesn't want a listing of those files, he wants to
// do some changes to them.
for( i=0;i<number;i++ )
{
if( defined(_NWNLM_) )
{
// For this to work, we must tell Netware to change the bits for file
// 'files[i].name' to the value (file[i].attrib &flagand)|flagor.
tempcreate[0] = DOSTimeFromCalendar(files[i].Create);
tempaccess[0] = DOSTimeFromCalendar(files[i].Access);
tempdate[0] = DOSTimeFromCalendar(files[i].Write);
tempbackup[0] = DOSTimeFromCalendar(files[i].Backup);
code = NLMLink("SetFileInfo",
files[i].name,
0x06,
(files[i].attrib & ~flagand) | flagor,
tempcreate,
tempaccess,
tempdate,
tempbackup,
files[i].uid);
if( code )
printf("\nError setting attributes for file %s\n",file[i].name);
continue;
}
if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
{
SetFileAttributes(files[i].name,
(files[i].attrib & ~flagand) | flagor);
continue;
}
if( defined(_NTCON_) || defined(_NTWIN_) || defined(_95CON_) ||
defined(_95WIN_) )
{
DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,files[i].name,
(files[i].attrib & ~flagand) | flagor);
continue;
}
printf("I do not know how to set file attributes on this system.\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}