summaryrefslogtreecommitdiff
path: root/testing/toxic/configdir.c
blob: 18e211ce965d7a03c64f38c250f1481e5298243c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 *  Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  Tox 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Tox 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 Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#ifdef WIN32
#include <shlobj.h>
#include <direct.h>
#else /* WIN32 */
#include <unistd.h>
#include <pwd.h>
#endif /* WIN32 */

#include "configdir.h"

/**
 * @brief Get the users config directory.
 *
 * This is without a trailing slash.
 *
 * @return The users config dir or NULL on error.
 */
char *get_user_config_dir(void)
{
    char *user_config_dir;
#ifdef WIN32
    char appdata[MAX_PATH];
    BOOL ok;

    ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE);
    if (!ok) {
        return NULL;
    }

    user_config_dir = strdup(appdata);

    return user_config_dir;

#else /* WIN32 */

#ifndef NSS_BUFLEN_PASSWD
#define NSS_BUFLEN_PASSWD 4096
#endif /* NSS_BUFLEN_PASSWD */

    struct passwd pwd;
    struct passwd *pwdbuf;
    const char *home;
    char buf[NSS_BUFLEN_PASSWD];
    size_t len;
    int rc;

    rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
    if (rc == 0) {
        home = pwd.pw_dir;
    } else {
        home = getenv("HOME");
        if (home == NULL) {
            return NULL;
        }
        /* env variables can be tainted */
        snprintf(buf, sizeof(buf), "%s", home);
        home = buf;
    }

# if defined(__APPLE__)
    len = strlen(home) + strlen("/Library/Application Support") + 1;
    user_config_dir = malloc(len);
    if (user_config_dir == NULL) {
        return NULL;
    }

    snprintf(user_config_dir, len, "%s/Library/Application Support", home);
# else /* __APPLE__ */
    len = strlen(home) + strlen("/.config") + 1;
    user_config_dir = malloc(len);
    if (user_config_dir == NULL) {
        return NULL;
    }

    snprintf(user_config_dir, len, "%s/.config", home);
# endif /* __APPLE__ */

    return user_config_dir;
#undef NSS_BUFLEN_PASSWD
#endif /* WIN32 */
}

/*
 * Creates the config directory.
 */
int create_user_config_dir(char *path)
{ 

  int mkdir_err;

  #ifdef WIN32

  char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
  strcpy(fullpath, path);
  strcat(fullpath, CONFIGDIR);

  mkdir_err = _mkdir(fullpath);
  struct __stat64 buf;
  if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
    free(fullpath);
    return -1;
  }

  #else

  mkdir_err = mkdir(path, 0700);
  struct stat buf;

  if(mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) {
    return -1;
  }

  char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
  strcpy(fullpath, path);
  strcat(fullpath, CONFIGDIR);

  mkdir_err = mkdir(fullpath, 0700);

  if(mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
    free(fullpath);
    return -1;
  }
    
  #endif
  free(fullpath);
  return 0;
}