~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to liboctave/oct-group.cc

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (C) 1996, 1997, 2000, 2002, 2003, 2005, 2006, 2007
 
4
              John W. Eaton
 
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
 
10
Free Software Foundation; either version 3 of the License, or (at your
 
11
option) any later version.
 
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
 
19
along with Octave; see the file COPYING.  If not, see
 
20
<http://www.gnu.org/licenses/>.
 
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) \
 
41
  nm ": not supported on this system"
 
42
 
 
43
std::string
 
44
octave_group::name (void) const
 
45
{
 
46
  if (! ok ())
 
47
    gripe_invalid ();
 
48
 
 
49
  return gr_name;
 
50
}
 
51
 
 
52
std::string
 
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
{
 
82
  std::string msg;
 
83
  return getgrent (msg);
 
84
}
 
85
 
 
86
octave_group
 
87
octave_group::getgrent (std::string& msg)
 
88
{
 
89
#if defined (HAVE_GETGRENT)
 
90
  msg = std::string ();
 
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
{
 
101
  std::string msg;
 
102
  return getgrgid (gid, msg);
 
103
}
 
104
 
 
105
octave_group
 
106
octave_group::getgrgid (gid_t gid, std::string& msg)
 
107
{
 
108
#if defined (HAVE_GETGRGID)
 
109
  msg = std::string ();
 
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
 
118
octave_group::getgrnam (const std::string& nm)
 
119
{
 
120
  std::string msg;
 
121
  return getgrnam (nm, msg);
 
122
}
 
123
 
 
124
octave_group
 
125
octave_group::getgrnam (const std::string& nm, std::string& msg)
 
126
{
 
127
#if defined (HAVE_GETGRNAM)
 
128
  msg = std::string ();
 
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
{
 
139
  std::string msg;
 
140
  return setgrent (msg);
 
141
}
 
142
 
 
143
int
 
144
octave_group::setgrent (std::string& msg)
 
145
{
 
146
#if defined (HAVE_SETGRENT)
 
147
  msg = std::string ();
 
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
{
 
159
  std::string msg;
 
160
  return endgrent (msg);
 
161
}
 
162
 
 
163
int
 
164
octave_group::endgrent (std::string& msg)
 
165
{
 
166
#if defined (HAVE_ENDGRENT)
 
167
  msg = std::string ();
 
168
  ::endgrent ();
 
169
  return 0;
 
170
#else
 
171
  msg = NOT_SUPPORTED ("endgrent");
 
172
  return -1;
 
173
#endif
 
174
}
 
175
 
 
176
octave_group::octave_group (void *p, std::string& msg)
 
177
  : gr_name (), gr_passwd (), gr_gid (0), gr_mem (), valid (false)
 
178
{
 
179
#if defined (HAVE_GRP_H)
 
180
  msg = std::string ();
 
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
 
 
192
      gr_gid = gr->gr_gid;
 
193
 
 
194
      // FIXME -- maybe there should be a string_vector
 
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
*/