Back to index

glibc  2.9
getttyent.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1989, 1993
00003  *     The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #if defined(LIBC_SCCS) && !defined(lint)
00031 static char sccsid[] = "@(#)getttyent.c   8.1 (Berkeley) 6/4/93";
00032 #endif /* LIBC_SCCS and not lint */
00033 
00034 #include <ttyent.h>
00035 #include <stdio.h>
00036 #include <stdio_ext.h>
00037 #include <ctype.h>
00038 #include <string.h>
00039 
00040 #ifdef USE_IN_LIBIO
00041 # define flockfile(s) _IO_flockfile (s)
00042 # define funlockfile(s) _IO_funlockfile (s)
00043 #endif
00044 
00045 static char zapchar;
00046 static FILE *tf;
00047 
00048 struct ttyent *
00049 getttynam(tty)
00050        const char *tty;
00051 {
00052        register struct ttyent *t;
00053 
00054        setttyent();
00055        while ((t = getttyent()))
00056               if (!strcmp(tty, t->ty_name))
00057                      break;
00058        endttyent();
00059        return (t);
00060 }
00061 
00062 static char *skip (char *) __THROW internal_function;
00063 static char *value (char *) __THROW internal_function;
00064 
00065 struct ttyent *
00066 getttyent()
00067 {
00068        static struct ttyent tty;
00069        register int c;
00070        register char *p;
00071 #define       MAXLINELENGTH 100
00072        static char line[MAXLINELENGTH];
00073 
00074        if (!tf && !setttyent())
00075               return (NULL);
00076        flockfile (tf);
00077        for (;;) {
00078               if (!fgets_unlocked(p = line, sizeof(line), tf)) {
00079                      funlockfile (tf);
00080                      return (NULL);
00081               }
00082               /* skip lines that are too big */
00083               if (!index(p, '\n')) {
00084                      while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
00085                             ;
00086                      continue;
00087               }
00088               while (isspace(*p))
00089                      ++p;
00090               if (*p && *p != '#')
00091                      break;
00092        }
00093 
00094        zapchar = 0;
00095        tty.ty_name = p;
00096        p = skip(p);
00097        if (!*(tty.ty_getty = p))
00098               tty.ty_getty = tty.ty_type = NULL;
00099        else {
00100               p = skip(p);
00101               if (!*(tty.ty_type = p))
00102                      tty.ty_type = NULL;
00103               else
00104                      p = skip(p);
00105        }
00106        tty.ty_status = 0;
00107        tty.ty_window = NULL;
00108 
00109 #define       scmp(e)       !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
00110 #define       vcmp(e)       !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
00111        for (; *p; p = skip(p)) {
00112               if (scmp(_TTYS_OFF))
00113                      tty.ty_status &= ~TTY_ON;
00114               else if (scmp(_TTYS_ON))
00115                      tty.ty_status |= TTY_ON;
00116               else if (scmp(_TTYS_SECURE))
00117                      tty.ty_status |= TTY_SECURE;
00118               else if (vcmp(_TTYS_WINDOW))
00119                      tty.ty_window = value(p);
00120               else
00121                      break;
00122        }
00123        /* We can release the lock only here since `zapchar' is global.  */
00124        funlockfile(tf);
00125 
00126        if (zapchar == '#' || *p == '#')
00127               while ((c = *++p) == ' ' || c == '\t')
00128                      ;
00129        tty.ty_comment = p;
00130        if (*p == 0)
00131               tty.ty_comment = 0;
00132        if ((p = index(p, '\n')))
00133               *p = '\0';
00134        return (&tty);
00135 }
00136 libc_hidden_def (getttyent)
00137 
00138 #define       QUOTED 1
00139 
00140 /*
00141  * Skip over the current field, removing quotes, and return a pointer to
00142  * the next field.
00143  */
00144 static char *
00145 internal_function
00146 skip(p)
00147        register char *p;
00148 {
00149        register char *t;
00150        register int c, q;
00151 
00152        for (q = 0, t = p; (c = *p) != '\0'; p++) {
00153               if (c == '"') {
00154                      q ^= QUOTED;  /* obscure, but nice */
00155                      continue;
00156               }
00157               if (q == QUOTED && *p == '\\' && *(p+1) == '"')
00158                      p++;
00159               *t++ = *p;
00160               if (q == QUOTED)
00161                      continue;
00162               if (c == '#') {
00163                      zapchar = c;
00164                      *p = 0;
00165                      break;
00166               }
00167               if (c == '\t' || c == ' ' || c == '\n') {
00168                      zapchar = c;
00169                      *p++ = 0;
00170                      while ((c = *p) == '\t' || c == ' ' || c == '\n')
00171                             p++;
00172                      break;
00173               }
00174        }
00175        *--t = '\0';
00176        return (p);
00177 }
00178 
00179 static char *
00180 internal_function
00181 value(p)
00182        register char *p;
00183 {
00184 
00185        return ((p = index(p, '=')) ? ++p : NULL);
00186 }
00187 
00188 int
00189 setttyent()
00190 {
00191 
00192        if (tf) {
00193               (void)rewind(tf);
00194               return (1);
00195        } else if ((tf = fopen(_PATH_TTYS, "rc"))) {
00196               /* We do the locking ourselves.  */
00197               __fsetlocking (tf, FSETLOCKING_BYCALLER);
00198               return (1);
00199        }
00200        return (0);
00201 }
00202 libc_hidden_def (setttyent)
00203 
00204 int
00205 endttyent()
00206 {
00207        int rval;
00208 
00209        if (tf) {
00210               rval = !(fclose(tf) == EOF);
00211               tf = NULL;
00212               return (rval);
00213        }
00214        return (1);
00215 }
00216 libc_hidden_def (endttyent)