# HG changeset patch # User jwe # Date 862873140 0 # Node ID dddc1b5c324e02e199e6d66fe05585017e9d4c18 # Parent b53d9b2acd88a1f71ea7a439769caa5437d24693 [project @ 1997-05-05 22:56:37 by jwe] diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,10 @@ +Mon May 5 17:53:01 1997 John W. Eaton + + * file-ops.cc: (file_ops::tilde_expand): Use new octave_passwd class. + * oct-env.cc (octave_env::do_get_user_name): Likewise. + + * oct-passwd.h, oct-passwd.cc: New files. + Sun May 4 22:17:08 1997 John W. Eaton * statdefs.h: Only include sys/types.h if HAVE_SYS_STAT_H is defined. diff --git a/liboctave/Makefile.in b/liboctave/Makefile.in --- a/liboctave/Makefile.in +++ b/liboctave/Makefile.in @@ -47,8 +47,8 @@ data-conv.h dir-ops.h file-ops.h file-stat.h getopt.h \ glob-match.h idx-vector.h lo-ieee.h lo-mappers.h lo-sysdep.h \ lo-utils.h mach-info.h oct-alloc.h oct-cmplx.h oct-env.h \ - oct-math.h pathsearch.h prog-args.h statdefs.h str-vec.h \ - sun-utils.h sysdir.h \ + oct-math.h oct-group.h oct-passwd.h pathsearch.h prog-args.h \ + statdefs.h str-vec.h sun-utils.h sysdir.h \ $(MATRIX_INC) \ $(MX_OP_INC) @@ -81,8 +81,8 @@ file-ops.cc file-stat.cc filemode.c gamma.c getopt.c getopt1.c \ glob-match.cc idx-vector.cc lgamma.c lo-ieee.cc lo-mappers.cc \ lo-sysdep.cc lo-utils.cc mach-info.cc mkdir.c oct-alloc.cc \ - oct-env.cc pathsearch.cc prog-args.cc rename.c rmdir.c \ - str-vec.cc tempname.c tempnam.c \ + oct-env.cc oct-group.cc oct-passwd.cc pathsearch.cc \ + prog-args.cc rename.c rmdir.c str-vec.cc tempname.c tempnam.c \ $(TEMPLATE_SRC) \ $(TI_SRC) \ $(MATRIX_SRC) \ diff --git a/liboctave/file-ops.cc b/liboctave/file-ops.cc --- a/liboctave/file-ops.cc +++ b/liboctave/file-ops.cc @@ -39,13 +39,10 @@ #include #endif -#ifdef HAVE_PWD_H -#include -#endif - #include "file-ops.h" #include "lo-error.h" #include "oct-env.h" +#include "oct-passwd.h" #include "statdefs.h" #include "str-vec.h" @@ -181,8 +178,6 @@ { string expansion = name; -#if defined (HAVE_PWD_H) - // If no leading tilde, do nothing. size_t beg = name.find_first_not_of (" \t"); @@ -218,12 +213,11 @@ string user = name.substr (beg+1, len); - struct passwd *p - = static_cast (::getpwnam (user.c_str ())); + octave_passwd pw = octave_passwd::getpwnam (user); // If no such user, just use `.'. - string home = p ? p->pw_dir : "."; + string home = pw.empty () ? : string (".") : pw.dir (); expansion = string (" ", beg) + home; @@ -232,8 +226,6 @@ } } -#endif - return expansion; } diff --git a/liboctave/oct-env.cc b/liboctave/oct-env.cc --- a/liboctave/oct-env.cc +++ b/liboctave/oct-env.cc @@ -50,14 +50,11 @@ #include #endif -#ifdef HAVE_PWD_H -#include -#endif - #include "lo-error.h" #include "lo-sysdep.h" #include "lo-utils.h" #include "oct-env.h" +#include "oct-passwd.h" octave_env::octave_env (void) : follow_symbolic_links (true), verbatim_pwd (true), @@ -374,9 +371,9 @@ if (user_name.empty ()) { - struct passwd *entry = getpwuid (getuid ()); + octave_passwd pw = octave_passwd::getpwuid (getuid ()); - user_name = entry ? entry->pw_name : "I have no name!"; + user_name = pw.empty () ? string ("I have no name!") : pw.name (); } return user_name; diff --git a/liboctave/oct-passwd.cc b/liboctave/oct-passwd.cc new file mode 100644 --- /dev/null +++ b/liboctave/oct-passwd.cc @@ -0,0 +1,192 @@ +/* + +Copyright (C) 1996, 1997 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#ifdef HAVE_PWD_H +#include +#endif + +#include "lo-error.h" +#include "oct-passwd.h" + +string +octave_passwd::name (void) const +{ + if (! ok ()) + gripe_invalid (); + + return pw_name; +} + +string +octave_passwd::passwd (void) const +{ + if (! ok ()) + gripe_invalid (); + + return pw_passwd; +} + +uid_t +octave_passwd::uid (void) const +{ + if (! ok ()) + gripe_invalid (); + + return pw_uid; +} + +gid_t +octave_passwd::gid (void) const +{ + if (! ok ()) + gripe_invalid (); + + return pw_gid; +} + +string +octave_passwd::gecos (void) const +{ + if (! ok ()) + gripe_invalid (); + + return pw_gecos; +} + +string +octave_passwd::dir (void) const +{ + if (! ok ()) + gripe_invalid (); + + return pw_dir; +} + +string +octave_passwd::shell (void) const +{ + if (! ok ()) + gripe_invalid (); + + return pw_shell; +} + +octave_passwd +octave_passwd::getpwent (void) +{ +#ifdef HAVE_GETPWENT + return octave_passwd (::getpwent ()); +#else + gripe_not_implemented ("getpwent"); + + return octave_passwd (); +#endif +} + +octave_passwd +octave_passwd::getpwuid (uid_t uid) +{ +#ifdef HAVE_GETPWUID + return octave_passwd (::getpwuid (uid)); +#else + gripe_not_implemented ("getpwuid"); + + return octave_passwd (); +#endif +} + +octave_passwd +octave_passwd::getpwnam (const string& nm) +{ +#ifdef HAVE_GETPWNAM + return octave_passwd (::getpwnam (nm.c_str ())); +#else + gripe_not_implemented ("getpwnam"); + + return octave_passwd (); +#endif +} + +void +octave_passwd::setpwent (void) +{ +#ifdef HAVE_SETPWENT + ::setpwent (); +#else + gripe_not_implemented ("setpwent"); +#endif +} + +void +octave_passwd::endpwent (void) +{ +#ifdef HAVE_ENDPWENT + ::endpwent (); +#else + gripe_not_implemented ("endpwent"); +#endif +} + +octave_passwd::octave_passwd (void *p) + : pw_name (), pw_passwd (), pw_uid (0), pw_gid (0), pw_gecos (), + pw_dir (), pw_shell (), valid (false) +{ +#ifdef HAVE_PWD_H + if (p) + { + struct passwd *pw = static_cast (p); + + pw_name = pw->pw_name; + pw_passwd = pw->pw_passwd; + pw_uid = pw->pw_uid; + pw_gid = pw->pw_gid; + pw_gecos = pw->pw_gecos; + pw_dir = pw->pw_dir; + pw_shell = pw->pw_shell; + + valid = true; + } +#endif +} + +void +octave_passwd::gripe_invalid (void) const +{ + (*current_liboctave_error_handler) ("invalid password object"); +} + +void +octave_passwd::gripe_not_supported (const string& fcn) const +{ + (*current_liboctave_error_handler) + ("%s: not supported on this system", fcn.c_str ()); +} + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/ diff --git a/liboctave/oct-passwd.h b/liboctave/oct-passwd.h new file mode 100644 --- /dev/null +++ b/liboctave/oct-passwd.h @@ -0,0 +1,138 @@ +/* + +Copyright (C) 1996, 1997 John W. Eaton + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, write to the Free +Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#if !defined (octave_passwd_h) +#define octave_passwd_h 1 + +#include + +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +class +octave_passwd +{ +public: + + octave_passwd (void) + : pw_name (), pw_passwd (), pw_uid (0), pw_gid (0), pw_gecos (), + pw_dir (), pw_shell (), valid (false) + { } + + octave_passwd (const octave_passwd& pw) + : pw_name (pw.pw_name), pw_passwd (pw.pw_passwd), + pw_uid (pw.pw_uid), pw_gid (pw.pw_gid), pw_gecos (pw.pw_gecos), + pw_dir (pw.pw_dir), pw_shell (pw.pw_shell), valid (pw.valid) + { } + + octave_passwd& operator = (const octave_passwd& pw) + { + if (this != &pw) + { + pw_name = pw.pw_name; + pw_passwd = pw.pw_passwd; + pw_uid = pw.pw_uid; + pw_gid = pw.pw_gid; + pw_gecos = pw.pw_gecos; + pw_dir = pw.pw_dir; + pw_shell = pw.pw_shell; + valid = pw.valid; + } + + return *this; + } + + ~octave_passwd (void) { } + + string name (void) const; + + string passwd (void) const; + + uid_t uid (void) const; + + gid_t gid (void) const; + + string gecos (void) const; + + string dir (void) const; + + string shell (void) const; + + bool ok (void) const { return valid; } + + operator void* () const + { return ok () + ? static_cast (-1) : static_cast (0); } + + static octave_passwd getpwent (void); + + static octave_passwd getpwuid (uid_t uid); + + static octave_passwd getpwnam (const string& nm); + + static void setpwent (void); + + static void endpwent (void); + +private: + + // User name. + string pw_name; + + // Encrypted password. + string pw_passwd; + + // Numeric user id. + uid_t pw_uid; + + // Numeric group id. + gid_t pw_gid; + + // Miscellaneous junk. + string pw_gecos; + + // Home directory. + string pw_dir; + + // Login shell. + string pw_shell; + + // Flag that says whether we have been properly initialized. + bool valid; + + // This is how we will create an octave_passwd object from a pointer + // to a struct passwd. + octave_passwd (void *); + + void gripe_invalid (void) const; + + void gripe_not_supported (const string& fcn) const; +}; + +#endif + +/* +;;; Local Variables: *** +;;; mode: C++ *** +;;; End: *** +*/