|
|
Power 6/32 Unix version 1.21
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <stdio.h>
char sccsid[] = "@(#)bsclean.c 1.3 REL";
char pathname[50] = "/usr/spool/bscbatch/";
/* D.L.Buck and Associates, Inc. - October, 1982.
*
* COPYRIGHT NOTICE:
* Copyright c 1982 - An unpublished work by
* D.L.Buck and Associates, Inc.
*
* PROPRIETARY RIGHTS NOTICE:
* All rights reserved. This document and program contains
* proprietary information of D.L.Buck and Associates,Inc.
* of San Jose, California, U.S.A., embodying confidential
* information, ideas, and expressions, no part of which
* may be reproduced, or transmitted in any form or by any
* means, electronic, mechanical, or otherwise, without
* the written permission of D.L.Buck and Associates, Inc.
*
* NAME
* bsclean -- bsc spool directory cleaner
*
* SYNOPSIS
* bsclean [-t=hh] [hostname]
*
* DESCRIPTION
* bsclean removes all user files older than hh hours from the
* spool directory of hostname. hh default is 72. hostname
* default is defined by the system administrator.
*
* ALGORITHM
* 1. get host and unlink age (from default,or command line if specified)
* 2. construct directory pathname
* 3. change directory using pathname
* 4. open directory
* 5. while there's another directory entry to read, read it
* 6. if it's not special:
* get its statistics
* if it's younger than unlink age, continue
* otherwise, unlink it
* 7. close directory when all done
*/
main(argc,argv)
int argc;
char *argv[];
{
int fd; /* file descriptor used by open */
int i; /* friendly, neighborhood index */
struct direct d; /* directory entry structure; contains file
name and inode number (index to where all the
data about a file is kept, except its name) */
struct stat t; /* function returns a structure which contains
the inode data for the specified file */
char *ctime(); /* function returns a string like:
Sat Aug 21 14:30:41 1982\n\0 */
char host[20]; /* contains the host name */
char filename[15]; /* filename holding area */
long age; /* age at which to unlink user spool files */
long time(); /* function returns current time */
long atol(); /* function converts ascii to long */
age = time((long *)0)-259200L; /* default age is 72 hours */
strcpy(host,"default"); /* default host name */
if (argc > 3) /* can have only 2 arguments besides progname */
{
fprintf(stderr,"usage: bsclean [-t=hh] [hostname]\n");
exit(1);
}
/* get options from command line */
for (i=1;i<argc;++i)
{
/* get age at which to unlink user spool files (if specified) */
if (strncmp(argv[i],"-t=",3) == 0)
{
if (argv[i][3] < '0' || argv[i][3] > '9') {
fprintf(stderr,
"bsclean: not a numeric value <%s>\n",
argv[i]);
exit(1);
}
age = (time((long *)0) - (3600L * atol(&argv[i][3])));
continue;
}
/* hostname, if specified */
strcpy(host,argv[i]);
}
strcat(pathname,host);
if (chdir(pathname) == -1)
{
fprintf(stderr,"bsclean: host <%s> has no spool directory\n",host);
exit(1);
}
fd = open(".",0); /* open current directory (".") */
/* the following reads through the directory,checks that the file is
active (d.d_inode != 0) and that it isn't a directory or a special file
(first char of file name != '.'). It gets the file's vital statistics using the
stat function, checks the file's age (t.st_ctime), and unlinks it if it's
older than either the default age (72 hours) or the user-specified age. */
while (read(fd,&d,sizeof d) == sizeof d)
{
if (d.d_ino > 0 && d.d_name[0] != '.' && d.d_name[0] == 'P')
{
stat(d.d_name,&t);
if (t.st_ctime > age)
continue;
if (unlink (d.d_name) == -1)
{
strncpy(filename,d.d_name,14);
filename[14] = '\0';
fprintf(stderr,
"bsclean: can't remove <%s> from <%s> spool directory\n",
filename,host);
exit(1);
}
}
}
close(fd);
}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.