[Aptitude-svn-commit] r4288 - in branches/aptitude-0.3/aptitude: .
src/generic/util
Daniel Burrows
dburrows at costa.debian.org
Mon Sep 26 19:14:06 UTC 2005
Author: dburrows
Date: Mon Sep 26 19:14:03 2005
New Revision: 4288
Modified:
branches/aptitude-0.3/aptitude/ChangeLog
branches/aptitude-0.3/aptitude/src/generic/util/util.cc
branches/aptitude-0.3/aptitude/src/generic/util/util.h
Log:
Add a routine to look up the user's home directory.
Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog (original)
+++ branches/aptitude-0.3/aptitude/ChangeLog Mon Sep 26 19:14:03 2005
@@ -1,5 +1,10 @@
2005-09-26 Daniel Burrows <dburrows at debian.org>
+ * src/generic/util/util.cc, src/generic/util/util.h:
+
+ Write a utility routine to look up the current user's home
+ directory from the password database.
+
* src/main.cc:
Automatically activate quiet mode if stdout is not a terminal.
Modified: branches/aptitude-0.3/aptitude/src/generic/util/util.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/util.cc (original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/util.cc Mon Sep 26 19:14:03 2005
@@ -20,7 +20,10 @@
#include "util.h"
#include <ctype.h>
+#include <pwd.h>
#include <stdarg.h>
+#include <sys/types.h>
+#include <unistd.h>
using namespace std;
@@ -109,3 +112,38 @@
return rval;
}
}
+
+string get_homedir()
+{
+ passwd pwbuf;
+ passwd *useless;
+ uid_t myuid = getuid();
+
+#ifdef _SC_GETPW_R_SIZE_MAX
+ long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ auto_ptr<char> buf(new char[bufsize]);
+
+ if(getpwuid_r(myuid, &pwbuf, buf.get(), bufsize, &useless) != 0)
+ return "";
+ else
+ return pwbuf.pw_dir;
+#else
+ long bufsize = 512;
+ bool done = false;
+
+ // The 512 * 512 is an arbitrary cutoff to avoid allocating
+ // unbounded amounts of memory when the system doesn't support a way
+ // to directly determine the largest possible password structure.
+ while(bufsize < 512 * 512)
+ {
+ auto_ptr<char> buf(new char[bufsize]);
+
+ if(getpwuid_r(myuid, &pwbuf, buf.get(), bufsize, &useless) == 0)
+ return pwbuf.pw_dir;
+ else
+ bufsize *= 2;
+ }
+
+ return "";
+#endif
+}
Modified: branches/aptitude-0.3/aptitude/src/generic/util/util.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/util.h (original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/util.h Mon Sep 26 19:14:03 2005
@@ -37,6 +37,12 @@
std::wstring vswsprintf(const wchar_t *format, va_list ap);
+/** \return the home directory of the current user as given in the
+ * password database. If no home directory can be looked up,
+ * returns an empty string.
+ */
+std::string get_homedir();
+
/** Compare pairs, with (a,b) considered eqivalent to (b,a). */
template<typename T>
struct orderless_lt
More information about the Aptitude-svn-commit
mailing list