dtcm: Coverity 88675
[oweals/cde.git] / cde / programs / dtprintinfo / DtPrintinfo.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 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 /* $TOG: DtPrintinfo.C /main/6 1998/07/24 16:10:28 mgreess $ */
24 // User Interface headers
25 #include "DtApp.h"
26 #include "DtPrinterIcon.h"
27 #include "Invoke.h"
28
29 #ifndef NO_CDE
30 extern "C" {
31   #include <Dt/EnvControlP.h>
32 }
33 #endif
34
35
36 // Object headers
37 #include "PrintSubSys.h"
38
39 // Message header
40 #include "dtprintinfomsg.h"
41 nl_catd dtprintinfo_cat = NULL;
42
43 #include <stdlib.h> // This is for the getenv function
44 #include <unistd.h> // This is for the getuid function
45 #include <string.h> 
46 #include <errno.h> 
47
48 #if defined(aix)
49 extern "C" { extern int seteuid(uid_t); }
50 #endif
51
52 #ifdef hpux
53 static char **msg_strings = NULL;
54
55 // Cannot use multiple catgets paramemter calls in functions because the 
56 // previous catgets returned value is overwritten by later catgets calls
57 // Example: this would fail on HP systems
58 //    sprintf(buf, "%s %s" catgets(...), catgets(...))
59
60 char *Catgets(nl_catd catd, int set_num, int msg_num, char *s)
61 {
62    if (!msg_strings)
63       return s;
64
65    if (!msg_strings[msg_num])
66       msg_strings[msg_num] = strdup(catgets(catd, set_num, msg_num, s));
67    return msg_strings[msg_num];
68 }
69 #endif
70
71 int main(int argc, char **argv)
72 {
73 #ifndef NO_CDE
74    _DtEnvControl(DT_ENV_SET);
75 #endif
76
77 // run as user's UID
78 #ifdef hpux
79    setresuid(getuid(), getuid(), (uid_t)0);
80 #else
81    seteuid(getuid());
82 #endif
83
84    setlocale(LC_ALL, "");
85
86    char *lang = getenv("LANG");
87    if (lang && strcmp(lang, "C"))
88     {
89       errno = 0;
90
91 #ifdef NL_CAT_LOCALE
92       dtprintinfo_cat = catopen("dtprintinfo", NL_CAT_LOCALE);
93 #else
94       dtprintinfo_cat = catopen("dtprintinfo", 0);
95 #endif
96
97       if ((nl_catd) errno)
98          dtprintinfo_cat = (nl_catd) -1;
99 #ifdef hpux
100       else
101          msg_strings = (char **)calloc(LAST_MSG_NO, sizeof(char *));
102 #endif
103     }
104
105    if (dtprintinfo_cat == NULL) {
106       dtprintinfo_cat = (nl_catd) -1;
107    }
108
109    if (!STRCMP(argv[1], "-help"))
110     {
111       char *output;
112       char *cmd = new char [strlen(LIST_QUEUES) + 30];
113       sprintf(cmd, "%s | awk '{print \"\\t\", $1}'", LIST_QUEUES);
114       Invoke *_thread = new Invoke(cmd, &output);
115       printf(MESSAGE(CommandLineHelpL), output);
116       printf("\n");
117       delete output;
118       delete [] cmd;
119       delete _thread;
120       return 0;
121     }
122
123    char *progname = strrchr(argv[0], '/');
124    if (progname)
125       progname++;
126    else
127       progname = argv[0];
128    if (!STRCMP(argv[1], "-populate"))
129     {
130       if (getuid() != 0)
131        {
132          fprintf(stderr, MESSAGE(RootUserL), progname, "-populate");
133          fprintf(stderr, "\n");
134          return 1;
135        }
136
137       PrintSubSystem *prt = new PrintSubSystem(NULL);
138       int n_queues = prt->NumChildren();
139       // Get Print Subsystem children, (these are queues)
140       Queue **queues = (Queue **)prt->Children();
141       int i;
142       for (i = 0; i < n_queues; i++)
143        {
144          DtPrinterIcon *icon = new DtPrinterIcon(NULL, NULL, queues[i],
145                                                  INITIALIZE_PRINTERS);
146          icon->CreateActionFile();
147          delete icon;
148        }
149       return 0;
150     }
151
152    DtApp *app = new DtApp(progname, &argc, argv);
153    app->Visible(true);
154    app->Run();
155
156    return 0;
157 }