Make getttyent() report what the pts ptys as well.

This commit is contained in:
Olivier Houchard 2006-01-26 01:34:26 +00:00
parent 67c7201e18
commit af09c34069
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=154838

View file

@ -42,12 +42,18 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <dirent.h>
#include <paths.h>
static char zapchar; static char zapchar;
static FILE *tf; static FILE *tf;
static int maxpts = 0;
static int curpts = 0;
static int pts_valid = 0;
static size_t lbsize; static size_t lbsize;
static char *line; static char *line;
#define PTS "pts/"
#define MALLOCCHUNK 100 #define MALLOCCHUNK 100
static char *skip(char *); static char *skip(char *);
@ -73,6 +79,7 @@ struct ttyent *
getttyent() getttyent()
{ {
static struct ttyent tty; static struct ttyent tty;
static char devpts_name[] = "pts/4294967295";
char *p; char *p;
int c; int c;
size_t i; size_t i;
@ -80,8 +87,19 @@ getttyent()
if (!tf && !setttyent()) if (!tf && !setttyent())
return (NULL); return (NULL);
for (;;) { for (;;) {
if (!fgets(p = line, lbsize, tf)) if (!fgets(p = line, lbsize, tf)) {
if (pts_valid == 1 && curpts <= maxpts) {
sprintf(devpts_name, "pts/%d", curpts++);
tty.ty_name = devpts_name;
tty.ty_getty = tty.ty_type = NULL;
tty.ty_status = TTY_NETWORK;
tty.ty_window = NULL;
tty.ty_comment = NULL;
tty.ty_group = _TTYS_NOGROUP;
return (&tty);
}
return (NULL); return (NULL);
}
/* extend buffer if line was too big, and retry */ /* extend buffer if line was too big, and retry */
while (!index(p, '\n')) { while (!index(p, '\n')) {
i = strlen(p); i = strlen(p);
@ -209,12 +227,30 @@ value(p)
int int
setttyent() setttyent()
{ {
DIR *devpts_dir;
if (line == NULL) { if (line == NULL) {
if ((line = malloc(MALLOCCHUNK)) == NULL) if ((line = malloc(MALLOCCHUNK)) == NULL)
return (0); return (0);
lbsize = MALLOCCHUNK; lbsize = MALLOCCHUNK;
} }
devpts_dir = opendir(_PATH_DEV PTS);
if (devpts_dir) {
struct dirent *dp;
while ((dp = readdir(devpts_dir))) {
if (strcmp(dp->d_name, ".") != 0 &&
strcmp(dp->d_name, "..") != 0) {
if (atoi(dp->d_name) > maxpts) {
maxpts = atoi(dp->d_name);
pts_valid = 1;
curpts = 0;
}
}
}
closedir(devpts_dir);
}
printf("it is %d %d\n", maxpts, curpts);
if (tf) { if (tf) {
rewind(tf); rewind(tf);
return (1); return (1);
@ -228,6 +264,7 @@ endttyent()
{ {
int rval; int rval;
pts_valid = 0;
/* /*
* NB: Don't free `line' because getttynam() * NB: Don't free `line' because getttynam()
* may still be referencing it * may still be referencing it