home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / star-1.3.1.tar.gz / star-1.3.1.tar / star-1.3.1 / star / names.c < prev    next >
C/C++ Source or Header  |  1998-07-05  |  5KB  |  220 lines

  1. /* @(#)names.c    1.7 98/07/05 Copyright 1993 J. Schilling */
  2. #ifndef lint
  3. static    char sccsid[] =
  4.     "@(#)names.c    1.7 98/07/05 Copyright 1993 J. Schilling";
  5. #endif
  6. /*
  7.  *    Handle user/group names for archive header
  8.  *
  9.  *    Copyright (c) 1993 J. Schilling
  10.  */
  11. /*
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2, or (at your option)
  15.  * any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; see the file COPYING.  If not, write to
  24.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  */
  26.  
  27. #include <mconfig.h>
  28. #include <stdio.h>
  29. #include <standard.h>
  30. #include "star.h"
  31. #include <pwd.h>
  32. #include <grp.h>
  33. #include <strdefs.h>
  34. #include "starsubs.h"
  35.  
  36. #define    C_SIZE    16
  37.  
  38. typedef struct id {
  39.     Ulong    id;
  40.     char    name[TUNMLEN];        /* TUNMLEN == TGNMLEN        */
  41.     char    valid;
  42. } idc_t;
  43.  
  44. LOCAL    idc_t    uidcache[C_SIZE];
  45. LOCAL    int    lastuidx;        /* Last index for new entry */
  46.  
  47. LOCAL    idc_t    gidcache[C_SIZE];
  48. LOCAL    int    lastgidx;        /* Last index for new entry */
  49.  
  50. EXPORT    BOOL    nameuid    __PR((char* name, int namelen, Ulong uid));
  51. EXPORT    BOOL    uidname    __PR((char* name, int namelen, Ulong* uidp));
  52. EXPORT    BOOL    namegid    __PR((char* name, int namelen, Ulong gid));
  53. EXPORT    BOOL     gidname    __PR((char* name, int namelen, Ulong* gidp));
  54.  
  55. /*
  56.  * Get name from uid
  57.  */
  58. EXPORT BOOL
  59. nameuid(name, namelen, uid)
  60.     char    *name;
  61.     int    namelen;
  62.     Ulong    uid;
  63. {
  64.     struct passwd    *pw;
  65.     register int    i;
  66.     register idc_t    *idp;
  67.  
  68.     for (i=0, idp = uidcache; i < C_SIZE; i++, idp++) {
  69.         if (idp->valid == 0)        /* Entry not yet filled */
  70.             break;
  71.         if (idp->id == uid)
  72.             goto out;
  73.     }
  74.     idp = &uidcache[lastuidx++];        /* Round robin fill next ent */
  75.     if (lastuidx >= C_SIZE)
  76.         lastuidx = 0;
  77.  
  78.     idp->id = uid;
  79.     idp->name[0] = '\0';
  80.     idp->valid = 1;
  81.     if ((pw = getpwuid(uid)) != NULL) {
  82.         strncpy(idp->name, pw->pw_name, TUNMLEN);
  83.         idp->name[namelen-1] = 0;
  84.     }
  85. out:
  86.     strcpy(name, idp->name);
  87.     return (name[0] != '\0');
  88. }
  89.  
  90. /*
  91.  * Get uid from name
  92.  */
  93. EXPORT BOOL
  94. uidname(name, namelen, uidp)
  95.     char    *name;
  96.     int    namelen;
  97.     Ulong    *uidp;
  98. {
  99.     struct passwd    *pw;
  100.     register int    len = namelen>TUNMLEN?TUNMLEN:namelen;
  101.     register int    i;
  102.     register idc_t    *idp;
  103.  
  104.     if (name[0] == '\0')
  105.         return (FALSE);
  106.  
  107.     for (i=0, idp = uidcache; i < C_SIZE; i++, idp++) {
  108.         if (idp->valid == 0)        /* Entry not yet filled */
  109.             break;
  110.         if (name[0] == idp->name[0] && 
  111.                     strncmp(name, idp->name, len) == 0) {
  112.             *uidp = idp->id;
  113.             if (idp->valid == 2)    /* Name not found */
  114.                 return (FALSE);
  115.             return (TRUE);
  116.         }
  117.     }
  118.     idp = &uidcache[lastuidx++];        /* Round robin fill next ent */
  119.     if (lastuidx >= C_SIZE)
  120.         lastuidx = 0;
  121.  
  122.     idp->id = 0;
  123.     idp->name[0] = '\0';
  124.     strncpy(idp->name, name, len);
  125.     idp->name[len] = '\0';
  126.     idp->valid = 1;
  127.     if ((pw = getpwnam(idp->name)) != NULL) {
  128.         idp->id = pw->pw_uid;
  129.         *uidp = idp->id;
  130.         return (TRUE);
  131.     } else {
  132.         idp->valid = 2;            /* Mark name as not found */
  133.         *uidp = 0;            /* XXX ??? */
  134.         return (FALSE);
  135.     }
  136. }
  137.  
  138. /*
  139.  * Get name from gid
  140.  */
  141. EXPORT BOOL
  142. namegid(name, namelen, gid)
  143.     char    *name;
  144.     int    namelen;
  145.     Ulong    gid;
  146. {
  147.     struct group    *gr;
  148.     register int    i;
  149.     register idc_t    *idp;
  150.  
  151.     for (i=0, idp = gidcache; i < C_SIZE; i++, idp++) {
  152.         if (idp->valid == 0)        /* Entry not yet filled */
  153.             break;
  154.         if (idp->id == gid)
  155.             goto out;
  156.     }
  157.     idp = &gidcache[lastgidx++];        /* Round robin fill next ent */
  158.     if (lastgidx >= C_SIZE)
  159.         lastgidx = 0;
  160.  
  161.     idp->id = gid;
  162.     idp->name[0] = '\0';
  163.     idp->valid = 1;
  164.     if ((gr = getgrgid(gid)) != NULL) {
  165.         strncpy(idp->name, gr->gr_name, TUNMLEN);
  166.         idp->name[namelen-1] = 0;
  167.     }
  168. out:
  169.     strcpy(name, idp->name);
  170.     return (name[0] != '\0');
  171. }
  172.  
  173. /*
  174.  * Get gid from name
  175.  */
  176. EXPORT BOOL
  177. gidname(name, namelen, gidp)
  178.     char    *name;
  179.     int    namelen;
  180.     Ulong    *gidp;
  181. {
  182.     struct group    *gr;
  183.     register int    len = namelen>TGNMLEN?TGNMLEN:namelen;
  184.     register int    i;
  185.     register idc_t    *idp;
  186.  
  187.     if (name[0] == '\0')
  188.         return (FALSE);
  189.  
  190.     for (i=0, idp = gidcache; i < C_SIZE; i++, idp++) {
  191.         if (idp->valid == 0)        /* Entry not yet filled */
  192.             break;
  193.         if (name[0] == idp->name[0] && 
  194.                     strncmp(name, idp->name, len) == 0) {
  195.             *gidp = idp->id;
  196.             if (idp->valid == 2)    /* Name not found */
  197.                 return (FALSE);
  198.             return (TRUE);
  199.         }
  200.     }
  201.     idp = &gidcache[lastgidx++];        /* Round robin fill next ent */
  202.     if (lastgidx >= C_SIZE)
  203.         lastgidx = 0;
  204.  
  205.     idp->id = 0;
  206.     idp->name[0] = '\0';
  207.     strncpy(idp->name, name, len);
  208.     idp->name[len] = '\0';
  209.     idp->valid = 1;
  210.     if ((gr = getgrnam(idp->name)) != NULL) {
  211.         idp->id = gr->gr_gid;
  212.         *gidp = idp->id;
  213.         return (TRUE);
  214.     } else {
  215.         idp->valid = 2;            /* Mark name as not found */
  216.         *gidp = 0;            /* XXX ??? */
  217.         return (FALSE);
  218.     }
  219. }
  220.