Initial revision master
authorcsc_disk <csc_disk>
Thu, 23 May 1996 07:54:55 +0000 (07:54 +0000)
committercsc_disk <csc_disk>
Thu, 23 May 1996 07:54:55 +0000 (07:54 +0000)
Makefile [new file with mode: 0644]
add.c [new file with mode: 0644]
csc.quota.h [new file with mode: 0644]
info.c [new file with mode: 0644]
main.c [new file with mode: 0644]
misc.c [new file with mode: 0644]
show.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..fbd9b4e
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+#
+# $Id$
+#
+
+DEBUG=-g
+
+CC=gcc
+CFLAGS=-Wall $(DEBUG) -I/opt/gdbm/include
+LDFLAGS=$(DEBUG) -L/opt/gdbm/lib
+
+LIBS=-lgdbm
+OBJS= add.o info.o main.o misc.o show.o 
+
+all: csc.quota
+
+csc.quota: $(OBJS)
+       gcc $(LDFLAGS) -o csc.quota $(OBJS) $(LIBS)
+
diff --git a/add.c b/add.c
new file mode 100644 (file)
index 0000000..a979977
--- /dev/null
+++ b/add.c
@@ -0,0 +1,56 @@
+/*
+ * $Id$
+ * add.c - add a new entry to the database
+ */
+
+#include <gdbm.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "csc.quota.h"
+
+void add(int argc, char ** argv) {
+    GDBM_FILE dbf;
+    char db_file_name[PATH_MAX];
+    struct quota_entry entry;
+    datum key, value;
+    int ret;
+
+    if (argc != 2) {
+        fputs("Usage: add userid quota\n", stderr);
+        return;
+    }
+
+    key.dptr = strdup(argv[0]);
+    key.dsize = strlen(key.dptr) + 1;
+    value.dptr = (char *) &entry;
+    value.dsize = sizeof(struct quota_entry);
+    entry.disk_num = 0;
+    entry.megabytes = atoi(argv[1]);
+    entry.flags = QUOTAF_NORMAL;
+
+    sprintf(db_file_name, "%s/" QUOTA_DB,
+        QUOTA_DIR, current_term());
+
+    dbf = gdbm_open(db_file_name, BUFSIZ, GDBM_WRCREAT, 0600, NULL);
+
+    if (dbf == NULL) {
+        fprintf(stderr, "error opening the quota database: %s\n",
+            gdbm_strerror(gdbm_errno));
+        return;
+    }
+
+    ret = gdbm_store(dbf, key, value, GDBM_INSERT);
+    /* we immediately close the gdbm since we're done with it */
+    gdbm_close(dbf);
+    if (ret == 1) {
+        fprintf(stderr, "%s already in the database\n",
+            argv[0]);
+    } else if (ret == -1) {
+        fputs("add error\n", stderr);
+    }
+
+    return;
+}
diff --git a/csc.quota.h b/csc.quota.h
new file mode 100644 (file)
index 0000000..7183efe
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * csc.quota.h - Header file for the CSC quota functions
+ */
+
+#ifndef csc_quota_h
+#define csc_quota_h
+
+#define QUOTA_DIR      "/u/csc_disk/records/new"
+#define QUOTA_DB       "quotas.%s.db"          /* %s -> term */
+#define QUOTA_LOG      "quota.%s.log"          /* %s -> term */
+
+const char *current_term(void);
+
+struct quota_entry {
+    unsigned char disk_num; /* disk number */
+    unsigned long megabytes; /* the number of megabytes requested */
+    int flags;
+};
+
+#define QUOTAF_NORMAL   0
+
+/* command functions */
+void add(int argc, char **argv);
+void info(int argc, char **argv);
+void show(int argc, char **argv);
+
+#endif /*  csc_quota_h */
+
diff --git a/info.c b/info.c
new file mode 100644 (file)
index 0000000..ac43e8e
--- /dev/null
+++ b/info.c
@@ -0,0 +1,12 @@
+/*
+ * $id$
+ * info.c - The info function
+ */
+
+#include <stdio.h>
+
+#include "csc.quota.h"
+
+void info(int argc, char ** argv) {
+    printf("The current term is %s\n", current_term());
+}
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..94273a7
--- /dev/null
+++ b/main.c
@@ -0,0 +1,43 @@
+/*
+ * $Id$
+ * main.c - the main program for the CSC quota
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "csc.quota.h"
+
+static struct {
+    const char * name;
+    void (*func)(int, char **);
+} commands[] = {
+   { "add", add },
+   { "info", info },
+   { "show", show },
+   { NULL, NULL }
+};
+
+int main(int argc, char **argv) {
+    int i;
+
+    if (argc < 2) {
+        fprintf(stderr, "Usage: %s command ...\n",
+                argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    for (i = 0; commands[i].name != NULL; i++) {
+        if (strcmp(argv[1], commands[i].name) == 0) {
+            commands[i].func(argc - 2, argv + 2);
+            return EXIT_SUCCESS;
+        }
+    }
+
+    fprintf(stderr, "%s: unrecognized command: %s\n",
+        argv[0], argv[1]);
+
+    return EXIT_FAILURE;
+}
+
diff --git a/misc.c b/misc.c
new file mode 100644 (file)
index 0000000..c100b4e
--- /dev/null
+++ b/misc.c
@@ -0,0 +1,52 @@
+/*
+ * $Id$
+ * misc.c - miscellaneous functions for the quota subsystem
+ */
+
+#include <assert.h>
+#include <time.h>
+
+/*
+ * current_term - returns a three character string (eg. w96)
+ *                specifying which term it is now
+ */
+const char * 
+current_term(void) {
+    time_t curtime = time(NULL);
+    struct tm * ltm = localtime(&curtime);
+    static char term[4];
+
+    switch (ltm->tm_mon) {
+    case 0:
+    case 1:
+    case 2:
+    case 3:
+        term[0] = 'w';
+        break;
+
+    case 4:
+    case 5:
+    case 6:
+    case 7:
+        term[0] = 's';
+        break;
+
+    case 8:
+    case 9:
+    case 10:
+    case 11:
+        term[0] = 'f';
+        break;
+
+    default:
+        assert(0);  /* can't happen */
+    }
+
+    term[1] = '0' + (ltm->tm_year % 100) / 10;
+    term[2] = '0' + (ltm->tm_year % 10);
+    term[3] = 0;
+
+    return term;
+}
+
+
diff --git a/show.c b/show.c
new file mode 100644 (file)
index 0000000..371b9e1
--- /dev/null
+++ b/show.c
@@ -0,0 +1,50 @@
+/*
+ * $Id$
+ * show.c - implements the show command.
+ */
+
+#include <gdbm.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "csc.quota.h"
+
+void show(int argc, char **argv) {
+    GDBM_FILE dbf;
+    datum key, value;
+
+    if (argc != 1) {
+        fputs("Usage: show user\n", stderr);
+        return;
+    }
+
+    key.dptr = strdup(argv[0]);
+    key.dsize = strlen(argv[0]) + 1;
+
+    dbf = gdbm_open("/u/csc_disk/records/new/quotas.s96.db",
+        BUFSIZ, GDBM_READER, 0600, NULL);
+    
+    if (!dbf) {
+        fprintf(stderr, "Error opening the quota database: %s\n",
+            gdbm_strerror(gdbm_errno));
+        return;
+    }
+
+    value = gdbm_fetch(dbf, key);
+    gdbm_close(dbf);
+    if (value.dptr) {
+        struct quota_entry * entry = (struct quota_entry *) value.dptr;
+
+        printf("Disk #: %d\n", entry->disk_num);
+        printf("Quota : %ldM\n", entry->megabytes);
+        printf("Flags :");
+        /* insert flag processing */
+        printf("\n");
+    } else {
+        fprintf(stderr, "%s is not in the database\n",
+            argv[0]);
+    }
+
+}
+
+