home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / inject / fullname.c < prev    next >
C/C++ Source or Header  |  1993-08-18  |  1KB  |  56 lines

  1. /*
  2.  * fullname - extract user's full name from a password entry
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <pwd.h>
  9.  
  10. /* imports */
  11. extern char *getenv(), *emalloc();
  12.  
  13. char *
  14. fullname(pwp, usggcos)
  15. register struct passwd *pwp;
  16. int usggcos;            /* strictly speaking, "BTL RJE format" */
  17. {
  18.     register char *name, *gcos, *delim;
  19.  
  20.     name = getenv("NAME");
  21.     if (name != NULL)
  22.         return name;
  23.  
  24.     if (pwp == NULL)
  25.         return "";        /* no such user */
  26.  
  27.     gcos = pwp->pw_gecos;
  28.     delim = strchr(gcos, ',');
  29.     if (delim != NULL)
  30.         *delim = '\0';        /* stop at first comma */
  31.     if (usggcos) {
  32.         delim = strchr(gcos, '-');
  33.         if (delim != NULL)
  34.             gcos = delim + 1;    /* skip past first dash */
  35.         delim = strchr(gcos, '(');
  36.         if (delim != NULL)
  37.             *delim = '\0';    /* stop at first paren */
  38.     }
  39.  
  40.     /* the horrible UCB & hack */
  41.     delim = strchr(gcos, '&');
  42.     if (delim != NULL) {
  43.         register char *cap = pwp->pw_name;
  44.         register char *newgcos;
  45.  
  46.         if (isascii(*cap) && islower(*cap))
  47.             *cap = toupper(*cap);
  48.         newgcos = emalloc(strlen(gcos) + strlen(cap) + 1);
  49.         (void) memcpy(newgcos, gcos, delim - gcos);
  50.         (void) strcpy(newgcos + (delim - gcos), cap);
  51.         (void) strcat(newgcos, delim + 1);
  52.         gcos = newgcos;
  53.     }
  54.     return gcos;
  55. }
  56.