From: Ben Gras Date: Tue, 19 Jul 2005 16:01:37 +0000 (+0000) Subject: new -lutil, only openpty() for now X-Git-Tag: v3.1.0~590 X-Git-Url: http://zhaoyanbai.com/repos/%22../static/howto.html?a=commitdiff_plain;h=0f48b1885d9c06607cd8b790cb49ded3a372ea97;p=minix.git new -lutil, only openpty() for now --- diff --git a/lib/Makefile b/lib/Makefile index 55fd6812a..a918b0f94 100755 --- a/lib/Makefile +++ b/lib/Makefile @@ -33,6 +33,7 @@ all: cd socket && $(MAKE) cd syscall && $(MAKE) cd syslib && $(MAKE) + cd util && $(MAKE) cd sysutil && $(MAKE) cd timers && $(MAKE) cd $(ZLIB) && $(MAKE) @@ -59,6 +60,7 @@ install_i86: \ $(LIB)/end.a \ $(LIB)/libsys.a \ $(LIB)/libtimers.a \ + $(LIB)/util.a \ $(LIB)/sysutil.a \ $(LIB)/libcurses.a \ $(LIB)/libedit.a \ @@ -106,6 +108,9 @@ $(LIB)/libtimers.a: libtimers.a $(LIB)/libsysutil.a: libsysutil.a install -c -o bin $? $@ +$(LIB)/libutil.a: libutil.a + install -c -o bin $? $@ + $(LIB)/libcurses.a: libcurses.a install -c -o bin $? $@ @@ -134,6 +139,7 @@ install_i386: \ $(LIB386)/end.a \ $(LIB386)/libsys.a \ $(LIB386)/libtimers.a \ + $(LIB386)/libutil.a \ $(LIB386)/libsysutil.a \ $(LIB386)/libcurses.a \ $(LIB386)/libedit.a \ @@ -184,6 +190,9 @@ $(LIB386)/libtimers.a: libtimers.a $(LIB386)/libsysutil.a: libsysutil.a install -c -o bin $? $@ +$(LIB386)/libutil.a: libutil.a + install -c -o bin $? $@ + $(LIB386)/libcurses.a: libcurses.a install -c -o bin $? $@ diff --git a/lib/util/Makefile b/lib/util/Makefile new file mode 100644 index 000000000..f5501bc00 --- /dev/null +++ b/lib/util/Makefile @@ -0,0 +1,19 @@ +# Makefile for lib/util. + +CFLAGS = -O -D_MINIX -D_POSIX_SOURCE +CC1 = $(CC) $(CFLAGS) -c + +LIBUTIL = ../libutil.a +all: $(LIBUTIL) + +OBJECTS = \ + $(LIBUTIL)(openpty.o) + + +$(LIBUTIL): $(OBJECTS) + aal cr $@ *.o + rm *.o + +$(LIBUTIL)(openpty.o): openpty.c + $(CC1) openpty.c + diff --git a/lib/util/openpty.c b/lib/util/openpty.c new file mode 100644 index 000000000..604b2bad5 --- /dev/null +++ b/lib/util/openpty.c @@ -0,0 +1,83 @@ +/* + * openpty() tries to open a pty; applications won't have to + * duplicate this code all the time (or change it if the system + * pty interface changes). + * + * First version by Ben Gras , + * Initially heavily based on telnetd/pty.c + * by Fred N. van Kempen, . + * + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEV_DIR "/dev" + +/* + * Allocate a PTY, by trying to open one repeatedly. + */ +int openpty(int *amaster, int *aslave, char *name, + struct termios *termp, struct winsize *winp) +{ + char buff[128], temp[128]; + register int i, j; + int pty_fd = -1, gr; + static char tty_name[128]; + struct group *ttygroup; + gid_t tty_gid = 0; + + if(!amaster || !aslave) { + errno = EINVAL; + return -1; + } + + for(i = 'p'; i < 'w'; i++) { + j = 0; + do { + sprintf(buff, "%s/pty%c%c", + DEV_DIR, i, (j < 10) ? j + '0' : j + 'a' - 10); + + if((*amaster = open(buff, O_RDWR)) >= 0) { + sprintf(tty_name, "%s/tty%c%c", DEV_DIR, + i, (j < 10) ? j + '0' : j + 'a' - 10); + if((*aslave = open(tty_name, O_RDWR)) >= 0) { + break; + } + close(*amaster); + } + + j++; + if (j == 16) break; + } while(1); + + /* Did we find one? */ + if (j < 16) break; + } + if (*amaster < 0) { errno = ENOENT; return(-1); } + + setgrent(); + ttygroup = getgrnam("tty"); + endgrent(); + if(ttygroup) tty_gid = ttygroup->gr_gid; + + if(name) strcpy(name, tty_name); + + /* Ignore errors on these. */ + chown(tty_name, getuid(), tty_gid); + chmod(tty_name, 0620); /* -rw--w---- */ + if(termp) tcsetattr(*aslave, TCSAFLUSH, termp); + if(winp) ioctl(*aslave, TIOCSWINSZ, winp); + + return(0); +} +