oct-passwd.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 <sys/types.h>
00028 
00029 #ifdef HAVE_PWD_H
00030 #include <pwd.h>
00031 #endif
00032 
00033 #include "lo-error.h"
00034 #include "oct-passwd.h"
00035 
00036 #define NOT_SUPPORTED(nm) \
00037   nm ": not supported on this system"
00038 
00039 std::string
00040 octave_passwd::name (void) const
00041 {
00042   if (! ok ())
00043     gripe_invalid ();
00044 
00045   return pw_name;
00046 }
00047 
00048 std::string
00049 octave_passwd::passwd (void) const
00050 {
00051   if (! ok ())
00052     gripe_invalid ();
00053 
00054   return pw_passwd;
00055 }
00056 
00057 uid_t
00058 octave_passwd::uid (void) const
00059 {
00060   if (! ok ())
00061     gripe_invalid ();
00062 
00063   return pw_uid;
00064 }
00065 
00066 gid_t
00067 octave_passwd::gid (void) const
00068 {
00069   if (! ok ())
00070     gripe_invalid ();
00071 
00072   return pw_gid;
00073 }
00074 
00075 std::string
00076 octave_passwd::gecos (void) const
00077 {
00078   if (! ok ())
00079     gripe_invalid ();
00080 
00081   return pw_gecos;
00082 }
00083 
00084 std::string
00085 octave_passwd::dir (void) const
00086 {
00087   if (! ok ())
00088     gripe_invalid ();
00089 
00090   return pw_dir;
00091 }
00092 
00093 std::string
00094 octave_passwd::shell (void) const
00095 {
00096   if (! ok ())
00097     gripe_invalid ();
00098 
00099   return pw_shell;
00100 }
00101 
00102 octave_passwd
00103 octave_passwd::getpwent (void)
00104 {
00105   std::string msg;
00106   return getpwent (msg);
00107 }
00108 
00109 octave_passwd
00110 octave_passwd::getpwent (std::string& msg)
00111 {
00112 #if defined HAVE_GETPWENT
00113   msg = std::string ();
00114   return octave_passwd (::getpwent (), msg);
00115 #else
00116   msg = NOT_SUPPORTED ("getpwent");
00117   return octave_passwd ();
00118 #endif
00119 }
00120 
00121 octave_passwd
00122 octave_passwd::getpwuid (uid_t uid)
00123 {
00124   std::string msg;
00125   return getpwuid (uid, msg);
00126 }
00127 
00128 octave_passwd
00129 octave_passwd::getpwuid (uid_t uid, std::string& msg)
00130 {
00131 #if defined (HAVE_GETPWUID)
00132   msg = std::string ();
00133   return octave_passwd (::getpwuid (uid), msg);
00134 #else
00135   msg = NOT_SUPPORTED ("getpwuid");
00136   return octave_passwd ();
00137 #endif
00138 }
00139 
00140 octave_passwd
00141 octave_passwd::getpwnam (const std::string& nm)
00142 {
00143   std::string msg;
00144   return getpwnam (nm, msg);
00145 }
00146 
00147 octave_passwd
00148 octave_passwd::getpwnam (const std::string& nm, std::string& msg)
00149 {
00150 #if defined (HAVE_GETPWNAM)
00151   msg = std::string ();
00152   return octave_passwd (::getpwnam (nm.c_str ()), msg);
00153 #else
00154   msg = NOT_SUPPORTED ("getpwnam");
00155   return octave_passwd ();
00156 #endif
00157 }
00158 
00159 int
00160 octave_passwd::setpwent (void)
00161 {
00162   std::string msg;
00163   return setpwent (msg);
00164 }
00165 
00166 int
00167 octave_passwd::setpwent (std::string& msg)
00168 {
00169 #if defined (HAVE_SETPWENT)
00170   msg = std::string ();
00171   ::setpwent ();
00172   return 0;
00173 #else
00174   msg = NOT_SUPPORTED ("setpwent");
00175   return -1;
00176 #endif
00177 }
00178 
00179 int
00180 octave_passwd::endpwent (void)
00181 {
00182   std::string msg;
00183   return endpwent (msg);
00184 }
00185 
00186 int
00187 octave_passwd::endpwent (std::string& msg)
00188 {
00189 #if defined (HAVE_ENDPWENT)
00190   msg = std::string ();
00191   ::endpwent ();
00192   return 0;
00193 #else
00194   msg = NOT_SUPPORTED ("endpwent");
00195   return -1;
00196 #endif
00197 }
00198 
00199 octave_passwd::octave_passwd (void *p, std::string& msg)
00200   : pw_name (), pw_passwd (), pw_uid (0), pw_gid (0), pw_gecos (),
00201     pw_dir (), pw_shell (), valid (false)
00202 {
00203 #if defined (HAVE_PWD_H)
00204   msg = std::string ();
00205 
00206   if (p)
00207     {
00208       struct passwd *pw = static_cast<struct passwd *> (p);
00209 
00210       pw_name = pw->pw_name;
00211       pw_passwd = pw->pw_passwd;
00212       pw_uid = pw->pw_uid;
00213       pw_gid = pw->pw_gid;
00214       pw_gecos = pw->pw_gecos;
00215       pw_dir = pw->pw_dir;
00216       pw_shell = pw->pw_shell;
00217 
00218       valid = true;
00219     }
00220 #else
00221   msg = NOT_SUPPORTED ("password functions");
00222 #endif
00223 }
00224 
00225 void
00226 octave_passwd::gripe_invalid (void) const
00227 {
00228   (*current_liboctave_error_handler) ("invalid password object");
00229 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines