home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / PAX20.ZIP / NAMES.C < prev    next >
C/C++ Source or Header  |  1990-11-12  |  6KB  |  269 lines

  1. /* $Source: /u/mark/src/pax/RCS/names.c,v $
  2.  *
  3.  * $Revision: 2.0.0.3 $
  4.  *
  5.  * names.c - Look up user and/or group names.
  6.  *
  7.  * DESCRIPTION
  8.  *
  9.  *    These functions support UID and GID name lookup.  The results are
  10.  *    cached to improve performance.
  11.  *
  12.  * AUTHOR
  13.  *
  14.  *    Mark H. Colburn, Open Systems Architects, Inc. (mark@minnetech.mn.org)
  15.  *
  16.  * COPYRIGHT
  17.  *
  18.  *    Copyright (c) 1989 Mark H. Colburn.  All rights reserved.
  19.  *
  20.  *    Redistribution and use in source and binary forms are permitted
  21.  *    provided that the above copyright notice and this paragraph are
  22.  *    duplicated in all such forms and that any documentation,
  23.  *    advertising materials, and other materials related to such
  24.  *    distribution and use acknowledge that the software was developed
  25.  *    by Mark H. Colburn.
  26.  *
  27.  *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  28.  *    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  29.  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  30.  *
  31.  * $Log:    names.c,v $
  32.  * Revision 2.0.0.3  89/10/13  02:35:24  mark
  33.  * Beta Test Freeze
  34.  *
  35.  */
  36.  
  37. #ifndef lint
  38. static char        *ident = "$Id: names.c,v 2.0.0.3 89/10/13 02:35:24 mark Exp Locker: mark $";
  39. static char        *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  40. #endif /* ! lint */
  41.  
  42.  
  43. /* Headers */
  44.  
  45. #include "pax.h"
  46.  
  47.  
  48. /* Defines */
  49.  
  50. #define myuid    ( my_uid < 0? (my_uid = getuid()): my_uid )
  51. #define    mygid    ( my_gid < 0? (my_gid = getgid()): my_gid )
  52.  
  53.  
  54. /* Internal Identifiers */
  55.  
  56. #ifndef MSDOS
  57. static UIDTYPE      saveuid = -993;
  58. static char         saveuname[TUNMLEN];
  59. static UIDTYPE      my_uid = -993;
  60.  
  61. static GIDTYPE      savegid = -993;
  62. static char         savegname[TGNMLEN];
  63. static GIDTYPE      my_gid = -993;
  64. #else /* MSDOS */
  65. static int      saveuid = 0;
  66. static char     saveuname[TUNMLEN] = "";
  67. static int      my_uid = 0;
  68.  
  69. static int      savegid = 0;
  70. static char     savegname[TGNMLEN] = "";
  71. static int      my_gid = 0;
  72. #endif /* MSDOS */
  73.  
  74. /* finduname - find a user or group name from a uid or gid
  75.  *
  76.  * DESCRIPTION
  77.  *
  78.  *     Look up a user name from a uid/gid, maintaining a cache.
  79.  *
  80.  * PARAMETERS
  81.  *
  82.  *    char    uname[]        - name (to be returned to user)
  83.  *    int    uuid        - id of name to find
  84.  *
  85.  *
  86.  * RETURNS
  87.  *
  88.  *    Returns a name which is associated with the user id given.  If there
  89.  *    is not name which corresponds to the user-id given, then a pointer
  90.  *    to a string of zero length is returned.
  91.  *
  92.  * FIXME
  93.  *
  94.  *     1. for now it's a one-entry cache.
  95.  *    2. The "-993" is to reduce the chance of a hit on the first lookup.
  96.  */
  97.  
  98. #ifdef __STDC__
  99.  
  100. char *
  101. finduname(UIDTYPE uuid)
  102.  
  103. #else
  104.  
  105. char *
  106. finduname(uuid)
  107.     UIDTYPE             uuid;
  108.  
  109. #endif
  110. {
  111.     struct passwd      *pw;
  112.  
  113.     DBUG_ENTER("finduname");
  114.     if (uuid != saveuid) {
  115.     saveuid = uuid;
  116.     saveuname[0] = '\0';
  117.     pw = getpwuid(uuid);
  118.     if (pw) {
  119.         strncpy(saveuname, pw->pw_name, TUNMLEN);
  120.     }
  121.     }
  122.     DBUG_RETURN(saveuname);
  123. }
  124.  
  125.  
  126. /* finduid - get the uid for a given user name
  127.  *
  128.  * DESCRIPTION
  129.  *
  130.  *    This does just the opposit of finduname.  Given a user name it
  131.  *    finds the corresponding UID for that name.
  132.  *
  133.  * PARAMETERS
  134.  *
  135.  *    char    uname[]        - username to find a UID for
  136.  *
  137.  * RETURNS
  138.  *
  139.  *    The UID which corresponds to the uname given, if any.  If no UID
  140.  *    could be found, then the UID which corrsponds the user running the
  141.  *    program is returned.
  142.  *
  143.  */
  144.  
  145. #ifdef __STDC__
  146.  
  147. UIDTYPE
  148. finduid(char *uname)
  149.  
  150. #else
  151.  
  152. UIDTYPE
  153. finduid(uname)
  154.     char               *uname;
  155.  
  156. #endif
  157. {
  158.     struct passwd      *pw;
  159.     extern struct passwd *getpwnam();
  160.  
  161.     DBUG_ENTER("finduid");
  162.     if (uname[0] != saveuname[0]/* Quick test w/o proc call */
  163.     ||0 != strncmp(uname, saveuname, TUNMLEN)) {
  164.     strncpy(saveuname, uname, TUNMLEN);
  165.     pw = getpwnam(uname);
  166.     if (pw) {
  167.         saveuid = pw->pw_uid;
  168.     } else {
  169.         saveuid = myuid;
  170.     }
  171.     }
  172.     DBUG_RETURN(saveuid);
  173. }
  174.  
  175.  
  176. /* findgname - look up a group name from a gid
  177.  *
  178.  * DESCRIPTION
  179.  *
  180.  *     Look up a group name from a gid, maintaining a cache.
  181.  *
  182.  * PARAMETERS
  183.  *
  184.  *    int    ggid        - goupid of group to find
  185.  *
  186.  * RETURNS
  187.  *
  188.  *    A string which is associated with the group ID given.  If no name
  189.  *    can be found, a string of zero length is returned.
  190.  */
  191.  
  192. #ifdef __STDC__
  193.  
  194. char *
  195. findgname(GIDTYPE ggid)
  196.  
  197. #else
  198.  
  199. char *
  200. findgname(ggid)
  201.     GIDTYPE             ggid;
  202.  
  203. #endif
  204. {
  205.     struct group       *gr;
  206.  
  207.     DBUG_ENTER("findgname");
  208.     if (ggid != savegid) {
  209.     savegid = ggid;
  210.     savegname[0] = '\0';
  211.     setgrent();
  212.     gr = getgrgid(ggid);
  213.     if (gr) {
  214.         strncpy(savegname, gr->gr_name, TGNMLEN);
  215.     }
  216.     }
  217.     DBUG_RETURN(savegname);
  218. }
  219.  
  220.  
  221.  
  222. /* findgid - get the gid for a given group name
  223.  *
  224.  * DESCRIPTION
  225.  *
  226.  *    This does just the opposit of finduname.  Given a group name it
  227.  *    finds the corresponding GID for that name.
  228.  *
  229.  * PARAMETERS
  230.  *
  231.  *    char    uname[]        - groupname to find a GID for
  232.  *
  233.  * RETURNS
  234.  *
  235.  *    The GID which corresponds to the uname given, if any.  If no GID
  236.  *    could be found, then the GID which corrsponds the group running the
  237.  *    program is returned.
  238.  *
  239.  */
  240.  
  241. #ifdef __STDC__
  242.  
  243. GIDTYPE
  244. findgid(char *gname)
  245.  
  246. #else
  247.  
  248. GIDTYPE
  249. findgid(gname)
  250.     char               *gname;
  251.  
  252. #endif
  253. {
  254.     struct group       *gr;
  255.  
  256.     DBUG_ENTER("findgid");
  257.     /* Quick test w/o proc call */
  258.     if (gname[0] != savegname[0] || strncmp(gname, savegname, TUNMLEN) != 0) {
  259.     strncpy(savegname, gname, TUNMLEN);
  260.     gr = getgrnam(gname);
  261.     if (gr) {
  262.         savegid = gr->gr_gid;
  263.     } else {
  264.         savegid = mygid;
  265.     }
  266.     }
  267.     DBUG_RETURN(savegid);
  268. }
  269.