getttyent.c (200150) | getttyent.c (203068) |
---|---|
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 17 unchanged lines hidden (view full) --- 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#if defined(LIBC_SCCS) && !defined(lint) 31static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; 32#endif /* LIBC_SCCS and not lint */ 33#include <sys/cdefs.h> | 1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 17 unchanged lines hidden (view full) --- 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#if defined(LIBC_SCCS) && !defined(lint) 31static char sccsid[] = "@(#)getttyent.c 8.1 (Berkeley) 6/4/93"; 32#endif /* LIBC_SCCS and not lint */ 33#include <sys/cdefs.h> |
34__FBSDID("$FreeBSD: head/lib/libc/gen/getttyent.c 200150 2009-12-05 19:31:38Z ed $"); | 34__FBSDID("$FreeBSD: head/lib/libc/gen/getttyent.c 203068 2010-01-27 11:54:42Z ed $"); |
35 36#include <ttyent.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <ctype.h> 40#include <string.h> | 35 36#include <ttyent.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <ctype.h> 40#include <string.h> |
41#include <dirent.h> 42#include <paths.h> | |
43 44static char zapchar; 45static FILE *tf; | 41 42static char zapchar; 43static FILE *tf; |
46static int maxpts = -1; 47static int curpts = 0; | |
48static size_t lbsize; 49static char *line; 50 | 44static size_t lbsize; 45static char *line; 46 |
51#define PTS "pts/" | |
52#define MALLOCCHUNK 100 53 54static char *skip(char *); 55static char *value(char *); 56 57struct ttyent * 58getttynam(const char *tty) 59{ --- 8 unchanged lines hidden (view full) --- 68 endttyent(); 69 return (t); 70} 71 72struct ttyent * 73getttyent(void) 74{ 75 static struct ttyent tty; | 47#define MALLOCCHUNK 100 48 49static char *skip(char *); 50static char *value(char *); 51 52struct ttyent * 53getttynam(const char *tty) 54{ --- 8 unchanged lines hidden (view full) --- 63 endttyent(); 64 return (t); 65} 66 67struct ttyent * 68getttyent(void) 69{ 70 static struct ttyent tty; |
76 static char devpts_name[] = "pts/4294967295"; | |
77 char *p; 78 int c; 79 size_t i; 80 81 if (!tf && !setttyent()) 82 return (NULL); 83 for (;;) { | 71 char *p; 72 int c; 73 size_t i; 74 75 if (!tf && !setttyent()) 76 return (NULL); 77 for (;;) { |
84 if (!fgets(p = line, lbsize, tf)) { 85 if (curpts <= maxpts) { 86 sprintf(devpts_name, "pts/%d", curpts++); 87 tty.ty_name = devpts_name; 88 tty.ty_getty = tty.ty_type = NULL; 89 tty.ty_status = TTY_NETWORK; 90 tty.ty_window = NULL; 91 tty.ty_comment = NULL; 92 tty.ty_group = _TTYS_NOGROUP; 93 return (&tty); 94 } | 78 if (!fgets(p = line, lbsize, tf)) |
95 return (NULL); | 79 return (NULL); |
96 } | |
97 /* extend buffer if line was too big, and retry */ 98 while (!index(p, '\n') && !feof(tf)) { 99 i = strlen(p); 100 lbsize += MALLOCCHUNK; 101 if ((p = realloc(line, lbsize)) == NULL) { 102 (void)endttyent(); 103 return (NULL); 104 } --- 109 unchanged lines hidden (view full) --- 214{ 215 216 return ((p = index(p, '=')) ? ++p : NULL); 217} 218 219int 220setttyent(void) 221{ | 80 /* extend buffer if line was too big, and retry */ 81 while (!index(p, '\n') && !feof(tf)) { 82 i = strlen(p); 83 lbsize += MALLOCCHUNK; 84 if ((p = realloc(line, lbsize)) == NULL) { 85 (void)endttyent(); 86 return (NULL); 87 } --- 109 unchanged lines hidden (view full) --- 197{ 198 199 return ((p = index(p, '=')) ? ++p : NULL); 200} 201 202int 203setttyent(void) 204{ |
222 DIR *devpts_dir; | |
223 224 if (line == NULL) { 225 if ((line = malloc(MALLOCCHUNK)) == NULL) 226 return (0); 227 lbsize = MALLOCCHUNK; 228 } | 205 206 if (line == NULL) { 207 if ((line = malloc(MALLOCCHUNK)) == NULL) 208 return (0); 209 lbsize = MALLOCCHUNK; 210 } |
229 devpts_dir = opendir(_PATH_DEV PTS); 230 if (devpts_dir) { 231 struct dirent *dp; 232 233 maxpts = -1; 234 while ((dp = readdir(devpts_dir))) { 235 if (strcmp(dp->d_name, ".") != 0 && 236 strcmp(dp->d_name, "..") != 0) { 237 if (atoi(dp->d_name) > maxpts) { 238 maxpts = atoi(dp->d_name); 239 curpts = 0; 240 } 241 } 242 } 243 closedir(devpts_dir); 244 } | |
245 if (tf) { 246 rewind(tf); 247 return (1); 248 } else if ( (tf = fopen(_PATH_TTYS, "r")) ) 249 return (1); 250 return (0); 251} 252 253int 254endttyent(void) 255{ 256 int rval; 257 | 211 if (tf) { 212 rewind(tf); 213 return (1); 214 } else if ( (tf = fopen(_PATH_TTYS, "r")) ) 215 return (1); 216 return (0); 217} 218 219int 220endttyent(void) 221{ 222 int rval; 223 |
258 maxpts = -1; | |
259 /* 260 * NB: Don't free `line' because getttynam() 261 * may still be referencing it 262 */ 263 if (tf) { 264 rval = (fclose(tf) != EOF); 265 tf = NULL; 266 return (rval); --- 26 unchanged lines hidden --- | 224 /* 225 * NB: Don't free `line' because getttynam() 226 * may still be referencing it 227 */ 228 if (tf) { 229 rval = (fclose(tf) != EOF); 230 tf = NULL; 231 return (rval); --- 26 unchanged lines hidden --- |