#include <stdio.h>
#include <stat.h>

/* DLT.C - delete multiple files on the command line, the DOS DEL and ERASE */
/* commands cannot delete more than 1 file without the wildcard masks used  */

static	char *Program[] = { "DLT - replmnt delete routine"
			    "S. Leoce - V1 R1 S0 - VA/PSJ" };

static	char *Usage = "usage: dlt [/cft] file [file ...]";

#include "b:cmdline.c"

#define	FALSE	0
#define	TRUE	~FALSE

int main (argc, argv)
int argc;
char *argv[];
{
	short unsigned int LineOption = FALSE;
	FILE	*CurF;
	int	CurFH;	/* current file, and handle here */

	long	unsigned register int	files = 0;	/* files counter */
	auto	short	 int		type  =	FALSE;	/* type names 	 */
	auto	short	 int		count =	FALSE;	/* count files	 */
	auto	short	 int		force = FALSE;

	if (argc < 2)
		_exit (fprintf(stderr, "%s\n", Usage) + 0x10);
	
	opterr = FALSE;
	while ((LineOption = getopt(argc, argv, "cft")) != EOF)
		switch(LineOption) {
			case '?':
				_exit(fprintf(stderr, "%s\n", Usage) + 0x10);
			case 'c':
				count = TRUE;
				break;
			case 'f':
				force = TRUE;
				break;
			case 't':
				type = TRUE;
				break;
		}
	if (argv[optind] == NULL)
		_exit(fprintf(stderr, "%s\n", Usage) + 0x12);

	if (!force) {
		while (argv[optind] != NULL) {
			if(unlink(argv[optind]) != 0) {
				if(type) {
					fprintf(stderr,"%s ",argv[optind]);
					fprintf(stderr,"force may be required\n");
				}
				else
					perror(argv[optind]);
			}
			else {
				if (type)
					fprintf(stdout,"%s removed\n", argv[optind]);
				files++;
			}
						
			optind++;
		}

		if(count)
			fprintf(stdout,"\n%10ld files removed\n", files);

		return (0);
	}
	else 	/* chmod all files first, then remove 	*/
		while(argv[optind] != NULL) {
			if(chmod(argv[optind], S_IREAD|S_IWRITE))
				fprintf(stderr,"%s couldn't force\n",argv[optind]);
			else if(unlink(argv[optind]) != 0)
				perror(argv[optind]);
			else {
				if (type)
					fprintf(stdout,"%s removed\n",argv[optind]);
				files++;
			}
			optind++;
		}
	if (count)
		fprintf(stdout,"\n%10ld files removed\n", files);

	return (0);
}