17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (c) 1985 Regents of the University of California. 37c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 47c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 57c478bd9Sstevel@tonic-gate */ 67c478bd9Sstevel@tonic-gate 7*5d54f3d8Smuffin #pragma ident "%Z%%M% %I% %E% SMI" 8*5d54f3d8Smuffin 9*5d54f3d8Smuffin 107c478bd9Sstevel@tonic-gate #include <stdio.h> 117c478bd9Sstevel@tonic-gate #include <strings.h> 127c478bd9Sstevel@tonic-gate #include <ttyent.h> 137c478bd9Sstevel@tonic-gate 147c478bd9Sstevel@tonic-gate static char *TTYFILE = "/etc/ttytab"; 157c478bd9Sstevel@tonic-gate #define LINE 256 167c478bd9Sstevel@tonic-gate static struct _ttyentjunk { 177c478bd9Sstevel@tonic-gate char zapchar; 187c478bd9Sstevel@tonic-gate FILE *tf; 197c478bd9Sstevel@tonic-gate char line[LINE]; 207c478bd9Sstevel@tonic-gate struct ttyent tty; 21*5d54f3d8Smuffin } *__ttyentjunk, *_ttyentjunk(void); 227c478bd9Sstevel@tonic-gate 237c478bd9Sstevel@tonic-gate static struct _ttyentjunk * 24*5d54f3d8Smuffin _ttyentjunk(void) 257c478bd9Sstevel@tonic-gate { 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate if (__ttyentjunk == 0) 287c478bd9Sstevel@tonic-gate __ttyentjunk = (struct _ttyentjunk *)calloc(1, sizeof (struct _ttyentjunk)); 297c478bd9Sstevel@tonic-gate return (__ttyentjunk); 307c478bd9Sstevel@tonic-gate } 317c478bd9Sstevel@tonic-gate 32*5d54f3d8Smuffin void 33*5d54f3d8Smuffin setttyent(void) 347c478bd9Sstevel@tonic-gate { 35*5d54f3d8Smuffin struct _ttyentjunk *t = _ttyentjunk(); 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate if (t == 0) 387c478bd9Sstevel@tonic-gate return; 397c478bd9Sstevel@tonic-gate if (t->tf == NULL) 407c478bd9Sstevel@tonic-gate t->tf = fopen(TTYFILE, "r"); 417c478bd9Sstevel@tonic-gate else 427c478bd9Sstevel@tonic-gate rewind(t->tf); 437c478bd9Sstevel@tonic-gate } 447c478bd9Sstevel@tonic-gate 45*5d54f3d8Smuffin void 46*5d54f3d8Smuffin endttyent(void) 477c478bd9Sstevel@tonic-gate { 48*5d54f3d8Smuffin struct _ttyentjunk *t = _ttyentjunk(); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate if (t == 0) 517c478bd9Sstevel@tonic-gate return; 527c478bd9Sstevel@tonic-gate if (t->tf != NULL) { 537c478bd9Sstevel@tonic-gate (void) fclose(t->tf); 547c478bd9Sstevel@tonic-gate t->tf = NULL; 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate } 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #define QUOTED 1 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * Skip over the current field, removing quotes, 627c478bd9Sstevel@tonic-gate * and return a pointer to the next field. 637c478bd9Sstevel@tonic-gate */ 647c478bd9Sstevel@tonic-gate static char * 65*5d54f3d8Smuffin skip(char *p) 667c478bd9Sstevel@tonic-gate { 67*5d54f3d8Smuffin struct _ttyentjunk *t = _ttyentjunk(); 68*5d54f3d8Smuffin char *cp = p; 69*5d54f3d8Smuffin int c; 70*5d54f3d8Smuffin int q = 0; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate if (t == 0) 737c478bd9Sstevel@tonic-gate return (0); 747c478bd9Sstevel@tonic-gate for (; (c = *p) != '\0'; p++) { 757c478bd9Sstevel@tonic-gate if (c == '"') { 767c478bd9Sstevel@tonic-gate q ^= QUOTED; /* obscure, but nice */ 777c478bd9Sstevel@tonic-gate continue; 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate if (q == QUOTED && *p == '\\' && *(p+1) == '"') 807c478bd9Sstevel@tonic-gate p++; 817c478bd9Sstevel@tonic-gate *cp++ = *p; 827c478bd9Sstevel@tonic-gate if (q == QUOTED) 837c478bd9Sstevel@tonic-gate continue; 847c478bd9Sstevel@tonic-gate if (c == '#') { 857c478bd9Sstevel@tonic-gate t->zapchar = c; 867c478bd9Sstevel@tonic-gate *p = 0; 877c478bd9Sstevel@tonic-gate break; 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate if (c == '\t' || c == ' ' || c == '\n') { 907c478bd9Sstevel@tonic-gate t->zapchar = c; 917c478bd9Sstevel@tonic-gate *p++ = 0; 927c478bd9Sstevel@tonic-gate while ((c = *p) == '\t' || c == ' ' || c == '\n') 937c478bd9Sstevel@tonic-gate p++; 947c478bd9Sstevel@tonic-gate break; 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate *--cp = '\0'; 987c478bd9Sstevel@tonic-gate return (p); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate static char * 102*5d54f3d8Smuffin value(char *p) 1037c478bd9Sstevel@tonic-gate { 1047c478bd9Sstevel@tonic-gate if ((p = index(p,'=')) == 0) 1057c478bd9Sstevel@tonic-gate return (NULL); 1067c478bd9Sstevel@tonic-gate p++; /* get past the = sign */ 1077c478bd9Sstevel@tonic-gate return (p); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate struct ttyent * 111*5d54f3d8Smuffin getttyent(void) 1127c478bd9Sstevel@tonic-gate { 113*5d54f3d8Smuffin struct _ttyentjunk *t = _ttyentjunk(); 114*5d54f3d8Smuffin char *p; 115*5d54f3d8Smuffin int c; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate if (t == 0) 1187c478bd9Sstevel@tonic-gate return (NULL); 1197c478bd9Sstevel@tonic-gate if (t->tf == NULL) { 1207c478bd9Sstevel@tonic-gate if ((t->tf = fopen(TTYFILE, "r")) == NULL) 1217c478bd9Sstevel@tonic-gate return (NULL); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate do { 1247c478bd9Sstevel@tonic-gate p = fgets(t->line, LINE, t->tf); 1257c478bd9Sstevel@tonic-gate if (p == NULL) 1267c478bd9Sstevel@tonic-gate return (NULL); 1277c478bd9Sstevel@tonic-gate while ((c = *p) == '\t' || c == ' ' || c == '\n') 1287c478bd9Sstevel@tonic-gate p++; 1297c478bd9Sstevel@tonic-gate } while (c == '\0' || c == '#'); 1307c478bd9Sstevel@tonic-gate t->zapchar = 0; 1317c478bd9Sstevel@tonic-gate t->tty.ty_name = p; 1327c478bd9Sstevel@tonic-gate p = skip(p); 1337c478bd9Sstevel@tonic-gate t->tty.ty_getty = p; 1347c478bd9Sstevel@tonic-gate p = skip(p); 1357c478bd9Sstevel@tonic-gate t->tty.ty_type = p; 1367c478bd9Sstevel@tonic-gate p = skip(p); 1377c478bd9Sstevel@tonic-gate t->tty.ty_status = 0; 1387c478bd9Sstevel@tonic-gate t->tty.ty_window = NULL; 1397c478bd9Sstevel@tonic-gate for (; *p; p = skip(p)) { 1407c478bd9Sstevel@tonic-gate #define space(x) ((c = p[x]) == ' ' || c == '\t' || c == '\n') 1417c478bd9Sstevel@tonic-gate if (strncmp(p, "on", 2) == 0 && space(2)) 1427c478bd9Sstevel@tonic-gate t->tty.ty_status |= TTY_ON; 1437c478bd9Sstevel@tonic-gate else if (strncmp(p, "off", 3) == 0 && space(3)) 1447c478bd9Sstevel@tonic-gate t->tty.ty_status &= ~TTY_ON; 1457c478bd9Sstevel@tonic-gate else if (strncmp(p, "secure", 6) == 0 && space(6)) 1467c478bd9Sstevel@tonic-gate t->tty.ty_status |= TTY_SECURE; 1477c478bd9Sstevel@tonic-gate else if (strncmp(p, "local", 5) == 0 && space(5)) 1487c478bd9Sstevel@tonic-gate t->tty.ty_status |= TTY_LOCAL; 1497c478bd9Sstevel@tonic-gate else if (strncmp(p, "window=", 7) == 0) 1507c478bd9Sstevel@tonic-gate t->tty.ty_window = value(p); 1517c478bd9Sstevel@tonic-gate else 1527c478bd9Sstevel@tonic-gate break; 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate if (t->zapchar == '#' || *p == '#') 1557c478bd9Sstevel@tonic-gate while ((c = *++p) == ' ' || c == '\t') 1567c478bd9Sstevel@tonic-gate ; 1577c478bd9Sstevel@tonic-gate t->tty.ty_comment = p; 1587c478bd9Sstevel@tonic-gate if (*p == 0) 1597c478bd9Sstevel@tonic-gate t->tty.ty_comment = 0; 1607c478bd9Sstevel@tonic-gate if (p = index(p, '\n')) 1617c478bd9Sstevel@tonic-gate *p = '\0'; 1627c478bd9Sstevel@tonic-gate return (&t->tty); 1637c478bd9Sstevel@tonic-gate } 164