Environ_c.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 librararies 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()
  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. if (localDisplayVar == NULL) {
  70. /* run uname to get the display name */
  71. FILE *pp;
  72. display = pipedata;
  73. pp = popen("uname -n", "r");
  74. if (NULL == pp) {
  75. perror("uname -n");
  76. return NULL;
  77. }
  78. *display = 0;
  79. fgets(display, BUFSIZ, pp);
  80. while (isspace(display[strlen(display)-1]))
  81. display[strlen(display)-1] = 0;
  82. pclose(pp);
  83. }
  84. else {
  85. display = malloc(strlen(localDisplayVar) + 1);
  86. strcpy(display, localDisplayVar);
  87. }
  88. /* Now determine the screen number. Throw away .0 */
  89. {
  90. char * s = strchr(display,':');
  91. if (s && strlen(s) < (size_t)BUFSIZ) {
  92. strcpy(screen,s+1);
  93. *s = 0;
  94. if ((s = strchr(screen,'.')) && *(s+1) == '0')
  95. *s = 0;
  96. }
  97. else {
  98. strcpy(screen,"0");
  99. }
  100. }
  101. envVar = malloc(strlen(logname) + strlen(display) + strlen(screen) + 3);
  102. if (envVar)
  103. sprintf (envVar, "%s-%s-%s", logname, display, screen);
  104. return envVar;
  105. }
  106. }
  107. ret_envVar = malloc(strlen(envVar) + 1);
  108. if (ret_envVar)
  109. strcpy(ret_envVar, envVar);
  110. return ret_envVar;
  111. }
  112. #ifdef TEST
  113. int main ()
  114. {
  115. char * value;
  116. value = _DtCliSrvGetDtUserSession();
  117. printf("DTUSERSESSION will be set to: %s\n", value);
  118. free(value);
  119. printf("value has been freed\n");
  120. }
  121. /*******************************************************
  122. Test cases: DTUSERSESSION LOGNAME DISPLAY
  123. -------------------------------------------------
  124. set - -
  125. unset userfoo unset
  126. unset userfoo hostname:0
  127. unset userfoo hostname:0.0
  128. unset userfoo hostname:0.1
  129. ********************************************************/
  130. #endif