ftp.nice.ch/pub/next/unix/communication/am.1.16.NIHS.bs.tar.gz#/am.1.16.NIHS.bs/src/modem.c

This is modem.c in view mode; [Download] [Up]

/*	TEXT WAS MADE WITH 4*SPACE = 1*TAB

	program	:	modem.c	
	date	:	05.April. 1993
	purpose	:	 
	author	:	by jolly ( who else )

*/

/*************************************************************************************modem */

#import <libc.h>
#import <sys/dir.h>

#import "precomp.h"

extern int modem;

void reset_modem(void)
{
#if DEBUG
	log("resetting modem");
#endif
	ioctl(modem,TIOCCDTR,NULL);
	/*say("\x10\x03\n\x10\x03\nYYY");wait_for("XXX",500);*/
	ioctl(modem,TIOCSDTR,NULL);
	usleep(100000);
	ask("ATZ","OK");
	ask("AT+FCLASS=0","OK");
	ask("at&D3&N0&S1E1S0=0","OK");
}


int tst_lock(void)
{
	DIR 	*dirp;
	struct direct *dp;
	int 	lckfeil,pid=0;
	char 	lck[MAXPATHLEN];
#if ASCII_PIDS
        char pid_str[12];
#endif

	dirp = opendir(LCKDIR);
	if(dirp==NULL) 
		fatal("Lock-Directory not found.");
	
	for(dp = readdir(dirp); dp != NULL; dp =readdir(dirp))
		if(!strncmp (dp->d_name, "LCK..", sizeof ("LCK..")-1) &&
		   !strcmp(LCKTST,dp->d_name+dp->d_namlen-strlen(LCKTST) ))
		{
			sprintf(lck,"%s%s",LCKDIR,dp->d_name);
			lckfeil=open(lck,O_RDONLY,NULL);
			if(lckfeil==-1) break;
#if ASCII_PIDS
		        read (lckfeil, pid_str, sizeof (pid_str));
		        pid = atoi (pid_str);
#else
		        read(lckfeil,&pid,sizeof(int));
#endif
			close(lckfeil);
			if(kill(pid,0)==0 && pid!=getpid())
			{
				closedir(dirp);
				return pid;
			}
		}
	closedir(dirp);	
	return 0;
}


void open_line(char *device)
{
	int		lckfeil,pid;
	struct 	sgttyb	tdis;
	char	lockfeil[MAXPATHLEN];


	/* rewritten to wait _at least_ 2 seconds after the release of the line
		by another process until open is issued. I hope this will prevent the
		system panic which occured when leaving kermit to disappear.
		kiwi@belly
	 */
	do
	{
		while ((pid=tst_lock()) && pid && (kill (pid, 0) == 0))
			sleep (2);
	
	sleep(2);
	} while((pid=tst_lock()) && pid && (kill (pid, 0) == 0));
	

	modem=open(device,O_RDWR);
	if(modem==-1)	fatal("in open_line failed to open device %s",device);

	if( ioctl(modem,TIOCGETP,&tdis) == -1 ) log("Can't execute ioctl TIOCGETP");

	tdis.sg_ispeed=SPD;
	tdis.sg_ospeed=SPD;
	tdis.sg_flags=RAW|TANDEM;
	/* how comes that in TANDEM mode we receive XON/XOFF characters? */
	if( ioctl(modem,TIOCSETP,&tdis) == -1 ) log("Can't execute ioctl TIOCSETP");
	//if( ioctl(modem,TIOCEXCL,NULL) == -1 ) log("Can't execute ioctl TIOCNXCL");

	if( ioctl(modem,TIOCCDTR,NULL) == -1 ) log("Can't execute ioctl TIOCSDTR");
	//say("\x10\x03");
	//say("YYY");wait_for("XXX",500);	// clear queue
	if( ioctl(modem,TIOCSDTR,NULL) == -1 ) log("Can't execute ioctl TIOCSDTR");

	/* set blocking read/write */
	/*fcntl(modem, F_SETFL, fcntl(modem, F_GETFL, 0)&~O_NDELAY);*/

	/* why no dup STD_IN? */
	dup2(modem,STD_OUT);
	dup2(modem,STD_ERR);

	sprintf(lockfeil,"%s%s",LOCKFILE,device+5);
	lckfeil=open(lockfeil,O_WRONLY | O_CREAT);
	pid=getpid();
	if(lckfeil!=-1)
	{
#if ASCII_PIDS
	        char pid_str[12];

		sprintf (pid_str, "%10d\n", pid);
		write (lckfeil, pid_str, strlen (pid_str));
#else
		write(lckfeil,&pid,sizeof(int));
#endif
		close(lckfeil);
	}
	else
		fatal("can't lock %s",lockfeil);
}



void unlock_line(char *device)
{	
	char	lockfeil[MAXPATHLEN];

	sprintf(lockfeil,"%s%s",LOCKFILE,device+5);
	if(!tst_lock())
		remove(lockfeil);
#if DEBUG
	else
		log("lockfile changed by someone else !");
#endif
}

int close_line(char *device)
{
	int	tty;
	
	unlock_line(device);

#if NORMALMODE
	tty=open("/dev/tty",O_RDWR);
	ioctl(tty, TIOCNOTTY, 0);
#endif

	close(STD_IN);
	close(STD_OUT);
	close(STD_ERR);
	
	return 1;
}







These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.