Test shows that native always outputs leading zeros for file time information, e.g. `2009/01/03 05:07`.
From: Akihiro Sagawa sagawa.aki@gmail.com
--- programs/cmd/tests/Makefile.in | 3 +- programs/cmd/tests/directory.c | 133 +++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 programs/cmd/tests/directory.c
diff --git a/programs/cmd/tests/Makefile.in b/programs/cmd/tests/Makefile.in index 87942fedcf9..9f50cbe042b 100644 --- a/programs/cmd/tests/Makefile.in +++ b/programs/cmd/tests/Makefile.in @@ -1,6 +1,7 @@ TESTDLL = cmd.exe
C_SRCS = \ - batch.c + batch.c \ + directory.c
RC_SRCS = rsrc.rc diff --git a/programs/cmd/tests/directory.c b/programs/cmd/tests/directory.c new file mode 100644 index 00000000000..7b965f068b0 --- /dev/null +++ b/programs/cmd/tests/directory.c @@ -0,0 +1,133 @@ +/* + * Copyright 2023 Akihiro Sagawa + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include <windows.h> +#include "wine/test.h" + +#define MAX_BUFFER 65536 + +static char stdout_buffer[MAX_BUFFER], stderr_buffer[MAX_BUFFER]; +static DWORD stdout_size, stderr_size; +static char work_dir[MAX_PATH]; + +static void read_all_from_handle(HANDLE handle, char *buffer, DWORD *size) +{ + char bytes[4096]; + DWORD bytes_read; + + memset(buffer, 0, MAX_BUFFER); + *size = 0; + for (;;) + { + BOOL success = ReadFile(handle, bytes, sizeof(bytes), &bytes_read, NULL); + if (!success || !bytes_read) + break; + if (*size + bytes_read > MAX_BUFFER) + { + ok(FALSE, "Insufficient buffer.\n"); + break; + } + memcpy(buffer + *size, bytes, bytes_read); + *size += bytes_read; + } +} + +#define run_dir(a, b) _run_dir(__FILE__, __LINE__, a, b) +static void _run_dir(const char *file, int line, const char *commandline, int exitcode_expected) +{ + HANDLE child_stdout_write, child_stderr_write, parent_stdout_read, parent_stderr_read; + SECURITY_ATTRIBUTES security_attributes = {0}; + PROCESS_INFORMATION process_info = {0}; + STARTUPINFOA startup_info = {0}; + DWORD exitcode; + char cmd[256]; + + security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); + security_attributes.bInheritHandle = TRUE; + + CreatePipe(&parent_stdout_read, &child_stdout_write, &security_attributes, 0); + CreatePipe(&parent_stderr_read, &child_stderr_write, &security_attributes, 0); + SetHandleInformation(parent_stdout_read, HANDLE_FLAG_INHERIT, 0); + SetHandleInformation(parent_stderr_read, HANDLE_FLAG_INHERIT, 0); + + startup_info.cb = sizeof(STARTUPINFOA); + startup_info.hStdOutput = child_stdout_write; + startup_info.hStdError = child_stderr_write; + startup_info.dwFlags |= STARTF_USESTDHANDLES; + + sprintf(cmd, "cmd.exe /d /c dir %s", commandline); + CreateProcessA(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startup_info, &process_info); + CloseHandle(child_stdout_write); + CloseHandle(child_stderr_write); + + read_all_from_handle(parent_stdout_read, stdout_buffer, &stdout_size); + read_all_from_handle(parent_stderr_read, stderr_buffer, &stderr_size); + CloseHandle(parent_stdout_read); + CloseHandle(parent_stderr_read); + + WaitForSingleObject(process_info.hProcess, INFINITE); + GetExitCodeProcess(process_info.hProcess, &exitcode); + CloseHandle(process_info.hProcess); + CloseHandle(process_info.hThread); + ok_(file, line)(exitcode == exitcode_expected, "expected exitcode %d, got %ld\n", + exitcode_expected, exitcode); +} + +static void test_basic(void) +{ + /* no options */ + run_dir("", 0); + ok(stdout_size > 0, "unexpected stdout buffer size %ld.\n", stdout_size); + ok(stderr_size == 0, "unexpected stderr buffer size %ld.\n", stderr_size); + + /* if file doesn't exist, cmd.exe prints an error message to stderr. */ + run_dir("nonexistent", 1); + ok(stdout_size > 0, "unexpected stdout buffer size %ld.\n", stdout_size); + ok(stderr_size > 0, "unexpected stderr buffer size %ld.\n", stderr_size); + + /* unknown option produces an error message to stderr. */ + run_dir("/*", 1); + ok(stdout_size == 0, "unexpected stdout buffer size %ld.\n", stdout_size); + ok(stderr_size > 0, "unexpected stderr buffer size %ld.\n", stderr_size); + + /* errorlevel for usage is 0. But, cmd.exe's exit code is 1. */ + todo_wine run_dir("/?", 1); + ok(stdout_size > 0, "unexpected stdout buffer size %ld.\n", stdout_size); + ok(stderr_size == 0, "unexpected stderr buffer size %ld.\n", stderr_size); +} + +START_TEST(directory) +{ + WCHAR curdir[MAX_PATH]; + BOOL ret; + + GetCurrentDirectoryW(ARRAY_SIZE(curdir), curdir); + GetTempPathA(ARRAY_SIZE(work_dir), work_dir); + lstrcatA(work_dir, "winetest.dir"); + ret = CreateDirectoryA(work_dir, NULL); + ok(ret, "Failed to create %s\n", work_dir); + ret = SetCurrentDirectoryA(work_dir); + ok(ret, "Failed to set the working directory\n"); + + test_basic(); + + ret = SetCurrentDirectoryW(curdir); + ok(ret, "Failed to restore the current directory\n"); + ret = RemoveDirectoryA(work_dir); + ok(ret, "Failed to remove the working directory\n"); +}
From: Akihiro Sagawa sagawa.aki@gmail.com
--- programs/cmd/tests/directory.c | 127 +++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+)
diff --git a/programs/cmd/tests/directory.c b/programs/cmd/tests/directory.c index 7b965f068b0..6644bc52b35 100644 --- a/programs/cmd/tests/directory.c +++ b/programs/cmd/tests/directory.c @@ -25,6 +25,8 @@ static char stdout_buffer[MAX_BUFFER], stderr_buffer[MAX_BUFFER]; static DWORD stdout_size, stderr_size; static char work_dir[MAX_PATH];
+typedef BOOL (*find_line_callback)(const char* line, void *data); + static void read_all_from_handle(HANDLE handle, char *buffer, DWORD *size) { char bytes[4096]; @@ -88,6 +90,36 @@ static void _run_dir(const char *file, int line, const char *commandline, int ex exitcode_expected, exitcode); }
+static BOOL find_line(const char* buf, find_line_callback callback, void *data) +{ + BOOL found = FALSE; + const char* p = buf; + char *line = NULL; + size_t size = 0; + + while (*p) + { + size_t len; + const char* eol = strpbrk(p, "\r\n"); + len = eol ? (eol - p) : strlen(p); + if (len + 1 > size) + { + char *ptr = realloc(line, len + 1); + if (!ptr) break; + line = ptr; + size = len + 1; + } + memcpy(line, p, len); + line[len] = '\0'; + found = callback(line, data); + if (found) break; + if (*eol == '\r' && *(eol+1) == '\n') eol++; + p = eol + 1; + } + free(line); + return found; +} + static void test_basic(void) { /* no options */ @@ -111,6 +143,100 @@ static void test_basic(void) ok(stderr_size == 0, "unexpected stderr buffer size %ld.\n", stderr_size); }
+struct timestamp_param { + SYSTEMTIME st; + const char* filename; +}; + +static BOOL match_timestamp(const char* line, void *data) +{ + const struct timestamp_param *param = (const struct timestamp_param *)data; + char pattern[MAX_BUFFER], *ptr; + + if ((ptr = strstr(line, " 0 ")) && strstr(ptr, param->filename)) { + char format[60]; + + while (*ptr == ' ') *ptr-- = '\0'; + + GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, format, sizeof(format)); + if ((ptr = strstr(format, "d")) && strncmp(ptr, "ddd", 3) && + (!strncmp(ptr, "dd", 2) || !strncmp(ptr, "d", 1))) + { + sprintf(pattern, "%02hd", param->st.wDay); + todo_wine_if(strncmp(ptr, "dd", 2)) + ok(!!strstr(line, pattern), "expected day %s, got %s\n", pattern, wine_dbgstr_a(line)); + } + else + skip("date format %s doesn't represent day of the month as digits\n", wine_dbgstr_a(format)); + + if ((ptr = strstr(format, "M")) && strncmp(ptr, "MMM", 3) && + (!strncmp(ptr, "MM", 2) || !strncmp(ptr, "M", 1))) + { + sprintf(pattern, "%02hd", param->st.wMonth); + todo_wine_if(strncmp(ptr, "MM", 2)) + ok(!!strstr(line, pattern), "expected month %s, got %s\n", pattern, wine_dbgstr_a(line)); + } + else + skip("date format %s doesn't represent month as digits\n", wine_dbgstr_a(format)); + + GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, format, sizeof(format)); + if (strstr(format, "h") || strstr(format, "H")) + { + sprintf(pattern, "%02hd", param->st.wHour); + todo_wine_if(!strstr(format, "hh") && !strstr(format, "HH")) + ok(!!strstr(line, pattern), "expected hour %s, got %s\n", pattern, wine_dbgstr_a(line)); + } + else + skip("time format %s doesn't represent hour as digits\n", wine_dbgstr_a(format)); + + if (strstr(format, "m")) + { + sprintf(pattern, "%02hd", param->st.wMinute); + todo_wine_if(!strstr(format, "mm")) + ok(!!strstr(line, pattern), "expected minute %s, got %s\n", pattern, wine_dbgstr_a(line)); + } + else + skip("time format %s doesn't represent minute as digits\n", wine_dbgstr_a(format)); + + return TRUE; + } + return FALSE; +} + +static void test_timestamp(void) +{ + static const char* filename = "test.dir"; + FILETIME local, ft; + SYSTEMTIME st = { + /* Use single digits for leading zeros except the year */ + .wYear = 2009, .wMonth = 1, .wDay = 3, .wHour = 5, .wMinute = 7, + }; + struct timestamp_param param = { + .filename = filename, + }; + HANDLE file; + BOOL ret; + + file = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + ok(file != INVALID_HANDLE_VALUE, "Can't create file, err %lu\n", GetLastError()); + + SystemTimeToFileTime(&st, &local); + LocalFileTimeToFileTime(&local, &ft); + SetFileTime(file, NULL, NULL, &ft); + FileTimeToLocalFileTime(&ft, &local); + FileTimeToSystemTime(&local, ¶m.st); + + CloseHandle(file); + + run_dir(filename, 0); + stdout_buffer[stdout_size] = '\0'; + ret = find_line(stdout_buffer, match_timestamp, ¶m); + ok(ret, "file name is not found in the output\n"); + + ret = DeleteFileA(filename); + ok(ret, "Can't delete file, err %lu\n", GetLastError()); +} + START_TEST(directory) { WCHAR curdir[MAX_PATH]; @@ -125,6 +251,7 @@ START_TEST(directory) ok(ret, "Failed to set the working directory\n");
test_basic(); + test_timestamp();
ret = SetCurrentDirectoryW(curdir); ok(ret, "Failed to restore the current directory\n");
From: Akihiro Sagawa sagawa.aki@gmail.com
--- programs/cmd/directory.c | 118 +++++++++++++++++++++++++++++++-- programs/cmd/tests/directory.c | 4 -- 2 files changed, 114 insertions(+), 8 deletions(-)
diff --git a/programs/cmd/directory.c b/programs/cmd/directory.c index 6efcbb3a841..4d3ae3f9c7d 100644 --- a/programs/cmd/directory.c +++ b/programs/cmd/directory.c @@ -41,6 +41,10 @@ typedef enum _DISPLAYORDER Date } DISPLAYORDER;
+#define MAX_DATETIME_FORMAT 80 + +static WCHAR date_format[MAX_DATETIME_FORMAT * 2]; +static WCHAR time_format[MAX_DATETIME_FORMAT * 2]; static int file_total, dir_total, max_width; static ULONGLONG byte_total; static DISPLAYTIME dirTime; @@ -345,8 +349,10 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le FileTimeToLocalFileTime (&fd[i].ftCreationTime, &ft); } FileTimeToSystemTime (&ft, &st); - GetDateFormatW(0, DATE_SHORTDATE, &st, NULL, datestring, ARRAY_SIZE(datestring)); - GetTimeFormatW(0, TIME_NOSECONDS, &st, NULL, timestring, ARRAY_SIZE(timestring)); + GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, date_format, + datestring, ARRAY_SIZE(datestring)); + GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, time_format, + timestring, ARRAY_SIZE(timestring));
if (wide) {
@@ -375,7 +381,7 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le dir_count++;
if (!bare) { - WCMD_output (L"%1!10s! %2!8s! <DIR> ", datestring, timestring); + WCMD_output (L"%1 %2 <DIR> ", datestring, timestring); if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName); if (usernames) WCMD_output(L"%1!-23s!", username); WCMD_output(L"%1",fd[i].cFileName); @@ -394,7 +400,7 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le file_size.u.HighPart = fd[i].nFileSizeHigh; byte_count.QuadPart += file_size.QuadPart; if (!bare) { - WCMD_output (L"%1!10s! %2!8s! %3!10s! ", datestring, timestring, + WCMD_output (L"%1 %2 %3!14s! ", datestring, timestring, WCMD_filesize64(file_size.QuadPart)); if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName); if (usernames) WCMD_output(L"%1!-23s!", username); @@ -523,6 +529,107 @@ static void WCMD_dir_trailer(const WCHAR *path) { } }
+/* Get the length of a date/time formatting pattern */ +/* copied from dlls/kernelbase/locale.c */ +static int get_pattern_len( const WCHAR *pattern, const WCHAR *accept ) +{ + int i; + + if (*pattern == ''') + { + for (i = 1; pattern[i]; i++) + { + if (pattern[i] != ''') continue; + if (pattern[++i] != ''') return i; + } + return i; + } + if (!wcschr( accept, *pattern )) return 1; + for (i = 1; pattern[i]; i++) if (pattern[i] != pattern[0]) break; + return i; +} + +/* Initialize date format to use abbreviated one with leading zeros */ +static void init_date_format(void) +{ + WCHAR sys_format[MAX_DATETIME_FORMAT]; + int src_pat_len, dst_pat_len; + const WCHAR *src; + WCHAR *dst = date_format; + + GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sys_format, ARRAY_SIZE(sys_format)); + + for (src = sys_format; *src; src += src_pat_len, dst += dst_pat_len) { + src_pat_len = dst_pat_len = get_pattern_len(src, L"yMd"); + + switch (*src) + { + case ''': + wmemcpy(dst, src, src_pat_len); + break; + + case 'd': + case 'M': + if (src_pat_len == 4) /* full name */ + dst_pat_len--; /* -> use abbreviated one */ + /* fallthrough */ + case 'y': + if (src_pat_len == 1) /* without leading zeros */ + dst_pat_len++; /* -> with leading zeros */ + wmemset(dst, *src, dst_pat_len); + break; + + default: + *dst = *src; + break; + } + } + *dst = '\0'; + + TRACE("date format: %s\n", wine_dbgstr_w(date_format)); +} + +/* Initialize time format to use leading zeros */ +static void init_time_format(void) +{ + WCHAR sys_format[MAX_DATETIME_FORMAT]; + int src_pat_len, dst_pat_len; + const WCHAR *src; + WCHAR *dst = time_format; + + GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, sys_format, ARRAY_SIZE(sys_format)); + + for (src = sys_format; *src; src += src_pat_len, dst += dst_pat_len) { + src_pat_len = dst_pat_len = get_pattern_len(src, L"Hhmst"); + + switch (*src) + { + case ''': + wmemcpy(dst, src, src_pat_len); + break; + + case 'H': + case 'h': + case 'm': + case 's': + if (src_pat_len == 1) /* without leading zeros */ + dst_pat_len++; /* -> with leading zeros */ + /* fallthrough */ + case 't': + wmemset(dst, *src, dst_pat_len); + break; + + default: + *dst = *src; + break; + } + } + *dst = '\0'; + + /* seconds portion will be dropped by TIME_NOSECONDS */ + TRACE("time format: %s\n", wine_dbgstr_w(time_format)); +} + /***************************************************************************** * WCMD_directory * @@ -727,6 +834,9 @@ void WCMD_directory (WCHAR *args) WCMD_enter_paged_mode(NULL); }
+ init_date_format(); + init_time_format(); + argno = 0; argN = args; GetCurrentDirectoryW(MAX_PATH, cwd); diff --git a/programs/cmd/tests/directory.c b/programs/cmd/tests/directory.c index 6644bc52b35..1a62ce120ca 100644 --- a/programs/cmd/tests/directory.c +++ b/programs/cmd/tests/directory.c @@ -163,7 +163,6 @@ static BOOL match_timestamp(const char* line, void *data) (!strncmp(ptr, "dd", 2) || !strncmp(ptr, "d", 1))) { sprintf(pattern, "%02hd", param->st.wDay); - todo_wine_if(strncmp(ptr, "dd", 2)) ok(!!strstr(line, pattern), "expected day %s, got %s\n", pattern, wine_dbgstr_a(line)); } else @@ -173,7 +172,6 @@ static BOOL match_timestamp(const char* line, void *data) (!strncmp(ptr, "MM", 2) || !strncmp(ptr, "M", 1))) { sprintf(pattern, "%02hd", param->st.wMonth); - todo_wine_if(strncmp(ptr, "MM", 2)) ok(!!strstr(line, pattern), "expected month %s, got %s\n", pattern, wine_dbgstr_a(line)); } else @@ -183,7 +181,6 @@ static BOOL match_timestamp(const char* line, void *data) if (strstr(format, "h") || strstr(format, "H")) { sprintf(pattern, "%02hd", param->st.wHour); - todo_wine_if(!strstr(format, "hh") && !strstr(format, "HH")) ok(!!strstr(line, pattern), "expected hour %s, got %s\n", pattern, wine_dbgstr_a(line)); } else @@ -192,7 +189,6 @@ static BOOL match_timestamp(const char* line, void *data) if (strstr(format, "m")) { sprintf(pattern, "%02hd", param->st.wMinute); - todo_wine_if(!strstr(format, "mm")) ok(!!strstr(line, pattern), "expected minute %s, got %s\n", pattern, wine_dbgstr_a(line)); } else
Hi,
It looks like your patch introduced the new failures shown below. Please investigate and fix them before resubmitting your patch. If they are not new, fixing them anyway would help a lot. Otherwise please ask for the known failures list to be updated.
The tests also ran into some preexisting test failures. If you know how to fix them that would be helpful. See the TestBot job for the details:
The full results can be found at: https://testbot.winehq.org/JobDetails.pl?Key=137627
Your paranoid android.
=== w10pro64_en_AE_u8 (32 bit report) ===
cmd.exe: batch.c:321: Test failed: unexpected char 0x2d position 0 in line 5 (got '--- Test 3', wanted 'Test quotes "&" work') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 7 (got '--- Test 4', wanted '"&"') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 9 (got '--- Test 5', wanted '"<"') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 11 (got '--- Test 6', wanted '">"') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 13 (got '--- Test 7', wanted '""') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 15 (got '--- Test 8', wanted '"|"') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 17 (got '--- Test 9', wanted '"`"') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 19 (got '--- Test 10', wanted '"""') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 23 (got '--- Test 13', wanted 'passed1') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 25 (got '--- Test 14', wanted 'passed2@space@') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 27 (got '--- Test 15', wanted 'foobar') batch.c:321: Test failed: unexpected char 0x2d position 0 in line 29 (got '--- Test 16', wanted 'No whitespace') batch.c:347: Test failed: unexpected end of output in line 33, missing No prompts or I would not get here2