Link with C++ linker
[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 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 /* $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) || defined(USL) || defined(__uxp__)
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 || dtprintinfo_cat == (nl_catd) -1)
98          dtprintinfo_cat = NULL;
99 #ifdef hpux
100       else
101          msg_strings = (char **)calloc(LAST_MSG_NO, sizeof(char *));
102 #endif
103     }
104
105    if (!STRCMP(argv[1], "-help"))
106     {
107       char *output;
108       char *cmd = new char [strlen(LIST_QUEUES) + 30];
109       sprintf(cmd, "%s | awk '{print \"\\t\", $1}'", LIST_QUEUES);
110       Invoke *_thread = new Invoke(cmd, &output);
111       printf(MESSAGE(CommandLineHelpL), output);
112       printf("\n");
113       delete output;
114       delete [] cmd;
115       delete _thread;
116       return 0;
117     }
118
119    char *progname = strrchr(argv[0], '/');
120    if (progname)
121       progname++;
122    else
123       progname = argv[0];
124    if (!STRCMP(argv[1], "-populate"))
125     {
126       if (getuid() != 0)
127        {
128          fprintf(stderr, MESSAGE(RootUserL), progname, "-populate");
129          fprintf(stderr, "\n");
130          return 1;
131        }
132
133       PrintSubSystem *prt = new PrintSubSystem(NULL);
134       int n_queues = prt->NumChildren();
135       // Get Print Subsystem children, (these are queues)
136       Queue **queues = (Queue **)prt->Children();
137       int i;
138       for (i = 0; i < n_queues; i++)
139        {
140          DtPrinterIcon *icon = new DtPrinterIcon(NULL, NULL, queues[i],
141                                                  INITIALIZE_PRINTERS);
142          icon->CreateActionFile();
143          delete icon;
144        }
145       return 0;
146     }
147
148    DtApp *app = new DtApp(progname, &argc, argv);
149    app->Visible(true);
150    app->Run();
151
152    return 0;
153 }