#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libadm.h"
#define DEFMSG "ERROR: "
#define MS sizeof (DEFMSG)
#define INVINP "invalid input"
void
puterror(FILE *fp, char *defmesg, char *error)
{
char *tmp;
size_t n;
if (error == NULL) {
n = (defmesg ? strlen(defmesg) : strlen(INVINP));
tmp = calloc(MS+n+1, sizeof (char));
(void) strcpy(tmp, DEFMSG);
(void) strcat(tmp, defmesg ? defmesg : INVINP);
} else if (defmesg != NULL) {
n = strlen(error);
if (error[0] == '~') {
tmp = calloc(MS+n+strlen(defmesg)+2, sizeof (char));
(void) strcpy(tmp, DEFMSG);
(void) strcat(tmp, defmesg);
(void) strcat(tmp, "\n");
++error;
(void) strcat(tmp, error);
} else if (n && (error[n-1] == '~')) {
tmp = calloc(MS+n+strlen(defmesg)+2, sizeof (char));
(void) strcpy(tmp, DEFMSG);
(void) strcat(tmp, error);
tmp[MS-1+n-1] = '\0';
(void) strcat(tmp, "\n");
(void) strcat(tmp, defmesg);
} else {
tmp = calloc(MS+n+1, sizeof (char));
(void) strcpy(tmp, DEFMSG);
(void) strcat(tmp, error);
}
} else {
n = strlen(error);
tmp = calloc(MS+n+1, sizeof (char));
(void) strcpy(tmp, DEFMSG);
(void) strcat(tmp, error);
}
(void) puttext(fp, tmp, ckindent, ckwidth);
(void) fputc('\n', fp);
free(tmp);
}