[news.software.b] spacefor.c

denny@mcmi.uucp (Denny Page) (08/03/89)

Everybody gripes about spacefor needing to be in C, but nobody bothers
to write one...  Anyway, here is code for sysVRx boxes.  Use as you
wish (but do so at your own risk... as always, your milage may vary).

Denny

/*
  Spacefor.c: SysV Replacement for spacefor (shell script) in cnews.
  Change the values below as appropriate.

  Denny Page	July 25, 1989
  */

/* #define HAVE_STATFS                   /* If you have statfs() syscall */

#define BLOCKSIZE 512

#define DIR_INCOMING	"/usr/spool/news/in.coming"
#define DIR_NEWSARTS	"/usr/spool/news"
#define DIR_NEWSCTL	"/usr/local/lib/news"
#define DIR_OUTGOING	"/usr/spool/uucp"
#define DIR_ARCHIVE	"/usr/spool/news"

#define MIN_INCOMING	5000
#define MIN_NEWSARTS	5000
#define MIN_NEWSCTL	3000
#define MIN_OUTGOING	10000
#define MIN_ARCHIVE	1000

/***************************************************************************/

#include	<sys/types.h>
#include	<stdio.h>


#ifdef HAVE_STATFS
#include <sys/statfs.h>
#define FREEBLOCKS statfsb.f_bfree
#define FREENODES statfsb.f_ffree
struct statfs statfsb;
#else /* HAVE_STATFS */
#include <sys/stat.h>
#include <ustat.h>
#define FREEBLOCKS ustatb.f_tfree
#define FREENODES ustatb.f_tinode
struct stat statb;
struct ustat ustatb;
#endif /* HAVE_STATFS */



main(argc, argv)
    int argc;
    char **argv;
{
    int size, minblocks, available;
    char *dirname;

    if (argc < 3) {
	fprintf(stderr, "usage: %s size place\n", argv[0]);
	exit(2);
    }

    if ((size = atoi(argv[1])) == 0) {
	printf("10000\n");
	exit(0);
    }

    if (! strcmp (argv[2], "incoming")) {
	dirname = DIR_INCOMING;
	minblocks = MIN_INCOMING;
    }
    else if (! strcmp (argv[2], "articles")) {
	dirname = DIR_NEWSARTS;
	minblocks = MIN_NEWSARTS;
    }
    else if (! strcmp (argv[2], "control")) {
	dirname = DIR_NEWSCTL;
	minblocks = MIN_NEWSCTL;
    }
    else if (! strcmp (argv[2], "outbound")) {
	dirname = DIR_OUTGOING;
	minblocks = MIN_OUTGOING;
    }
    else if (! strcmp (argv[2], "archive")) {
	dirname = DIR_ARCHIVE;
	minblocks = MIN_ARCHIVE;
    }
    else {
	fprintf(stderr, "%s: bad argument type `%s'\n",
		       argv[0], argv[2]);
	exit(2);
    }


#ifdef HAVE_STATFS
    if (statfs(dirname, &statfsb, sizeof(statfsb), 0) != 0) {
	perror("statfs failed");
	exit(1);
    }
#else /* HAVE_STATFS */
    if (stat(dirname, &statb) != 0) {
	perror("stat failed");
	exit(1);
    }
    if (ustat(statb.st_dev, &ustatb) != 0) {
	perror("ustat failed");
	exit(1);
    }
#endif /* HAVE_STATFS */


    available = ((FREEBLOCKS - minblocks) * BLOCKSIZE) / size;

    if (available <= 0 || FREENODES == 0)
	available = 0;

    printf("%d\n", available);
    exit(0);
}
-- 
Someday has arrived