<commit_before>/*      $Id: serial.c,v 5.5 2000/09/30 17:47:31 columbus Exp $      */

/****************************************************************************
 ** serial.c ****************************************************************
 ****************************************************************************
 *
 * common routines for hardware that uses the standard serial port driver
 * 
 * Copyright (C) 1999 <NAME> <<EMAIL>>
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#include "lircd.h"

int tty_reset(int fd)
{
	struct termios options;

	if(tcgetattr(fd,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcgetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	cfmakeraw(&options);
	if(tcsetattr(fd,TCSAFLUSH,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcsetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	return(1);
}

int tty_setrtscts(int fd,int enable)
{
	struct termios options;

	if(tcgetattr(fd,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcgetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	if(enable)
	{
		options.c_cflag|=CRTSCTS;
		options.c_cflag|=CSTOPB;
	}
	else
	{
		options.c_cflag&=~CRTSCTS;
		options.c_cflag&=~CSTOPB;
	}
	if(tcsetattr(fd,TCSAFLUSH,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcsetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	return(1);
}

int tty_setbaud(int fd,int baud)
{
	struct termios options;
	int speed;

	switch(baud)
	{
	case 300:
		speed=B300;
		break;
	case 1200:
		speed=B1200;
		break;
	case 2400:
                speed=B2400;
                break;
	case 4800:
                speed=B4800;
                break;
	case 9600:
                speed=B9600;
                break;
	case 19200:
                speed=B19200;
                break;
	case 38400:
                speed=B38400;
                break;
	case 57600:
                speed=B57600;
                break;
	case 115200:
                speed=B115200;
                break;
	default:
		LOGPRINTF(1,"tty_setbaud(): bad baud rate %d",baud);
		return(0);
	}		
	if(tcgetattr(fd, &options)==-1)
	{
		LOGPRINTF(1,"tty_setbaud(): tcgetattr() failed");
		LOGPERROR(1,"tty_setbaud()");
		return(0);
	}
	(void) cfsetispeed(&options,speed);
	(void) cfsetospeed(&options,speed);
	if(tcsetattr(fd,TCSAFLUSH,&options)==-1)
	{
		LOGPRINTF(1,"tty_setbaud(): tcsetattr() failed");
		LOGPERROR(1,"tty_setbaud()");
		return(0);
	}
	return(1);
}

int tty_create_lock(char *name)
{
	char filename[FILENAME_MAX+1];
	char symlink[FILENAME_MAX+1];
	char cwd[FILENAME_MAX+1];
	char *last,*s;
	char id[10+1+1];
	int lock;
	int len;
	
	strcpy(filename,"/var/lock/LCK..");
	
	last=strrchr(name,'/');
	if(last!=NULL)
		s=last+1;
	else
		s=name;
	
	if(strlen(filename)+strlen(s)>FILENAME_MAX)
	{
		logprintf(LOG_ERR,"%s: invalid filename \"%s%s\"",
			  filename,s);
		return(0);
	}
	strcat(filename,s);
	
	if((len=snprintf(id,10+1+1,"%10d\n",getpid()))==-1)
	{
		logprintf(LOG_ERR,"invalid pid \"%d\"",getpid());
		return(0);
	}

	lock=open(filename,O_CREAT|O_EXCL|O_WRONLY,0644);
	if(lock==-1)
	{
		logprintf(LOG_ERR,"could not create lock file \"%s\"",
			  filename);
		logperror(LOG_ERR,NULL);
		lock=open(filename,O_RDONLY);
		if(lock!=-1)
		{
			len=read(lock,id,10+1);
			if(len==10+1)
			{
				if(read(lock,id,1)==0)
				{
					logprintf(LOG_ERR,
						  "%s is locked by PID %s",
						  name,id);
				}
			}
			close(lock);
		}
	}
	if(write(lock,id,len)!=len)
	{
		logprintf(LOG_ERR,"%s: could not write pid to lock file");
		logperror(LOG_ERR,NULL);
		close(lock);
		if(unlink(filename)==-1)
		{
			logprintf(LOG_ERR,"could not delete file \"%s\"",
				  filename);
			logperror(LOG_ERR,NULL);
			/* FALLTHROUGH */
		}
		return(0);
	}
	if(close(lock)==-1)
	{
		logprintf(LOG_ERR,"could not close lock file");
		logperror(LOG_ERR,NULL);<commit_msg>removed too much<commit_after>/*      $Id: serial.c,v 5.6 2000/10/02 10:54:53 columbus Exp $      */

/****************************************************************************
 ** serial.c ****************************************************************
 ****************************************************************************
 *
 * common routines for hardware that uses the standard serial port driver
 * 
 * Copyright (C) 1999 <NAME> <<EMAIL>>
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#include "lircd.h"

int tty_reset(int fd)
{
	struct termios options;

	if(tcgetattr(fd,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcgetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	cfmakeraw(&options);
	if(tcsetattr(fd,TCSAFLUSH,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcsetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	return(1);
}

int tty_setrtscts(int fd,int enable)
{
	struct termios options;

	if(tcgetattr(fd,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcgetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	if(enable)
	{
		options.c_cflag|=CRTSCTS;
		options.c_cflag|=CSTOPB;
	}
	else
	{
		options.c_cflag&=~CRTSCTS;
		options.c_cflag&=~CSTOPB;
	}
	if(tcsetattr(fd,TCSAFLUSH,&options)==-1)
	{
		LOGPRINTF(1,"tty_reset(): tcsetattr() failed");
		LOGPERROR(1,"tty_reset()");
		return(0);
	}
	return(1);
}

int tty_setbaud(int fd,int baud)
{
	struct termios options;
	int speed;

	switch(baud)
	{
	case 300:
		speed=B300;
		break;
	case 1200:
		speed=B1200;
		break;
	case 2400:
                speed=B2400;
                break;
	case 4800:
                speed=B4800;
                break;
	case 9600:
                speed=B9600;
                break;
	case 19200:
                speed=B19200;
                break;
	case 38400:
                speed=B38400;
                break;
	case 57600:
                speed=B57600;
                break;
	case 115200:
                speed=B115200;
                break;
	default:
		LOGPRINTF(1,"tty_setbaud(): bad baud rate %d",baud);
		return(0);
	}		
	if(tcgetattr(fd, &options)==-1)
	{
		LOGPRINTF(1,"tty_setbaud(): tcgetattr() failed");
		LOGPERROR(1,"tty_setbaud()");
		return(0);
	}
	(void) cfsetispeed(&options,speed);
	(void) cfsetospeed(&options,speed);
	if(tcsetattr(fd,TCSAFLUSH,&options)==-1)
	{
		LOGPRINTF(1,"tty_setbaud(): tcsetattr() failed");
		LOGPERROR(1,"tty_setbaud()");
		return(0);
	}
	return(1);
}

int tty_create_lock(char *name)
{
	char filename[FILENAME_MAX+1];
	char symlink[FILENAME_MAX+1];
	char cwd[FILENAME_MAX+1];
	char *last,*s;
	char id[10+1+1];
	int lock;
	int len;
	
	strcpy(filename,"/var/lock/LCK..");
	
	last=strrchr(name,'/');
	if(last!=NULL)
		s=last+1;
	else
		s=name;
	
	if(strlen(filename)+strlen(s)>FILENAME_MAX)
	{
		logprintf(LOG_ERR,"%s: invalid filename \"%s%s\"",
			  filename,s);
		return(0);
	}
	strcat(filename,s);
	
	if((len=snprintf(id,10+1+1,"%10d\n",getpid()))==-1)
	{
		logprintf(LOG_ERR,"invalid pid \"%d\"",getpid());
		return(0);
	}

	lock=open(filename,O_CREAT|O_EXCL|O_WRONLY,0644);
	if(lock==-1)
	{
		logprintf(LOG_ERR,"could not create lock file \"%s\"",
			  filename);
		logperror(LOG_ERR,NULL);
		lock=open(filename,O_RDONLY);
		if(lock!=-1)
		{
			len=read(lock,id,10+1);
			if(len==10+1)
			{
				if(read(lock,id,1)==0)
				{
					logprintf(LOG_ERR,
						  "%s is locked by PID %s",
						  name,id);
				}
			}
			close(lock);
		}
		return(0);
	}
	if(write(lock,id,len)!=len)
	{
		logprintf(LOG_ERR,"%s: could not write pid to lock file");
		logperror(LOG_ERR,NULL);
		close(lock);
		if(unlink(filename)==-1)
		{
			logprintf(LOG_ERR,"could not delete file \"%s\"",
				  filename);
			logperror(LOG_ERR,NULL);
			/* FALLTHROUGH */
		}
		return(0);
	}
	if(close(lock)==-1)
	{
		logprintf(LOG_ERR,"could not close lock file");
		logperror(LOG_ERR,NULL);
{'fraction_non_alphanumeric': 0.22519083969465647, 'fraction_numerical': 0.04026101945333661, 'mean_word_length': 3.599660249150623, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '866913', 'n_tokens_mistral': 3722, 'n_tokens_neox': 3287, 'n_words': 516}