home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / names.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  4KB  |  163 lines

  1. /*****************************************************************************
  2.  * $Id: names.c,v 1.2 1992/09/02 20:08:26 ak Exp $
  3.  *****************************************************************************
  4.  * $Log: names.c,v $
  5.  * Revision 1.2  1992/09/02  20:08:26  ak
  6.  * Version AK200
  7.  * - Tape access
  8.  * - Quick file access
  9.  * - OS/2 extended attributes
  10.  * - Some OS/2 fixes
  11.  * - Some fixes of Kai Uwe Rommel
  12.  *
  13.  * Revision 1.1.1.1  1992/09/02  19:22:08  ak
  14.  * Original GNU Tar 1.10 with some filenames changed for FAT compatibility.
  15.  *
  16.  * Revision 1.1  1992/09/02  19:22:06  ak
  17.  * Initial revision
  18.  *
  19.  *****************************************************************************/
  20.  
  21. static char *rcsid = "$Id: names.c,v 1.2 1992/09/02 20:08:26 ak Exp $";
  22.  
  23. /*
  24.  * Modified by Andreas Kaiser July 92.
  25.  * See CHANGES.AK for info.
  26.  */
  27.  
  28. /* Look up user and/or group names.
  29.    Copyright (C) 1988 Free Software Foundation
  30.  
  31. This file is part of GNU Tar.
  32.  
  33. GNU Tar is free software; you can redistribute it and/or modify
  34. it under the terms of the GNU General Public License as published by
  35. the Free Software Foundation; either version 1, or (at your option)
  36. any later version.
  37.  
  38. GNU Tar is distributed in the hope that it will be useful,
  39. but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  41. GNU General Public License for more details.
  42.  
  43. You should have received a copy of the GNU General Public License
  44. along with GNU Tar; see the file COPYING.  If not, write to
  45. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  46.  
  47. /*
  48.  * Look up user and/or group names.
  49.  *
  50.  * This file should be modified for non-unix systems to do something
  51.  * reasonable.
  52.  *
  53.  * @(#)names.c 1.3 10/30/87 - gnu
  54.  */ 
  55. #include <sys/types.h>
  56. #include "tar.h"
  57.  
  58. extern    char    *strncpy();
  59.  
  60. #ifndef NONAMES
  61. /* Whole module goes away if NONAMES defined.  Otherwise... */
  62. #include <pwd.h>
  63. #include <grp.h>
  64.  
  65. static int    saveuid = -993;
  66. static char    saveuname[TUNMLEN];
  67. static int    my_uid = -993;
  68.  
  69. static int    savegid = -993;
  70. static char    savegname[TGNMLEN];
  71. static int    my_gid = -993;
  72.  
  73. #define myuid    ( my_uid < 0? (my_uid = getuid()): my_uid )
  74. #define    mygid    ( my_gid < 0? (my_gid = getgid()): my_gid )
  75.  
  76. /*
  77.  * Look up a user or group name from a uid/gid, maintaining a cache.
  78.  * FIXME, for now it's a one-entry cache.
  79.  * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  80.  *
  81.  * This is ifdef'd because on Suns, it drags in about 38K of "yellow
  82.  * pages" code, roughly doubling the program size.  Thanks guys.
  83.  */
  84. void
  85. finduname(uname, uid)
  86.     char    uname[TUNMLEN];
  87.     int    uid;
  88. {
  89.     struct passwd    *pw;
  90.     extern struct passwd *getpwuid ();
  91.  
  92.     if (uid != saveuid) {
  93.         saveuid = uid;
  94.         saveuname[0] = '\0';
  95.         pw = getpwuid(uid); 
  96.         if (pw) 
  97.             strncpy(saveuname, pw->pw_name, TUNMLEN);
  98.     }
  99.     strncpy(uname, saveuname, TUNMLEN);
  100. }
  101.  
  102. int
  103. finduid(uname)
  104.     char    uname[TUNMLEN];
  105. {
  106.     struct passwd    *pw;
  107.     extern struct passwd *getpwnam();
  108.  
  109.     if (uname[0] != saveuname[0]    /* Quick test w/o proc call */
  110.         || 0!=strncmp(uname, saveuname, TUNMLEN)) {
  111.         strncpy(saveuname, uname, TUNMLEN);
  112.         pw = getpwnam(uname); 
  113.         if (pw) {
  114.             saveuid = pw->pw_uid;
  115.         } else {
  116.             saveuid = myuid;
  117.         }
  118.     }
  119.     return saveuid;
  120. }
  121.  
  122.  
  123. void
  124. findgname(gname, gid)
  125.     char    gname[TGNMLEN];
  126.     int    gid;
  127. {
  128.     struct group    *gr;
  129.     extern struct group *getgrgid ();
  130.  
  131.     if (gid != savegid) {
  132.         savegid = gid;
  133.         savegname[0] = '\0';
  134.         (void)setgrent();
  135.         gr = getgrgid(gid); 
  136.         if (gr) 
  137.             strncpy(savegname, gr->gr_name, TGNMLEN);
  138.     }
  139.     (void) strncpy(gname, savegname, TGNMLEN);
  140. }
  141.  
  142.  
  143. int
  144. findgid(gname)
  145.     char    gname[TUNMLEN];
  146. {
  147.     struct group    *gr;
  148.     extern struct group *getgrnam();
  149.  
  150.     if (gname[0] != savegname[0]    /* Quick test w/o proc call */
  151.         || 0!=strncmp(gname, savegname, TUNMLEN)) {
  152.         strncpy(savegname, gname, TUNMLEN);
  153.         gr = getgrnam(gname); 
  154.         if (gr) {
  155.             savegid = gr->gr_gid;
  156.         } else {
  157.             savegid = mygid;
  158.         }
  159.     }
  160.     return savegid;
  161. }
  162. #endif
  163.