home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / src / names.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  155 lines

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