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

  1. /* History:
  2. 5/1/91 DJB baseline public domain. todo: cache groups.
  3. */
  4.  
  5. /*
  6.  
  7. int gid2groupname(gid,gnp) int gid; char **gnp; looks up group gid and
  8. points *gnp to the name for that group (possibly stored in a static area
  9. interfering with getgrgid()). If gid does not have a system name,
  10. gid2groupname() uses gid in decimal as the name and returns 1. Normally
  11. it returns 0.
  12.  
  13. int groupname2gid(gn,gid); char *gn; int *gid; looks up group name gn
  14. and sets *gid to the number of that group. If gn is entirely numeric it
  15. simply sets *gid to that number are returns 1. Otherwise it returns 0.
  16. If gn is not numeric and not a system group name, groupname2gid returns
  17. -1 and leaves *gid alone.
  18.  
  19. */
  20.  
  21. #include <grp.h>
  22. #include "groupname.h"
  23. #include "numeric.h"
  24.  
  25. int gid2groupname(gid,gnp)
  26. int gid;
  27. char **gnp;
  28. {
  29.  struct group *gr;
  30.  static char gn[20];
  31.  
  32.  if (gr = getgrgid(gid))
  33.   {
  34.    *gnp = gr->gr_name;
  35.    return 0;
  36.   }
  37.  sprintf(gn,"%d",gid);
  38.  *gnp = gn;
  39.  return 1;
  40. }
  41.  
  42. int groupname2gid(gn,gid)
  43. char *gn;
  44. int *gid;
  45. {
  46.  struct group *gr;
  47.  
  48.  if (numeric(gn))
  49.   {
  50.    *gid = atoi(gn);
  51.    return 1;
  52.   }
  53.  gr = getgrnam(gn);
  54.  if (!gr)
  55.    return -1; /*XXX*/
  56.  *gid = gr->gr_gid;
  57.  return 0;
  58. }
  59.