home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
cowen_tools.lzh
/
delbak.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-06
|
7KB
|
286 lines
/*
** Delbak
** Deletes all files in current directory with names
** ending in _bak (or alternative given -f option)
** and optionaly recursively down the tree.
** optionally will merely list files instead of deleting them.
**
** options are:
** -n = no recursion
** -l = list only
** -f = extension to delete on, default = _bak
** -? = report usage
**
**
** copyright 1991 by Cowen Software Ltd
** 23 Bristol Ave, Levenshulme, Manchester,
** GB-M19 3NU, England, UK
** released to the public domain for non-commercial use
**
*/
#include <stdio.h>
#include <modes.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
#define trace FALSE
#define FNSIZ 132
#define dirlength 300
#define maxf 20
struct dirent {
char dir_name[29];
char dir_addr[3];
} ;
char rflag = TRUE; /* recursive*/
char lflag = FALSE; /* list only*/
char fflag = FALSE; /* file specified
(default = curr dir */
char defdir[] = "."; /* default directory */
char dirname[dirlength]; /* holds expanded filenames*/
char filename[32]; /* holds each file name */
char *fptr[maxf];
int flen[maxf];
int numf = 0;
int i;
char dext[] = "_bak";
/*page*/
main(argc,argv)
int argc;
char **argv;
{
char *p;
getflags(argc,argv);
if (numf == 0) fptr[numf++] = dext;
for (i=0;i<numf;i++)
flen[i] = strlen(fptr[i]);
if ( fflag )
{
while (--argc)
if (*(p = *++argv) != '-')
dodel(p);
}
else
dodel(defdir);
}
getflags(carg,varg)
int carg;
char **varg;
{
char *p, c;
while ( (--carg) )
{
if (*(p = *++varg) == '-')
{
while (c = *++p)
{
switch (c)
{
case 'n' :
rflag = FALSE; /* not recursive*/
break;
case 'l' : /*list only*/
lflag = TRUE;
break;
case 'f' :
while(*++p == ' ');
if (*p++ != '=') error ("no suffix for -f option",0);
if (*p == '\0') error ("null suffix for -f option",0);
if (numf >= maxf) error("Too many -f parameters",0);
fptr[numf++] = p;
while (c = *p) p++; /* skip past extension name */
p--; /* and point to the closing null */
break;
case '?' :
help();
default :
error("bad option",0);
}
}
}
else
fflag = TRUE;
}
}
dodel(rootname)
char *rootname;
/* This procedure is called for each root directory
which is to be cleaned up.
It copies the root dir to dirname, then starts the
recursive calls to donext.
NOTE if the total path name ever goes beyond 300 words
then this procedure will fail!!!.
*/
{
int fd;
int nlen; /* length of filename*/
strcpy(dirname, rootname);
nlen = strlen(dirname);
if ( (fd = open(dirname,S_IFDIR + S_IREAD) ) == -1)
error ("Can't open file ",dirname);
donext(fd,nlen);
}
donext(fd,nlen)
int fd; /* dir path */
int nlen; /* length of filename*/
/* This procedure is called for each directory
which is to be cleaned up.
It opens the directory, and reads all files in it.
Each valid file is passed to checkfile for processing
NOTE the file name as given on entry has each
filename in turn appended to it, then the original length restored
this means that we must check to see if the tree of directories
has got too long, and overflowed the dirname buffer.
*/
{
struct dirent dirbuf; /* single dir ent read buf */
#if trace
fprintf(stderr,"donext %d \n",fd);
fflush(stderr);
#endif
while(read(fd, &dirbuf, sizeof(dirbuf))) /* now copy names */
{
if (isalpha(dirbuf.dir_name[0] & 0x7f))
{
strhcpy(filename, &dirbuf);
filename[28] = '\0'; /* shouldn't be needed
but I am a pessimist */
if (nlen + 2 + strlen(filename) > dirlength)
error ("Tree structure has too many levels", dirname);
dirname[nlen] = '/';
dirname[nlen+1] = '\0';
strcat(dirname,filename); /* stick the next element
on to the filename*/
checkfile(); /* check this file */
dirname[nlen] = '\0'; /* restore original length*/
}
}
close(fd);
}
checkfile()
/* now check each file, as follows
1, try to open it as a directory, if it is one then
pass it back to donext (unless rflag is false ie -n) .
2. otherwise look at the filename. if it is .bak
then delete the file, unless lflag is true - ie -l.
in which case just output the file name.
*/
{
int fd;
int nlen;
nlen = strlen(dirname);
if ( ( (fd = open(dirname,S_IFDIR + S_IREAD) ) != -1) && rflag)
donext(fd,nlen);
else
for (i=0;i<numf;i++)
{
if ( ! strucmp(fptr[i],&dirname[nlen-flen[i]]) )
{
delfile();
break;
}
}
}
delfile()
/* Now we have a file to delete - delete it or if lflag just
list it.
*/
{
int x;
if (lflag) printf(" %s\n",dirname);
else
{
if (!lflag) (x = unlink(dirname) );
printf(" %s ", dirname);
if (!x) printf("deleted\n");
else printf("delete failed\n");
}
}
error(s1, s2)
char *s1, *s2;
/*
** write the two strings passed and then a <cr>
*/
{
if (s1)
fprintf(stderr, "%s", s1 );
if (s2)
fprintf(stderr, "%s", s2 );
fprintf(stderr, "\n");
fflush(stderr);
exit (1);
}
help()
/*
** provide usage info for this command
*/
{
fprintf(stderr, "Usage: delbak [-options] [directory] [-options]\n" );
fprintf(stderr, " -n = no recursion\n" );
fprintf(stderr, " -l = list only \n" );
fprintf(stderr, " -f=<ext>= extension to delete, default _bak\n" );
fprintf(stderr, " -? = report help \n" );
fprintf(stderr, "\n");
fflush(stderr);
exit (0);
}