2012-03-02 16:35:02 +04:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
2012-03-02 16:47:45 +04:00
|
|
|
* Copyright (c) 2012 by Andreas Schneider <asn@cryptomilk.org>
|
2012-03-02 16:35:02 +04:00
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-03-02 19:47:34 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-03-02 16:35:02 +04:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2013-03-05 13:45:33 +04:00
|
|
|
#include <stdio.h>
|
2012-03-02 16:35:02 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#if _WIN32
|
|
|
|
# ifndef _WIN32_IE
|
|
|
|
# define _WIN32_IE 0x0501 // SHGetSpecialFolderPath
|
|
|
|
# endif
|
|
|
|
# include <shlobj.h>
|
|
|
|
#else /* _WIN32 */
|
|
|
|
# include <pwd.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
#include "c_lib.h"
|
|
|
|
#include "csync_misc.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
char *csync_get_user_home_dir(void) {
|
|
|
|
char tmp[MAX_PATH] = {0};
|
|
|
|
char *szPath = NULL;
|
|
|
|
|
|
|
|
if (SHGetSpecialFolderPathA(NULL, tmp, CSIDL_PROFILE, TRUE)) {
|
2012-03-02 17:45:31 +04:00
|
|
|
szPath = c_malloc(strlen(tmp) + 1);
|
2012-03-02 16:35:02 +04:00
|
|
|
if (szPath == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(szPath, tmp);
|
|
|
|
return szPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *csync_get_local_username(void) {
|
|
|
|
DWORD size = 0;
|
|
|
|
char *user;
|
|
|
|
|
|
|
|
/* get the size */
|
|
|
|
GetUserName(NULL, &size);
|
|
|
|
|
2012-03-02 17:45:31 +04:00
|
|
|
user = (char *) c_malloc(size);
|
2012-03-02 16:35:02 +04:00
|
|
|
if (user == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetUserName(user, &size)) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* ************* !WIN32 ************ */
|
|
|
|
|
|
|
|
#ifndef NSS_BUFLEN_PASSWD
|
|
|
|
#define NSS_BUFLEN_PASSWD 4096
|
|
|
|
#endif /* NSS_BUFLEN_PASSWD */
|
|
|
|
|
|
|
|
char *csync_get_user_home_dir(void) {
|
2013-03-05 13:24:34 +04:00
|
|
|
char home[PATH_MAX] = {0};
|
|
|
|
const char *envp;
|
2012-03-02 16:35:02 +04:00
|
|
|
struct passwd pwd;
|
|
|
|
struct passwd *pwdbuf;
|
|
|
|
char buf[NSS_BUFLEN_PASSWD];
|
|
|
|
int rc;
|
|
|
|
|
2013-03-05 13:24:34 +04:00
|
|
|
envp = getenv("HOME");
|
|
|
|
if (envp != NULL) {
|
|
|
|
snprintf(home, sizeof(home), "%s", envp);
|
|
|
|
if (home[0] != '\0') {
|
|
|
|
return c_strdup(home);
|
|
|
|
}
|
2012-03-18 18:29:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Still nothing found, read the password file */
|
2012-03-02 16:35:02 +04:00
|
|
|
rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
|
|
|
|
if (rc != 0) {
|
2013-03-05 13:24:34 +04:00
|
|
|
return c_strdup(pwd.pw_dir);
|
2012-03-02 16:35:02 +04:00
|
|
|
}
|
|
|
|
|
2013-03-05 13:24:34 +04:00
|
|
|
return NULL;
|
2012-03-02 16:35:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *csync_get_local_username(void) {
|
|
|
|
struct passwd pwd;
|
|
|
|
struct passwd *pwdbuf;
|
|
|
|
char buf[NSS_BUFLEN_PASSWD];
|
|
|
|
char *name;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
|
|
|
|
if (rc != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = c_strdup(pwd.pw_name);
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ************* WIN32 ************ */
|
2012-03-02 17:54:53 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_FNMATCH
|
|
|
|
#include <fnmatch.h>
|
|
|
|
|
|
|
|
int csync_fnmatch(__const char *__pattern, __const char *__name, int __flags) {
|
|
|
|
return fnmatch(__pattern, __name, __flags);
|
|
|
|
}
|
|
|
|
|
2012-03-02 19:47:34 +04:00
|
|
|
#else /* HAVE_FNMATCH */
|
2012-03-02 17:54:53 +04:00
|
|
|
|
|
|
|
#include <shlwapi.h>
|
|
|
|
int csync_fnmatch(__const char *__pattern, __const char *__name, int __flags) {
|
|
|
|
if(PathMatchSpec(__name, __pattern))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-02 19:47:34 +04:00
|
|
|
#endif /* HAVE_FNMATCH */
|