diff options
author | Joseph Hunkeler <jhunk@stsci.edu> | 2011-04-06 10:16:42 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunk@stsci.edu> | 2011-04-06 10:16:42 -0400 |
commit | 6c81e2b11d5339ec290945b480c9054f5f9ba556 (patch) | |
tree | cc0d78a00b7ed83dad5065073de3f7c866cb3eac | |
parent | 1a984e6cca24a77b3c62253a605f3645d25d459e (diff) | |
download | duser-6c81e2b11d5339ec290945b480c9054f5f9ba556.tar.gz |
Command line argument "new" addedtesting-pre1b
user_new_list() function added
-rw-r--r-- | duser.c | 70 | ||||
-rw-r--r-- | duser.h | 1 |
2 files changed, 70 insertions, 1 deletions
@@ -8,6 +8,7 @@ #include <ctype.h> #include <errno.h> #include <unistd.h> +#include <time.h> #include <fcntl.h> #include <libgen.h> @@ -29,6 +30,7 @@ int CMD_FLAG_ADD = 0; int CMD_FLAG_LIST = 0; int CMD_FLAG_HELP = 0; int CMD_FLAG_LOOK = 0; +int CMD_FLAG_NEW = 0; int CMD_FLAG_NULL = 0; #ifndef HAVE_STRCHRNUL @@ -490,7 +492,7 @@ int user_add(const char* filename, const char* needle) void usage(const char* progname) { printf("Domouser v0.1a - jhunk@stsci.edu\n"); - printf("Usage: %s command address [list]\n", progname); + printf("Usage: %s command [address [list]]\n", progname); printf("Commands:\n"); printf(" help This usage statement\n"); printf(" add Add a user to a list\n"); @@ -498,6 +500,7 @@ void usage(const char* progname) printf(" delA Delete a user from all lists\n"); printf(" delL Completely delete a mailing list\n"); printf(" mod Modify a user in a list\n"); + printf(" new Creates a new list\n"); printf(" list Find and list a user in all lists\n"); printf(" look Find user in specific list\n"); printf("\n"); @@ -533,6 +536,10 @@ int user_cmd(const int argc, char* argv[]) { CMD_FLAG_MOD = 1; } + if((strncmp(cmd, "new", strlen(cmd))) == 0) + { + CMD_FLAG_NEW = 1; + } if((strncmp(cmd, "list", strlen(cmd))) == 0) { CMD_FLAG_LIST = 1; @@ -575,6 +582,37 @@ int check_cmd_string(char** args, const char* str2, int count) return -1; } +int user_new_list(const char* fname) +{ + char* filename = strdup(fname); + char message[BUFSIZ]; + FILE *fp = NULL; + time_t ttm; + struct tm *tmptr; + + if((access(filename, F_OK)) == 0) + { + fprintf(stderr, "%s: %s: File already exists\n", SELF, basename(filename)); + return -1; + } + + if((fp = fopen(filename, "w+")) == NULL) + { + fprintf(stderr, "FATAL: %s: %s: %s\n", SELF, basename(filename), strerror(errno)); + return -1; + } + + time(&ttm); + tmptr = localtime(&ttm); + snprintf(message, BUFSIZ, "#\n# Created on %s by UID %d\n#\n", asctime(tmptr), getuid()); + fputs(message, fp); + fflush(fp); + fclose(fp); + + free(filename); + return 0; +} + int main(int argc, char* argv[]) { if((cfg_open(CFG_PATH)) == 0) @@ -759,7 +797,37 @@ int main(int argc, char* argv[]) } return 0; } + if(CMD_FLAG_NEW) + { + memset(filename, 0L, PATH_MAX); + snprintf(filename, PATH_MAX, "%s%s", list_path, needle); + if(needle == NULL) + { + printf("You must specify a list to create\n"); + return -1; + } + + printf("Please review the information below:\n\n");; + printf("File: %s\n", filename); + printf("\nDo you wish to wish to create this list? [y/N] "); + + char choice = getchar(); + if((user_choice(choice)) == 0) + { + if((user_new_list(filename)) == 0) + { + printf("List added\n"); + COM(SELF, "Commmand: NEW\n"); + COM(SELF, "Created new list '%s'\n", basename(filename)); + } + } + else + { + printf("Aborting...\n"); + } + return 0; + } if(CMD_FLAG_HELP) { usage(progname); @@ -49,6 +49,7 @@ int user_del(record_t* rec); int user_cmd(const int argc, char* argv[]); int user_choice(char c); int user_add(const char* filename, const char* needle); +int user_new_list(const char* fname); char *dstrchrnul(const char* s, int c); #endif |