comparison src/getgrent.cc @ 2475:1d7925d6bede

[project @ 1996-11-07 04:36:00 by jwe]
author jwe
date Thu, 07 Nov 1996 04:46:54 +0000
parents
children b50cc31aa0cd
comparison
equal deleted inserted replaced
2474:b8c53143581b 2475:1d7925d6bede
1 /*
2
3 Copyright (C) 1996 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 #include <string>
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_GRP_H
34 #include <grp.h>
35 #endif
36
37 #include "defun-dld.h"
38 #include "error.h"
39 #include "gripes.h"
40 #include "help.h"
41 #include "oct-map.h"
42 #include "ov.h"
43 #include "oct-obj.h"
44 #include "utils.h"
45
46 // Group file functions. (Why not?)
47
48 static octave_value
49 mk_gr_map (struct group *gr)
50 {
51 octave_value retval;
52
53 if (gr)
54 {
55 Octave_map m;
56
57 m ["name"] = gr->gr_name;
58 m ["passwd"] = gr->gr_passwd;
59 m ["gid"] = STATIC_CAST (double, gr->gr_gid);
60
61 if (gr->gr_mem)
62 {
63 // XXX FIXME XXX -- maybe there should be a string_vector
64 // constructor that takes a NULL terminated list of C
65 // strings.
66
67 char **tmp = gr->gr_mem;
68
69 int k = 0;
70 while (*tmp++)
71 k++;
72
73 if (k > 0)
74 {
75 tmp = gr->gr_mem;
76
77 string_vector members (k);
78
79 for (int i = 0; i < k; i++)
80 members[i] = tmp[i];
81
82 m ["mem"] = members;
83 }
84 else
85 m ["mem"] = "";
86 }
87
88 retval = m;
89 }
90 else
91 retval = 0.0;
92
93 return retval;
94 }
95
96 DEFUN_DLD (getgrent, args, ,
97 "getgrent ()\n\
98 \n\
99 Read an entry from the group-file stream, opening it if necessary.")
100 {
101 octave_value retval;
102
103 int nargin = args.length ();
104
105 if (nargin == 0)
106 {
107 #ifdef HAVE_GETGRENT
108 retval = mk_gr_map (getgrent ());
109 #else
110 gripe_not_supported ("getgrent");
111 #endif
112 }
113 else
114 print_usage ("getgrent");
115
116 return retval;
117 }
118
119 DEFUN_DLD (getgrgid, args, ,
120 "getgrgid (GID)\n\
121 \n\
122 Search for a group entry with a matching group ID.")
123 {
124 octave_value retval;
125
126 int nargin = args.length ();
127
128 if (nargin == 1)
129 {
130 #ifdef HAVE_GETGRGID
131 double dval = args(0).double_value ();
132
133 if (! error_state)
134 {
135 if (D_NINT (dval) == dval)
136 {
137 gid_t gid = STATIC_CAST (gid_t, dval);
138
139 retval = mk_gr_map (getgrgid (gid));
140 }
141 else
142 error ("getgrgid: argument must be an integer");
143 }
144 #else
145 gripe_not_supported ("getgrgid");
146 #endif
147 }
148 else
149 print_usage ("getgrgid");
150
151 return retval;
152 }
153
154 DEFUN_DLD (getgrnam, args, ,
155 "getgrnam (NAME)\n\
156 \n\
157 Search for group entry with a matching group name.")
158 {
159 octave_value retval;
160
161 int nargin = args.length ();
162
163 if (nargin == 1)
164 {
165 #ifdef HAVE_GETGRNAM
166 string s = args(0).string_value ();
167
168 if (! error_state)
169 retval = mk_gr_map (getgrnam (s.c_str ()));
170 #else
171 gripe_not_supported ("getgrnam");
172 #endif
173 }
174 else
175 print_usage ("getgrnam");
176
177 return retval;
178 }
179
180 DEFUN_DLD (setgrent, args, ,
181 "setgrent ()\n\
182 \n\
183 Rewind the group-file stream.")
184 {
185 octave_value retval;
186
187 int nargin = args.length ();
188
189 if (nargin == 0)
190 {
191 #ifdef HAVE_SETGRENT
192 setgrent ();
193 #else
194 gripe_not_supported ("setgrent");
195 #endif
196 }
197 else
198 print_usage ("setgrent");
199
200 return retval;
201 }
202
203 DEFUN_DLD (endgrent, args, ,
204 "endgrent ()\n\
205 \n\
206 Close the group-file stream.")
207 {
208 octave_value retval;
209
210 int nargin = args.length ();
211
212 if (nargin == 0)
213 {
214 #ifdef HAVE_ENDGRENT
215 endgrent ();
216 #else
217 gripe_not_supported ("endgrent");
218 #endif
219 }
220 else
221 print_usage ("endgrent");
222
223 return retval;
224 }
225
226 /*
227 ;;; Local Variables: ***
228 ;;; mode: C++ ***
229 ;;; End: ***
230 */