1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/termio.h>
31*0Sstevel@tonic-gate #include <unistd.h>
32*0Sstevel@tonic-gate #include <stdlib.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <pwd.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <project.h>
38*0Sstevel@tonic-gate #include <locale.h>
39*0Sstevel@tonic-gate #include <libintl.h>
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate struct projlist {
42*0Sstevel@tonic-gate void *pl_next;
43*0Sstevel@tonic-gate char *pl_name;
44*0Sstevel@tonic-gate char *pl_comm;
45*0Sstevel@tonic-gate };
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate static struct projlist *projects;
48*0Sstevel@tonic-gate static char *progname;
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate static void *
safe_malloc(size_t size)51*0Sstevel@tonic-gate safe_malloc(size_t size)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate void *buf;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate if ((buf = malloc(size)) == NULL) {
56*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: not enough memory\n"),
57*0Sstevel@tonic-gate progname);
58*0Sstevel@tonic-gate exit(1);
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate return (buf);
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate static int
find_projects(char * name,int default_only)64*0Sstevel@tonic-gate find_projects(char *name, int default_only)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate struct projlist *tail, *prev;
67*0Sstevel@tonic-gate char *projname, *projcomm;
68*0Sstevel@tonic-gate struct project proj;
69*0Sstevel@tonic-gate void *buffer, *tmp;
70*0Sstevel@tonic-gate int found = 0;
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate tmp = safe_malloc(PROJECT_BUFSZ);
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate if (default_only) {
75*0Sstevel@tonic-gate if (getdefaultproj(name, &proj, tmp, PROJECT_BUFSZ) != NULL) {
76*0Sstevel@tonic-gate projects = safe_malloc(sizeof (struct projlist));
77*0Sstevel@tonic-gate projname = safe_malloc(strlen(proj.pj_name) + 1);
78*0Sstevel@tonic-gate projcomm = safe_malloc(strlen(proj.pj_comment) + 1);
79*0Sstevel@tonic-gate (void) strcpy(projname, proj.pj_name);
80*0Sstevel@tonic-gate (void) strcpy(projcomm, proj.pj_comment);
81*0Sstevel@tonic-gate projects->pl_next = NULL;
82*0Sstevel@tonic-gate projects->pl_name = projname;
83*0Sstevel@tonic-gate projects->pl_comm = projcomm;
84*0Sstevel@tonic-gate found = 1;
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate } else {
87*0Sstevel@tonic-gate buffer = safe_malloc(PROJECT_BUFSZ);
88*0Sstevel@tonic-gate setprojent();
89*0Sstevel@tonic-gate while (getprojent(&proj, tmp, PROJECT_BUFSZ) != NULL) {
90*0Sstevel@tonic-gate if (inproj(name, proj.pj_name, buffer, PROJECT_BUFSZ)) {
91*0Sstevel@tonic-gate tail = safe_malloc(sizeof (struct projlist));
92*0Sstevel@tonic-gate projname =
93*0Sstevel@tonic-gate safe_malloc(strlen(proj.pj_name) + 1);
94*0Sstevel@tonic-gate projcomm =
95*0Sstevel@tonic-gate safe_malloc(strlen(proj.pj_comment) + 1);
96*0Sstevel@tonic-gate (void) strcpy(projname, proj.pj_name);
97*0Sstevel@tonic-gate (void) strcpy(projcomm, proj.pj_comment);
98*0Sstevel@tonic-gate tail->pl_next = NULL;
99*0Sstevel@tonic-gate tail->pl_name = projname;
100*0Sstevel@tonic-gate tail->pl_comm = projcomm;
101*0Sstevel@tonic-gate if (!projects) {
102*0Sstevel@tonic-gate projects = tail;
103*0Sstevel@tonic-gate prev = projects;
104*0Sstevel@tonic-gate } else {
105*0Sstevel@tonic-gate prev->pl_next = tail;
106*0Sstevel@tonic-gate prev = tail;
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate found = 1;
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate endprojent();
112*0Sstevel@tonic-gate free(buffer);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate free(tmp);
115*0Sstevel@tonic-gate return (found);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate * Get the maximum length of the project name string.
120*0Sstevel@tonic-gate */
121*0Sstevel@tonic-gate static int
max_projname()122*0Sstevel@tonic-gate max_projname()
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate struct projlist *pl;
125*0Sstevel@tonic-gate int max = 0;
126*0Sstevel@tonic-gate int len;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate for (pl = projects; pl; pl = pl->pl_next)
129*0Sstevel@tonic-gate if ((len = strlen(pl->pl_name)) > max)
130*0Sstevel@tonic-gate max = len;
131*0Sstevel@tonic-gate return (max);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate static int
print_projects(char * name,int verbose,int default_only)135*0Sstevel@tonic-gate print_projects(char *name, int verbose, int default_only)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate struct projlist *pl, *next;
138*0Sstevel@tonic-gate struct winsize ws;
139*0Sstevel@tonic-gate int length = 0;
140*0Sstevel@tonic-gate int smart = isatty(STDOUT_FILENO);
141*0Sstevel@tonic-gate int columns;
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate if (!find_projects(name, default_only)) {
144*0Sstevel@tonic-gate if (default_only)
145*0Sstevel@tonic-gate (void) fprintf(stderr,
146*0Sstevel@tonic-gate gettext("%s: no default project for user %s\n"),
147*0Sstevel@tonic-gate progname, name);
148*0Sstevel@tonic-gate else
149*0Sstevel@tonic-gate (void) fprintf(stderr,
150*0Sstevel@tonic-gate gettext("%s: no projects for user %s\n"),
151*0Sstevel@tonic-gate progname, name);
152*0Sstevel@tonic-gate return (1);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate if (verbose)
156*0Sstevel@tonic-gate length = max_projname();
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate if (smart) {
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate * Get the number of columns.
161*0Sstevel@tonic-gate */
162*0Sstevel@tonic-gate if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
163*0Sstevel@tonic-gate ws.ws_col > 0)
164*0Sstevel@tonic-gate columns = ws.ws_col;
165*0Sstevel@tonic-gate else
166*0Sstevel@tonic-gate columns = 80;
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate for (pl = projects; pl; ) {
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate * Display information about projects.
172*0Sstevel@tonic-gate */
173*0Sstevel@tonic-gate if (verbose) {
174*0Sstevel@tonic-gate (void) printf("%1-*3$s %s\n",
175*0Sstevel@tonic-gate pl->pl_name, pl->pl_comm, length);
176*0Sstevel@tonic-gate } else {
177*0Sstevel@tonic-gate if (smart &&
178*0Sstevel@tonic-gate length + strlen(pl->pl_name) >= columns) {
179*0Sstevel@tonic-gate (void) printf("\n");
180*0Sstevel@tonic-gate length = 0;
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate (void) printf("%s ", pl->pl_name);
183*0Sstevel@tonic-gate length += strlen(pl->pl_name) + 1;
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * Free previously allocated buffers.
187*0Sstevel@tonic-gate */
188*0Sstevel@tonic-gate next = pl->pl_next;
189*0Sstevel@tonic-gate free(pl->pl_name);
190*0Sstevel@tonic-gate free(pl->pl_comm);
191*0Sstevel@tonic-gate free(pl);
192*0Sstevel@tonic-gate pl = next;
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate if (!verbose && length != 0)
195*0Sstevel@tonic-gate (void) printf("\n");
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate return (0);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate void
print_projent(struct project * projent)201*0Sstevel@tonic-gate print_projent(struct project *projent)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate char **next;
204*0Sstevel@tonic-gate char *nextc;
205*0Sstevel@tonic-gate char *nextsemi;
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", projent->pj_name);
208*0Sstevel@tonic-gate (void) fprintf(stdout, "\tprojid : %d\n", projent->pj_projid);
209*0Sstevel@tonic-gate (void) fprintf(stdout, "\tcomment: \"%s\"\n", projent->pj_comment);
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate (void) fprintf(stdout, "\tusers : ");
212*0Sstevel@tonic-gate next = projent->pj_users;
213*0Sstevel@tonic-gate if (*next == NULL) {
214*0Sstevel@tonic-gate (void) fprintf(stdout, "(none)\n");
215*0Sstevel@tonic-gate } else {
216*0Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", *next);
217*0Sstevel@tonic-gate for (next++; *next != NULL; next++) {
218*0Sstevel@tonic-gate (void) fprintf(stdout, "\t %s\n", *next);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate (void) fprintf(stdout, "\tgroups : ");
223*0Sstevel@tonic-gate next = projent->pj_groups;
224*0Sstevel@tonic-gate if (*next == NULL) {
225*0Sstevel@tonic-gate (void) fprintf(stdout, "(none)\n");
226*0Sstevel@tonic-gate } else {
227*0Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", *next);
228*0Sstevel@tonic-gate for (next++; *next != NULL; next++) {
229*0Sstevel@tonic-gate (void) fprintf(stdout, "\t %s\n", *next);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate (void) fprintf(stdout, "\tattribs: ");
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate nextc = projent->pj_attr;
236*0Sstevel@tonic-gate if (nextc == NULL) {
237*0Sstevel@tonic-gate (void) fprintf(stdout, "(none)\n");
238*0Sstevel@tonic-gate } else {
239*0Sstevel@tonic-gate /* print first attribute */
240*0Sstevel@tonic-gate nextsemi = strchr(nextc, ';');
241*0Sstevel@tonic-gate if (nextsemi)
242*0Sstevel@tonic-gate *nextsemi = '\0';
243*0Sstevel@tonic-gate (void) fprintf(stdout, "%s\n", nextc);
244*0Sstevel@tonic-gate
245*0Sstevel@tonic-gate while (nextsemi) {
246*0Sstevel@tonic-gate nextc = nextsemi + 1;
247*0Sstevel@tonic-gate nextsemi = strchr(nextc, ';');
248*0Sstevel@tonic-gate if (nextsemi)
249*0Sstevel@tonic-gate *nextsemi = '\0';
250*0Sstevel@tonic-gate (void) fprintf(stdout, "\t %s\n", nextc);
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate static int
print_projents(char ** projlist)256*0Sstevel@tonic-gate print_projents(char **projlist)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate struct project projent;
259*0Sstevel@tonic-gate char buf[PROJECT_BUFSZ];
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate if (*projlist == NULL) {
262*0Sstevel@tonic-gate setprojent();
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate while (getprojent(&projent, buf, sizeof (buf)) != NULL) {
265*0Sstevel@tonic-gate print_projent(&projent);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate endprojent();
268*0Sstevel@tonic-gate return (0);
269*0Sstevel@tonic-gate }
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate while (*projlist != NULL) {
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate if (getprojbyname(*projlist, &projent, buf, sizeof (buf))
274*0Sstevel@tonic-gate == NULL) {
275*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: project \"%s\" does "
276*0Sstevel@tonic-gate "not exist\n", progname, *projlist);
277*0Sstevel@tonic-gate exit(1);
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate print_projent(&projent);
280*0Sstevel@tonic-gate projlist++;
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate return (0);
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate int
main(int argc,char * argv[])287*0Sstevel@tonic-gate main(int argc, char *argv[])
288*0Sstevel@tonic-gate {
289*0Sstevel@tonic-gate struct passwd *pwd;
290*0Sstevel@tonic-gate char *name;
291*0Sstevel@tonic-gate int c;
292*0Sstevel@tonic-gate int verbose = 0;
293*0Sstevel@tonic-gate int default_only = 0;
294*0Sstevel@tonic-gate int listmode = 0;
295*0Sstevel@tonic-gate uid_t uid;
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
298*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
299*0Sstevel@tonic-gate progname = argv[0];
300*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "dvl")) != EOF) {
301*0Sstevel@tonic-gate switch (c) {
302*0Sstevel@tonic-gate case 'd':
303*0Sstevel@tonic-gate default_only = 1;
304*0Sstevel@tonic-gate break;
305*0Sstevel@tonic-gate case 'v':
306*0Sstevel@tonic-gate verbose = 1;
307*0Sstevel@tonic-gate break;
308*0Sstevel@tonic-gate case 'l':
309*0Sstevel@tonic-gate listmode = 1;
310*0Sstevel@tonic-gate break;
311*0Sstevel@tonic-gate default:
312*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(
313*0Sstevel@tonic-gate "Usage: %s [-dv] [user]\n"
314*0Sstevel@tonic-gate " %s -l [project [project...]]\n"),
315*0Sstevel@tonic-gate progname, progname);
316*0Sstevel@tonic-gate return (2);
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gate /* just list projects if -l is specified */
321*0Sstevel@tonic-gate if (listmode) {
322*0Sstevel@tonic-gate if (default_only || verbose) {
323*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(
324*0Sstevel@tonic-gate "%s: -l incompatible with -d and -v\n"),
325*0Sstevel@tonic-gate progname);
326*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(
327*0Sstevel@tonic-gate "Usage: %s [-dv] [user]\n"
328*0Sstevel@tonic-gate " %s -l [project [project...]]\n"),
329*0Sstevel@tonic-gate progname, progname);
330*0Sstevel@tonic-gate }
331*0Sstevel@tonic-gate exit(print_projents(argv + optind));
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate if (optind == argc) {
334*0Sstevel@tonic-gate uid = getuid();
335*0Sstevel@tonic-gate if ((pwd = getpwuid(uid)) == NULL) {
336*0Sstevel@tonic-gate (void) fprintf(stderr,
337*0Sstevel@tonic-gate gettext("%s: getpwuid failed (%s)\n"),
338*0Sstevel@tonic-gate progname, strerror(errno));
339*0Sstevel@tonic-gate return (1);
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate name = pwd->pw_name;
342*0Sstevel@tonic-gate } else {
343*0Sstevel@tonic-gate name = argv[optind];
344*0Sstevel@tonic-gate if (getpwnam(name) == NULL) {
345*0Sstevel@tonic-gate (void) fprintf(stderr,
346*0Sstevel@tonic-gate gettext("%s: user %s does not exist\n"),
347*0Sstevel@tonic-gate progname, name);
348*0Sstevel@tonic-gate return (1);
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate }
351*0Sstevel@tonic-gate return (print_projects(name, verbose, default_only));
352*0Sstevel@tonic-gate }
353