Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygengetargs.c
Go to the documentation of this file.
00001 #include <precomp.h> 00002 #include <stdlib.h> 00003 #include <string.h> 00004 00005 00006 extern char*_acmdln; 00007 extern wchar_t* _wcmdln; 00008 #undef _pgmptr 00009 extern char*_pgmptr; 00010 #undef _wpgmptr 00011 extern wchar_t*_wpgmptr; 00012 #undef _environ 00013 extern char**_environ; 00014 00015 #undef __argv 00016 #undef __argc 00017 00018 char**__argv = NULL; 00019 #undef __wargv 00020 wchar_t**__wargv = NULL; 00021 int __argc = 0; 00022 00023 extern wchar_t **__winitenv; 00024 00025 char* strndup(char const* name, size_t len) 00026 { 00027 char *s = malloc(len + 1); 00028 if (s != NULL) 00029 { 00030 memcpy(s, name, len); 00031 s[len] = 0; 00032 } 00033 return s; 00034 } 00035 00036 wchar_t* wcsndup(wchar_t* name, size_t len) 00037 { 00038 wchar_t *s = malloc((len + 1) * sizeof(wchar_t)); 00039 if (s != NULL) 00040 { 00041 memcpy(s, name, len*sizeof(wchar_t)); 00042 s[len] = 0; 00043 } 00044 return s; 00045 } 00046 00047 #define SIZE (4096 / sizeof(char*)) 00048 00049 int wadd(wchar_t* name) 00050 { 00051 wchar_t** _new; 00052 if ((__argc % SIZE) == 0) 00053 { 00054 if (__wargv == NULL) 00055 _new = malloc(sizeof(wchar_t*) * (1 + SIZE)); 00056 else 00057 _new = realloc(__wargv, sizeof(wchar_t*) * (__argc + 1 + SIZE)); 00058 if (_new == NULL) 00059 return -1; 00060 __wargv = _new; 00061 } 00062 __wargv[__argc++] = name; 00063 __wargv[__argc] = NULL; 00064 return 0; 00065 } 00066 00067 int wexpand(wchar_t* name, int expand_wildcards) 00068 { 00069 wchar_t* s; 00070 WIN32_FIND_DATAW fd; 00071 HANDLE hFile; 00072 BOOLEAN first = TRUE; 00073 wchar_t buffer[256]; 00074 uintptr_t pos; 00075 00076 if (expand_wildcards && (s = wcspbrk(name, L"*?"))) 00077 { 00078 hFile = FindFirstFileW(name, &fd); 00079 if (hFile != INVALID_HANDLE_VALUE) 00080 { 00081 while(s != name && *s != L'/' && *s != L'\\') 00082 s--; 00083 pos = s - name; 00084 if (*s == L'/' || *s == L'\\') 00085 pos++; 00086 wcsncpy(buffer, name, pos); 00087 do 00088 { 00089 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00090 { 00091 wcscpy(&buffer[pos], fd.cFileName); 00092 if (wadd(_wcsdup(buffer)) < 0) 00093 { 00094 FindClose(hFile); 00095 return -1; 00096 } 00097 first = FALSE; 00098 } 00099 } 00100 while(FindNextFileW(hFile, &fd)); 00101 FindClose(hFile); 00102 } 00103 } 00104 if (first) 00105 { 00106 if (wadd(name) < 0) 00107 return -1; 00108 } 00109 else 00110 free(name); 00111 return 0; 00112 } 00113 00114 int aadd(char* name) 00115 { 00116 char** _new; 00117 if ((__argc % SIZE) == 0) 00118 { 00119 if (__argv == NULL) 00120 _new = malloc(sizeof(char*) * (1 + SIZE)); 00121 else 00122 _new = realloc(__argv, sizeof(char*) * (__argc + 1 + SIZE)); 00123 if (_new == NULL) 00124 return -1; 00125 __argv = _new; 00126 } 00127 __argv[__argc++] = name; 00128 __argv[__argc] = NULL; 00129 return 0; 00130 } 00131 00132 int aexpand(char* name, int expand_wildcards) 00133 { 00134 char* s; 00135 WIN32_FIND_DATAA fd; 00136 HANDLE hFile; 00137 BOOLEAN first = TRUE; 00138 char buffer[256]; 00139 uintptr_t pos; 00140 00141 if (expand_wildcards && (s = strpbrk(name, "*?"))) 00142 { 00143 hFile = FindFirstFileA(name, &fd); 00144 if (hFile != INVALID_HANDLE_VALUE) 00145 { 00146 while(s != name && *s != '/' && *s != '\\') 00147 s--; 00148 pos = s - name; 00149 if (*s == '/' || *s == '\\') 00150 pos++; 00151 strncpy(buffer, name, pos); 00152 do 00153 { 00154 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00155 { 00156 strcpy(&buffer[pos], fd.cFileName); 00157 if (aadd(_strdup(buffer)) < 0) 00158 { 00159 FindClose(hFile); 00160 return -1; 00161 } 00162 first = FALSE; 00163 } 00164 } 00165 while(FindNextFileA(hFile, &fd)); 00166 FindClose(hFile); 00167 } 00168 } 00169 if (first) 00170 { 00171 if (aadd(name) < 0) 00172 return -1; 00173 } 00174 else 00175 free(name); 00176 return 0; 00177 } 00178 00179 /* 00180 * @implemented 00181 */ 00182 void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, int* new_mode) 00183 { 00184 int i, afterlastspace, ignorespace, doexpand; 00185 size_t len; 00186 00187 /* missing threading init */ 00188 00189 i = 0; 00190 afterlastspace = 0; 00191 ignorespace = 0; 00192 doexpand = expand_wildcards; 00193 00194 if (__argv && _environ) 00195 { 00196 *argv = __argv; 00197 *env = _environ; 00198 *argc = __argc; 00199 return; 00200 } 00201 00202 __argc = 0; 00203 00204 len = strlen(_acmdln); 00205 00206 00207 while (_acmdln[i]) 00208 { 00209 if (_acmdln[i] == '"') 00210 { 00211 if(ignorespace) 00212 { 00213 ignorespace = 0; 00214 } 00215 else 00216 { 00217 ignorespace = 1; 00218 doexpand = 0; 00219 } 00220 memmove(_acmdln + i, _acmdln + i + 1, len - i); 00221 len--; 00222 continue; 00223 } 00224 00225 if (_acmdln[i] == ' ' && !ignorespace) 00226 { 00227 aexpand(strndup(_acmdln + afterlastspace, i - afterlastspace), doexpand); 00228 i++; 00229 while (_acmdln[i]==' ') 00230 i++; 00231 afterlastspace=i; 00232 doexpand = expand_wildcards; 00233 } 00234 else 00235 { 00236 i++; 00237 } 00238 } 00239 00240 if (_acmdln[afterlastspace] != 0) 00241 { 00242 aexpand(strndup(_acmdln+afterlastspace, i - afterlastspace), doexpand); 00243 } 00244 00245 HeapValidate(GetProcessHeap(), 0, NULL); 00246 00247 *argc = __argc; 00248 if (__argv == NULL) 00249 { 00250 __argv = (char**)malloc(sizeof(char*)); 00251 __argv[0] = 0; 00252 } 00253 *argv = __argv; 00254 *env = _environ; 00255 _pgmptr = _strdup(__argv[0]); 00256 00257 // if (new_mode) _set_new_mode(*new_mode); 00258 } 00259 00260 /* 00261 * @implemented 00262 */ 00263 void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv, 00264 int expand_wildcards, int* new_mode) 00265 { 00266 int i, afterlastspace, ignorespace, doexpand; 00267 size_t len; 00268 00269 /* missing threading init */ 00270 00271 i = 0; 00272 afterlastspace = 0; 00273 ignorespace = 0; 00274 doexpand = expand_wildcards; 00275 00276 if (__wargv && __winitenv) 00277 { 00278 *wargv = __wargv; 00279 *wenv = __winitenv; 00280 *argc = __argc; 00281 return; 00282 } 00283 00284 __argc = 0; 00285 00286 len = wcslen(_wcmdln); 00287 00288 while (_wcmdln[i]) 00289 { 00290 if (_wcmdln[i] == L'"') 00291 { 00292 if(ignorespace) 00293 { 00294 ignorespace = 0; 00295 } 00296 else 00297 { 00298 ignorespace = 1; 00299 doexpand = 0; 00300 } 00301 memmove(_wcmdln + i, _wcmdln + i + 1, (len - i) * sizeof(wchar_t)); 00302 len--; 00303 continue; 00304 } 00305 00306 if (_wcmdln[i] == L' ' && !ignorespace) 00307 { 00308 wexpand(wcsndup(_wcmdln + afterlastspace, i - afterlastspace), doexpand); 00309 i++; 00310 while (_wcmdln[i]==L' ') 00311 i++; 00312 afterlastspace=i; 00313 doexpand = expand_wildcards; 00314 } 00315 else 00316 { 00317 i++; 00318 } 00319 } 00320 00321 if (_wcmdln[afterlastspace] != 0) 00322 { 00323 wexpand(wcsndup(_wcmdln+afterlastspace, i - afterlastspace), doexpand); 00324 } 00325 00326 HeapValidate(GetProcessHeap(), 0, NULL); 00327 00328 *argc = __argc; 00329 if (__wargv == NULL) 00330 { 00331 __wargv = (wchar_t**)malloc(sizeof(wchar_t*)); 00332 __wargv[0] = 0; 00333 } 00334 *wargv = __wargv; 00335 *wenv = __winitenv; 00336 _wpgmptr = _wcsdup(__wargv[0]); 00337 00338 // if (new_mode) _set_new_mode(*new_mode); 00339 } 00340 00341 /* 00342 * @implemented 00343 */ 00344 int* __p___argc(void) 00345 { 00346 return &__argc; 00347 } 00348 00349 /* 00350 * @implemented 00351 */ 00352 char*** __p___argv(void) 00353 { 00354 return &__argv; 00355 } 00356 00357 /* 00358 * @implemented 00359 */ 00360 wchar_t*** __p___wargv(void) 00361 { 00362 return &__wargv; 00363 } 00364 00365 Generated on Mon May 28 2012 04:36:17 for ReactOS by
1.7.6.1
|