Environ_c.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /****************************<+>*************************************
  24. ********************************************************************
  25. **
  26. ** File: Environ_c.c
  27. **
  28. ** $XConsortium: Environ_c.c /main/6 1995/12/18 16:31:33 cde-hp $
  29. **
  30. ** Project: libCliSrv Library
  31. **
  32. ** Description: Return the value of the DTUSERSESSION environ-
  33. ** ment variable or create it if it doesn't exist.
  34. ** If malloc fails, NULL is returned.
  35. **
  36. **(c) Copyright 1992,1993,1994 by Hewlett-Packard Company
  37. **(c) Copyright 1993,1994 International Business Machines Corp.
  38. **(c) Copyright 1993,1994 Sun Microsystems, Inc.
  39. **(c) Copyright 1993,1994 Unix System Labs, Inc., a subsidiary of Novell, Inc.
  40. **
  41. ********************************************************************
  42. ****************************<+>*************************************/
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. char * _DtCliSrvGetDtUserSession(void)
  48. {
  49. char * envVar = getenv("DTUSERSESSION");
  50. char * ret_envVar = NULL;
  51. /* See if the environment variable exists */
  52. if (envVar == NULL) {
  53. /* It doesn't, so it needs to be constructed. Use LOGNAME, which
  54. * always seems to be set, and DISPLAY, which may or may not be
  55. * set.
  56. */
  57. char pipedata[BUFSIZ];
  58. char logname_local[8];
  59. char * logname = getenv("LOGNAME");
  60. if (logname == NULL) {
  61. strcpy(logname_local,"generic");
  62. logname = logname_local;
  63. }
  64. /* determine DISPLAY and screen number */
  65. {
  66. char screen[BUFSIZ];
  67. char * display = NULL;
  68. char * localDisplayVar = getenv("DISPLAY");
  69. int needsfree = 0;
  70. if (localDisplayVar == NULL) {
  71. /* run uname to get the display name */
  72. FILE *pp;
  73. display = pipedata;
  74. pp = popen("uname -n", "r");
  75. if (NULL == pp) {
  76. perror("uname -n");
  77. return NULL;
  78. }
  79. *display = 0;
  80. if(NULL == fgets(display, BUFSIZ, pp)) {
  81. perror("fgets() failed to read");
  82. return NULL;
  83. }
  84. while (isspace(display[strlen(display)-1]))
  85. display[strlen(display)-1] = 0;
  86. pclose(pp);
  87. }
  88. else {
  89. display = malloc(strlen(localDisplayVar) + 1);
  90. needsfree = 1;
  91. strcpy(display, localDisplayVar);
  92. }
  93. /* Now determine the screen number. Throw away .0 */
  94. {
  95. char * s = strchr(display,':');
  96. if (s && strlen(s) < (size_t)BUFSIZ) {
  97. snprintf(screen, sizeof(screen), "%s", s + 1);
  98. *s = 0;
  99. if ((s = strchr(screen,'.')) && *(s+1) == '0')
  100. *s = 0;
  101. }
  102. else {
  103. strcpy(screen,"0");
  104. }
  105. }
  106. envVar = malloc(strlen(logname) + strlen(display) + strlen(screen) + 3);
  107. if (envVar)
  108. sprintf (envVar, "%s-%s-%s", logname, display, screen);
  109. if(needsfree) {
  110. free(display);
  111. }
  112. return envVar;
  113. }
  114. }
  115. ret_envVar = malloc(strlen(envVar) + 1);
  116. if (ret_envVar)
  117. strcpy(ret_envVar, envVar);
  118. return ret_envVar;
  119. }
  120. #ifdef TEST
  121. int main ()
  122. {
  123. char * value;
  124. value = _DtCliSrvGetDtUserSession();
  125. printf("DTUSERSESSION will be set to: %s\n", value);
  126. free(value);
  127. printf("value has been freed\n");
  128. }
  129. /*******************************************************
  130. Test cases: DTUSERSESSION LOGNAME DISPLAY
  131. -------------------------------------------------
  132. set - -
  133. unset userfoo unset
  134. unset userfoo hostname:0
  135. unset userfoo hostname:0.0
  136. unset userfoo hostname:0.1
  137. ********************************************************/
  138. #endif