158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1989, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1558f0484fSRodney W. Grimes * without specific prior written permission. 1658f0484fSRodney W. Grimes * 1758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2758f0484fSRodney W. Grimes * SUCH DAMAGE. 2858f0484fSRodney W. Grimes */ 2958f0484fSRodney W. Grimes 3058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3158f0484fSRodney W. Grimes static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; 3258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 33b231cb39SDavid E. O'Brien #include <sys/cdefs.h> 34b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3558f0484fSRodney W. Grimes 3658f0484fSRodney W. Grimes #include <ttyent.h> 3758f0484fSRodney W. Grimes #include <stdio.h> 385fae0297SJoerg Wunsch #include <stdlib.h> 3958f0484fSRodney W. Grimes #include <ctype.h> 4058f0484fSRodney W. Grimes #include <string.h> 41af09c340SOlivier Houchard #include <dirent.h> 42af09c340SOlivier Houchard #include <paths.h> 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes static char zapchar; 4558f0484fSRodney W. Grimes static FILE *tf; 46a73306e1SEd Schouten static int maxpts = -1; 47af09c340SOlivier Houchard static int curpts = 0; 485fae0297SJoerg Wunsch static size_t lbsize; 495fae0297SJoerg Wunsch static char *line; 505fae0297SJoerg Wunsch 51af09c340SOlivier Houchard #define PTS "pts/" 525fae0297SJoerg Wunsch #define MALLOCCHUNK 100 535fae0297SJoerg Wunsch 54b231cb39SDavid E. O'Brien static char *skip(char *); 55b231cb39SDavid E. O'Brien static char *value(char *); 5658f0484fSRodney W. Grimes 5758f0484fSRodney W. Grimes struct ttyent * 582c201a9aSEd Schouten getttynam(const char *tty) 5958f0484fSRodney W. Grimes { 60b231cb39SDavid E. O'Brien struct ttyent *t; 6158f0484fSRodney W. Grimes 625afcddaeSDavid Nugent if (strncmp(tty, "/dev/", 5) == 0) 63b06ebb32SDavid Nugent tty += 5; 6458f0484fSRodney W. Grimes setttyent(); 6551295a4dSJordan K. Hubbard while ( (t = getttyent()) ) 6658f0484fSRodney W. Grimes if (!strcmp(tty, t->ty_name)) 6758f0484fSRodney W. Grimes break; 6858f0484fSRodney W. Grimes endttyent(); 6958f0484fSRodney W. Grimes return (t); 7058f0484fSRodney W. Grimes } 7158f0484fSRodney W. Grimes 7258f0484fSRodney W. Grimes struct ttyent * 732c201a9aSEd Schouten getttyent(void) 7458f0484fSRodney W. Grimes { 7558f0484fSRodney W. Grimes static struct ttyent tty; 76af09c340SOlivier Houchard static char devpts_name[] = "pts/4294967295"; 77b231cb39SDavid E. O'Brien char *p; 78b231cb39SDavid E. O'Brien int c; 795fae0297SJoerg Wunsch size_t i; 8058f0484fSRodney W. Grimes 8158f0484fSRodney W. Grimes if (!tf && !setttyent()) 8258f0484fSRodney W. Grimes return (NULL); 8358f0484fSRodney W. Grimes for (;;) { 84af09c340SOlivier Houchard if (!fgets(p = line, lbsize, tf)) { 85a73306e1SEd Schouten if (curpts <= maxpts) { 86af09c340SOlivier Houchard sprintf(devpts_name, "pts/%d", curpts++); 87af09c340SOlivier Houchard tty.ty_name = devpts_name; 88af09c340SOlivier Houchard tty.ty_getty = tty.ty_type = NULL; 89af09c340SOlivier Houchard tty.ty_status = TTY_NETWORK; 90af09c340SOlivier Houchard tty.ty_window = NULL; 91af09c340SOlivier Houchard tty.ty_comment = NULL; 92af09c340SOlivier Houchard tty.ty_group = _TTYS_NOGROUP; 93af09c340SOlivier Houchard return (&tty); 94af09c340SOlivier Houchard } 9558f0484fSRodney W. Grimes return (NULL); 96af09c340SOlivier Houchard } 975fae0297SJoerg Wunsch /* extend buffer if line was too big, and retry */ 9800a32d0cSDavid Schultz while (!index(p, '\n') && !feof(tf)) { 995fae0297SJoerg Wunsch i = strlen(p); 1005fae0297SJoerg Wunsch lbsize += MALLOCCHUNK; 101a098bfd7SJoerg Wunsch if ((p = realloc(line, lbsize)) == NULL) { 1025fae0297SJoerg Wunsch (void)endttyent(); 1035fae0297SJoerg Wunsch return (NULL); 1045fae0297SJoerg Wunsch } 105a098bfd7SJoerg Wunsch line = p; 1065fae0297SJoerg Wunsch if (!fgets(&line[i], lbsize - i, tf)) 1075fae0297SJoerg Wunsch return (NULL); 10858f0484fSRodney W. Grimes } 10949435560SAndrey A. Chernov while (isspace((unsigned char)*p)) 11058f0484fSRodney W. Grimes ++p; 11158f0484fSRodney W. Grimes if (*p && *p != '#') 11258f0484fSRodney W. Grimes break; 11358f0484fSRodney W. Grimes } 11458f0484fSRodney W. Grimes 11549435560SAndrey A. Chernov #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace((unsigned char)p[sizeof(e) - 1]) 116b06ebb32SDavid Nugent #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '=' 117b06ebb32SDavid Nugent 11858f0484fSRodney W. Grimes zapchar = 0; 11958f0484fSRodney W. Grimes tty.ty_name = p; 12050007d44SMatthew N. Dodd tty.ty_status = 0; 12150007d44SMatthew N. Dodd tty.ty_window = NULL; 12250007d44SMatthew N. Dodd tty.ty_group = _TTYS_NOGROUP; 12350007d44SMatthew N. Dodd 12458f0484fSRodney W. Grimes p = skip(p); 12558f0484fSRodney W. Grimes if (!*(tty.ty_getty = p)) 12658f0484fSRodney W. Grimes tty.ty_getty = tty.ty_type = NULL; 12758f0484fSRodney W. Grimes else { 12858f0484fSRodney W. Grimes p = skip(p); 12958f0484fSRodney W. Grimes if (!*(tty.ty_type = p)) 13058f0484fSRodney W. Grimes tty.ty_type = NULL; 131b06ebb32SDavid Nugent else { 132b06ebb32SDavid Nugent /* compatibility kludge: handle network/dialup specially */ 133b06ebb32SDavid Nugent if (scmp(_TTYS_DIALUP)) 134b06ebb32SDavid Nugent tty.ty_status |= TTY_DIALUP; 135b06ebb32SDavid Nugent else if (scmp(_TTYS_NETWORK)) 136b06ebb32SDavid Nugent tty.ty_status |= TTY_NETWORK; 13758f0484fSRodney W. Grimes p = skip(p); 13858f0484fSRodney W. Grimes } 139b06ebb32SDavid Nugent } 14058f0484fSRodney W. Grimes 14158f0484fSRodney W. Grimes for (; *p; p = skip(p)) { 14258f0484fSRodney W. Grimes if (scmp(_TTYS_OFF)) 14358f0484fSRodney W. Grimes tty.ty_status &= ~TTY_ON; 14458f0484fSRodney W. Grimes else if (scmp(_TTYS_ON)) 14558f0484fSRodney W. Grimes tty.ty_status |= TTY_ON; 14658f0484fSRodney W. Grimes else if (scmp(_TTYS_SECURE)) 14758f0484fSRodney W. Grimes tty.ty_status |= TTY_SECURE; 148a60c8a80SDavid Nugent else if (scmp(_TTYS_INSECURE)) 149a60c8a80SDavid Nugent tty.ty_status &= ~TTY_SECURE; 150b06ebb32SDavid Nugent else if (scmp(_TTYS_DIALUP)) 151b06ebb32SDavid Nugent tty.ty_status |= TTY_DIALUP; 152b06ebb32SDavid Nugent else if (scmp(_TTYS_NETWORK)) 153b06ebb32SDavid Nugent tty.ty_status |= TTY_NETWORK; 15458f0484fSRodney W. Grimes else if (vcmp(_TTYS_WINDOW)) 15558f0484fSRodney W. Grimes tty.ty_window = value(p); 1564ae89ecdSDavid Nugent else if (vcmp(_TTYS_GROUP)) 1574ae89ecdSDavid Nugent tty.ty_group = value(p); 15858f0484fSRodney W. Grimes else 15958f0484fSRodney W. Grimes break; 16058f0484fSRodney W. Grimes } 16158f0484fSRodney W. Grimes 16258f0484fSRodney W. Grimes if (zapchar == '#' || *p == '#') 16358f0484fSRodney W. Grimes while ((c = *++p) == ' ' || c == '\t') 16458f0484fSRodney W. Grimes ; 16558f0484fSRodney W. Grimes tty.ty_comment = p; 16658f0484fSRodney W. Grimes if (*p == 0) 16758f0484fSRodney W. Grimes tty.ty_comment = 0; 16851295a4dSJordan K. Hubbard if ( (p = index(p, '\n')) ) 16958f0484fSRodney W. Grimes *p = '\0'; 17058f0484fSRodney W. Grimes return (&tty); 17158f0484fSRodney W. Grimes } 17258f0484fSRodney W. Grimes 17358f0484fSRodney W. Grimes #define QUOTED 1 17458f0484fSRodney W. Grimes 17558f0484fSRodney W. Grimes /* 17658f0484fSRodney W. Grimes * Skip over the current field, removing quotes, and return a pointer to 17758f0484fSRodney W. Grimes * the next field. 17858f0484fSRodney W. Grimes */ 17958f0484fSRodney W. Grimes static char * 1802c201a9aSEd Schouten skip(char *p) 18158f0484fSRodney W. Grimes { 182b231cb39SDavid E. O'Brien char *t; 183b231cb39SDavid E. O'Brien int c, q; 18458f0484fSRodney W. Grimes 18558f0484fSRodney W. Grimes for (q = 0, t = p; (c = *p) != '\0'; p++) { 18658f0484fSRodney W. Grimes if (c == '"') { 18758f0484fSRodney W. Grimes q ^= QUOTED; /* obscure, but nice */ 18858f0484fSRodney W. Grimes continue; 18958f0484fSRodney W. Grimes } 19058f0484fSRodney W. Grimes if (q == QUOTED && *p == '\\' && *(p+1) == '"') 19158f0484fSRodney W. Grimes p++; 19258f0484fSRodney W. Grimes *t++ = *p; 19358f0484fSRodney W. Grimes if (q == QUOTED) 19458f0484fSRodney W. Grimes continue; 19558f0484fSRodney W. Grimes if (c == '#') { 19658f0484fSRodney W. Grimes zapchar = c; 19758f0484fSRodney W. Grimes *p = 0; 19858f0484fSRodney W. Grimes break; 19958f0484fSRodney W. Grimes } 20058f0484fSRodney W. Grimes if (c == '\t' || c == ' ' || c == '\n') { 20158f0484fSRodney W. Grimes zapchar = c; 20258f0484fSRodney W. Grimes *p++ = 0; 20358f0484fSRodney W. Grimes while ((c = *p) == '\t' || c == ' ' || c == '\n') 20458f0484fSRodney W. Grimes p++; 20558f0484fSRodney W. Grimes break; 20658f0484fSRodney W. Grimes } 20758f0484fSRodney W. Grimes } 20858f0484fSRodney W. Grimes *--t = '\0'; 20958f0484fSRodney W. Grimes return (p); 21058f0484fSRodney W. Grimes } 21158f0484fSRodney W. Grimes 21258f0484fSRodney W. Grimes static char * 2132c201a9aSEd Schouten value(char *p) 21458f0484fSRodney W. Grimes { 21558f0484fSRodney W. Grimes 21658f0484fSRodney W. Grimes return ((p = index(p, '=')) ? ++p : NULL); 21758f0484fSRodney W. Grimes } 21858f0484fSRodney W. Grimes 21958f0484fSRodney W. Grimes int 2202c201a9aSEd Schouten setttyent(void) 22158f0484fSRodney W. Grimes { 222af09c340SOlivier Houchard DIR *devpts_dir; 22358f0484fSRodney W. Grimes 2245fae0297SJoerg Wunsch if (line == NULL) { 2255fae0297SJoerg Wunsch if ((line = malloc(MALLOCCHUNK)) == NULL) 2265fae0297SJoerg Wunsch return (0); 2275fae0297SJoerg Wunsch lbsize = MALLOCCHUNK; 2285fae0297SJoerg Wunsch } 229af09c340SOlivier Houchard devpts_dir = opendir(_PATH_DEV PTS); 230af09c340SOlivier Houchard if (devpts_dir) { 231af09c340SOlivier Houchard struct dirent *dp; 232af09c340SOlivier Houchard 233a73306e1SEd Schouten maxpts = -1; 234af09c340SOlivier Houchard while ((dp = readdir(devpts_dir))) { 235af09c340SOlivier Houchard if (strcmp(dp->d_name, ".") != 0 && 236af09c340SOlivier Houchard strcmp(dp->d_name, "..") != 0) { 237af09c340SOlivier Houchard if (atoi(dp->d_name) > maxpts) { 238af09c340SOlivier Houchard maxpts = atoi(dp->d_name); 239af09c340SOlivier Houchard curpts = 0; 240af09c340SOlivier Houchard } 241af09c340SOlivier Houchard } 242af09c340SOlivier Houchard } 243af09c340SOlivier Houchard closedir(devpts_dir); 244af09c340SOlivier Houchard } 24558f0484fSRodney W. Grimes if (tf) { 246e78bad23SJeffrey Hsu rewind(tf); 24758f0484fSRodney W. Grimes return (1); 24851295a4dSJordan K. Hubbard } else if ( (tf = fopen(_PATH_TTYS, "r")) ) 24958f0484fSRodney W. Grimes return (1); 25058f0484fSRodney W. Grimes return (0); 25158f0484fSRodney W. Grimes } 25258f0484fSRodney W. Grimes 25358f0484fSRodney W. Grimes int 2542c201a9aSEd Schouten endttyent(void) 25558f0484fSRodney W. Grimes { 25658f0484fSRodney W. Grimes int rval; 25758f0484fSRodney W. Grimes 258a73306e1SEd Schouten maxpts = -1; 2594e176059SJoerg Wunsch /* 2604e176059SJoerg Wunsch * NB: Don't free `line' because getttynam() 2614e176059SJoerg Wunsch * may still be referencing it 2624e176059SJoerg Wunsch */ 26358f0484fSRodney W. Grimes if (tf) { 2645fae0297SJoerg Wunsch rval = (fclose(tf) != EOF); 26558f0484fSRodney W. Grimes tf = NULL; 26658f0484fSRodney W. Grimes return (rval); 26758f0484fSRodney W. Grimes } 26858f0484fSRodney W. Grimes return (1); 26958f0484fSRodney W. Grimes } 270b06ebb32SDavid Nugent 271b06ebb32SDavid Nugent static int 2722c201a9aSEd Schouten isttystat(const char *tty, int flag) 273b06ebb32SDavid Nugent { 274b231cb39SDavid E. O'Brien struct ttyent *t; 275b06ebb32SDavid Nugent 276b06ebb32SDavid Nugent return ((t = getttynam(tty)) == NULL) ? 0 : !!(t->ty_status & flag); 277b06ebb32SDavid Nugent } 278b06ebb32SDavid Nugent 279b06ebb32SDavid Nugent 280b06ebb32SDavid Nugent int 2812c201a9aSEd Schouten isdialuptty(const char *tty) 282b06ebb32SDavid Nugent { 283b06ebb32SDavid Nugent 284b06ebb32SDavid Nugent return isttystat(tty, TTY_DIALUP); 285b06ebb32SDavid Nugent } 286b06ebb32SDavid Nugent 2872c201a9aSEd Schouten int 2882c201a9aSEd Schouten isnettty(const char *tty) 289b06ebb32SDavid Nugent { 290b06ebb32SDavid Nugent 291b06ebb32SDavid Nugent return isttystat(tty, TTY_NETWORK); 292b06ebb32SDavid Nugent } 293