home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / flistfrontend / src / uid2s.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-25  |  3.3 KB  |  167 lines

  1. #ifndef NO_IDENT
  2. static char *Id = "$Id: uid2s.c,v 1.2 1995/10/25 00:18:06 tom Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Title:    uid2s.c
  7.  * Author:    T.E.Dickey
  8.  * Created:    15 Dec 1988 (for TD_LIB)
  9.  * Modified:
  10.  *        23 Oct 1995, standalone module for use in FLIST distribution
  11.  *
  12.  * Function:    Given a unix-style uid, finds the corresponding username string
  13.  *        and returns a pointer to it.
  14.  *
  15.  *        The corresponding inverse translation is bundled with this
  16.  *        module on VMS since no password support is provided.
  17.  *
  18.  *        The corresponding functions for group-id are bundled here also
  19.  *        because the VMS system call which returns user-name does not
  20.  *        distinguish (except by use of "-1" for group) between these
  21.  *        and group-names.
  22.  */
  23.  
  24. #include    <stdlib.h>
  25. #include    <stdio.h>
  26. #include    <string.h>
  27.  
  28. #include    <starlet.h>
  29. #include    <ssdef.h>
  30. #include    <descrip.h>
  31.  
  32. #include    "sysutils.h"
  33.  
  34. #define    ID_MASK        0xffff
  35. #define    GID_SHIFT    16
  36.  
  37. typedef    struct    my_list {
  38.     struct    my_list    *next;
  39.     unsigned id;
  40.     char    *name;
  41.     } MY_LIST;
  42.  
  43. static    MY_LIST    *identifiers;
  44.  
  45. /*
  46.  * Lookup an identifier in the system rights database, and append it to the
  47.  * list we've cached in memory.
  48.  */
  49. static
  50. char *    lookup_id (unsigned id)
  51. {
  52.     static    unsigned contxt    = 0;
  53.  
  54.     char    *result = 0;
  55.     short    namlen    = 0;
  56.     unsigned resid    = 0;
  57.     unsigned attrib    = 0;
  58.     static    char    buffer[80];
  59.     $DESCRIPTOR(nambuf,buffer);
  60.  
  61.     MY_LIST *p;
  62.  
  63.     if (sys$idtoasc(
  64.             id,
  65.             &namlen,
  66.             &nambuf,
  67.             &resid,
  68.             &attrib,
  69.             &contxt) == SS$_NORMAL) {
  70.         result = strncpy(calloc(namlen+1,1), buffer, namlen);
  71.     }
  72.  
  73.     p = (MY_LIST *)malloc(sizeof(MY_LIST));
  74.     p->next = identifiers;
  75.     p->id   = id;
  76.     p->name = result;
  77.     identifiers = p;
  78. #if 0
  79.     if (st != SS$_NOSUCHID)
  80.         sys$finish_rdb(contxt);
  81. #endif
  82.     return (result);
  83. }
  84.  
  85. /*
  86.  * Translate a uid to a string, returning a pointer to text-constant.
  87.  * Note that on VAX/VMS we may want to lookup the ordered pair [gid,uid].
  88.  */
  89. static
  90. char *    uid_to_string(unsigned uid, unsigned gid)
  91. {
  92.     char    *result = 0;
  93.     MY_LIST *p;
  94.  
  95.     if (uid != ID_MASK) {
  96.         unsigned id0 = uid;
  97.         unsigned id1 = (uid | (gid << GID_SHIFT));
  98.         int    found_0 = 0;
  99.         int    found_1 = 0;
  100.         for (p = identifiers; p != 0; p = p->next) {
  101.             if (p->id == id0) {
  102.                 found_0 = 1;
  103.                 result = p->name;
  104.             }
  105.             if (p->id == id1) {
  106.                 found_1 = 1;
  107.                 result = p->name;
  108.             }
  109.             if (result != 0 || (found_0 && found_1))
  110.                 break;
  111.         }
  112.         if (result == 0 && !found_1)
  113.             result = lookup_id(id1);
  114.         if (result == 0 && !found_0)
  115.             result = lookup_id(id0);
  116.     }
  117.     return (result);
  118. }
  119.  
  120. /*
  121.  * Translate a gid to a string, returning a pointer to text-constant
  122.  */
  123. static
  124. char *    gid_to_string(unsigned gid)
  125. {
  126.     char    *result = 0;
  127.     MY_LIST *p;
  128.  
  129.     if (gid != ID_MASK) {
  130.         unsigned id = (gid << GID_SHIFT) | ID_MASK;
  131.         int found = 0;
  132.         for (p = identifiers; p != 0; p = p->next) {
  133.             if (id == p->id) {
  134.                 found = 1;
  135.                 result = p->name;
  136.                 break;
  137.             }
  138.         }
  139.         if (result == 0 && !found)
  140.             result = lookup_id(id);
  141.     }
  142.     return (result);
  143. }
  144.  
  145. /*
  146.  * Special VMS-only entrypoint to translate id to string
  147.  */
  148. char *    vms_uid2s(char *result, unsigned member, unsigned group)
  149. {
  150.     int    bracket = ((group & 0100000) == 0);
  151.     char    *s;
  152.  
  153.     *result = '\0';
  154.     if (bracket) {
  155.         strcat(result, "[");
  156.         if ((s = gid_to_string(group)) != 0) {
  157.             strcat(result, s);
  158.             strcat(result, ",");
  159.         }
  160.     }
  161.     if ((s = uid_to_string(member, group)) != 0)
  162.         strcat(result, s);
  163.     if (bracket)
  164.         strcat(result, "]");
  165.     return (result);
  166. }
  167.