getpwent.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <string>
00028 
00029 #include <sys/types.h>
00030 
00031 #include "oct-passwd.h"
00032 
00033 #include "defun-dld.h"
00034 #include "error.h"
00035 #include "gripes.h"
00036 #include "oct-map.h"
00037 #include "ov.h"
00038 #include "oct-obj.h"
00039 #include "utils.h"
00040 
00041 // Password file functions.  (Why not?)
00042 
00043 static octave_value
00044 mk_pw_map (const octave_passwd& pw)
00045 {
00046   octave_value retval;
00047 
00048   if (pw)
00049     {
00050       octave_scalar_map m;
00051 
00052       m.assign ("name", pw.name ());
00053       m.assign ("passwd", pw.passwd ());
00054       m.assign ("uid", static_cast<double> (pw.uid ()));
00055       m.assign ("gid", static_cast<double> (pw.gid ()));
00056       m.assign ("gecos", pw.gecos ());
00057       m.assign ("dir", pw.dir ());
00058       m.assign ("shell", pw.shell ());
00059 
00060       retval = m;
00061     }
00062   else
00063     retval = 0;
00064 
00065   return retval;
00066 }
00067 
00068 DEFUN_DLD (getpwent, args, ,
00069  "-*- texinfo -*-\n\
00070 @deftypefn {Loadable Function} {@var{pw_struct} =} getpwent ()\n\
00071 Return a structure containing an entry from the password database,\n\
00072 opening it if necessary.  Once the end of the data has been reached,\n\
00073 @code{getpwent} returns 0.\n\
00074 @end deftypefn")
00075 {
00076   octave_value_list retval;
00077 
00078   retval(1) = std::string ();
00079   retval(0) = 0;
00080 
00081   int nargin = args.length ();
00082 
00083   if (nargin == 0)
00084     {
00085       std::string msg;
00086 
00087       retval(1) = msg;
00088       retval(0) = mk_pw_map (octave_passwd::getpwent (msg));
00089     }
00090   else
00091     print_usage ();
00092 
00093   return retval;
00094 }
00095 
00096 DEFUN_DLD (getpwuid, args, ,
00097   "-*- texinfo -*-\n\
00098 @deftypefn {Loadable Function} {@var{pw_struct} =} getpwuid (@var{uid}).\n\
00099 Return a structure containing the first entry from the password database\n\
00100 with the user ID @var{uid}.  If the user ID does not exist in the\n\
00101 database, @code{getpwuid} returns 0.\n\
00102 @end deftypefn")
00103 {
00104   octave_value_list retval;
00105 
00106   retval(1) = std::string ();
00107   retval(0) = 0;
00108 
00109   int nargin = args.length ();
00110 
00111   if (nargin == 1)
00112     {
00113       double dval = args(0).double_value ();
00114 
00115       if (! error_state)
00116         {
00117           if (D_NINT (dval) == dval)
00118             {
00119               uid_t uid = static_cast<uid_t> (dval);
00120 
00121               std::string msg;
00122 
00123               retval(1) = msg;
00124               retval(0) = mk_pw_map (octave_passwd::getpwuid (uid, msg));
00125             }
00126           else
00127             error ("getpwuid: UID must be an integer");
00128         }
00129     }
00130   else
00131     print_usage ();
00132 
00133   return retval;
00134 }
00135 
00136 DEFUN_DLD (getpwnam, args, ,
00137   "-*- texinfo -*-\n\
00138 @deftypefn {Loadable Function} {@var{pw_struct} =} getpwnam (@var{name})\n\
00139 Return a structure containing the first entry from the password database\n\
00140 with the user name @var{name}.  If the user name does not exist in the\n\
00141 database, @code{getpwname} returns 0.\n\
00142 @end deftypefn")
00143 {
00144   octave_value_list retval;
00145 
00146   retval(1) = std::string ();
00147   retval(0) = 0.0;
00148 
00149   int nargin = args.length ();
00150 
00151   if (nargin == 1)
00152     {
00153       std::string s = args(0).string_value ();
00154 
00155       if (! error_state)
00156         {
00157           std::string msg;
00158 
00159           retval(1) = msg;
00160           retval(0) = mk_pw_map (octave_passwd::getpwnam (s, msg));
00161         }
00162     }
00163   else
00164     print_usage ();
00165 
00166   return retval;
00167 }
00168 
00169 DEFUN_DLD (setpwent, args, ,
00170   "-*- texinfo -*-\n\
00171 @deftypefn {Loadable Function} {} setpwent ()\n\
00172 Return the internal pointer to the beginning of the password database.\n\
00173 @end deftypefn")
00174 {
00175   octave_value_list retval;
00176 
00177   retval(1) = std::string ();
00178   retval(0) = -1.0;
00179 
00180   int nargin = args.length ();
00181 
00182   if (nargin == 0)
00183     {
00184       std::string msg;
00185 
00186       retval(1) = msg;
00187       retval(0) = static_cast<double> (octave_passwd::setpwent (msg));
00188     }
00189   else
00190     print_usage ();
00191 
00192   return retval;
00193 }
00194 
00195 DEFUN_DLD (endpwent, args, ,
00196   "-*- texinfo -*-\n\
00197 @deftypefn {Loadable Function} {} endpwent ()\n\
00198 Close the password database.\n\
00199 @end deftypefn")
00200 {
00201   octave_value_list retval;
00202 
00203   retval(1) = std::string ();
00204   retval(0) = -1.0;
00205 
00206   int nargin = args.length ();
00207 
00208   if (nargin == 0)
00209     {
00210       std::string msg;
00211 
00212       retval(1) = msg;
00213       retval(0) = static_cast<double> (octave_passwd::endpwent (msg));
00214     }
00215   else
00216     print_usage ();
00217 
00218   return retval;
00219 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines