root/gtags-cscope/logdir.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nextfield
  2. logdir

   1 /*===========================================================================
   2  Copyright (c) 1998-2000, The Santa Cruz Operation 
   3  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 are met:
   7 
   8  *Redistributions of source code must retain the above copyright notice,
   9  this list of conditions and the following disclaimer.
  10 
  11  *Redistributions in binary form must reproduce the above copyright notice,
  12  this list of conditions and the following disclaimer in the documentation
  13  and/or other materials provided with the distribution.
  14 
  15  *Neither name of The Santa Cruz Operation nor the names of its contributors
  16  may be used to endorse or promote products derived from this software
  17  without specific prior written permission. 
  18 
  19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  20  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  23  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26  INTERRUPTION)
  27  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  30  DAMAGE. 
  31  =========================================================================*/
  32 
  33 /** @file
  34  *      logdir()
  35  *
  36  *      This routine does not use the @XREF{getpwent,3} library routine
  37  *      because the latter uses the @NAME{stdio} package.  The allocation of
  38  *      storage in this package destroys the integrity of the shell's
  39  *      storage allocation.
  40  */
  41 
  42 #include <unistd.h>
  43 #include <string.h>
  44 #include "global-cscope.h"
  45 
  46 #define OURBUFSIZ       160     /* renamed: avoid conflict with <stdio.h> */
  47 
  48 static char const rcsid[] = "$Id: logdir.c,v 1.2 2012/10/13 07:02:07 shigio Exp $";
  49 
  50 static char line[OURBUFSIZ+1];
  51 
  52 /* Internal prototypes: */
  53 static  char    *nextfield(char *p);
  54 
  55 
  56 static char *
  57 nextfield(char *p)
  58 {
  59         while (*p && *p != ':')
  60                 ++p;
  61         if (*p) *p++ = 0;
  62         return(p);
  63 }
  64 
  65 char *
  66 logdir(char *name)
  67 {
  68         char    *p;
  69         int     i, j;
  70         int     pwf;
  71         
  72         /* attempt to open the password file */
  73         if ((pwf = myopen("/etc/passwd", 0, 0)) == -1)
  74                 return(0);
  75                 
  76         /* find the matching password entry */
  77         do {
  78                 /* get the next line in the password file */
  79                 i = read(pwf, line, OURBUFSIZ);
  80                 for (j = 0; j < i; j++)
  81                         if (line[j] == '\n')
  82                                 break;
  83                 /* return a null pointer if the whole file has been read */
  84                 if (j >= i)
  85                         return(0);
  86                 line[++j] = 0;                  /* terminate the line */
  87                 (void) lseek(pwf, (long) (j - i), 1);   /* point at the next line */
  88                 p = nextfield(line);            /* get the logname */
  89         } while (*name != *line ||      /* fast pretest */
  90             strcmp(name, line) != 0);
  91         (void) close(pwf);
  92         
  93         /* skip the intervening fields */
  94         p = nextfield(p);
  95         p = nextfield(p);
  96         p = nextfield(p);
  97         p = nextfield(p);
  98         
  99         /* return the login directory */
 100         (void) nextfield(p);
 101         return(p);
 102 }

/* [previous][next][first][last][top][bottom][index][help] */