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