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