branch update for HEAD-2003021201
[reactos.git] / lib / msvcrt / misc / getargs.c
1 #include <windows.h>
2 #include <msvcrt/stdlib.h>
3 #include <msvcrt/string.h>
4
5
6 extern char*_acmdln;
7 extern char*_pgmptr;
8 #undef _environ
9 extern char**_environ;
10
11 #undef __argv
12 #undef __argc
13
14 char**__argv = NULL;
15 int __argc = 0;
16
17 extern HANDLE hHeap;
18
19 char* strndup(char* name, int len)
20 {
21     char *s = malloc(len + 1);
22     if (s != NULL) {
23         strncpy(s, name, len);
24     }
25     return s;
26 }
27
28 #define SIZE (4096 / sizeof(char*))
29
30 int add(char* name)
31 {
32     char** _new;
33     if ((__argc % SIZE) == 0) {
34         _new = malloc(sizeof(char*) * (__argc + SIZE));
35         if (_new == NULL) {
36             return -1;
37         }
38         if (__argv) {
39             memcpy(_new, __argv, sizeof(char*) * __argc);
40             free(__argv);
41         }
42         __argv = _new;
43     }
44     __argv[__argc++] = name;
45     return 0;
46 }
47
48 int expand(char* name)
49 {
50     char* s;
51     WIN32_FIND_DATA fd;
52     HANDLE hFile;
53     BOOLEAN first = TRUE;
54     char buffer[256];
55     int pos;
56
57     s = strpbrk(name, "*?");
58     if (s) {
59         hFile = FindFirstFile(name, &fd);
60         if (hFile != INVALID_HANDLE_VALUE) {
61             while(s != name && *s != '/' && *s != '\\')
62                 s--;
63             pos = s - name;
64             if (*s == '/' || *s == '\\')
65                 pos++;
66             strncpy(buffer, name, pos);
67             do {
68                 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
69                     strcpy(&buffer[pos], fd.cFileName);
70                     if (add(strdup(buffer)) < 0) {
71                         FindClose(hFile);
72                         return -1;
73                     }
74                     first = FALSE;
75                 }
76             }
77             while(FindNextFile(hFile, &fd));
78             FindClose(hFile);
79         }
80     }
81     if (first) {
82         if (add(name) < 0)
83             return -1;
84     }
85     else
86         free(name);
87     return 0;
88 }
89
90 int __getmainargs(int* argc, char*** argv, char*** env, int flag)
91 {
92     int i, afterlastspace;
93
94     /* missing threading init */
95
96     i = 0;
97     afterlastspace = 0;
98
99     while (_acmdln[i]) {
100         if (_acmdln[i] == ' ') {
101             expand(strndup(_acmdln + afterlastspace, i - afterlastspace));
102             i++;
103             while (_acmdln[i]==' ')
104                 i++;
105             afterlastspace=i;
106         } else {
107             i++;
108         }
109     }
110
111     if (_acmdln[afterlastspace] != 0) {
112         expand(strndup(_acmdln+afterlastspace, i - afterlastspace));
113     }
114     HeapValidate(hHeap, 0, NULL);
115     *argc = __argc;
116     *argv = __argv;
117     *env  = _environ;
118     _pgmptr = strdup((char*)argv[0]);
119     return 0;
120 }
121
122 int* __p___argc(void)
123 {
124     return &__argc;
125 }
126
127 char*** __p___argv(void)
128 {
129     return &__argv;
130 }
131
132 #if 0
133 int _chkstk(void)
134 {
135     return 0;
136 }
137 #endif