2937
|
1 /* |
|
2 |
8920
|
3 Copyright (C) 1996, 1997, 2000, 2002, 2003, 2005, 2006, 2007, 2008 |
7017
|
4 John W. Eaton |
2937
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2937
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2937
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <sys/types.h> |
|
29 |
|
30 #ifdef HAVE_GRP_H |
|
31 #include <grp.h> |
|
32 #endif |
|
33 |
|
34 #include "lo-error.h" |
|
35 #include "oct-group.h" |
|
36 #include "str-vec.h" |
|
37 |
|
38 #define NOT_SUPPORTED(nm) \ |
4062
|
39 nm ": not supported on this system" |
2937
|
40 |
3504
|
41 std::string |
2937
|
42 octave_group::name (void) const |
|
43 { |
|
44 if (! ok ()) |
|
45 gripe_invalid (); |
|
46 |
|
47 return gr_name; |
|
48 } |
|
49 |
3504
|
50 std::string |
2937
|
51 octave_group::passwd (void) const |
|
52 { |
|
53 if (! ok ()) |
|
54 gripe_invalid (); |
|
55 |
|
56 return gr_passwd; |
|
57 } |
|
58 |
|
59 gid_t |
|
60 octave_group::gid (void) const |
|
61 { |
|
62 if (! ok ()) |
|
63 gripe_invalid (); |
|
64 |
|
65 return gr_gid; |
|
66 } |
|
67 |
|
68 string_vector |
|
69 octave_group::mem (void) const |
|
70 { |
|
71 if (! ok ()) |
|
72 gripe_invalid (); |
|
73 |
|
74 return gr_mem; |
|
75 } |
|
76 |
|
77 octave_group |
|
78 octave_group::getgrent (void) |
|
79 { |
3504
|
80 std::string msg; |
2937
|
81 return getgrent (msg); |
|
82 } |
|
83 |
|
84 octave_group |
3504
|
85 octave_group::getgrent (std::string& msg) |
2937
|
86 { |
|
87 #if defined (HAVE_GETGRENT) |
3504
|
88 msg = std::string (); |
2937
|
89 return octave_group (::getgrent (), msg); |
|
90 #else |
|
91 msg = NOT_SUPPORTED ("getgrent"); |
|
92 return octave_group (); |
|
93 #endif |
|
94 } |
|
95 |
|
96 octave_group |
|
97 octave_group::getgrgid (gid_t gid) |
|
98 { |
3504
|
99 std::string msg; |
2937
|
100 return getgrgid (gid, msg); |
|
101 } |
|
102 |
|
103 octave_group |
3504
|
104 octave_group::getgrgid (gid_t gid, std::string& msg) |
2937
|
105 { |
|
106 #if defined (HAVE_GETGRGID) |
3504
|
107 msg = std::string (); |
2937
|
108 return octave_group (::getgrgid (gid), msg); |
|
109 #else |
|
110 msg = NOT_SUPPORTED ("getgruid"); |
|
111 return octave_group (); |
|
112 #endif |
|
113 } |
|
114 |
|
115 octave_group |
3504
|
116 octave_group::getgrnam (const std::string& nm) |
2937
|
117 { |
3504
|
118 std::string msg; |
4663
|
119 return getgrnam (nm, msg); |
2937
|
120 } |
|
121 |
|
122 octave_group |
3504
|
123 octave_group::getgrnam (const std::string& nm, std::string& msg) |
2937
|
124 { |
|
125 #if defined (HAVE_GETGRNAM) |
3504
|
126 msg = std::string (); |
2937
|
127 return octave_group (::getgrnam (nm.c_str ()), msg); |
|
128 #else |
|
129 msg = NOT_SUPPORTED ("getgrnam"); |
|
130 return octave_group (); |
|
131 #endif |
|
132 } |
|
133 |
|
134 int |
|
135 octave_group::setgrent (void) |
|
136 { |
3504
|
137 std::string msg; |
2937
|
138 return setgrent (msg); |
|
139 } |
|
140 |
|
141 int |
3504
|
142 octave_group::setgrent (std::string& msg) |
2937
|
143 { |
|
144 #if defined (HAVE_SETGRENT) |
3504
|
145 msg = std::string (); |
2937
|
146 ::setgrent (); |
|
147 return 0; |
|
148 #else |
|
149 msg = NOT_SUPPORTED ("setgrent"); |
|
150 return -1; |
|
151 #endif |
|
152 } |
|
153 |
|
154 int |
|
155 octave_group::endgrent (void) |
|
156 { |
3504
|
157 std::string msg; |
2937
|
158 return endgrent (msg); |
|
159 } |
|
160 |
|
161 int |
3504
|
162 octave_group::endgrent (std::string& msg) |
2937
|
163 { |
|
164 #if defined (HAVE_ENDGRENT) |
3504
|
165 msg = std::string (); |
2937
|
166 ::endgrent (); |
|
167 return 0; |
|
168 #else |
|
169 msg = NOT_SUPPORTED ("endgrent"); |
|
170 return -1; |
|
171 #endif |
|
172 } |
|
173 |
3504
|
174 octave_group::octave_group (void *p, std::string& msg) |
2937
|
175 : gr_name (), gr_passwd (), gr_gid (0), gr_mem (), valid (false) |
|
176 { |
|
177 #if defined (HAVE_GRP_H) |
3504
|
178 msg = std::string (); |
2937
|
179 |
|
180 if (p) |
|
181 { |
|
182 struct group *gr = static_cast<struct group *> (p); |
|
183 |
|
184 gr_name = gr->gr_name; |
|
185 |
|
186 #if defined (HAVE_GR_PASSWD) |
|
187 gr_passwd = gr->gr_passwd; |
|
188 #endif |
|
189 |
4076
|
190 gr_gid = gr->gr_gid; |
|
191 |
5775
|
192 // FIXME -- maybe there should be a string_vector |
7520
|
193 // constructor that takes a NUL terminated list of C |
2937
|
194 // strings. |
|
195 |
|
196 const char * const *tmp = gr->gr_mem; |
|
197 |
|
198 int k = 0; |
|
199 while (*tmp++) |
|
200 k++; |
|
201 |
|
202 if (k > 0) |
|
203 { |
|
204 tmp = gr->gr_mem; |
|
205 |
|
206 gr_mem.resize (k); |
|
207 |
|
208 for (int i = 0; i < k; i++) |
|
209 gr_mem[i] = tmp[i]; |
|
210 } |
|
211 |
|
212 valid = true; |
|
213 } |
|
214 #else |
|
215 msg = NOT_SUPPORTED ("group functions"); |
|
216 #endif |
|
217 } |
|
218 |
|
219 void |
|
220 octave_group::gripe_invalid (void) const |
|
221 { |
|
222 (*current_liboctave_error_handler) ("invalid group object"); |
|
223 } |