4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <sys/termio.h>
47 static struct projlist
*projects
;
48 static char *progname
;
51 safe_malloc(size_t size
)
55 if ((buf
= malloc(size
)) == NULL
) {
56 (void) fprintf(stderr
, gettext("%s: not enough memory\n"),
64 find_projects(char *name
, int default_only
)
66 struct projlist
*tail
, *prev
;
67 char *projname
, *projcomm
;
72 tmp
= safe_malloc(PROJECT_BUFSZ
);
75 if (getdefaultproj(name
, &proj
, tmp
, PROJECT_BUFSZ
) != NULL
) {
76 projects
= safe_malloc(sizeof (struct projlist
));
77 projname
= safe_malloc(strlen(proj
.pj_name
) + 1);
78 projcomm
= safe_malloc(strlen(proj
.pj_comment
) + 1);
79 (void) strcpy(projname
, proj
.pj_name
);
80 (void) strcpy(projcomm
, proj
.pj_comment
);
81 projects
->pl_next
= NULL
;
82 projects
->pl_name
= projname
;
83 projects
->pl_comm
= projcomm
;
87 buffer
= safe_malloc(PROJECT_BUFSZ
);
89 while (getprojent(&proj
, tmp
, PROJECT_BUFSZ
) != NULL
) {
90 if (inproj(name
, proj
.pj_name
, buffer
, PROJECT_BUFSZ
)) {
91 tail
= safe_malloc(sizeof (struct projlist
));
93 safe_malloc(strlen(proj
.pj_name
) + 1);
95 safe_malloc(strlen(proj
.pj_comment
) + 1);
96 (void) strcpy(projname
, proj
.pj_name
);
97 (void) strcpy(projcomm
, proj
.pj_comment
);
99 tail
->pl_name
= projname
;
100 tail
->pl_comm
= projcomm
;
105 prev
->pl_next
= tail
;
119 * Get the maximum length of the project name string.
128 for (pl
= projects
; pl
; pl
= pl
->pl_next
)
129 if ((len
= strlen(pl
->pl_name
)) > max
)
135 print_projects(char *name
, int verbose
, int default_only
)
137 struct projlist
*pl
, *next
;
140 int smart
= isatty(STDOUT_FILENO
);
143 if (!find_projects(name
, default_only
)) {
145 (void) fprintf(stderr
,
146 gettext("%s: no default project for user %s\n"),
149 (void) fprintf(stderr
,
150 gettext("%s: no projects for user %s\n"),
156 length
= max_projname();
160 * Get the number of columns.
162 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
169 for (pl
= projects
; pl
; ) {
171 * Display information about projects.
174 (void) printf("%1-*3$s %s\n",
175 pl
->pl_name
, pl
->pl_comm
, length
);
178 length
+ strlen(pl
->pl_name
) >= columns
) {
182 (void) printf("%s ", pl
->pl_name
);
183 length
+= strlen(pl
->pl_name
) + 1;
186 * Free previously allocated buffers.
194 if (!verbose
&& length
!= 0)
201 print_projent(struct project
*projent
)
207 (void) fprintf(stdout
, "%s\n", projent
->pj_name
);
208 (void) fprintf(stdout
, "\tprojid : %d\n", projent
->pj_projid
);
209 (void) fprintf(stdout
, "\tcomment: \"%s\"\n", projent
->pj_comment
);
211 (void) fprintf(stdout
, "\tusers : ");
212 next
= projent
->pj_users
;
214 (void) fprintf(stdout
, "(none)\n");
216 (void) fprintf(stdout
, "%s\n", *next
);
217 for (next
++; *next
!= NULL
; next
++) {
218 (void) fprintf(stdout
, "\t %s\n", *next
);
222 (void) fprintf(stdout
, "\tgroups : ");
223 next
= projent
->pj_groups
;
225 (void) fprintf(stdout
, "(none)\n");
227 (void) fprintf(stdout
, "%s\n", *next
);
228 for (next
++; *next
!= NULL
; next
++) {
229 (void) fprintf(stdout
, "\t %s\n", *next
);
233 (void) fprintf(stdout
, "\tattribs: ");
235 nextc
= projent
->pj_attr
;
237 (void) fprintf(stdout
, "(none)\n");
239 /* print first attribute */
240 nextsemi
= strchr(nextc
, ';');
243 (void) fprintf(stdout
, "%s\n", nextc
);
246 nextc
= nextsemi
+ 1;
247 nextsemi
= strchr(nextc
, ';');
250 (void) fprintf(stdout
, "\t %s\n", nextc
);
256 print_projents(char **projlist
)
258 struct project projent
;
259 char buf
[PROJECT_BUFSZ
];
261 if (*projlist
== NULL
) {
264 while (getprojent(&projent
, buf
, sizeof (buf
)) != NULL
) {
265 print_projent(&projent
);
271 while (*projlist
!= NULL
) {
273 if (getprojbyname(*projlist
, &projent
, buf
, sizeof (buf
))
275 (void) fprintf(stderr
, "%s: project \"%s\" does "
276 "not exist\n", progname
, *projlist
);
279 print_projent(&projent
);
287 main(int argc
, char *argv
[])
293 int default_only
= 0;
297 (void) setlocale(LC_ALL
, "");
298 (void) textdomain(TEXT_DOMAIN
);
300 while ((c
= getopt(argc
, argv
, "dvl")) != EOF
) {
312 (void) fprintf(stderr
, gettext(
313 "Usage: %s [-dv] [user]\n"
314 " %s -l [project [project...]]\n"),
320 /* just list projects if -l is specified */
322 if (default_only
|| verbose
) {
323 (void) fprintf(stderr
, gettext(
324 "%s: -l incompatible with -d and -v\n"),
326 (void) fprintf(stderr
, gettext(
327 "Usage: %s [-dv] [user]\n"
328 " %s -l [project [project...]]\n"),
331 exit(print_projents(argv
+ optind
));
333 if (optind
== argc
) {
335 if ((pwd
= getpwuid(uid
)) == NULL
) {
336 (void) fprintf(stderr
,
337 gettext("%s: getpwuid failed (%s)\n"),
338 progname
, strerror(errno
));
344 if (getpwnam(name
) == NULL
) {
345 (void) fprintf(stderr
,
346 gettext("%s: user %s does not exist\n"),
351 return (print_projects(name
, verbose
, default_only
));