/* FILE: REPT_MSG.C Routines to report message to screen and to specify if same message must be echoed to some file. */ #include #include #include static FILE *fp_msg = NULL; static char buf[1024]; static char bigbuf[10000]; static int to_stdout = 1; static int to_bigbuf = 0; void set_msg_stdout(int tf) { to_stdout = tf; } void set_msg_bigbuf(int tf) { to_bigbuf = tf; bigbuf[0] = '\0'; } void set_msg_file(FILE *fp) /* original api */ { fp_msg = fp; /* set file pointer for message echo */ } int get_msg(char *msg_ptr, int n) /* n is the number of bytes available in msg_ptr, not including the null */ /* copy bigbuf contents into msg_ptr, and reset bigbuf */ /* This can be called twice, once with a NULL first argument to return the length of bigbuf, and then a second time after allocating sufficient memory, pointed to by non-NULL msg_ptr. */ { int nbuf; nbuf = strlen(bigbuf); if (msg_ptr == NULL) { return nbuf; } if (nbuf <= n) /* msg_ptr must be of length n+1 */ { strcpy(msg_ptr, bigbuf); } else { nbuf = -1; /* error return */ } bigbuf[0] = '\0'; return nbuf; } void report_msg(char *msg) /* original api */ { if (to_stdout) { fprintf(stdout, "%s", msg); /* print to screen */ } if (fp_msg != NULL) { fprintf(fp_msg, "%s", msg); /* echo to file */ } if (to_bigbuf) { strncat(bigbuf, msg, 9999); } } void sprintf_msg(char *fmt, ...) { va_list args; va_start(args, fmt); vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); report_msg(buf); }