4 * Compatibility functions for different OSes
6 * $Id: compat.c,v 1.6 2005/08/22 00:48:34 quozl Exp $
19 void strlcpy(char *dst
, const char *src
, size_t size
)
21 strncpy(dst
, src
, size
- 1);
27 void *memmove(void *dst
, const void *src
, size_t size
)
29 bcopy(src
, dst
, size
);
36 * Finds a free PTY/TTY pair.
38 * This is derived from C.S. Ananian's pty.c that was with his pptp client.
40 *************************************************************************
41 * pty.c - find a free pty/tty pair.
42 * inspired by the xterm source.
43 * NOTE: This is very likely to be highly non-portable.
44 * C. Scott Ananian <cananian@alumni.princeton.edu>
46 * Heavily modified to chage from getpseudopty() to openpty().
49 #include <sys/types.h>
55 #include "our_syslog.h"
58 int openpty(int *master
, int *slave
, char *name
, void *unused1
, void *unused2
)
60 int devindex
= 0, letter
= 0;
62 char ttydev
[PTYMAX
], ptydev
[TTYMAX
];
64 syslog(LOG_DEBUG
, "CTRL: Allocating pty/tty pair");
65 strcpy(ttydev
, TTYDEV
);
66 strcpy(ptydev
, PTYDEV
);
67 while (PTYCHAR1
[letter
]) {
68 ttydev
[TTYMAX
- 3] = ptydev
[PTYMAX
- 3] = PTYCHAR1
[letter
];
69 while (PTYCHAR2
[devindex
]) {
70 ttydev
[TTYMAX
- 2] = ptydev
[PTYMAX
- 2] = PTYCHAR2
[devindex
];
71 if ((fd1
= open(ptydev
, O_RDWR
)) >= 0) {
72 if ((fd2
= open(ttydev
, O_RDWR
)) >= 0) {
83 syslog(LOG_ERR
, "CTRL: Failed to allocate pty");
84 return -1; /* Unable to allocate pty */
87 syslog(LOG_INFO
, "CTRL: Allocated pty/tty pair (%s,%s)", ptydev
, ttydev
);
93 strcpy(name
, ttydev
); /* no way to bounds check */
99 char *strerror(int errnum
) {
101 sprintf(buf
, "Error %d", errnum
);
106 #ifndef HAVE_SETPROCTITLE
114 void my_setproctitle(int argc
, char **argv
, const char *format
, ...) {
117 va_start(parms
, format
);
118 vsnprintf(proctitle
, sizeof(proctitle
), format
, parms
);
120 #ifndef HAVE_SETPROCTITLE
121 inststr(argc
, argv
, proctitle
);
123 setproctitle(proctitle
);
128 /* signal to pipe delivery implementation */
133 /* pipe private to process */
134 static int sigpipe
[2];
136 /* create a signal pipe, returns 0 for success, -1 with errno for failure */
142 if (rc
< 0) return rc
;
144 fcntl(sigpipe
[0], F_SETFD
, FD_CLOEXEC
);
145 fcntl(sigpipe
[1], F_SETFD
, FD_CLOEXEC
);
148 #define FLAG_TO_SET O_NONBLOCK
151 #define FLAG_TO_SET O_NDELAY
153 #define FLAG_TO_SET FNDELAY
157 rc
= fcntl(sigpipe
[1], F_GETFL
);
159 rc
= fcntl(sigpipe
[1], F_SETFL
, rc
| FLAG_TO_SET
);
160 if (rc
< 0) return rc
;
165 /* generic handler for signals, writes signal number to pipe */
166 void sigpipe_handler(int signum
)
168 write(sigpipe
[1], &signum
, sizeof(signum
));
169 signal(signum
, sigpipe_handler
);
172 /* assign a signal number to the pipe */
173 void sigpipe_assign(int signum
)
177 memset(&sa
, 0, sizeof(sa
));
178 sa
.sa_handler
= sigpipe_handler
;
179 sigaction(signum
, &sa
, NULL
);
182 /* return the signal pipe read file descriptor for select(2) */
188 /* read and return the pending signal from the pipe */
192 read(sigpipe
[0], &signum
, sizeof(signum
));