1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define import_spp
#define import_knames
#include <iraf.h>
#include "../bootProto.h"
#define MAXEXTN 128
#define ALL 0 /* delete all files */
#define ALLBUT 1 /* delete all but listed files */
#define ONLY 2 /* delete only listed files */
int verbose; /* print names of deleted files */
int execute; /* permission to delete files */
int debug; /* print debugging info */
extern char *vfn2osfn(char *, int);
extern int ZZSTRT (void);
extern int ZZSTOP (void);
static void rmfiles (char *prog, int oneliner);
static void stripdir (char *dir, char *path, char *extnlist[], int mode);
static int got_one (char *fname, char *extnlist[]);
/*
* RMFILES -- Delete all files with the listed extensions in the listed
* directory trees.
*
* rmfiles [-dnv] [-f progfile] dir action extns
*
* -d debug
* -n no execute; do not delete files
* -v print names of files as they are deleted
* -f progfile name of file containing program script
* dir root directory of tree to be pruned
* action one of "-all", "-allbut", "-only"
* extns extensions of files to be deleted
*
* There is no default action as a safety measure. If -all is specifed,
* the extension list is ignored.
*/
int main (int argc, char *argv[])
{
char prog[SZ_LINE+1];
char *argp, *ip, *op;
int oneliner, argno;
ZZSTRT();
if (argc < 2)
goto help_;
verbose = 0;
execute = 1;
debug = 0;
for (argno=1; (argp = argv[argno]) != NULL; argno++)
if (*argp == '-') {
for (argp++; *argp; argp++)
switch (*argp) {
case 'd':
debug++;
break;
case 'n': /* no execute */
execute = 0;
/* fall through */
case 'v': /* set verbose mode */
verbose = 1;
break;
case 'f':
argno++;
if (argv[argno] == NULL) {
fprintf (stderr, "illegal `-f progfile' switch\n");
exit (OSOK+1);
}
rmfiles (argv[argno], oneliner=NO);
break;
default:
goto help_;
}
} else {
/* Program is on command line. The rest of the command
* line is assumed to be the program.
*/
for (op=prog; (ip = argv[argno]) != NULL; argno++) {
while ((*op = *ip++))
op++;
*op++ = ' ';
}
*op = EOS;
rmfiles (prog, oneliner=YES);
break;
}
ZZSTOP();
exit (OSOK);
help_:
fprintf (stderr, "rmfiles [-dnv] [-p prog] [progfile]\n");
ZZSTOP();
exit (OSOK+1);
return (0);
}
/* RMFILES -- Strip (delete) the indicated files in the indicated
* directories. We are driven either by a program in the named text file,
* or in the prog string itself.
*/
static void
rmfiles (
char *prog, /* program, or program file name */
int oneliner /* if !oneliner, open program file */
)
{
char dir[SZ_PATHNAME+1], path[SZ_PATHNAME+1];
char *extnlist[MAXEXTN], *ip, *op;
char lbuf[SZ_LINE+1];
int nextn, mode;
FILE *fp = NULL;
if (debug) {
fprintf (stderr, "rmfiles @(%s), exe=%d, ver=%d\n", prog, execute,
verbose);
fflush (stderr);
}
/* Is program in a file, or in the "prog" string?
*/
if (oneliner)
strcpy (lbuf, prog);
else {
/* Open the program file.
*/
if ((fp = fopen (vfn2osfn(prog,0), "r")) == NULL) {
fprintf (stderr, "cannot open progfile `%s'\n", prog);
fflush (stderr);
return;
}
}
while (oneliner || fgets (lbuf, SZ_LINE, fp) != NULL) {
/* Skip comment lines and blank lines, and any whitespace at
* the beginning of program lines.
*/
for (ip=lbuf; isspace(*ip); ip++)
;
if (*ip == EOS || *ip == '#') {
if (oneliner)
break;
else
continue;
}
/* Check for a single filename entry of the form `-file filename',
* deleting the named file if this type of entry is encountered.
*/
if (strncmp (ip, "-file", 5) == 0) {
for (ip=ip+5; isspace(*ip); ip++)
;
for (op=path; (*op = *ip); ip++, op++)
if (isspace(*op))
break;
*op = EOS;
if (*path == EOS)
continue;
if (verbose) {
printf ("%s\n", path);
fflush (stdout);
}
if (execute) {
if (os_delete (path) == ERR) {
fprintf (stderr, "cannot delete `%s'\n", path);
fflush (stderr);
}
}
continue;
}
/* Parse the program line into the directory pathname, mode,
* and extension list. The program entry must be all on one
* line.
*/
for (op=dir; (*op = *ip); ip++, op++)
if (isspace(*op))
break;
*op = EOS;
while (isspace(*ip))
ip++;
if (strncmp (ip, "-allbut", 7) == 0) {
mode = ALLBUT;
ip += 7;
} else if (strncmp (ip, "-all", 4) == 0) {
mode = ALL;
ip += 4;
} else if (strncmp (ip, "-only", 5) == 0) {
mode = ONLY;
ip += 5;
} else {
fprintf (stderr, "error: no action specified: %s\n", lbuf);
fflush (stderr);
if (oneliner)
return;
else
continue;
}
/* Construct a list of pointers to the extension strings.
*/
for (nextn=0; nextn < MAXEXTN; nextn++) {
while (isspace(*ip))
ip++;
if (*ip == EOS || *ip == '#')
break;
extnlist[nextn] = ip;
while (*ip && !isspace(*ip))
ip++;
*ip++ = EOS;
}
extnlist[nextn] = NULL;
if (debug) {
fprintf (stderr, "rootdir=%s, mode=%d, extns:", dir, mode);
for (nextn=0; extnlist[nextn]; nextn++)
fprintf (stderr, " %s", extnlist[nextn]);
fprintf (stderr, "\n");
fflush (stderr);
}
/* Strip the named directory tree.
*/
path[0] = EOS;
stripdir (dir, path, extnlist, mode);
if (oneliner)
break;
}
if (!oneliner)
fclose (fp);
}
/* STRIPDIR -- Starting with the named directory, scan that directory and
* all subdirectories, deleting (or listing) the files therein depending
* on the mode, which can be ALL, ALLBUT, or ONLY. We chdir to each directory
* to minimize path searches.
*/
static void
stripdir (
char *dir, /* start with this directory */
char *path, /* pathname of current directory */
char *extnlist[], /* list of file extensions */
int mode /* ALL, ALLBUT, ONLY */
)
{
char oldpath[SZ_PATHNAME+1];
char newpath[SZ_PATHNAME+1];
char fname[SZ_PATHNAME+1];
int deleteit, dp;
if (debug) {
fprintf (stderr, "stripdir %s%s\n", path, dir);
fflush (stderr);
}
if ((dp = os_diropen (dir)) == ERR) {
fprintf (stderr, "cannot open subdirectory `%s'\n", dir);
fflush (stderr);
return;
}
os_fpathname ("", oldpath, SZ_PATHNAME);
sprintf (newpath, "%s%s/", path, dir);
/* Descend into the subdirectory.
*/
if (strcmp (dir, ".") != 0)
if (os_chdir (dir) == ERR) {
os_dirclose (dp);
fprintf (stderr, "cannot change directory to `%s'\n", newpath);
fflush (stderr);
return;
}
/* Scan through the directory.
*/
while (os_gfdir (dp, fname, SZ_PATHNAME) > 0) {
if (os_filetype (fname) == DIRECTORY_FILE) {
stripdir (fname, newpath, extnlist, mode);
continue;
} else if (mode == ALL) {
deleteit = YES;
} else {
deleteit = got_one (fname, extnlist);
if (mode == ALLBUT)
deleteit = !deleteit;
}
if (!deleteit)
continue;
if (verbose) {
printf ("%s%s\n", newpath, fname);
fflush (stdout);
}
if (execute) {
if (os_delete (fname) == ERR) {
fprintf (stderr, "cannot delete `%s'\n", fname);
fflush (stderr);
}
}
}
/* Return from the subdirectory.
*/
if (strcmp (dir, ".") != 0)
if (os_chdir (oldpath) == ERR) {
fprintf (stderr, "cannot return from subdirectory `%s'\n",
newpath);
fflush (stderr);
}
os_dirclose (dp);
}
/* GOT_ONE -- Check the file extension, if there is one, to see if the
* file is on the list of extensions.
*/
static int
got_one (
char *fname, /* file to be examined */
char *extnlist[] /* list of extensions */
)
{
register char *ip, *ep;
register int ch, i;
char *extn;
extn = NULL;
for (ip=fname; (ch = *ip); ip++)
if (ch == '.')
extn = ip;
/* If the file has no extension it is not on the list.
*/
if (extn == NULL)
return (NO);
/* Check the list of extensions.
*/
ch = *(extn + 1);
if (extnlist[0] != NULL)
for (i=0; (ep = extnlist[i]); i++)
if (*(ep+1) == ch)
if (strcmp (ep, extn) == 0)
return (YES);
return (NO);
}
|