Should I be annoyed with this code.

Frank P. Bresz fpb at ittc.wec.com
Sat Jul 21 07:41:52 AEST 1990


First off, this is not my code.  However fairly often I must look at code
similar to this to debug anothers.  Is it me or does this look OK to you.

P.S.  I left in the tabs to annoy you since they annoy me too.

All flames etc via email don't waste bandwidth.  If you want to flame go
ahead I can take it.  Either flames accepted anti-me or anti-code.

					Frank P. Bresz }*{
					ITTC Network Administrator
+--------------------+
|fbresz at ittc.wec.com |  My opinions are my own, I'm not paid
|uunet!ittc!fbresz   |  enough to make an official statement  
|(412)733-6749       |  +-----------------------------------+
|Fax: (412)733-6444  |  |      THIS SPACE FOR SALE!!!       |
+--------------------+  +-----------------------------------+

---------------     Code ---------------------------
#include <stdio.h>
#include <strings.h>
#include <sys/fcntl.h>
#include <sys/termios.h>

extern	char *	optarg;
extern	int		optind;
extern	int		opterr;

/*----------------------------------------------------------------------------*/

#define USAGE\
	"Usage: %s [-d <device>] [<cmd>...]\n"

#define CR "\r"

/* Evaluate the result of a system call. */
#define SYSCHK(op)\
	if (-1 == (op)) {\
		perror (ProgId);\
		fprintf (stderr, "%s: line %d: file %s: system error.\n",\
			ProgId, __LINE__, __FILE__);\
		exit (__LINE__);\
	}

/*----------------------------------------------------------------------------*/
/* Cast of procedures in order of appearance: */

/*public*/ int main ();
static void openatty ();
static void closetty ();
static void force2tty ();

/*----------------------------------------------------------------------------*/
/* Local Data Definitions: */

static char *	ProgId = __FILE__;
static int		tty_fd = -1;

/*----------------------------------------------------------------------------*/

/*PROC*/ int main (argc, argv)
	int		argc;
	char *	argv[];
{
	static	char *	device = "/dev/console";
	auto	char	ch;

	ProgId = argv[0];
	while ((ch = getopt (argc, argv, "d:")) != -1) {
		switch (ch) {
		  case 'd':
			device = optarg;
			break;
		  case '?':
			fprintf (stderr, "%s: error in getopt().\n", ProgId);
			fprintf (stderr, USAGE, ProgId);
			exit (1);
		}
	}
	openatty (device);
	while (optind < argc) {
		force2tty (argv[optind++]), force2tty (CR);
		if (optind < argc) sleep (3);
	}
	closetty ();
	exit (0);
}
/*ENDP(main)*/

/*----------------------------------------------------------------------------*/

/*PROC*/ static void openatty (device)
	char	device[];
{
	SYSCHK(tty_fd = open (device, (O_RDWR|O_NDELAY), 0))
}
/*ENDP(openatty)*/

/*----------------------------------------------------------------------------*/

/*PROC*/ static void closetty ()
{
	SYSCHK(close (tty_fd))
}
/*ENDP(closetty)*/

/*----------------------------------------------------------------------------*/

/*PROC*/ static void force2tty (string)
	char *	string;
{
	while (*string) {
		SYSCHK(ioctl(tty_fd,TIOCSTI,string))
		++string;
	}
}
/*ENDP(force2tty)*/

/*----------------------------------------------------------------------------*/



More information about the Comp.lang.c mailing list