-#if (!defined(lint) && !defined(SABER))
- static char rcsid_module_c[] = "$Header$";
-#endif
-
-/* This is the file utils.c for the Moira Client, which allows users
+/* $Id$
+ *
+ * This is the file utils.c for the Moira Client, which allows users
* to quickly and easily maintain most parts of the Moira database.
* It Contains: Many useful utility functions.
*
* Created: 4/25/88
* By: Chris D. Peterson
*
- * $Source$
- * $Author$
- * $Header$
- *
- * Copyright 1988 by the Massachusetts Institute of Technology.
- *
- * For further information on copyright and distribution
- * see the file mit-copyright.h
+ * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <mit-copyright.h>
#include <moira.h>
#include <moira_site.h>
-#include <menu.h>
-#include <ctype.h>
-
-#include "mit-copyright.h"
#include "defs.h"
#include "f_defs.h"
#include "globals.h"
-#include <netdb.h> /* for gethostbyname. */
+
#include <sys/types.h>
+
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <netdb.h> /* for gethostbyname. */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+RCSID("$Header$");
/* Function Name: AddQueue
* Description: Adds an element to a queue
* Returns: none.
*/
-static void AddQueue(struct qelem *elem, struct qelem *pred)
+static void AddQueue(struct mqelem *elem, struct mqelem *pred)
{
if (!pred)
{
* Returns: none.
*/
-static void RemoveQueue(struct qelem *elem)
+static void RemoveQueue(struct mqelem *elem)
{
if (elem->q_forw)
elem->q_forw->q_back = elem->q_back;
if (!ret)
return ret;
for (i = 0; info[i]; i++)
- ret[i] = Strsave(info[i]);
+ ret[i] = strdup(info[i]);
ret[i] = NULL;
return ret;
}
}
/* Function Name: QueueTop
- * Description: returns a qelem pointer that points to the top of
+ * Description: returns a mqelem pointer that points to the top of
* a queue.
* Arguments: elem - any element of a queue.
* Returns: top element of a queue.
*/
-struct qelem *QueueTop(struct qelem *elem)
+struct mqelem *QueueTop(struct mqelem *elem)
{
if (!elem) /* NULL returns NULL. */
return NULL;
* Returns: none
*/
-static void FreeQueueElem(struct qelem *elem)
+static void FreeQueueElem(struct mqelem *elem)
{
- char **info = (char **) elem->q_data;
+ char **info = elem->q_data;
if (info)
{
* Returns: none.
*/
-void FreeQueue(struct qelem *elem)
+void FreeQueue(struct mqelem *elem)
{
- struct qelem *temp, *local = QueueTop(elem);
+ struct mqelem *temp, *local = QueueTop(elem);
while (local)
{
* Returns: none.
*/
-int QueueCount(struct qelem *elem)
+int QueueCount(struct mqelem *elem)
{
int count = 0;
elem = QueueTop(elem);
* Arguments: argc, argv, - information returned from the query returned
* in argv.
* data - the previous element on the queue, this data will be
- * stored in a qelem struct immediatly after this elem.
+ * stored in a mqelem struct immediatly after this elem.
* If NULL then a new queue will be created. This value
* is updated to the current element at the end off the
* call.
* Returns: MR_CONT, or MR_ABORT if it has problems.
*/
-int StoreInfo(int argc, char **argv, char *data)
+int StoreInfo(int argc, char **argv, void *data)
{
char **info = malloc(MAX_ARGS_SIZE * sizeof(char *));
- struct qelem **old_elem = (struct qelem **) data;
- struct qelem *new_elem = malloc(sizeof(struct qelem));
+ struct mqelem **old_elem = data;
+ struct mqelem *new_elem = malloc(sizeof(struct mqelem));
int count;
if (!new_elem || !info)
}
for (count = 0; count < argc; count++)
- info[count] = Strsave(argv[count]);
+ info[count] = strdup(argv[count]);
info[count] = NULL; /* NULL terminate this sucker. */
- new_elem->q_data = (char *) info;
+ new_elem->q_data = info;
AddQueue(new_elem, *old_elem);
*old_elem = new_elem;
return number;
}
-/* Function Name: Scream
- * Description: Bitch Loudly and exit, it is intended as a callback
- * function for queries that should never return a value.
- * Arguments: none
- * Returns: doesn't exit.
- */
-
-int Scream(void)
-{
- com_err(program_name, 0,
- "\nA Moira update returned a value -- programmer botch\n");
- mr_disconnect();
- exit(1);
- /*NOTREACHED*/
- return -1;
-}
-
/* Function Name: PromptWithDefault
* Description: allows a user to be prompted for input, and given a
* default choice.
* Returns: DM_NORMAL.
*/
-int ToggleVerboseMode(void)
+int ToggleVerboseMode(int argc, char **argv)
{
verbose = !verbose;
return DM_NORMAL;
}
-/* Function Name: NullFunc
- * Description: dummy callback routine
- * Arguments: none
- * Returns: MR_CONT
- */
-
-int NullFunc(void)
-{
- return MR_CONT;
-}
-
/* Function Name: SlipInNewName
* Description: Slips the new name into the number 2 slot of a list, and
* returns a pointer to the new list.
if (strcmp(buf, *pointer))
{
free(*pointer);
- *pointer = Strsave(buf);
+ *pointer = strdup(buf);
}
}
return SUB_NORMAL;
case TRUE:
if (*pointer)
free(*pointer);
- *pointer = Strsave(DEFAULT_YES);
+ *pointer = strdup(DEFAULT_YES);
break;
case FALSE:
if (*pointer)
free(*pointer);
- *pointer = Strsave(DEFAULT_NO);
+ *pointer = strdup(DEFAULT_NO);
break;
case -1:
default:
FreeAndClear(current, TRUE);
sprintf(ret_value, "%d", new_val);
- *current = Strsave(ret_value);
+ *current = strdup(ret_value);
return SUB_NORMAL;
}
-/* Function Name: Strsave
- * Description: save a string.
- * Arguments: string - the string to save.
- * Returns: The malloced string, now safely saved, or NULL.
- */
-
-char *Strsave(char *str)
-{
- char *newstr = malloc(strlen(str) + 1);
-
- if (!newstr)
- return NULL;
- else
- return strcpy(newstr, str);
-}
-
-
/* atot: convert ASCII integer unix time into human readable date string */
char *atot(char *itime)
{
time_t time;
- char *ct, *ctime();
+ char *ct;
time = (time_t) atoi(itime);
ct = ctime(&time);
* Returns: MR_CONT
*/
-int Print(int argc, char **argv, char *callback)
+int Print(int argc, char **argv, void *callback)
{
char buf[BUFSIZ];
int i;
* Returns: MR_CONT or MR_QUIT.
*/
-int PrintByType(int argc, char **argv, char *callback)
+int PrintByType(int argc, char **argv, void *callback)
{
if (!callback)
return Print(argc, argv, callback);
* Returns: none.
*/
-void Loop(struct qelem *elem, FVoid func)
+void Loop(struct mqelem *elem, void (*func)(char **))
{
while (elem)
{
- char **info = (char **) elem->q_data;
- (*func) (info);
+ (*func) (elem->q_data);
elem = elem->q_forw;
}
}
* "Delete the list"
*/
-void QueryLoop(struct qelem *elem, FCharStar print_func,
- FVoid op_func, char *query_string)
+void QueryLoop(struct mqelem *elem, char * (*print_func)(char **),
+ void (*op_func)(char **, Bool), char *query_string)
{
Bool one_item;
char temp_buf[BUFSIZ], *name;
one_item = (QueueCount(elem) == 1);
while (elem)
{
- char **info = (char **) elem->q_data;
+ char **info = elem->q_data;
if (one_item)
(*op_func) (info, one_item);
* Returns: argv of values
*/
-struct qelem *GetTypeValues(char *tname)
+struct mqelem *GetTypeValues(char *tname)
{
int stat;
- char *argv[3], *p, **pp, *strsave();
- struct qelem *elem, *oelem;
- static struct qelem *cache = NULL;
+ char *argv[3], *p, **pp;
+ struct mqelem *elem, *oelem;
+ static struct mqelem *cache = NULL;
struct cache_elem {
char *cache_name;
- struct qelem *cache_data;
+ struct mqelem *cache_data;
} *ce;
for (elem = cache; elem; elem = elem->q_forw)
{
- ce = (struct cache_elem *) elem->q_data;
+ ce = elem->q_data;
if (!strcmp(ce->cache_name, tname))
return ce->cache_data;
}
argv[1] = "TYPE";
argv[2] = "*";
elem = NULL;
- if ((stat = do_mr_query("get_alias", 3, argv, StoreInfo, (char *)&elem)))
+ if ((stat = do_mr_query("get_alias", 3, argv, StoreInfo, &elem)))
{
com_err(program_name, stat, " in GetTypeValues");
return NULL;
oelem = elem;
for (elem = QueueTop(elem); elem; elem = elem->q_forw)
{
- pp = (char **) elem->q_data;
- p = strsave(pp[2]);
+ pp = elem->q_data;
+ p = strdup(pp[2]);
FreeInfo(pp);
elem->q_data = p;
}
- elem = malloc(sizeof(struct qelem));
+ elem = malloc(sizeof(struct mqelem));
ce = malloc(sizeof(struct cache_elem));
- ce->cache_name = strsave(tname);
+ ce->cache_name = strdup(tname);
ce->cache_data = QueueTop(oelem);
- elem->q_data = (char *) ce;
+ elem->q_data = ce;
AddQueue(elem, cache);
cache = QueueTop(elem);
return ce->cache_data;
int GetTypeFromUser(char *prompt, char *tname, char **pointer)
{
char def[BUFSIZ], buffer[BUFSIZ], *p, *argv[3];
- struct qelem *elem;
+ struct mqelem *elem;
int stat;
strcpy(def, *pointer);
Put_message(buffer);
for (elem = GetTypeValues(tname); elem; elem = elem->q_forw)
Put_message(elem->q_data);
- *pointer = strsave(def);
+ *pointer = strdup(def);
return GetTypeFromUser(prompt, tname, pointer);
}
for (elem = GetTypeValues(tname); elem; elem = elem->q_forw)
*p = toupper(*p);
}
}
- if ((stat = do_mr_query("add_alias", 3, argv, Scream, NULL)))
+ if ((stat = do_mr_query("add_alias", 3, argv, NULL, NULL)))
com_err(program_name, stat, " in add_alias");
else
{
- elem = malloc(sizeof(struct qelem));
- elem->q_data = strsave(*pointer);
+ elem = malloc(sizeof(struct mqelem));
+ elem->q_data = strdup(*pointer);
AddQueue(elem, GetTypeValues(tname));
Put_message("Done.");
}
}
- *pointer = strsave(def);
+ *pointer = strdup(def);
return GetTypeFromUser(prompt, tname, pointer);
}
int ret;
addr.s_addr = htonl(atoi(*pointer));
- value = strsave(inet_ntoa(addr));
+ value = strdup(inet_ntoa(addr));
ret = GetValueFromUser(prompt, &value);
if (ret == SUB_ERROR)
return SUB_ERROR;
addr.s_addr = inet_addr(value);
free(pointer);
- sprintf(buf, "%d", ntohl(addr.s_addr));
- *pointer = strsave(buf);
+ sprintf(buf, "%ld", ntohl(addr.s_addr));
+ *pointer = strdup(buf);
return SUB_NORMAL;
}
-int do_mr_query(char *name, int argc, char **argv, int (*proc)(), char *hint)
+int do_mr_query(char *name, int argc, char **argv,
+ int (*proc)(int, char **, void *), void *hint)
{
int status;
extern char *whoami, *moira_server;