2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /****************************<+>*************************************
24 ********************************************************************
28 ** $XConsortium: Environ_c.c /main/6 1995/12/18 16:31:33 cde-hp $
30 ** Project: libCliSrv Library
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.
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.
41 ********************************************************************
42 ****************************<+>*************************************/
48 char * _DtCliSrvGetDtUserSession()
50 char * envVar = getenv("DTUSERSESSION");
51 char * ret_envVar = NULL;
53 /* See if the environment variable exists */
57 /* It doesn't, so it needs to be constructed. Use LOGNAME, which
58 * always seems to be set, and DISPLAY, which may or may not be
62 char pipedata[BUFSIZ];
63 char logname_local[8];
64 char * logname = getenv("LOGNAME");
66 if (logname == NULL) {
67 strcpy(logname_local,"generic");
68 logname = logname_local;
71 /* determine DISPLAY and screen number */
75 char * display = NULL;
76 char * localDisplayVar = getenv("DISPLAY");
78 if (localDisplayVar == NULL) {
80 /* run uname to get the display name */
85 pp = popen("uname -n", "r");
91 if(NULL == fgets(display, BUFSIZ, pp)) {
92 perror("fgets() failed to read");
95 while (isspace(display[strlen(display)-1]))
96 display[strlen(display)-1] = 0;
100 display = malloc(strlen(localDisplayVar) + 1);
101 strcpy(display, localDisplayVar);
105 /* Now determine the screen number. Throw away .0 */
108 char * s = strchr(display,':');
109 if (s && strlen(s) < (size_t)BUFSIZ) {
112 if ((s = strchr(screen,'.')) && *(s+1) == '0')
119 envVar = malloc(strlen(logname) + strlen(display) + strlen(screen) + 3);
121 sprintf (envVar, "%s-%s-%s", logname, display, screen);
127 ret_envVar = malloc(strlen(envVar) + 1);
129 strcpy(ret_envVar, envVar);
139 value = _DtCliSrvGetDtUserSession();
141 printf("DTUSERSESSION will be set to: %s\n", value);
145 printf("value has been freed\n");
148 /*******************************************************
149 Test cases: DTUSERSESSION LOGNAME DISPLAY
150 -------------------------------------------------
153 unset userfoo hostname:0
154 unset userfoo hostname:0.0
155 unset userfoo hostname:0.1
156 ********************************************************/