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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_SYS_TYPES_H |
|
28 #include <sys/types.h> |
|
29 #endif |
|
30 |
|
31 #ifdef HAVE_GRP_H |
|
32 #include <grp.h> |
|
33 #endif |
|
34 |
|
35 #include "lo-error.h" |
|
36 #include "oct-group.h" |
|
37 #include "str-vec.h" |
|
38 |
|
39 #define NOT_SUPPORTED(nm) \ |
|
40 nm ## ": not supported on this system" |
|
41 |
3504
|
42 std::string |
2937
|
43 octave_group::name (void) const |
|
44 { |
|
45 if (! ok ()) |
|
46 gripe_invalid (); |
|
47 |
|
48 return gr_name; |
|
49 } |
|
50 |
3504
|
51 std::string |
2937
|
52 octave_group::passwd (void) const |
|
53 { |
|
54 if (! ok ()) |
|
55 gripe_invalid (); |
|
56 |
|
57 return gr_passwd; |
|
58 } |
|
59 |
|
60 gid_t |
|
61 octave_group::gid (void) const |
|
62 { |
|
63 if (! ok ()) |
|
64 gripe_invalid (); |
|
65 |
|
66 return gr_gid; |
|
67 } |
|
68 |
|
69 string_vector |
|
70 octave_group::mem (void) const |
|
71 { |
|
72 if (! ok ()) |
|
73 gripe_invalid (); |
|
74 |
|
75 return gr_mem; |
|
76 } |
|
77 |
|
78 octave_group |
|
79 octave_group::getgrent (void) |
|
80 { |
3504
|
81 std::string msg; |
2937
|
82 return getgrent (msg); |
|
83 } |
|
84 |
|
85 octave_group |
3504
|
86 octave_group::getgrent (std::string& msg) |
2937
|
87 { |
|
88 #if defined (HAVE_GETGRENT) |
3504
|
89 msg = std::string (); |
2937
|
90 return octave_group (::getgrent (), msg); |
|
91 #else |
|
92 msg = NOT_SUPPORTED ("getgrent"); |
|
93 return octave_group (); |
|
94 #endif |
|
95 } |
|
96 |
|
97 octave_group |
|
98 octave_group::getgrgid (gid_t gid) |
|
99 { |
3504
|
100 std::string msg; |
2937
|
101 return getgrgid (gid, msg); |
|
102 } |
|
103 |
|
104 octave_group |
3504
|
105 octave_group::getgrgid (gid_t gid, std::string& msg) |
2937
|
106 { |
|
107 #if defined (HAVE_GETGRGID) |
3504
|
108 msg = std::string (); |
2937
|
109 return octave_group (::getgrgid (gid), msg); |
|
110 #else |
|
111 msg = NOT_SUPPORTED ("getgruid"); |
|
112 return octave_group (); |
|
113 #endif |
|
114 } |
|
115 |
|
116 octave_group |
3504
|
117 octave_group::getgrnam (const std::string& nm) |
2937
|
118 { |
3504
|
119 std::string msg; |
2937
|
120 return getgrnam (msg); |
|
121 } |
|
122 |
|
123 octave_group |
3504
|
124 octave_group::getgrnam (const std::string& nm, std::string& msg) |
2937
|
125 { |
|
126 #if defined (HAVE_GETGRNAM) |
3504
|
127 msg = std::string (); |
2937
|
128 return octave_group (::getgrnam (nm.c_str ()), msg); |
|
129 #else |
|
130 msg = NOT_SUPPORTED ("getgrnam"); |
|
131 return octave_group (); |
|
132 #endif |
|
133 } |
|
134 |
|
135 int |
|
136 octave_group::setgrent (void) |
|
137 { |
3504
|
138 std::string msg; |
2937
|
139 return setgrent (msg); |
|
140 } |
|
141 |
|
142 int |
3504
|
143 octave_group::setgrent (std::string& msg) |
2937
|
144 { |
|
145 #if defined (HAVE_SETGRENT) |
3504
|
146 msg = std::string (); |
2937
|
147 ::setgrent (); |
|
148 return 0; |
|
149 #else |
|
150 msg = NOT_SUPPORTED ("setgrent"); |
|
151 return -1; |
|
152 #endif |
|
153 } |
|
154 |
|
155 int |
|
156 octave_group::endgrent (void) |
|
157 { |
3504
|
158 std::string msg; |
2937
|
159 return endgrent (msg); |
|
160 } |
|
161 |
|
162 int |
3504
|
163 octave_group::endgrent (std::string& msg) |
2937
|
164 { |
|
165 #if defined (HAVE_ENDGRENT) |
3504
|
166 msg = std::string (); |
2937
|
167 ::endgrent (); |
|
168 return 0; |
|
169 #else |
|
170 msg = NOT_SUPPORTED ("endgrent"); |
|
171 return -1; |
|
172 #endif |
|
173 } |
|
174 |
3504
|
175 octave_group::octave_group (void *p, std::string& msg) |
2937
|
176 : gr_name (), gr_passwd (), gr_gid (0), gr_mem (), valid (false) |
|
177 { |
|
178 #if defined (HAVE_GRP_H) |
3504
|
179 msg = std::string (); |
2937
|
180 |
|
181 if (p) |
|
182 { |
|
183 struct group *gr = static_cast<struct group *> (p); |
|
184 |
|
185 gr_name = gr->gr_name; |
|
186 |
|
187 #if defined (HAVE_GR_PASSWD) |
|
188 gr_passwd = gr->gr_passwd; |
|
189 #endif |
|
190 |
|
191 // XXX FIXME XXX -- maybe there should be a string_vector |
|
192 // constructor that takes a NULL terminated list of C |
|
193 // strings. |
|
194 |
|
195 const char * const *tmp = gr->gr_mem; |
|
196 |
|
197 int k = 0; |
|
198 while (*tmp++) |
|
199 k++; |
|
200 |
|
201 if (k > 0) |
|
202 { |
|
203 tmp = gr->gr_mem; |
|
204 |
|
205 gr_mem.resize (k); |
|
206 |
|
207 for (int i = 0; i < k; i++) |
|
208 gr_mem[i] = tmp[i]; |
|
209 } |
|
210 |
|
211 valid = true; |
|
212 } |
|
213 #else |
|
214 msg = NOT_SUPPORTED ("group functions"); |
|
215 #endif |
|
216 } |
|
217 |
|
218 void |
|
219 octave_group::gripe_invalid (void) const |
|
220 { |
|
221 (*current_liboctave_error_handler) ("invalid group object"); |
|
222 } |
|
223 |
|
224 /* |
|
225 ;;; Local Variables: *** |
|
226 ;;; mode: C++ *** |
|
227 ;;; End: *** |
|
228 */ |