Rename cygwin_convert_path* to cygwin_convert_file_name*
[bpt/emacs.git] / src / cygw32.c
CommitLineData
f701ab72 1/* Cygwin support routines.
245dc6da 2 Copyright (C) 2011-2012 Free Software Foundation, Inc.
f701ab72
DC
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19
20#include "cygw32.h"
21#include "character.h"
22#include "buffer.h"
23#include <unistd.h>
24#include <fcntl.h>
f701ab72
DC
25
26static Lisp_Object
27fchdir_unwind (Lisp_Object dir_fd)
28{
29 (void) fchdir (XFASTINT (dir_fd));
30 (void) close (XFASTINT (dir_fd));
31 return Qnil;
32}
33
34static void
35chdir_to_default_directory ()
36{
37 Lisp_Object new_cwd;
38 int old_cwd_fd = open (".", O_RDONLY | O_DIRECTORY);
39
40 if (old_cwd_fd == -1)
41 error ("could not open current directory: %s", strerror (errno));
42
43 record_unwind_protect (fchdir_unwind, make_number (old_cwd_fd));
44
45 new_cwd = Funhandled_file_name_directory (
46 Fexpand_file_name (build_string ("."), Qnil));
47 if (!STRINGP (new_cwd))
48 new_cwd = build_string ("/");
49
50 if (chdir (SDATA (ENCODE_FILE (new_cwd))))
51 error ("could not chdir: %s", strerror (errno));
52}
53
54static Lisp_Object
55conv_filename_to_w32_unicode (Lisp_Object in, int absolute_p)
56{
57 ssize_t converted_len;
58 Lisp_Object converted;
59 unsigned flags;
60 int count = SPECPDL_INDEX ();
61
62 chdir_to_default_directory ();
63
64 flags = CCP_POSIX_TO_WIN_W;
65 if (!absolute_p) {
66 flags |= CCP_RELATIVE;
67 }
68
69 in = ENCODE_FILE (in);
70
71 converted_len = cygwin_conv_path (flags, SDATA (in), NULL, 0);
72 if (converted_len < 2)
73 error ("cygwin_conv_path: %s", strerror (errno));
74
75 converted = make_uninit_string (converted_len - 1);
76 if (cygwin_conv_path (flags, SDATA (in),
77 SDATA (converted), converted_len))
78 error ("cygwin_conv_path: %s", strerror (errno));
79
80 return unbind_to (count, converted);
81}
82
83static Lisp_Object
84conv_filename_from_w32_unicode (const wchar_t* in, int absolute_p)
85{
86 ssize_t converted_len;
87 Lisp_Object converted;
88 unsigned flags;
89 int count = SPECPDL_INDEX ();
90
91 chdir_to_default_directory ();
92
93 flags = CCP_WIN_W_TO_POSIX;
94 if (!absolute_p) {
95 flags |= CCP_RELATIVE;
96 }
97
98 converted_len = cygwin_conv_path (flags, in, NULL, 0);
99 if (converted_len < 1)
100 error ("cygwin_conv_path: %s", strerror (errno));
101
102 converted = make_uninit_string (converted_len - 1 /*subtract terminator*/);
103 if (cygwin_conv_path (flags, in, SDATA (converted), converted_len))
104 error ("cygwin_conv_path: %s", strerror (errno));
105
106 return unbind_to (count, DECODE_FILE (converted));
107}
108
6e9f7997
DC
109DEFUN ("cygwin-convert-file-name-to-windows",
110 Fcygwin_convert_file_name_to_windows,
111 Scygwin_convert_file_name_to_windows,
f701ab72 112 1, 2, 0,
6e9f7997
DC
113 doc: /* Convert PATH to a Windows path. If ABSOLUTE-P is
114non-nil, return an absolute path.*/)
f701ab72
DC
115 (Lisp_Object path, Lisp_Object absolute_p)
116{
117 return from_unicode (
fcf14875 118 conv_filename_to_w32_unicode (path, EQ (absolute_p, Qnil) ? 0 : 1));
f701ab72
DC
119}
120
6e9f7997
DC
121DEFUN ("cygwin-convert-file-name-from-windows",
122 Fcygwin_convert_file_name_from_windows,
123 Scygwin_convert_file_name_from_windows,
f701ab72
DC
124 1, 2, 0,
125 doc: /* Convert a Windows path to a Cygwin path. If ABSOLUTE-P
6e9f7997 126is non-nil, return an absolute path.*/)
f701ab72
DC
127 (Lisp_Object path, Lisp_Object absolute_p)
128{
129 return conv_filename_from_w32_unicode (to_unicode (path, &path),
fcf14875 130 EQ (absolute_p, Qnil) ? 0 : 1);
f701ab72
DC
131}
132
133void
134syms_of_cygw32 (void)
135{
6e9f7997
DC
136 defsubr (&Scygwin_convert_file_name_from_windows);
137 defsubr (&Scygwin_convert_file_name_to_windows);
f701ab72 138}