home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
NDEL2.ZIP
/
NDEL2.C
next >
Wrap
C/C++ Source or Header
|
1991-07-17
|
8KB
|
220 lines
/***************************************************************************
* NAME HEADER: FILE : NDEL2.C (not delete for OS/2 and DOS) *
* AUTHOR: RAJESH NIHAL, compuserve [73427,1377] *
* DATE : 6/11/90 UPDATE: 7/14/91 *
* SYS : OS/2 1.x xE *
* PROG : NDEL2 version 0.1 Copyright (c) 1991 RAJESH NIHAL*
* REMARK: This program will delete all the files, Except *
* those which are specified on the command line. *
* Use this program as you wish (except abusing it).*
* If distributting, leave the name of the header *
* (this whole commented block) intact and *
* do not change the run time help screen. I am NOT *
* responsible for any damages(so far works great). *
* Comments, Advises, Corrections are welcome. Next *
* version will have wildcard support. *
* PS : THIS PROGRAM IS NOT A SHAREWEAR. DO NOT USE *
* WILDCARDS, IF USED WILL WIPE OUT EVERYTHING. *
* THE FILE[S] WHICH YOU SPECIFY ON THE COMMAND *
* LINE SHOULD NOT CONTAIN DIR PATH. THE PROGRAM *
* WILL NOT DELETE ITSELF. *
* *
* Compiler: MSC 6.00A *
* Compilation syntax: cl /A{<S,M,C,L>} ndel.c *
* Runtime arguments: ndel2 <filename(s)> | <?,-?,-h,/h> *
* Special: Use BIND under DOS to create a family exe *
***************************************************************************/
/******************************* HEADER FILES ******************************/
#include <stdio.h>
#include <os2.h> //Don't really need it
#include <direct.h>
#include <dos.h>
#include <string.h>
#include <stdlib.h>
/******************************* DEFINES AND MACROS ************************/
#define read_only 0x01
#define archive 0x20
#define getdir getcwd(buffer,128)
/******************************* GLOBAL VARS *******************************/
unsigned extern far pascal \
DosFindFirst(char far *, unsigned far *, unsigned, void far *, \
int, int far *, unsigned long);
// The above prototype produces warnings.
// Maybe the type of the arguments has been changed for MSC 6.00A?
/* .
.
.
more prototypes <don't wanna type>; may produce more warnings.
.
.
.
*/
int getfiles(void);
int delete_file(char *);
int tempargc;
char near buffer[128]; // Buffer size could vary
int err,i;
char temp[100][12];
char probuf[128];
unsigned hand = -1;
int ms = 1;
struct buu { // DosFind{First/Next} return struct
unsigned dt,tm,dtt,tmm,dttt,tmmm;
long fs,flc;
unsigned ft;
char fc;
char fn[13]; // This is used in the program
} bu;
/******************************** MAIN MODULE ******************************/
int main(int argc,char *argv[])
{
int f;
register int targc;
int pro;
if (argc == 1)
{
printf("\a\n\n");
puts("Required parameter missing!");
//argv[0] [strlen(argv[0]) - 4] = NULL; don't work under OS/2
printf("Synopsis: %s file[s].\n",argv[0]);
printf("Use %s [/? /h[H] -? -h[H] ? ] for help screen.\n",argv[0]);
exit (1);
}
if(argc > 1)
{
if((strcmp(argv[1],"/?")==0) || (strcmp(argv[1],"/h")==0) ||
(strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"-?")==0) ||
(strcmp(argv[1],"-H")==0) || (strcmp(argv[1],"/H")==0) ||
(strcmp(argv[1],"?")==0))
{
printf("%s Version 0.1 Copyright (c) 1991 RAJESH NIHAL\n",argv[0]);
printf("\nThis Utility Deletes all the files, except those specified");
printf(" on the command line\n");
printf("Syntax is : %s FILE(S) leave a space between files.\n",argv[0]);
printf("Example : %s cc.exe all the files except cc.exe will be deleted.\n",argv[0]);
printf("Example : %s awk.exe bc.exe prjcnvt.txt comp.txt\n",argv[0]);
printf("\aNO WILDCARDS!!\n");
exit (1);
}
/*************************** EXTRACT THE NAME OF THE PROGRAM ****************/
pro = 0;
for(i=(strlen(argv[0])-1);i>=0;i--)
{
if(argv[0][i] == '\\')
break;
else
probuf[pro] = argv[0][i];
pro++;
}
strrev(probuf);
/*************************** SAVE THE GIVEN FILE NAMES **********************/
tempargc = argc; /* why :')@#*&%? */
targc = tempargc;
i = 0;
while (argc != 1)
{
getdir;
strcpy(temp[i],argv[argc-1]);
if (strlen(buffer) > 3) // if !root
strcat(buffer,"\\");
strcat(buffer,temp[i]);
if(!DosFindFirst (buffer,&hand,0x20|0x00,\
(void far *)&bu,sizeof(bu),&ms,0L))
for(f=0;f <= 81;f++)
{
buffer[f] = 0;
}
else
{
printf("%s not found ...program aborted.\n",temp[i]);
exit(-1);
}
argc--;
i++;
}
argc = targc;
/************************* MAKE THE GIVEN FILES READ ONLY *******************/
i = 0;
DosSetFileMode(probuf,0x01,0);
while(argc != 1)
{
DosSetFileMode(temp[argc-2],0x01,0);
i++;
argc--;
}
argc = targc;
/******************** FIND THE REMAINING FILES AND DELETE THEM **************/
getfiles(); // Just to make the program more readble
/************************* MAKE THE GIVEN FILES ARCHIVE ********************/
while(argc != 1)
{
DosSetFileMode(temp[argc-2],0x20,0);
argc--;
}
/***************************************************************************/
DosSetFileMode(probuf,0x20,0); /* change the attrib of program to archive */
} /* if argc > 1 */
DosFindClose(hand);
return 0;
} /* end main module */
/***************************************************************************/
int delete_file(char *filename) /* delete all the files */
/***********
* input: filename
* output: Delete that file
* Notes : Could have made nice inline function if OOP was Supported
* Could have made a macro too.
***********/
{
#ifdef SHOW_NAMES // Undocumented feature for NDEL2
// Don't get nervous looking at the file names on screen
printf("deleting %s\n",filename);
#endif
DosDelete(filename,0);
return 0;
}
/***************************************************************************/
int getfiles(void) /* get all the files */
{
int done;
getdir;
if (strlen(buffer) > 3)
strcat(buffer,"\\*.*"); /* current dir and all the files */
else
strcat(buffer,"*.*"); /* current root dir and all the files */
done = DosFindFirst(buffer,&hand,0x20|0x00,\
(void far *)&bu,sizeof(bu),&ms,0L); /* get first file */
delete_file(bu.fn); /* delete that file*/
while (!done)
{
done = DosFindNext(hand,(void far *)&bu,\
sizeof(bu),&ms); /* find next file */
err = delete_file(bu.fn); /* delete that file */
}
return 0;
}