#include <xfbuf.h>

/*
	.........................................
	.					.
	.	      C H M O D S		.
	.					.
	.	T. Jennings 16 June 83		.
	.	  created 2 June 83		.
	.					.
	.   Phoenix Software Associates Ltd	.
	.					.
	.........................................


	Change or list the attributes for a bunch of files.

	CHMODS filespec			List all attributes

	CHMODS filespec (- +) s r h	Set and list attributes

		s	System
		r	Read only
		h	Hidden

*/

#define SY 0x04		/* System attribute, */
#define HI 0x02		/* Hidden attribute, */
#define RO 0x01		/* Read only attribute. */
#define AR 0x20		/* Archive, */
#define VO 0x08		/* Volume ID */
#define DI 0x10		/* directory */

int mask,newval;
int sign,attrib;
int setflg,help;

main(argc,argv)
int argc;
char *argv[];
{
int i;
char path[80];
char pathname[80];
char *p,c;
int error;
struct _xfbuf xfbuf;

	mask= 0xff;			/* remove none, */
	newval= 0;			/* add none, */
	setflg= 0;			/* list attribs only */

	for (i= 2; i < argc; i++) {		/* Process any options, */
		p= argv[i];
		sign= 0xff;			/* assume plus, */

		while (*p) {			/* do each arg, */
			c= tolower(*p); ++p;

			if (c == '-') {		/* if arg has a leading */
				sign= 0; 	/* sign, remove it, */
			} else if (c == '+') {
				sign= 0xff; 
			} else if (c == 's') {
				setflg= 1;
				mask &= ~SY;
				newval &= ~SY;
				newval |= SY & sign;
			} else if (c == 'r') {
				setflg= 1;
				mask &= ~RO;
				newval &= ~RO;
				newval |= RO & sign;
			} else if (c == 'h') {
				setflg= 1;
				mask &= ~HI;
				newval &= ~HI;
				newval |= HI & sign;
			} else if (c == 'a') {
				setflg= 1;
				mask &= ~AR;
				newval &= ~AR;
				newval |= AR & sign;
			} else
				help= 1;	/* none of the above */
		}

	}
	if (argc < 2 || help) {
		printf("CHMOD:  List or set file attributes. Usage is:\r\n");
		printf("\r\n");
		printf("    CHMOD <files>              List attributes,\r\n");
		printf("    CHMOD <files> +<attribs>   Set attributes,\r\n");
		printf("    CHMOD <files> -<attribs>   Remove attributes.\r\n");
		printf("\r\n");
		printf("Attributes are: S == System, R == Read only, H == Hidden,\r\n");
		printf("A == Archive, V == Volume ID. Volume ID cannot be changed.\r\n");
		printf("Each file is listed with the new or current attributes.\r\n");
		printf("The <files> specifier may be a full pathname.\r\n");
		exit(1);
	}

	strip_path(path,argv[1]);		/* separate the path, */
	stolower(path);				/* make lower case, */
	i= 0;

	xfbuf.s_attrib= 0xff;
	while (_find(argv[1],i,&xfbuf)) {
		i++;
		strcpy(pathname,path);		/* build the full name, */
		strcat(pathname,xfbuf.name);	/* from the path and found */
		stolower(pathname);		/* make lower case, */
		attrib= 0;			/* DEBUG: Clear DIR, */
		attrib= _chmod(pathname,0,0);	/* name. Get current attrib, */
		if (setflg && !(attrib & VO)) {	/* if setting new ones, */
			attrib &= mask;		/* mask off old, */
			attrib &= ~(DI | VO);	/* clear ir and volume, */
			attrib |= newval;	/* add new ones, */
			error= _chmod(pathname,attrib,1); /* set them, */
		} else
			error= 0;
		printf("%30s %s %s %s %s %s %s ",pathname,
			((attrib & DI) ? "<dir>" : "     "),
			((attrib & AR) ? "A" : "."),
			((attrib & SY) ? "S" : "."),
			((attrib & RO) ? "R" : "."),
			((attrib & HI) ? "H" : "."),
			((attrib & VO) ? "V" : ".") );
		if (error == -1)
			printf(" * not changed");
		printf("\r\n");
	}
	if (i == 0) {
		printf("No matching files\r\n");
		exit(1);
	}
	if (i > 1)
		printf("%u files.\r\n",i);
	exit(0);
}
stolower(s)
char *s;
{
	while (*s) {
		*s= tolower(*s);
		++s;
	}
}

stoupper(s)
char *s;
{	while (*s) {
		*s= toupper(*s);
		++s;
	}
}

