File: main.c

package info (click to toggle)
wine 10.0~repack-12
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 326,476 kB
  • sloc: ansic: 4,155,993; perl: 23,800; yacc: 22,031; javascript: 15,872; makefile: 12,346; pascal: 9,519; objc: 6,923; lex: 5,273; xml: 3,219; python: 2,688; cpp: 1,741; sh: 871; java: 750; asm: 299; cs: 62
file content (122 lines) | stat: -rw-r--r-- 3,483 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright 2020 Louis Lenders
 * Copyright 2024 Alex Henrie
 *
 * 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 <fileapi.h>
#include <shlwapi.h>

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(where);

static BOOL found;

static void search(const WCHAR *search_path, const WCHAR *pattern)
{
    static const WCHAR *extensions[] = {L"", L".bat", L".cmd", L".com", L".exe"};
    WCHAR glob[MAX_PATH];
    WCHAR match_path[MAX_PATH];
    WIN32_FIND_DATAW match;
    HANDLE handle;
    int i;

    for (i = 0; i < ARRAY_SIZE(extensions); i++)
    {
        if (wcslen(search_path) + 1 + wcslen(pattern) + wcslen(extensions[i]) + 1 > ARRAY_SIZE(glob))
        {
            ERR("Path too long\n");
            return;
        }
        /* Treat the extension as part of the pattern */
        wcscpy(glob, search_path);
        wcscat(glob, L"\\");
        wcscat(glob, pattern);
        wcscat(glob, extensions[i]);

        handle = FindFirstFileExW(glob, FindExInfoBasic, &match, 0, NULL, 0);
        if (handle != INVALID_HANDLE_VALUE)
        {
            do
            {
                if (PathCombineW(match_path, search_path, match.cFileName))
                {
                    printf("%ls\n", match_path);
                    found = TRUE;
                }
            }
            while (FindNextFileW(handle, &match));
            FindClose(handle);
        }
    }
}

int __cdecl wmain(int argc, WCHAR *argv[])
{
    WCHAR *pattern, *colon, *search_paths, *search_path, *next_search_path;
    int i;

    for (i = 0; i < argc; i++)
    {
        if (argv[i][0] == '/')
        {
            FIXME("Unsupported option %ls\n", argv[i]);
            return 1;
        }
    }

    for (i = 1; i < argc; i++)
    {
        pattern = argv[i];
        colon = wcsrchr(pattern, ':');

        /* Check for a set of search paths prepended to the pattern */
        if (colon)
        {
            *colon = 0;
            search_paths = pattern;
            pattern = colon + 1;
        }
        else
        {
            search_paths = _wgetenv(L"PATH");
        }

        if (wcspbrk(pattern, L"\\/\r\n")) continue; /* Silently ignore invalid patterns */

        if (!colon)
        {
            /* If the search paths were not explicitly specified, search the current directory first */
            WCHAR current_dir[MAX_PATH];
            if (GetCurrentDirectoryW(ARRAY_SIZE(current_dir), current_dir))
                search(current_dir, pattern);
        }

        next_search_path = wcsdup(search_paths);
        while ((search_path = wcstok(NULL, L";", &next_search_path)))
            search(search_path, pattern);
    }

    if (!found)
    {
        fputs("File not found\n", stderr);
        return 1;
    }

    return 0;
}