Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2023 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : #include <config.h>
21 :
22 : #include <stdio.h>
23 :
24 : #if HAVE_STDLIB_H
25 : #include <stdlib.h>
26 : #endif /* HAVE_STDLIB_H */
27 :
28 : #if HAVE_STRING_H
29 : #include <string.h>
30 : #endif /* HAVE_STRING_H */
31 :
32 : #include <freetds/windows.h>
33 : #include <freetds/macros.h>
34 : #include <freetds/sysdep_private.h>
35 : #include <freetds/utils/path.h>
36 :
37 : /**
38 : * Return filename from HOME directory
39 : * @return allocated string or NULL if error
40 : */
41 : tds_dir_char *
42 3023 : tds_get_home_file(const tds_dir_char *file)
43 : {
44 : tds_dir_char *home, *path;
45 :
46 3023 : home = tds_get_homedir();
47 3023 : if (!home)
48 : return NULL;
49 3023 : path = tds_join_path(home, file);
50 3023 : free(home);
51 3023 : return path;
52 : }
53 :
54 : tds_dir_char*
55 3039 : tds_join_path(const tds_dir_char *dir, const tds_dir_char *file)
56 : {
57 : tds_dir_char *ret;
58 :
59 3039 : ret = tds_new(tds_dir_char, tds_dir_len(dir) + tds_dir_len(file) + 4);
60 3039 : if (!ret)
61 : return ret;
62 :
63 3039 : if (dir[0] == '\0') {
64 8 : ret[0] = '\0';
65 : } else
66 : #ifndef _WIN32
67 : {
68 3031 : strcpy(ret, dir);
69 3031 : strcat(ret, TDS_SDIR_SEPARATOR);
70 : }
71 3039 : strcat(ret, file);
72 : #else
73 : {
74 : wcscpy(ret, dir);
75 : wcscat(ret, TDS_SDIR_SEPARATOR);
76 : }
77 : wcscat(ret, file);
78 : #endif
79 3039 : return ret;
80 : }
81 :
82 : #ifdef _WIN32
83 : tds_dir_char *
84 : tds_dir_from_cstr(const char *path)
85 : {
86 : /* include NUL terminator so output string will be terminated and MultiByteToWideChar won't
87 : * return 0 on succesful empty strings */
88 : size_t len = strlen(path) + 1;
89 : tds_dir_char *res = tds_new(tds_dir_char, len);
90 : if (res) {
91 : int out_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, len, res, len);
92 : if (!out_len)
93 : out_len = MultiByteToWideChar(CP_ACP, 0, path, len, res, len);
94 : if (!out_len)
95 : TDS_ZERO_FREE(res);
96 : }
97 : return res;
98 : }
99 : #endif
|