Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / pgrep / psexp.c
blobbe14393bd15d7af1dfe83b0bdd1eab3895413a12
1 /*
2 * CDDL HEADER START
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.
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]
20 * CDDL HEADER END
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 <string.h>
30 #include <stdlib.h>
31 #include <alloca.h>
32 #include <libuutil.h>
34 #include "idtab.h"
35 #include "psexp.h"
37 void
38 psexp_create(psexp_t *psexp)
40 idtab_create(&psexp->ps_euids);
41 idtab_create(&psexp->ps_ruids);
42 idtab_create(&psexp->ps_rgids);
43 idtab_create(&psexp->ps_ppids);
44 idtab_create(&psexp->ps_pgids);
45 idtab_create(&psexp->ps_sids);
46 idtab_create(&psexp->ps_ttys);
47 idtab_create(&psexp->ps_projids);
48 idtab_create(&psexp->ps_taskids);
49 idtab_create(&psexp->ps_zoneids);
50 idtab_create(&psexp->ps_ctids);
52 psexp->ps_pat = NULL;
55 void
56 psexp_destroy(psexp_t *psexp)
58 idtab_destroy(&psexp->ps_euids);
59 idtab_destroy(&psexp->ps_ruids);
60 idtab_destroy(&psexp->ps_rgids);
61 idtab_destroy(&psexp->ps_ppids);
62 idtab_destroy(&psexp->ps_pgids);
63 idtab_destroy(&psexp->ps_sids);
64 idtab_destroy(&psexp->ps_ttys);
65 idtab_destroy(&psexp->ps_projids);
66 idtab_destroy(&psexp->ps_taskids);
67 idtab_destroy(&psexp->ps_zoneids);
68 idtab_destroy(&psexp->ps_ctids);
70 if (psexp->ps_pat)
71 regfree(&psexp->ps_reg);
74 int
75 psexp_compile(psexp_t *psexp)
77 size_t nbytes;
78 char *buf;
79 int err;
81 idtab_sort(&psexp->ps_euids);
82 idtab_sort(&psexp->ps_ruids);
83 idtab_sort(&psexp->ps_rgids);
84 idtab_sort(&psexp->ps_ppids);
85 idtab_sort(&psexp->ps_pgids);
86 idtab_sort(&psexp->ps_sids);
87 idtab_sort(&psexp->ps_ttys);
88 idtab_sort(&psexp->ps_projids);
89 idtab_sort(&psexp->ps_taskids);
90 idtab_sort(&psexp->ps_zoneids);
91 idtab_sort(&psexp->ps_ctids);
93 if (psexp->ps_pat != NULL) {
94 if ((err = regcomp(&psexp->ps_reg, psexp->ps_pat,
95 REG_EXTENDED)) != 0) {
97 nbytes = regerror(err, &psexp->ps_reg, NULL, 0);
98 buf = alloca(nbytes + 1);
99 (void) regerror(err, &psexp->ps_reg, buf, nbytes);
100 (void) strcat(buf, "\n");
101 uu_warn(buf);
102 return (-1);
106 return (0);
109 #define NOMATCH(__f1, __f2) \
110 psexp->__f1.id_data && !idtab_search(&psexp->__f1, psinfo->__f2)
113 psexp_match(psexp_t *psexp, psinfo_t *psinfo, int flags)
115 regmatch_t pmatch;
116 const char *s;
118 if (NOMATCH(ps_euids, pr_euid))
119 return (0);
120 if (NOMATCH(ps_ruids, pr_uid))
121 return (0);
122 if (NOMATCH(ps_rgids, pr_gid))
123 return (0);
124 if (NOMATCH(ps_ppids, pr_ppid))
125 return (0);
126 if (NOMATCH(ps_pgids, pr_pgid))
127 return (0);
128 if (NOMATCH(ps_sids, pr_sid))
129 return (0);
130 if (NOMATCH(ps_ttys, pr_ttydev))
131 return (0);
132 if (NOMATCH(ps_projids, pr_projid))
133 return (0);
134 if (NOMATCH(ps_taskids, pr_taskid))
135 return (0);
136 if (NOMATCH(ps_zoneids, pr_zoneid))
137 return (0);
138 if (NOMATCH(ps_ctids, pr_contract))
139 return (0);
141 if (psexp->ps_pat != NULL) {
142 s = (flags & PSEXP_PSARGS) ?
143 psinfo->pr_psargs : psinfo->pr_fname;
145 if (regexec(&psexp->ps_reg, s, 1, &pmatch, 0) != 0)
146 return (0);
148 if ((flags & PSEXP_EXACT) &&
149 (pmatch.rm_so != 0 || s[pmatch.rm_eo] != '\0'))
150 return (0);
153 return (1);