4.1bsd carrier enable program

utzoo!decvax!ittvax!swatt utzoo!decvax!ittvax!swatt
Sun Aug 30 18:00:57 AEST 1981


4.1bsd hassles:	whose got a carrier signal this week?

This is for all of you 4.1bsd users who have flakey cables and don't
want to build the system again each time you discover a new one.  I
don't have a manual page for it, but the options are obvious.

	carrier enable tty ...

will enable HARDWARE carrier detection on the specified lines
(the device name with the initial "/dev/" omitted).

	carrier disable tty ...

will disable HARDWARE carrier detection on the specified lines
(will use a software carrier bit that is ALWAYS set).

	carrier print tty ...

will show the current status of the named lines.  The special
line name "all" will apply to all lines.  This version only
handles DZ devices (sigh).

	- Alan S. Watt (decvax!ittvax!swatt)
::::::::::::
carrier.c
::::::::::::
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

#ifdef lint
# define IGNORE(X)	__void__=(int)(X)
  int __void__;
#else
# define IGNORE(X)	X
#endif

/* set/clear carrier required status for
 * dz terminals (KLUDGE)
 */
int cmd;
int todo;
int maxdev;
char devices[128];
char USAGE[] = "usage: \"carrier {enable|disable|print} {all|ttyname ...}\".";
char sysname[]	= "/vmunix";
char kmem[]	= "/dev/kmem";
extern char	*sys_errlisg[];
extern errno;
#define ENABLE	1
#define DISABLE 0
#define PRINT	2

char	*strcpy(),
	*strcat();

long	lseek();
main (argc, argv)
int argc;
char **argv;
{
	register i;

	if (argc < 3)
		error (USAGE);
	if (strcmp (argv[1], "enable") == 0)
		cmd = ENABLE;
	else if (strcmp (argv[1], "disable") == 0)
		cmd = DISABLE;
	else if (strcmp (argv[1], "print") == 0)
		cmd = PRINT;
	else
		error (USAGE);
	if (argc == 3 && strcmp (argv[2], "all") == 0) {
		for (i = 0; i < sizeof devices; i++)
			devices[i] = 1;
		todo = 1;
	}
	else
		for (argc-=2,argv+=2; argc > 0; argc--, argv++)
			bldcarrier (*argv);
	if (todo == 0)
		error ("no termials to fix");
	fixterms ();
}

/* VARARGS1 */
fperror (gripe, args)
char *gripe;
{
	extern int errno;
	extern char *sys_errlist[];
	int sverrno = errno;

	_doprnt (gripe, &args, stderr);
	error (": %s", sys_errlist[sverrno]);
}

/* VARARGS1 */
remark (fmt, args)
char *fmt;
{
	_doprnt (fmt, &args, stderr);
	IGNORE (putc ('\n', stderr));
}

/* VARARGS1 */
error(gripe, args)
char *gripe;
{
	_doprnt (gripe, &args, stderr);
	IGNORE (putc ('\n', stderr));
	exit (-1);
}

bldcarrier (term)
char *term;
{
	static char devdir[] = "/dev/";
	char devbuf[64];
	struct stat sb;
	int devno;

	if (stat (term, (struct stat *)&sb) == -1 && errno == ENOENT) {
		strcpy (devbuf, devdir);
		strcat (devbuf, term);
		if (stat (devbuf, (struct stat *)&sb) == -1) {
			remark ("device %s not found (skipped)", term);
			return;
		}
	}
	devno = minor (sb.st_rdev);
	if (devno > maxdev)
		maxdev = devno;
	devices[devno] = 1;
	todo += 1;
}

#include <nlist.h>
struct nlist nl[] ={
	{ "_dzsoftCAR" },	/* Software carrier present bit array */
#define SOFTCAR	0
	{ "_dz_cnt" },		/* count of DZ devices configured in system */
#define DZCOUNT	1
	0
};

#define BITSET(i, base)	(base[i>>3]|=(1<<(i%8)))
#define BITCLR(i, base)	(base[i>>3]&= ~(1<<(i%8)))
#define BITTST(i, base)	(base[i>>3]&(1<<(i%8)))
/* go muck with /dev/mem */
fixterms ()
{
	int dzcount;
	char dzcarrier[64];
	register i;
	int mfd;
	int mode;

	nlist (sysname, nl);
	if (nl[0].n_type = 0)
		error ("cannot get symbols from symbol file %s", sysname);

	mode = (cmd == PRINT) ? 0 : 2;
	if ((mfd = open (kmem, mode)) < 0)
		fperror ("cannot open %s", kmem);

	IGNORE (lseek (mfd, (long)nl[DZCOUNT].n_value, 0));
	IGNORE (read (mfd, (char *)&dzcount, sizeof dzcount));
	IGNORE (lseek (mfd, (long)nl[SOFTCAR].n_value, 0));
	IGNORE (read (mfd, dzcarrier, dzcount));
	if (maxdev > dzcount)
		remark ("warning: only %d dz devices in system", dzcount);
	for (i = 0 ; i < dzcount; i++) {
		if (devices[i]) {
			if (cmd == PRINT) {
				printf ("dz%02o = %s\n", i,
					 (BITTST(i,dzcarrier)
					 ? "disabled" : "enabled"));
			}
			else if (cmd == ENABLE)
				BITCLR(i,dzcarrier);
			else
				BITSET(i,dzcarrier);
		}
	}
	if (cmd != PRINT) {
		IGNORE (lseek (mfd, (long)nl[SOFTCAR].n_value, 0));
		if (write (mfd, dzcarrier, dzcount) != dzcount)
			fperror ("cannot write to mem");
	}
}



More information about the Comp.unix.wizards mailing list