dtcalc: change from obsoleted MAXFLOAT to FLT_MAX from std C
[oweals/cde.git] / cde / lib / DtHelp / Environ_c.c
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
48 char * _DtCliSrvGetDtUserSession()
49 {
50   char * envVar = getenv("DTUSERSESSION");
51   char * ret_envVar = NULL;
52
53   /* See if the environment variable exists */
54
55   if (envVar == NULL) {
56
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
59      * set.
60      */
61
62     char pipedata[BUFSIZ];
63     char logname_local[8];
64     char * logname = getenv("LOGNAME");
65
66     if (logname == NULL) {
67       strcpy(logname_local,"generic");
68       logname = logname_local;
69     }
70
71     /* determine DISPLAY and screen number */
72
73     {
74       char   screen[BUFSIZ];
75       char * display = NULL;
76       char * localDisplayVar = getenv("DISPLAY");
77
78       if (localDisplayVar == NULL) {
79
80         /* run uname to get the display name */
81
82         FILE *pp;
83         display = pipedata;
84
85         pp = popen("uname -n", "r");
86         if (NULL == pp) {
87           perror("uname -n");
88           return NULL;
89         }
90         *display = 0;
91         if(NULL == fgets(display, BUFSIZ, pp)) {
92            perror("fgets() failed to read");
93            return NULL; 
94         }
95         while (isspace(display[strlen(display)-1]))
96           display[strlen(display)-1] = 0;
97         pclose(pp);
98       }
99       else {
100         display = malloc(strlen(localDisplayVar) + 1);
101         strcpy(display, localDisplayVar);
102       }
103           
104
105       /* Now determine the screen number. Throw away .0 */
106
107       {
108         char * s = strchr(display,':');
109         if (s && strlen(s) < (size_t)BUFSIZ) {
110           strcpy(screen,s+1);
111           *s = 0;
112           if ((s = strchr(screen,'.')) && *(s+1) == '0')
113             *s = 0;
114         }
115         else {
116           strcpy(screen,"0");
117         }
118       }
119       envVar = malloc(strlen(logname) + strlen(display) + strlen(screen) + 3);
120       if (envVar)
121         sprintf (envVar, "%s-%s-%s", logname, display, screen);
122
123       return envVar;
124     }
125   }
126
127   ret_envVar = malloc(strlen(envVar) + 1);
128   if (ret_envVar)
129     strcpy(ret_envVar, envVar);
130   return ret_envVar;
131
132 }
133
134 #ifdef TEST
135 int main ()
136 {
137   char * value;
138
139   value = _DtCliSrvGetDtUserSession();
140
141   printf("DTUSERSESSION will be set to: %s\n", value);
142
143   free(value);
144
145   printf("value has been freed\n");
146 }
147
148 /*******************************************************
149   Test cases:  DTUSERSESSION   LOGNAME    DISPLAY
150   -------------------------------------------------
151                     set           -          -
152                    unset       userfoo     unset
153                    unset       userfoo   hostname:0
154                    unset       userfoo   hostname:0.0
155                    unset       userfoo   hostname:0.1
156 ********************************************************/
157 #endif