/* * Copyright (c) 1980 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * 1999-02-22 Arkadiusz Mi¶kiewicz * - added Native Language Support * * 2000-07-30 Per Andreas Buer - added "q"-option */ /* * script */ #include #include #ifndef SUN_SOLARIS #include #endif #include #include #include #include #include #include #include #include #include "nls.h" #include /* * supertracker */ #include #include #include extern char **environ; #define SYSLOGBUFSIZ 256 #ifdef SUN_SOLARIS #include #endif void map_chars(char *, int , char *, int); char *char_table1[] = { "", // 0x00 "", // 0x01 "", // 0x02 "", // 0x03 "", // 0x04 "", // 0x05 "", // 0x06 "", // 0x07 "", // 0x08 "", // 0x09 "", // 0x0a "", // 0x0b "", // 0x0c "", // 0x0d "", // 0x0e "", // 0x0f "", // 0x10 "", // 0x11 "", // 0x12 "", // 0x13 "", // 0x14 "", // 0x15 "", // 0x16 "", // 0x17 "", // 0x18 "", // 0x19 "", // 0x1a "", // 0x1b "", // 0x1c "", // 0x1d "", // 0x1e "", // 0x1f " ", // 0x20 "!", // 0x21 "\"", // 0x22 "#", // 0x23 "$", // 0x24 "%", // 0x25 "&", // 0x26 "'", // 0x27 "(", // 0x28 ")", // 0x29 "*", // 0x2a "+", // 0x2b ",", // 0x2c "-", // 0x2d ".", // 0x2e "/", // 0x2f "0", // 0x30 "1", // 0x31 "2", // 0x32 "3", // 0x33 "4", // 0x34 "5", // 0x35 "6", // 0x36 "7", // 0x37 "8", // 0x38 "9", // 0x39 ":", // 0x3a ";", // 0x3b "<", // 0x3c "=", // 0x3d ">", // 0x3e "?", // 0x3f "@", // 0x40 "A", // 0x41 "B", // 0x42 "C", // 0x43 "D", // 0x44 "E", // 0x45 "F", // 0x46 "G", // 0x47 "H", // 0x48 "I", // 0x49 "J", // 0x4a "K", // 0x4b "L", // 0x4c "M", // 0x4d "N", // 0x4e "O", // 0x4f "P", // 0x50 "Q", // 0x51 "R", // 0x52 "S", // 0x53 "T", // 0x54 "U", // 0x55 "V", // 0x56 "W", // 0x57 "X", // 0x58 "Y", // 0x59 "Z", // 0x5a "[", // 0x5b "\\", // 0x5c "]", // 0x5d "^", // 0x5e "_", // 0x5f "`", // 0x60 "a", // 0x61 "b", // 0x62 "c", // 0x63 "d", // 0x64 "e", // 0x65 "f", // 0x66 "g", // 0x67 "h", // 0x68 "i", // 0x69 "j", // 0x6a "k", // 0x6b "l", // 0x6c "m", // 0x6d "n", // 0x6e "o", // 0x6f "p", // 0x70 "q", // 0x71 "r", // 0x72 "s", // 0x73 "t", // 0x74 "u", // 0x75 "v", // 0x76 "w", // 0x77 "x", // 0x78 "y", // 0x79 "z", // 0x7a "{", // 0x7b "|", // 0x7c "}", // 0x7d "~", // 0x7e "", // 0x7f "\0x80", // 0x80 "\0x81", // 0x81 "\0x82", // 0x82 "\0x83", // 0x83 "\0x84", // 0x84 "\0x85", // 0x85 "\0x86", // 0x86 "\0x87", // 0x83 "\0x88", // 0x83 "\0x89", // 0x83 "\0x8a", // 0x83 "\0x8b", // 0x83 "\0x8c", // 0x83 "\0x8d", // 0x83 "\0x8e", // 0x83 "\0x8f", // 0x83 "\0x90", // 0x83 "\0x91", // 0x83 "\0x92", // 0x83 "\0x93", // 0x83 "\0x94", // 0x83 "\0x95", // 0x83 "\0x96", // 0x83 "\0x97", // 0x83 "\0x98", // 0x83 "\0x99", // 0x83 "\0x9a", // 0x83 "\0x9b", // 0x83 "\0x9c", // 0x83 "\0x9d", // 0x83 "\0x9e", // 0x83 "\0x9f", // 0x83 "\0xa0", // 0x83 "\0xa1", // 0x83 "\0xa2", // 0x83 "\0xa3", // 0x83 "\0xa4", // 0x83 "\0xa5", // 0x83 "\0xa6", // 0x83 "\0xa7", // 0x83 "\0xa8", // 0x83 "\0xa9", // 0x83 "\0xaa", // 0x83 "\0xab", // 0x83 "\0xac", // 0x83 "\0xad", // 0x83 "\0xae", // 0x83 "\0xaf", // 0x83 "\0xb0", // 0x83 "\0xb1", // 0x83 "\0xb2", // 0x83 "\0xb3", // 0x83 "\0xb4", // 0x83 "\0xb5", // 0x83 "\0xb6", // 0x83 "\0xb7", // 0x83 "\0xb8", // 0x83 "\0xb9", // 0x83 "\0xba", // 0x83 "\0xbb", // 0x83 "\0xbc", // 0x83 "\0xbd", // 0x83 "\0xbe", // 0x83 "\0xbf", // 0x83 "\0xc0", // 0x83 "\0xc1", // 0x83 "\0xc2", // 0x83 "\0xc3", // 0x83 "\0xc4", // 0x83 "\0xc5", // 0x83 "\0xc6", // 0x83 "\0xc7", // 0x83 "\0xc8", // 0x83 "\0xc9", // 0x83 "\0xca", // 0x83 "\0xcb", // 0x83 "\0xcc", // 0x83 "\0xcd", // 0x83 "\0xce", // 0x83 "\0xcf", // 0x83 "\0xd0", // 0x83 "\0xd1", // 0x83 "\0xd2", // 0x83 "\0xd3", // 0x83 "\0xd4", // 0x83 "\0xd5", // 0x83 "\0xd6", // 0x83 "\0xd7", // 0x83 "\0xd8", // 0x83 "\0xd9", // 0x83 "\0xda", // 0x83 "\0xdb", // 0x83 "\0xdc", // 0x83 "\0xde", // 0x83 "\0xdf", // 0x83 "\0xe0", // 0x83 "\0xe1", // 0x83 "\0xe2", // 0x83 "\0xe3", // 0x83 "\0xe4", // 0x83 "\0xe5", // 0x83 "\0xe6", // 0x83 "\0xe7", // 0x83 "\0xe8", // 0x83 "\0xe9", // 0x83 "\0xea", // 0x83 "\0xeb", // 0x83 "\0xec", // 0x83 "\0xee", // 0x83 "\0xef", // 0x83 "\0xf0", // 0x83 "\0xf1", // 0x83 "\0xf2", // 0x83 "\0xf3", // 0x83 "\0xf4", // 0x83 "\0xf5", // 0x83 "\0xf6", // 0x83 "\0xf7", // 0x83 "\0xf8", // 0x83 "\0xf9", // 0x83 "\0xfa", // 0x83 "\0xfb", // 0x83 "\0xfc", // 0x83 "\0xfe", // 0x83 "\0xff", // 0x83 }; int syslog_flg = 1; // default is to do syslog int syslog_opened = 0; // yuck... char *syslog_label = "SUPERTRACKER"; // default label for openlog() int syslog_cooked_mode = 1; // default is to send cooked mode -- raw_mode is discouraged // unless you are sure that your syslog processes are happy with // non-printable characters int syslog_facility = LOG_LOCAL2; // default facility int syslog_bufsiz = SYSLOGBUFSIZ; // how many characters to buffer before spilling // #ifdef __DIETLIBC /* * dietlibc doesn't haves: */ int getpt(void) { return(open("/dev/ptmx",O_RDWR|O_NOCTTY)); } int clearenv(void) { *environ = NULL; return 0; } // #endif #ifdef SUN_SOLARIS #include #endif #ifdef SUN_SOLARIS #include "defines.h" #endif #ifndef _PATH_DEV #define _PATH_DEV "/dev/" #endif /* * end supertracker */ #ifdef __linux__ #include #include #endif #ifndef SUN_SOLARIS // #include "../defines.h" #include "defines.h" #endif #ifdef HAVE_openpty #include #endif void finish(int); void done(void); void fail(void); void resize(int); void int_handler(int); // supertracker void fixtty(void); void getmaster(void); void getslave(void); void doinput(void); void dooutput(void); void doshell(void); char *shell = "/bin/bash"; // fsu --> setting a default value FILE *fscript; int master; int slave; int child; int subchild; char fname[256]; struct termios tt; struct winsize win; int lb; int l; #ifndef HAVE_openpty char line[] = "/dev/ptyXX"; #endif int aflg = 0; char *cflg = NULL; int fflg = 1; // changed for tracking int qflg = 1; // changed for tracking int tflg = 1; // changed for tracking int vflg = 1; // changed for tracking static char *progname; /* * Stop extremely silly gcc complaint on %c: * warning: `%c' yields only last 2 digits of year in some locales */ static void my_strftime(char *buf, size_t len, const char *fmt, const struct tm *tm) { strftime(buf, len, fmt, tm); } static void die_if_link(char *fn) { struct stat s; if (lstat(fn, &s) == 0 && (S_ISLNK(s.st_mode) || s.st_nlink > 1)) { fprintf(stderr, _("Warning: `%s' is a link.\n" "Use `%s [options] %s' if you really " "want to use it.\n" "FSU not started.\n"), fn, progname, fn); exit(1); } } int main(int argc, char **argv) { extern int optind; char *p; int ch; // supertracker char *term; struct passwd pwd; struct passwd *pwdptr; char pwdbuff[256]; char the_time[256]; time_t now; progname = argv[0]; if ((p = strrchr(progname, '/')) != NULL) progname = p+1; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); if (argc == 2) { if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) { printf(_("%s from %s\n"), progname, util_linux_version); return 0; } } // while ((ch = getopt(argc, argv, "ac:fqt")) != -1) while ((ch = getopt(argc, argv, "ab:c:fF:l:nqrs:t")) != -1) switch((char)ch) { int temp; case 'F': temp = atoi(optarg); if(temp > 0) { syslog_facility = temp; } else { fprintf(stderr,"Error in -F value '%s' --> must be an integer.\n",optarg); exit(1); } case 'a': aflg++; break; case 'b': temp = atoi(optarg); if(temp > 0) { syslog_bufsiz = temp; } else { fprintf(stderr,"Error in -b value '%s' --> must be an integer > 0.\n",optarg); exit(1); } // case 'c': // cflg = optarg; // break; case 'f': fflg++; break; case 'l': syslog_label = strdup(optarg); break; case 'n': syslog_flg = 0; // no syslogging break; case 'q': qflg++; break; case 'r': syslog_cooked_mode = 0; // raw mode --> make sure your syslog is happy with non-printable chars! break; case 's': shell = strdup(optarg); break; case 't': tflg++; break; case 'v': vflg++; break; case '?': default: fprintf(stderr, _("usage: fsu [-a] [-b] [-f] [-F] [-l] [-n] [-q] [-r] [-t] [-v]\n")); exit(1); } argc -= optind; argv += optind; /* if (argc > 0) fname = argv[0]; else { fname = "typescript"; die_if_link(fname); } */ now = time(NULL); my_strftime(the_time,256,"%Y-%m-%d-%T",localtime(&now)); printf("the_time == '%s'\n",the_time); snprintf(fname,256,"/var/log/superuser-trans/%s-%06d",the_time,getpid()); if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) { perror(fname); fail(); } /* shell = getenv("SHELL"); if (shell == NULL) shell = _PATH_BSHELL; */ /* environmental variables to preserve */ term = getenv("TERM"); /* clear the environment */ clearenv(); /* chdir to home directory */ #ifdef SUN_SOLARIS getpwuid_r(getuid(), &pwd, pwdbuff, 256); #else getpwuid_r(getuid(), &pwd, pwdbuff, 256, &pwdptr); #endif chdir(pwd.pw_dir); /* restore selected variables */ setenv("TERM",term,1); setenv("HOME",pwd.pw_dir,1); if(access(shell,X_OK)) { // oops, cannot access the shell! perror(shell); fail(); } getmaster(); if (!qflg) printf(_("Script started, file is %s\n"), fname); fixtty(); (void) signal(SIGCHLD, finish); child = fork(); if (child < 0) { perror("fork"); fail(); } if (child == 0) { subchild = child = fork(); if (child < 0) { perror("fork"); fail(); } if (child) dooutput(); else doshell(); } else (void) signal(SIGWINCH, resize); (void) signal(SIGINT, int_handler); doinput(); return 0; } void map_chars(char *src, int src_ct, char *target, int target_ct) { char *s = src; char *t = target; while((*s) && (s < src + src_ct) && (t < (target + target_ct))) { int ct = strlen(char_table1[(int)*s]); memcpy(t,char_table1[(int)*s],ct); t += ct; s++; } *t = '\0'; return; } void doinput() { register int cc; char ibuf[BUFSIZ]; // char svbuf1[SYSLOGBUFSIZ]; // raw input char svbuf1[syslog_bufsiz]; // char svbuf2[SYSLOGBUFSIZ]; // cooked version char svbuf2[syslog_bufsiz]; // cooked version int svbuf1where = 0; // where are we in the raw input int svbuf2where = 0; // where are we in the cooked input (void) fclose(fscript); if(syslog_flg && !syslog_opened) { openlog(syslog_label,LOG_PID,syslog_facility); syslog_opened = 1; } while ((cc = read(0, ibuf, BUFSIZ)) > 0) { if(svbuf1where + cc < SYSLOGBUFSIZ) { memcpy(svbuf1+svbuf1where,ibuf,cc); svbuf1where += cc; *(svbuf1+svbuf1where) = '\0'; } else { if(syslog_cooked_mode) map_chars(svbuf1,SYSLOGBUFSIZ,svbuf2,SYSLOGBUFSIZ); if(syslog_flg) syslog(LOG_WARNING,"%s",svbuf2); svbuf1where = 0; *svbuf1 = '\0'; } if(strrchr(svbuf1,'\r')) { if(syslog_cooked_mode) map_chars(svbuf1,SYSLOGBUFSIZ,svbuf2,SYSLOGBUFSIZ); if(syslog_flg) syslog(LOG_WARNING,"[%d] %s",getuid(),svbuf2); svbuf1where = 0; *svbuf1 = '\0'; } (void) write(master, ibuf, cc); } done(); } #include void finish(int dummy) { int status; register int pid; register int die = 0; while ((pid = wait3(&status, WNOHANG, 0)) > 0) if (pid == child) die = 1; if (die) done(); } void resize(int dummy) { /* transmit window change information to the child */ (void) ioctl(0, TIOCGWINSZ, (char *)&win); (void) ioctl(slave, TIOCSWINSZ, (char *)&win); kill(child, SIGWINCH); } void int_handler(int dummy) { kill(child, SIGINT); } void dooutput() { register int cc; time_t tvec; char obuf[BUFSIZ]; struct timeval tv; double oldtime=time(NULL), newtime; char *out_cr; (void) close(0); #ifdef HAVE_openpty (void) close(slave); #endif tvec = time((time_t *)NULL); my_strftime(obuf, sizeof obuf, "%c\n", localtime(&tvec)); fprintf(fscript, _("Script started on %s"), obuf); for (;;) { if (tflg) { gettimeofday(&tv, NULL); tvec = time((time_t *)NULL); } cc = read(master, obuf, sizeof (obuf)); if (cc <= 0) break; /* if (tflg) { newtime = tv.tv_sec + (double) tv.tv_usec / 1000000; fprintf(stderr, "%f %i\n", newtime - oldtime, cc); oldtime = newtime; } (void) write(1, obuf, cc); (void) fwrite(obuf, 1, cc, fscript); if (fflg) (void) fflush(fscript); */ /* go ahead and write to subproc */ (void) write(1, obuf, cc); *(obuf+cc) = '\0'; /* now go log it to supertrans */ if((tflg) && (out_cr = strrchr(obuf,'\r'))) { char ts[256]; int tscount = 0; newtime = tv.tv_sec + (double) tv.tv_usec / 1000000; oldtime = newtime; if(vflg) { char mytime[256]; my_strftime(mytime, 256, "%X", localtime(&tvec)); tscount = snprintf(ts, 256, "[ts:%s]", mytime); } else tscount = snprintf(ts, 256, "[ts:%f]", newtime); fwrite(obuf, 1, out_cr - obuf, fscript); fwrite(ts, 1, tscount, fscript); // fprintf(fscript,"[ts:%f]\n",newtime); fwrite(out_cr, 1, (obuf + cc) - out_cr, fscript); } else { fwrite(obuf, 1, cc, fscript); } if (fflg) (void) fflush(fscript); } done(); } void doshell() { char *shname; #if 0 int t; t = open(_PATH_TTY, O_RDWR); if (t >= 0) { (void) ioctl(t, TIOCNOTTY, (char *)0); (void) close(t); } #endif getslave(); (void) close(master); (void) fclose(fscript); (void) dup2(slave, 0); (void) dup2(slave, 1); (void) dup2(slave, 2); (void) close(slave); shname = strrchr(shell, '/'); if (shname) shname++; else shname = shell; // if (cflg) // execl(shell, shname, "-c", cflg, 0); // else execl(shell, shname, "-i", "-l", 0); perror(shell); fail(); } #ifdef SUN_SOLARIS void cfmakeraw(struct termios *rtt) { rtt->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |INLCR|IGNCR|ICRNL|IXON); rtt->c_oflag &= ~OPOST; rtt->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); rtt->c_cflag &= ~(CSIZE|PARENB); rtt->c_cflag |= CS8; } #endif void fixtty() { struct termios rtt; rtt = tt; cfmakeraw(&rtt); rtt.c_lflag &= ~ECHO; (void) tcsetattr(0, TCSAFLUSH, &rtt); } void fail() { (void) kill(0, SIGTERM); done(); } void done() { time_t tvec; if (subchild) { // changed ... char buf[BUFSIZ]; tvec = time((time_t *)NULL); my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec)); fprintf(fscript, _("\nScript done on %s"), buf); (void) fclose(fscript); (void) close(master); } else { (void) tcsetattr(0, TCSAFLUSH, &tt); if (!qflg) printf(_("Script done, file is %s\n"), fname); if(syslog_flg && !syslog_opened) { openlog(syslog_label,LOG_PID,syslog_facility); syslog_opened = 1; } if(syslog_flg) { syslog(LOG_WARNING,"[%d] EXITING %s",getuid(),syslog_label); } } exit(0); } void getmaster() { #ifdef HAVE_openpty (void) tcgetattr(0, &tt); (void) ioctl(0, TIOCGWINSZ, (char *)&win); if (openpty(&master, &slave, NULL, &tt, &win) < 0) { fprintf(stderr, _("openpty failed\n")); fail(); } #else char *pty, *bank, *cp; struct stat stb; pty = &line[strlen("/dev/ptyp")]; for (bank = "pqrs"; *bank; bank++) { line[strlen("/dev/pty")] = *bank; *pty = '0'; if (stat(line, &stb) < 0) break; for (cp = "0123456789abcdef"; *cp; cp++) { *pty = *cp; master = open(line, O_RDWR); if (master >= 0) { char *tp = &line[strlen("/dev/")]; int ok; /* verify slave side is usable */ *tp = 't'; ok = access(line, R_OK|W_OK) == 0; *tp = 'p'; if (ok) { (void) tcgetattr(0, &tt); (void) ioctl(0, TIOCGWINSZ, (char *)&win); return; } (void) close(master); } } } fprintf(stderr, _("Out of pty's\n")); fail(); #endif /* not HAVE_openpty */ } void getslave() { #ifndef HAVE_openpty line[strlen("/dev/")] = 't'; slave = open(line, O_RDWR); if (slave < 0) { perror(line); fail(); } (void) tcsetattr(slave, TCSAFLUSH, &tt); (void) ioctl(slave, TIOCSWINSZ, (char *)&win); #endif (void) setsid(); #ifdef TIOCSCTTY (void) ioctl(slave, TIOCSCTTY, 0); #endif } // #ifdef __DIETLIBC /* * * GNU CODE * */ /* Store at most BUFLEN characters of the pathname of the slave pseudo terminal associated with the master FD is open on in BUF. Return 0 on success, otherwise an error number. */ int ptsname_r (int fd, char *buf, size_t buflen) { int save_errno = errno; struct stat st; printf("ptsname_r with fd = %d...\n",fd); if (buf == NULL) { return EINVAL; } if(!isatty(fd)) { return errno; } if (ttyname(fd) != 0) strncpy(buf,ttyname(fd),buflen); else return errno; printf("buf == %s\n", buf); buf[sizeof (_PATH_DEV) - 1] = 't'; printf("buf == %s\n", buf); if (stat (buf, &st) < 0) return errno; return 0; } // #endif /* Return the result of ptsname_r in the buffer pointed to by PTS, which should be of length BUF_LEN. If it is too long to fit in this buffer, a sufficiently long buffer is allocated using malloc, and returned in PTS. 0 is returned upon success, -1 otherwise. */ static int pts_name (int fd, char **pts, size_t buf_len) { int rv; char *buf = *pts; for (;;) { char *new_buf; if (buf_len) { rv = ptsname_r (fd, buf, buf_len); if (rv != 0 || memchr (buf, '\0', buf_len)) /* We either got an error, or we succeeded and the returned name fit in the buffer. */ break; /* Try again with a longer buffer. */ buf_len += buf_len; /* Double it */ } else /* No initial buffer; start out by mallocing one. */ buf_len = 128; /* First time guess. */ if (buf != *pts) /* We've already malloced another buffer at least once. */ new_buf = realloc (buf, buf_len); else new_buf = malloc (buf_len); if (! new_buf) { rv = -1; break; } buf = new_buf; } if (rv == 0) *pts = buf; /* Return buffer to the user. */ else if (buf != *pts) free (buf); /* Free what we malloced when returning an error. */ return rv; } // You don't need this. If you are using dietlibc, it has a good openpty function; if // you are using glibc, you can use libutil (and you cannot statically link anyway since // we are using getpwuid(), which will force you to use the nss_* libs which must be // dynamically linked. #ifdef NOTDEFINED /* Create pseudo tty master slave pair and set terminal attributes according to TERMP and WINP. Return handles for both ends in AMASTER and ASLAVE, and return the name of the slave end in NAME. */ int openpty (int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp) { #ifdef PATH_MAX char _buf[PATH_MAX]; #else char _buf[512]; #endif char *buf = _buf; int master, slave; master = getpt (); if (master == -1) return -1; if (grantpt (master)) goto fail; if (unlockpt (master)) goto fail; if (pts_name (master, &buf, sizeof (_buf))) goto fail; slave = open (buf, O_RDWR | O_NOCTTY); if (slave == -1) { if (buf != _buf) free (buf); goto fail; } /* XXX Should we ignore errors here? */ if(termp) tcsetattr (slave, TCSAFLUSH, termp); if (winp) ioctl (slave, TIOCSWINSZ, winp); *amaster = master; *aslave = slave; if (name != NULL) strcpy (name, buf); if (buf != _buf) free (buf); return 0; fail: close (master); return -1; } #endif