Mercurial > hg > octave-nkf
annotate libinterp/corefcn/getpwent.cc @ 19334:b8bd0b55af45
codesprint: Add tests to time handling functions
* time.cc: Add tests and input validation for time, mktime, strftime, and
strptime.
author | Mike Miller <mtmiller@ieee.org> |
---|---|
date | Sun, 21 Sep 2014 16:08:25 -0400 |
parents | 175b392e91fe |
children | 4197fc428c7d |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
3 Copyright (C) 1996-2013 John W. Eaton |
2928 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2928 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2928 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <string> | |
28 | |
29 #include <sys/types.h> | |
30 | |
2933 | 31 #include "oct-passwd.h" |
2928 | 32 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
33 #include "defun.h" |
2928 | 34 #include "error.h" |
35 #include "gripes.h" | |
36 #include "oct-map.h" | |
37 #include "ov.h" | |
38 #include "oct-obj.h" | |
39 #include "utils.h" | |
40 | |
41 // Password file functions. (Why not?) | |
42 | |
43 static octave_value | |
2933 | 44 mk_pw_map (const octave_passwd& pw) |
2928 | 45 { |
46 octave_value retval; | |
47 | |
48 if (pw) | |
49 { | |
11043
e9966851619b
getpwent.cc, getgrent.cc, getrusage.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
50 octave_scalar_map m; |
2928 | 51 |
4675 | 52 m.assign ("name", pw.name ()); |
53 m.assign ("passwd", pw.passwd ()); | |
54 m.assign ("uid", static_cast<double> (pw.uid ())); | |
55 m.assign ("gid", static_cast<double> (pw.gid ())); | |
56 m.assign ("gecos", pw.gecos ()); | |
57 m.assign ("dir", pw.dir ()); | |
58 m.assign ("shell", pw.shell ()); | |
2928 | 59 |
60 retval = m; | |
61 } | |
62 else | |
4233 | 63 retval = 0; |
2928 | 64 |
65 return retval; | |
66 } | |
67 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
68 DEFUN (getpwent, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
70 @deftypefn {Built-in Function} {@var{pw_struct} =} getpwent ()\n\ |
3301 | 71 Return a structure containing an entry from the password database,\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
72 opening it if necessary. Once the end of the data has been reached,\n\ |
3301 | 73 @code{getpwent} returns 0.\n\ |
74 @end deftypefn") | |
2928 | 75 { |
2937 | 76 octave_value_list retval; |
77 | |
3523 | 78 retval(1) = std::string (); |
4233 | 79 retval(0) = 0; |
2928 | 80 |
81 int nargin = args.length (); | |
82 | |
83 if (nargin == 0) | |
2937 | 84 { |
3523 | 85 std::string msg; |
2937 | 86 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
87 retval(1) = msg; |
2937 | 88 retval(0) = mk_pw_map (octave_passwd::getpwent (msg)); |
89 } | |
2928 | 90 else |
5823 | 91 print_usage (); |
2928 | 92 |
93 return retval; | |
94 } | |
95 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
96 DEFUN (getpwuid, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
98 @deftypefn {Built-in Function} {@var{pw_struct} =} getpwuid (@var{uid}).\n\ |
3301 | 99 Return a structure containing the first entry from the password database\n\ |
100 with the user ID @var{uid}. If the user ID does not exist in the\n\ | |
101 database, @code{getpwuid} returns 0.\n\ | |
102 @end deftypefn") | |
2928 | 103 { |
2937 | 104 octave_value_list retval; |
105 | |
3523 | 106 retval(1) = std::string (); |
4233 | 107 retval(0) = 0; |
2928 | 108 |
109 int nargin = args.length (); | |
110 | |
111 if (nargin == 1) | |
112 { | |
113 double dval = args(0).double_value (); | |
114 | |
115 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
116 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
117 if (D_NINT (dval) == dval) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
118 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
119 uid_t uid = static_cast<uid_t> (dval); |
2928 | 120 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
121 std::string msg; |
2937 | 122 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
123 retval(1) = msg; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
124 retval(0) = mk_pw_map (octave_passwd::getpwuid (uid, msg)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
125 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
126 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
127 error ("getpwuid: UID must be an integer"); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
128 } |
2928 | 129 } |
130 else | |
5823 | 131 print_usage (); |
2928 | 132 |
133 return retval; | |
134 } | |
135 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
136 DEFUN (getpwnam, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
137 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
138 @deftypefn {Built-in Function} {@var{pw_struct} =} getpwnam (@var{name})\n\ |
3301 | 139 Return a structure containing the first entry from the password database\n\ |
140 with the user name @var{name}. If the user name does not exist in the\n\ | |
141 database, @code{getpwname} returns 0.\n\ | |
142 @end deftypefn") | |
2928 | 143 { |
2937 | 144 octave_value_list retval; |
145 | |
3523 | 146 retval(1) = std::string (); |
2937 | 147 retval(0) = 0.0; |
2928 | 148 |
149 int nargin = args.length (); | |
150 | |
151 if (nargin == 1) | |
152 { | |
3523 | 153 std::string s = args(0).string_value (); |
2928 | 154 |
155 if (! error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
156 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
157 std::string msg; |
2937 | 158 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
159 retval(1) = msg; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
160 retval(0) = mk_pw_map (octave_passwd::getpwnam (s, msg)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
161 } |
2928 | 162 } |
163 else | |
5823 | 164 print_usage (); |
2928 | 165 |
166 return retval; | |
167 } | |
168 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
169 DEFUN (setpwent, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
170 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
171 @deftypefn {Built-in Function} {} setpwent ()\n\ |
3301 | 172 Return the internal pointer to the beginning of the password database.\n\ |
173 @end deftypefn") | |
2928 | 174 { |
2937 | 175 octave_value_list retval; |
176 | |
3523 | 177 retval(1) = std::string (); |
2937 | 178 retval(0) = -1.0; |
2928 | 179 |
180 int nargin = args.length (); | |
181 | |
182 if (nargin == 0) | |
2937 | 183 { |
3523 | 184 std::string msg; |
2937 | 185 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
186 retval(1) = msg; |
2937 | 187 retval(0) = static_cast<double> (octave_passwd::setpwent (msg)); |
188 } | |
2928 | 189 else |
5823 | 190 print_usage (); |
2928 | 191 |
192 return retval; | |
193 } | |
194 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
195 DEFUN (endpwent, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
196 "-*- texinfo -*-\n\ |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
197 @deftypefn {Built-in Function} {} endpwent ()\n\ |
3301 | 198 Close the password database.\n\ |
199 @end deftypefn") | |
2928 | 200 { |
2937 | 201 octave_value_list retval; |
202 | |
3523 | 203 retval(1) = std::string (); |
2937 | 204 retval(0) = -1.0; |
2928 | 205 |
206 int nargin = args.length (); | |
207 | |
208 if (nargin == 0) | |
2937 | 209 { |
3523 | 210 std::string msg; |
2937 | 211 |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
11553
diff
changeset
|
212 retval(1) = msg; |
2937 | 213 retval(0) = static_cast<double> (octave_passwd::endpwent (msg)); |
214 } | |
2928 | 215 else |
5823 | 216 print_usage (); |
2928 | 217 |
218 return retval; | |
219 } |