home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3298 / printucred.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  3.1 KB  |  133 lines

  1. /* History:
  2. 5/3/91 DJB modified to print something sensible under !HAVE_UCRED
  3. 5/1/91 DJB baseline public domain
  4. */
  5.  
  6. /*
  7.  
  8. char *printucred(uc,style,ruids,tags,uns) struct ucred *uc; int style;
  9. int ruids; int tags; int uns; prints uc in a variety of styles.
  10.  
  11. style is not yet officially defined. XXX this whole interface
  12.  
  13. If ruids is 0, the effective uid is printed, and the real uid is printed
  14. if different. If ruids is 1, only the effective uid is printed. If ruids
  15. is 2, only the real uid is printed.
  16.  
  17. If tags is 1, information is tagged with inline labels. If tags is 0, it
  18. is assumed that labels are placed at the top of columns.
  19.  
  20. If uns is 1, uids are printed as usernames if possible. If uns is 0,
  21. uids are always printed as uids.
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <strings.h>
  27. #include "structucred.h"
  28. #include "printucred.h"
  29. #include "username.h"
  30. #include "groupname.h"
  31. #include "confhaveucred.h"
  32. /* XXX: should provide for string group names! */
  33.  
  34. static char result[50 + NGROUPS * 8];
  35. /* XXX: assumes uids and gids are at most 8 (really 5) characters */
  36. /* XXX: assumes uids and gids are short */
  37. /* XXX: string representation? */
  38. /* XXX: under newer Suns, auid? audit? label? */
  39.  
  40. char *printucred(uc,style,ruids,tags,uns)
  41. struct ucred *uc;
  42. int style;
  43. int ruids;
  44. int tags;
  45. int uns;
  46. {
  47.  int i;
  48.  char *t;
  49.  unsigned gid;
  50.  unsigned rgid;
  51.  unsigned uid;
  52.  unsigned ruid;
  53.  unsigned puid;
  54.  
  55. #ifdef HAVE_UCRED
  56.  uid = uc->cr_uid;
  57.  ruid = uc->cr_ruid;
  58. #else
  59.  uid = uc->uid;
  60.  ruid = uc->uid;
  61. #endif
  62.  puid = (ruids == 2) ? ruid : uid;
  63.  if (uns)
  64.   {
  65.    char *pun;
  66.    uid2username(puid,&pun); /* XXX: care about return value? */
  67.    if (style == 1)
  68.      sprintf(result,"%s%8s",tags ? "uid " : "",pun);
  69.    else
  70.      sprintf(result,"%s%s",tags ? "uid " : "",pun);
  71.   }
  72.  else
  73.    if (style == 1)
  74.      sprintf(result,"%s%5u",tags ? "uid " : "",puid);
  75.    else
  76.      sprintf(result,"%s%u",tags ? "uid " : "",puid);
  77.  t = result + strlen(result);
  78.  if (!ruids && (uid != ruid))
  79.   {
  80.    if (uns)
  81.     {
  82.      char *pun;
  83.      uid2username(ruid,&pun); /* XXX: care about return value? */
  84.      if (style == 1)
  85.        sprintf(t,"/%8u",ruid);
  86.      else
  87.        sprintf(t,"/%u",ruid);
  88.     }
  89.    else
  90.      if (style == 1)
  91.        sprintf(t,"/%5u",ruid);
  92.      else
  93.        sprintf(t,"/%u",ruid);
  94.    t += strlen(t);
  95.   }
  96.  if (style == 1 && !ruids && (uid == ruid))
  97.   {
  98.    if (uns) { sprintf(t,"         "); t += 9; }
  99.    else { sprintf(t,"      "); t += 6; }
  100.   }
  101.  if (style == 2)
  102.   {
  103.    sprintf(t,"             ");
  104.    t = result + (tags ? 4 : 0) + (!ruids ? 6 : 0) + 5 + (uns ? 6 : 0);
  105.    *t = 0;
  106.   }
  107. #ifdef HAVE_UCRED /*XXX*/
  108.  if (style == 3)
  109.   {
  110.    gid = (unsigned short) uc->cr_gid;
  111.    rgid = (unsigned short) uc->cr_rgid;
  112.    sprintf(t," %s%u",tags ? "gid " : "",gid); t += strlen(t);
  113.    if (gid != rgid)
  114.     {
  115.      sprintf(t,"/%u",rgid); t += strlen(t);
  116.     }
  117.    for (i = 0;i < NGROUPS;++i)
  118.     {
  119.      register short g;
  120.      g = uc->cr_groups[i];
  121.      if (g == -1)
  122.        break; /* XXX: is this right? */
  123.      if (g != gid && g != rgid)
  124.        sprintf(t," %u",(unsigned) (unsigned short) g);
  125.      t += strlen(t);
  126.      if (!g)
  127.        break; /* XXX: is this right? */
  128.     }
  129.   }
  130. #endif
  131.  return result;
  132. }
  133.