1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
|
/*
* srxtv -- launch rxvt directly on a serial (RS-232) line
*
* PREREQUISITS:
* rxvt binary in search path
*
* USAGE (example):
* stty -F /dev/ttyS0 57600
* srxvt /dev/ttyS0
*
* 02/2009 Felix M. Palmen <fmp@palmen.homeip.net>
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#define RXVT "rxvt"
#define RXVT_LEN 4
int main(int argc, char **argv)
{
int fd;
char fdnum[16];
struct termios tflags;
struct stat fstat;
char *path;
char *envPath;
char *pathElement;
char *rxvt = NULL;
char foundRxvt = 0;
if (argc != 2)
{
fprintf(stderr, "USAGE: %s [tty-device]\n", *argv);
fprintf(stderr, "example: %s /dev/ttyS0\n\n", *argv);
return 1;
}
envPath = getenv("PATH");
path = (char *) malloc((strlen(envPath)+1) * sizeof(char));
strcpy(path, envPath);
pathElement = strtok(path, ":");
while (pathElement)
{
rxvt = (char *) realloc(
rxvt, (strlen(pathElement) + RXVT_LEN + 2) * sizeof(char));
strcpy(rxvt, pathElement);
strcpy(rxvt + strlen(pathElement), "/" RXVT);
if ((stat(rxvt, &fstat) == 0) &&
(fstat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
{
foundRxvt = 1;
break;
}
pathElement = strtok(NULL, ":");
}
free(path);
if (!foundRxvt)
{
fprintf(stderr, RXVT " binary not found in path!\n");
return 1;
}
if ((fd = open(argv[1], O_RDWR)) < 0)
{
fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(errno));
return 1;
}
if (tcgetattr(fd, &tflags) != 0)
{
fprintf(stderr, "error getting terminal settings of %s: %s\n",
argv[1], strerror(errno));
close(fd);
return 1;
}
tflags.c_cflag |= CS8 | CRTSCTS | CLOCAL | CREAD;
tflags.c_oflag = OPOST | ONLCR;
tflags.c_iflag = 0;
tflags.c_lflag = 0;
if (tcsetattr(fd, TCSAFLUSH, &tflags) != 0)
{
fprintf(stderr, "error setting terminal settings of %s: %s\n",
argv[1], strerror(errno));
close(fd);
return 1;
}
snprintf(fdnum, 16, "%d", fd);
execl(rxvt, RXVT, "-pty-fd", &fdnum, NULL);
}
|