Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/fsutil/main.c
4387 views
1
/*
2
* Copyright 2016 Austin English
3
* Copyright 2016 Michael Müller
4
*
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
9
*
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
14
*
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18
*/
19
20
#include <windows.h>
21
22
#include "wine/debug.h"
23
#include "resources.h"
24
25
WINE_DEFAULT_DEBUG_CHANNEL(fsutil);
26
27
static void output_write(const WCHAR *str, DWORD wlen)
28
{
29
DWORD count;
30
31
if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL))
32
{
33
DWORD len;
34
char *msgA;
35
36
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
37
* back to WriteFile() with OEM code page.
38
*/
39
len = WideCharToMultiByte(GetOEMCP(), 0, str, wlen, NULL, 0, NULL, NULL);
40
msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char));
41
if (!msgA) return;
42
43
WideCharToMultiByte(GetOEMCP(), 0, str, wlen, msgA, len, NULL, NULL);
44
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
45
HeapFree(GetProcessHeap(), 0, msgA);
46
}
47
}
48
49
static int WINAPIV output_string(int msg, ...)
50
{
51
WCHAR out[8192];
52
va_list arguments;
53
int len;
54
55
va_start(arguments, msg);
56
len = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE, NULL, msg, 0, out, ARRAY_SIZE(out), &arguments);
57
va_end(arguments);
58
59
if (len == 0 && GetLastError() != NO_ERROR)
60
WINE_FIXME("Could not format string: le=%lu, msg=%d\n", GetLastError(), msg);
61
else
62
output_write(out, len);
63
64
return 0;
65
}
66
67
static BOOL output_error_string(DWORD error)
68
{
69
LPWSTR pBuffer;
70
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
71
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
72
NULL, error, 0, (LPWSTR)&pBuffer, 0, NULL))
73
{
74
output_write(pBuffer, lstrlenW(pBuffer));
75
LocalFree(pBuffer);
76
return TRUE;
77
}
78
return FALSE;
79
}
80
81
static int create_hardlink(int argc, WCHAR *argv[])
82
{
83
if (argc != 5)
84
{
85
output_string(STRING_HARDLINK_CREATE_USAGE);
86
return 1;
87
}
88
89
if (CreateHardLinkW(argv[3], argv[4], NULL))
90
return 0;
91
92
output_error_string(GetLastError());
93
return 1;
94
}
95
96
static int hardlink(int argc, WCHAR *argv[])
97
{
98
int ret = 0;
99
100
if (argc > 2)
101
{
102
if (!wcsicmp(argv[2], L"create"))
103
return create_hardlink(argc, argv);
104
else
105
{
106
FIXME("unsupported parameter %s\n", debugstr_w(argv[2]));
107
ret = 1;
108
}
109
}
110
111
output_string(STRING_HARDLINK_USAGE);
112
return ret;
113
}
114
115
int __cdecl wmain(int argc, WCHAR *argv[])
116
{
117
int i, ret = 0;
118
119
TRACE("Command line:");
120
for (i = 0; i < argc; i++)
121
TRACE(" %s", debugstr_w(argv[i]));
122
TRACE("\n");
123
124
if (argc > 1)
125
{
126
if (!wcsicmp(argv[1], L"hardlink"))
127
return hardlink(argc, argv);
128
else
129
{
130
FIXME("unsupported command %s\n", debugstr_w(argv[1]));
131
ret = 1;
132
}
133
}
134
135
output_string(STRING_USAGE);
136
return ret;
137
}
138
139