
/*
 * common.c - functions used by client.c, server.c, RPCdg.c & RPCst.c.
 *
 * CSc 645 Spring 1995  Jim Warhol
 *
 * common.c contains basic error and output handling functions called by
 * the client.c & server.c programs, and by the RPCdg.c & RPCst.c network
 * library routines. It must be compiled in with the main program by
 * including the name common.c on the cc command line.
 *
 * common.c is used in conjunction with the common.h header file.
 *
 */

#include <strings.h>

/* compile-time constants */

#define stdoutx	1		/* standard output file ordinal */
#define stderrx	2		/* standard error file ordinal */

/*
 * error(char errmsg[]);
 *
 * error prints out an error message followed by a new line character, 
 * and then aborts program execution.
 *
 */

void error(char errmsg[])
{
   write(stderrx, errmsg, strlen(errmsg));
   write(stderrx, "\n", 2);
   exit(-1);
}

/*
 * void print(char msg[]);
 *
 * print writes a message to standard output. NOTE: stdoutx is used 
 * instead of stdout because of NeXT system header file anomalies.
 *
 */

void print(char msg[])
{
   write(stdoutx, msg, strlen(msg));
   return;
}

/*
 * void printn(char msg[], int n);
 *
 * printn writes a format string containing an integer to standard output.
 * NOTE: stdoutx is used instead of stdout because of NeXT system header 
 * file anomalies.
 *
 */

void printn(char msg[], int n)
{
   char printbuf[100];

   if (strlen(msg) > 100 - 10)	/* ensure room for msg and integer */
      error("server printn: Message too long to fit in print buffer.");

   sprintf(printbuf, msg, n);
   write(stdoutx, printbuf, strlen(printbuf));
   return;
}

/*
 * void prints(char msg[], char string[]);
 *
 * printn writes a format string containing a string to standard output.
 * NOTE: stdoutx is used instead of stdout because of NeXT system header 
 * file anomalies.
 *
 */

void prints(char msg[], char string[])
{
   char printbuf[100];

   if (strlen(msg) > 100 - strlen(string))	/* ensure room */
      error("server prints: Message too long to fit in print buffer.");

   sprintf(printbuf, msg, string);
   write(stdoutx, printbuf, strlen(printbuf));
   return;
}

