GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-group.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <sys/types.h>
28 
29 #ifdef HAVE_GRP_H
30 #include <grp.h>
31 #endif
32 
33 #include "lo-error.h"
34 #include "oct-group.h"
35 #include "str-vec.h"
36 
37 #define NOT_SUPPORTED(nm) \
38  nm ": not supported on this system"
39 
40 std::string
41 octave_group::name (void) const
42 {
43  if (! ok ())
44  gripe_invalid ();
45 
46  return gr_name;
47 }
48 
49 std::string
51 {
52  if (! ok ())
53  gripe_invalid ();
54 
55  return gr_passwd;
56 }
57 
58 gid_t
59 octave_group::gid (void) const
60 {
61  if (! ok ())
62  gripe_invalid ();
63 
64  return gr_gid;
65 }
66 
68 octave_group::mem (void) const
69 {
70  if (! ok ())
71  gripe_invalid ();
72 
73  return gr_mem;
74 }
75 
78 {
79  std::string msg;
80  return getgrent (msg);
81 }
82 
84 octave_group::getgrent (std::string& msg)
85 {
86 #if defined (HAVE_GETGRENT)
87  msg = std::string ();
88  return octave_group (::getgrent (), msg);
89 #else
90  msg = NOT_SUPPORTED ("getgrent");
91  return octave_group ();
92 #endif
93 }
94 
97 {
98  std::string msg;
99  return getgrgid (gid, msg);
100 }
101 
103 octave_group::getgrgid (gid_t gid, std::string& msg)
104 {
105 #if defined (HAVE_GETGRGID)
106  msg = std::string ();
107  return octave_group (::getgrgid (gid), msg);
108 #else
109  msg = NOT_SUPPORTED ("getgruid");
110  return octave_group ();
111 #endif
112 }
113 
115 octave_group::getgrnam (const std::string& nm)
116 {
117  std::string msg;
118  return getgrnam (nm, msg);
119 }
120 
122 octave_group::getgrnam (const std::string& nm, std::string& msg)
123 {
124 #if defined (HAVE_GETGRNAM)
125  msg = std::string ();
126  return octave_group (::getgrnam (nm.c_str ()), msg);
127 #else
128  msg = NOT_SUPPORTED ("getgrnam");
129  return octave_group ();
130 #endif
131 }
132 
133 int
135 {
136  std::string msg;
137  return setgrent (msg);
138 }
139 
140 int
141 octave_group::setgrent (std::string& msg)
142 {
143 #if defined (HAVE_SETGRENT)
144  msg = std::string ();
145  ::setgrent ();
146  return 0;
147 #else
148  msg = NOT_SUPPORTED ("setgrent");
149  return -1;
150 #endif
151 }
152 
153 int
155 {
156  std::string msg;
157  return endgrent (msg);
158 }
159 
160 int
161 octave_group::endgrent (std::string& msg)
162 {
163 #if defined (HAVE_ENDGRENT)
164  msg = std::string ();
165  ::endgrent ();
166  return 0;
167 #else
168  msg = NOT_SUPPORTED ("endgrent");
169  return -1;
170 #endif
171 }
172 
173 octave_group::octave_group (void *p, std::string& msg)
174  : gr_name (), gr_passwd (), gr_gid (0), gr_mem (), valid (false)
175 {
176 #if defined (HAVE_GRP_H)
177  msg = std::string ();
178 
179  if (p)
180  {
181  struct group *gr = static_cast<struct group *> (p);
182 
183  gr_name = gr->gr_name;
184 
185 #if defined (HAVE_GR_PASSWD)
186  gr_passwd = gr->gr_passwd;
187 #endif
188 
189  gr_gid = gr->gr_gid;
190 
191  // FIXME: Maybe there should be a string_vector constructor
192  // that takes a NUL terminated list of C strings?
193 
194  const char * const *tmp = gr->gr_mem;
195 
196  int k = 0;
197  while (*tmp++)
198  k++;
199 
200  if (k > 0)
201  {
202  tmp = gr->gr_mem;
203 
204  gr_mem.resize (k);
205 
206  for (int i = 0; i < k; i++)
207  gr_mem[i] = tmp[i];
208  }
209 
210  valid = true;
211  }
212 #else
213  msg = NOT_SUPPORTED ("group functions");
214 #endif
215 }
216 
217 void
219 {
220  (*current_liboctave_error_handler) ("invalid group object");
221 }
static int endgrent(void)
Definition: oct-group.cc:154
static int setgrent(void)
Definition: oct-group.cc:134
static octave_group getgrgid(gid_t gid)
Definition: oct-group.cc:96
string_vector mem(void) const
Definition: oct-group.cc:68
bool ok(void) const
Definition: oct-group.h:69
gid_t gid(void) const
Definition: oct-group.cc:59
void resize(octave_idx_type n, const std::string &rfv=std::string())
Definition: str-vec.h:91
std::string passwd(void) const
Definition: oct-group.cc:50
octave_group(void)
Definition: oct-group.h:38
void gripe_invalid(void) const
Definition: oct-group.cc:218
static octave_group getgrnam(const std::string &nm)
Definition: oct-group.cc:115
#define NOT_SUPPORTED(nm)
Definition: oct-group.cc:37
static octave_group getgrent(void)
Definition: oct-group.cc:77
std::string name(void) const
Definition: oct-group.cc:41
std::string gr_passwd
Definition: oct-group.h:94
string_vector gr_mem
Definition: oct-group.h:100
std::string gr_name
Definition: oct-group.h:91
gid_t gr_gid
Definition: oct-group.h:97