1 /* Cygwin support routines.
2 Copyright (C) 2011-2013 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include "character.h"
27 fchdir_unwind (Lisp_Object dir_fd
)
29 (void) fchdir (XFASTINT (dir_fd
));
30 (void) close (XFASTINT (dir_fd
));
35 chdir_to_default_directory ()
38 int old_cwd_fd
= open (".", O_RDONLY
| O_DIRECTORY
);
41 error ("could not open current directory: %s", strerror (errno
));
43 record_unwind_protect (fchdir_unwind
, make_number (old_cwd_fd
));
45 new_cwd
= Funhandled_file_name_directory (
46 Fexpand_file_name (build_string ("."), Qnil
));
47 if (!STRINGP (new_cwd
))
48 new_cwd
= build_string ("/");
50 if (chdir (SDATA (ENCODE_FILE (new_cwd
))))
51 error ("could not chdir: %s", strerror (errno
));
55 conv_filename_to_w32_unicode (Lisp_Object in
, int absolute_p
)
57 ssize_t converted_len
;
58 Lisp_Object converted
;
60 int count
= SPECPDL_INDEX ();
62 chdir_to_default_directory ();
64 flags
= CCP_POSIX_TO_WIN_W
;
66 flags
|= CCP_RELATIVE
;
69 in
= ENCODE_FILE (in
);
71 converted_len
= cygwin_conv_path (flags
, SDATA (in
), NULL
, 0);
72 if (converted_len
< 2)
73 error ("cygwin_conv_path: %s", strerror (errno
));
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
));
80 return unbind_to (count
, converted
);
84 conv_filename_from_w32_unicode (const wchar_t* in
, int absolute_p
)
86 ssize_t converted_len
;
87 Lisp_Object converted
;
89 int count
= SPECPDL_INDEX ();
91 chdir_to_default_directory ();
93 flags
= CCP_WIN_W_TO_POSIX
;
95 flags
|= CCP_RELATIVE
;
98 converted_len
= cygwin_conv_path (flags
, in
, NULL
, 0);
99 if (converted_len
< 1)
100 error ("cygwin_conv_path: %s", strerror (errno
));
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
));
106 return unbind_to (count
, DECODE_FILE (converted
));
109 DEFUN ("cygwin-convert-file-name-to-windows",
110 Fcygwin_convert_file_name_to_windows
,
111 Scygwin_convert_file_name_to_windows
,
113 doc
: /* Convert a Cygwin file name FILE to a Windows-style file name.
114 If ABSOLUTE-P is non-nil, return an absolute file name.
115 For the reverse operation, see `cygwin-convert-file-name-from-windows'. */)
116 (Lisp_Object file
, Lisp_Object absolute_p
)
118 return from_unicode (
119 conv_filename_to_w32_unicode (file
, EQ (absolute_p
, Qnil
) ? 0 : 1));
122 DEFUN ("cygwin-convert-file-name-from-windows",
123 Fcygwin_convert_file_name_from_windows
,
124 Scygwin_convert_file_name_from_windows
,
126 doc
: /* Convert a Windows-style file name FILE to a Cygwin file name.
127 If ABSOLUTE-P is non-nil, return an absolute file name.
128 For the reverse operation, see `cygwin-convert-file-name-to-windows'. */)
129 (Lisp_Object file
, Lisp_Object absolute_p
)
131 return conv_filename_from_w32_unicode (to_unicode (file
, &file
),
132 EQ (absolute_p
, Qnil
) ? 0 : 1);
136 syms_of_cygw32 (void)
138 defsubr (&Scygwin_convert_file_name_from_windows
);
139 defsubr (&Scygwin_convert_file_name_to_windows
);