whoami.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * \file whoami.c
  3. *
  4. * \brief GIS Library - Login name functions.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #ifndef __MINGW32__
  18. #include <pwd.h>
  19. #endif
  20. #include <grass/gis.h>
  21. /**
  22. * \brief Gets user's name.
  23. *
  24. * Returns a pointer to a string containing the user's login name.
  25. *
  26. * Tries getlogin() first, then goes to the password file.
  27. * However, some masscomp getlogin() fails in ucb universe
  28. * because the ttyname(0) rotuine fails in ucb universe.
  29. * So we check for this, too.
  30. *
  31. * \retval char * Pointer to string
  32. */
  33. char *G_whoami(void)
  34. {
  35. #ifdef __MINGW32__
  36. char *name = getenv("USERNAME");
  37. if (name == NULL) {
  38. name = "user_name";
  39. }
  40. #else
  41. static char *name = NULL;
  42. #ifdef COMMENTED_OUT
  43. char *getlogin();
  44. char *ttyname();
  45. if (name == NULL) {
  46. char *x;
  47. x = ttyname(0);
  48. if (x && *x) {
  49. x = getlogin();
  50. if (x && *x)
  51. name = G_store(x);
  52. }
  53. }
  54. #endif /* COMMENTED_OUT */
  55. if (name == NULL) {
  56. struct passwd *p;
  57. if ((p = getpwuid(getuid())))
  58. name = G_store(p->pw_name);
  59. }
  60. if (name == NULL)
  61. name = G_store("?");
  62. #endif
  63. return name;
  64. }