2 * Task termination utility
4 * Copyright 2008 Andrew Riedi
5 * Copyright 2010 Andrew Nguyen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <wine/debug.h>
24 #include <wine/unicode.h>
28 WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
30 int force_termination;
33 unsigned int task_count;
35 static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
39 WCHAR msg_buffer[8192];
41 wlen = vsprintfW(msg_buffer, msg, va_args);
43 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
49 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
51 msgA = HeapAlloc(GetProcessHeap(), 0, len);
55 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
57 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
58 HeapFree(GetProcessHeap(), 0, msgA);
64 static int taskkill_printfW(const WCHAR *msg, ...)
69 va_start(va_args, msg);
70 len = taskkill_vprintfW(msg, va_args);
76 static int taskkill_message_printfW(int msg, ...)
79 WCHAR msg_buffer[8192];
82 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
83 sizeof(msg_buffer)/sizeof(WCHAR));
85 va_start(va_args, msg);
86 len = taskkill_vprintfW(msg_buffer, va_args);
92 static int taskkill_message(int msg)
94 static const WCHAR formatW[] = {'%','s',0};
95 WCHAR msg_buffer[8192];
97 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
98 sizeof(msg_buffer)/sizeof(WCHAR));
100 return taskkill_printfW(formatW, msg_buffer);
103 static BOOL add_to_task_list(WCHAR *name)
105 static unsigned int list_size = 16;
109 task_list = HeapAlloc(GetProcessHeap(), 0,
110 list_size * sizeof(*task_list));
114 else if (task_count == list_size)
119 realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
120 list_size * sizeof(*task_list));
124 task_list = realloc_list;
127 task_list[task_count++] = name;
131 /* FIXME Argument processing does not match behavior observed on Windows.
132 * Stringent argument counting and processing is performed, and unrecognized
133 * options are detected as parameters when placed after options that accept one. */
134 static BOOL process_arguments(int argc, WCHAR *argv[])
136 static const WCHAR slashForceTerminate[] = {'/','f',0};
137 static const WCHAR slashImage[] = {'/','i','m',0};
138 static const WCHAR slashPID[] = {'/','p','i','d',0};
139 static const WCHAR slashHelp[] = {'/','?',0};
144 BOOL has_im = 0, has_pid = 0;
146 /* Only the lone help option is recognized. */
147 if (argc == 2 && !strcmpW(slashHelp, argv[1]))
149 taskkill_message(STRING_USAGE);
153 for (i = 1; i < argc; i++)
155 int got_im = 0, got_pid = 0;
157 if (!strcmpiW(slashForceTerminate, argv[i]))
158 force_termination = 1;
159 /* Options /IM and /PID appear to behave identically, except for
160 * the fact that they cannot be specified at the same time. */
161 else if ((got_im = !strcmpiW(slashImage, argv[i])) ||
162 (got_pid = !strcmpiW(slashPID, argv[i])))
166 taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
167 taskkill_message(STRING_USAGE);
171 if (got_im) has_im = 1;
172 if (got_pid) has_pid = 1;
174 if (has_im && has_pid)
176 taskkill_message(STRING_MUTUAL_EXCLUSIVE);
177 taskkill_message(STRING_USAGE);
181 if (!add_to_task_list(argv[i + 1]))
187 taskkill_message(STRING_INVALID_OPTION);
188 taskkill_message(STRING_USAGE);
195 taskkill_message(STRING_MISSING_OPTION);
196 taskkill_message(STRING_USAGE);
203 int wmain(int argc, WCHAR *argv[])
205 if (!process_arguments(argc, argv))
207 HeapFree(GetProcessHeap(), 0, task_list);
211 WINE_FIXME("taskkill.exe functionality is not implemented\n");
213 HeapFree(GetProcessHeap(), 0, task_list);