Annotation of cci/usr/src/usr.bin/bsc/cmd/batch/bsclean.c, revision 1.1.1.1

1.1       root        1: #include <sys/types.h>
                      2: #include <sys/dir.h>
                      3: #include <sys/stat.h>
                      4: #include <stdio.h>
                      5: 
                      6: char sccsid[] = "@(#)bsclean.c 1.3 REL";
                      7: char pathname[50] = "/usr/spool/bscbatch/";
                      8: 
                      9: /*     D.L.Buck and Associates, Inc. - October, 1982.
                     10:  *
                     11:  *     COPYRIGHT NOTICE:
                     12:  *             Copyright c 1982 - An unpublished work by 
                     13:  *             D.L.Buck and Associates, Inc.
                     14:  *
                     15:  *     PROPRIETARY RIGHTS NOTICE:
                     16:  *             All rights reserved. This document and program contains
                     17:  *             proprietary information of D.L.Buck and Associates,Inc.
                     18:  *             of San Jose, California, U.S.A., embodying confidential
                     19:  *             information,  ideas, and expressions,  no part of which
                     20:  *             may be reproduced, or transmitted in any form or by any
                     21:  *             means,  electronic,  mechanical,  or otherwise, without
                     22:  *             the written permission of D.L.Buck and Associates, Inc.
                     23:  *
                     24:  * NAME
                     25:  *     bsclean -- bsc spool directory cleaner
                     26:  *
                     27:  * SYNOPSIS
                     28:  *     bsclean [-t=hh] [hostname]
                     29:  *
                     30:  * DESCRIPTION
                     31:  *     bsclean removes all user files older than hh hours from the
                     32:  *     spool directory of hostname. hh default is 72. hostname
                     33:  *     default is defined by the system administrator.
                     34:  *
                     35:  * ALGORITHM
                     36:  *     1. get host and unlink age (from default,or command line if specified)
                     37:  *     2. construct directory pathname
                     38:  *     3. change directory using pathname
                     39:  *     4. open directory
                     40:  *     5. while there's another directory entry to read, read it
                     41:  *     6. if it's not special:
                     42:  *             get its statistics
                     43:  *             if it's younger than unlink age, continue
                     44:  *             otherwise, unlink it
                     45:  *     7. close directory when all done
                     46:  */
                     47: main(argc,argv)
                     48: int argc;
                     49: char *argv[];
                     50: {
                     51:        int fd;                 /* file descriptor used by open */
                     52:        int i;                  /* friendly, neighborhood index */
                     53:        struct direct d;        /* directory entry structure; contains file
                     54:                                  name and inode number (index to where all the
                     55:                                  data about a file is kept, except its name) */
                     56: 
                     57:        struct stat t;          /* function returns a structure which contains
                     58:                                   the inode data for the specified file */
                     59: 
                     60:        char *ctime();          /* function returns a string like:
                     61:                                   Sat Aug 21 14:30:41 1982\n\0 */
                     62:        char host[20];          /* contains the host name */
                     63:        char filename[15];      /* filename holding area */
                     64:        long age;               /* age at which to unlink user spool files */
                     65:        long time();            /* function returns current time */
                     66:        long atol();            /* function converts ascii to long */
                     67: 
                     68:        age = time((long *)0)-259200L;  /* default age is 72 hours */
                     69:         strcpy(host,"default"); /* default host name */
                     70: 
                     71:        if (argc > 3)           /* can have only 2 arguments besides progname */
                     72:        {
                     73:           fprintf(stderr,"usage: bsclean [-t=hh] [hostname]\n");
                     74:          exit(1);
                     75:        }
                     76:        /* get options from command line */
                     77:        for (i=1;i<argc;++i)
                     78:        {
                     79:        /* get age at which to unlink user spool files (if specified) */
                     80:                 if (strncmp(argv[i],"-t=",3) == 0)
                     81:                {
                     82:                        if (argv[i][3] < '0' || argv[i][3] > '9') {
                     83:                                fprintf(stderr,
                     84:                                "bsclean: not a numeric value <%s>\n",
                     85:                                argv[i]);
                     86:                                exit(1);
                     87:                        }
                     88:                  age = (time((long *)0) - (3600L * atol(&argv[i][3])));
                     89:                  continue;
                     90:                }
                     91: 
                     92:                /* hostname, if specified */
                     93:                strcpy(host,argv[i]);
                     94:        }
                     95: 
                     96:        strcat(pathname,host);
                     97:        if (chdir(pathname) == -1)
                     98:        {
                     99:             fprintf(stderr,"bsclean: host <%s> has no spool directory\n",host);
                    100:            exit(1);
                    101:        }
                    102: 
                    103:         fd = open(".",0);               /* open current directory (".") */
                    104: 
                    105: /* the following reads through the directory,checks that the file is
                    106: active (d.d_inode != 0) and that it isn't a directory or a special file
                    107: (first char of file name != '.'). It gets the file's vital statistics using the
                    108: stat function, checks the file's age (t.st_ctime), and unlinks it if it's
                    109: older than either the default age (72 hours) or the user-specified age. */
                    110: 
                    111:        while (read(fd,&d,sizeof d) == sizeof d)
                    112:        {
                    113:                if (d.d_ino > 0 && d.d_name[0] != '.' && d.d_name[0] == 'P')
                    114:                {
                    115:                    stat(d.d_name,&t);
                    116:                    if (t.st_ctime > age)
                    117:                       continue;
                    118:                    if (unlink (d.d_name) == -1)
                    119:                    {
                    120:                       strncpy(filename,d.d_name,14);
                    121:                       filename[14] = '\0';
                    122: 
                    123:                       fprintf(stderr,
                    124:                        "bsclean: can't remove <%s> from <%s> spool directory\n",
                    125:                       filename,host);
                    126:                       exit(1);
                    127:                    }
                    128:                }
                    129:        }
                    130:        close(fd);
                    131:   }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.