xref: /illumos-gate/usr/src/cmd/getent/dogetexecattr.c (revision e4c795beb33bf59dd4ad2e3f88f493111484b890)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2018 Peter Tribble.
24  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <exec_attr.h>
31 #include "getent.h"
32 
33 static int
34 putexecattr(const execattr_t *exec, FILE *fp)
35 {
36 	int i;
37 	kva_t *attrs;
38 	kv_t *data;
39 
40 	if (exec == NULL)
41 		return (1);
42 
43 	if (fprintf(fp, "%s:%s:%s:%s:%s:%s:",
44 	    exec->name != NULL ? exec->name : "",
45 	    exec->policy != NULL ? exec->policy : "",
46 	    exec->type != NULL ? exec->type : "",
47 	    exec->res1 != NULL ? exec->res1 : "",
48 	    exec->res2 != NULL ? exec->res2 : "",
49 	    exec->id != NULL ? exec->id : "") == EOF)
50 		return (1);
51 	attrs = exec->attr;
52 	if (attrs != NULL) {
53 		data = attrs->data;
54 		for (i = 0; i < attrs->length; i++) {
55 			if (fprintf(fp, "%s=%s%s",
56 			    data[i].key != NULL ? data[i].key : "",
57 			    data[i].value != NULL ? data[i].value : "",
58 			    i < (attrs->length)-1 ? ";" : "") == EOF)
59 				return (1);
60 		}
61 	}
62 	if (putc('\n', fp) == EOF)
63 		return (1);
64 	return (0);
65 }
66 
67 int
68 dogetexecattr(const char **list)
69 {
70 	execattr_t *pexec;
71 	int rc = EXC_SUCCESS;
72 
73 	if (list == NULL || *list == NULL) {
74 		setexecattr();
75 		while ((pexec = getexecattr()) != NULL)
76 			(void) putexecattr(pexec, stdout);
77 		endexecattr();
78 	} else {
79 		for (; *list != NULL; list++) {
80 			pexec = getexecprof(*list, NULL, NULL, GET_ALL);
81 			if (pexec == NULL) {
82 				rc = EXC_NAME_NOT_FOUND;
83 			} else {
84 				for (; pexec != NULL; pexec = pexec->next) {
85 					(void) putexecattr(pexec, stdout);
86 				}
87 
88 			}
89 		}
90 	}
91 
92 	return (rc);
93 }
94