00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CArchFileUnix.h"
00020 #include <stdio.h>
00021 #include <unistd.h>
00022 #include <pwd.h>
00023 #include <sys/types.h>
00024 #include <cstring>
00025
00026
00027
00028
00029
00030 CArchFileUnix::CArchFileUnix()
00031 {
00032
00033 }
00034
00035 CArchFileUnix::~CArchFileUnix()
00036 {
00037
00038 }
00039
00040 const char*
00041 CArchFileUnix::getBasename(const char* pathname)
00042 {
00043 if (pathname == NULL) {
00044 return NULL;
00045 }
00046
00047 const char* basename = strrchr(pathname, '/');
00048 if (basename != NULL) {
00049 return basename + 1;
00050 }
00051 else {
00052 return pathname;
00053 }
00054 }
00055
00056 std::string
00057 CArchFileUnix::getUserDirectory()
00058 {
00059 char* buffer = NULL;
00060 std::string dir;
00061 #if HAVE_GETPWUID_R
00062 struct passwd pwent;
00063 struct passwd* pwentp;
00064 #if defined(_SC_GETPW_R_SIZE_MAX)
00065 long size = sysconf(_SC_GETPW_R_SIZE_MAX);
00066 if (size == -1) {
00067 size = BUFSIZ;
00068 }
00069 #else
00070 long size = BUFSIZ;
00071 #endif
00072 buffer = new char[size];
00073 getpwuid_r(getuid(), &pwent, buffer, size, &pwentp);
00074 #else
00075 struct passwd* pwentp = getpwuid(getuid());
00076 #endif
00077 if (pwentp != NULL && pwentp->pw_dir != NULL) {
00078 dir = pwentp->pw_dir;
00079 }
00080 delete[] buffer;
00081 return dir;
00082 }
00083
00084 std::string
00085 CArchFileUnix::getSystemDirectory()
00086 {
00087 return "/etc";
00088 }
00089
00090 std::string
00091 CArchFileUnix::concatPath(const std::string& prefix,
00092 const std::string& suffix)
00093 {
00094 std::string path;
00095 path.reserve(prefix.size() + 1 + suffix.size());
00096 path += prefix;
00097 if (path.size() == 0 || path[path.size() - 1] != '/') {
00098 path += '/';
00099 }
00100 path += suffix;
00101 return path;
00102 }