nullterm correct string in strndup

nullterm argv list
reformat


svn path=/trunk/; revision=18406
This commit is contained in:
Gunnar Dalsnes 2005-10-11 03:23:01 +00:00
parent aa105de9d7
commit 75e85af499

View file

@ -16,37 +16,43 @@ char**__argv = NULL;
wchar_t**__wargv = NULL; wchar_t**__wargv = NULL;
int __argc = 0; int __argc = 0;
extern wchar_t **__winitenv;
extern HANDLE hHeap; extern HANDLE hHeap;
char* strndup(char* name, int len) char* strndup(char* name, int len)
{ {
char *s = malloc(len + 1); char *s = malloc(len + 1);
if (s != NULL) { if (s != NULL)
strncpy(s, name, len); {
name[len] = 0; memcpy(s, name, len);
s[len] = 0;
} }
return s; return s;
} }
#define SIZE (4096 / sizeof(char*)) #define SIZE (4096 / sizeof(char*))
int add(char* name) int add
(char* name)
{ {
char** _new; char** _new;
if ((__argc % SIZE) == 0) { if ((__argc % SIZE) == 0)
{
if (__argv == NULL) if (__argv == NULL)
_new = malloc(sizeof(char*) * SIZE); _new = malloc(sizeof(char*) * (1 + SIZE));
else else
_new = realloc(__argv, sizeof(char*) * (__argc + SIZE)); _new = realloc(__argv, sizeof(char*) * (__argc + 1 + SIZE));
if (_new == NULL) if (_new == NULL)
return -1; return -1;
__argv = _new; __argv = _new;
} }
__argv[__argc++] = name; __argv[__argc++] = name;
__argv[__argc] = NULL;
return 0; return 0;
} }
int expand(char* name, int flag) int expand(char* name, int expand_wildcards)
{ {
char* s; char* s;
WIN32_FIND_DATAA fd; WIN32_FIND_DATAA fd;
@ -55,20 +61,25 @@ int expand(char* name, int flag)
char buffer[256]; char buffer[256];
int pos; int pos;
s = strpbrk(name, "*?"); if (expand_wildcards && (s = strpbrk(name, "*?")))
if (s && flag) { {
hFile = FindFirstFileA(name, &fd); hFile = FindFirstFileA(name, &fd);
if (hFile != INVALID_HANDLE_VALUE) { if (hFile != INVALID_HANDLE_VALUE)
{
while(s != name && *s != '/' && *s != '\\') while(s != name && *s != '/' && *s != '\\')
s--; s--;
pos = s - name; pos = s - name;
if (*s == '/' || *s == '\\') if (*s == '/' || *s == '\\')
pos++; pos++;
strncpy(buffer, name, pos); strncpy(buffer, name, pos);
do { do
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
strcpy(&buffer[pos], fd.cFileName); strcpy(&buffer[pos], fd.cFileName);
if (add(_strdup(buffer)) < 0) { if (add
(_strdup(buffer)) < 0)
{
FindClose(hFile); FindClose(hFile);
return -1; return -1;
} }
@ -79,8 +90,10 @@ int expand(char* name, int flag)
FindClose(hFile); FindClose(hFile);
} }
} }
if (first) { if (first)
if (add(name) < 0) {
if (add
(name) < 0)
return -1; return -1;
} }
else else
@ -91,7 +104,7 @@ int expand(char* name, int flag)
/* /*
* @unimplemented * @unimplemented
*/ */
int __getmainargs(int* argc, char*** argv, char*** env, int flag) void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, int* new_mode)
{ {
int i, afterlastspace, ignorespace, len, doexpand; int i, afterlastspace, ignorespace, len, doexpand;
@ -100,15 +113,20 @@ int __getmainargs(int* argc, char*** argv, char*** env, int flag)
i = 0; i = 0;
afterlastspace = 0; afterlastspace = 0;
ignorespace = 0; ignorespace = 0;
doexpand = flag; doexpand = expand_wildcards;
len = strlen(_acmdln); len = strlen(_acmdln);
while (_acmdln[i]) { while (_acmdln[i])
if (_acmdln[i] == '"') { {
if(ignorespace) { if (_acmdln[i] == '"')
{
if(ignorespace)
{
ignorespace = 0; ignorespace = 0;
} else { }
else
{
ignorespace = 1; ignorespace = 1;
doexpand = 0; doexpand = 0;
} }
@ -117,27 +135,34 @@ int __getmainargs(int* argc, char*** argv, char*** env, int flag)
continue; continue;
} }
if (_acmdln[i] == ' ' && !ignorespace) { if (_acmdln[i] == ' ' && !ignorespace)
{
expand(strndup(_acmdln + afterlastspace, i - afterlastspace), doexpand); expand(strndup(_acmdln + afterlastspace, i - afterlastspace), doexpand);
i++; i++;
while (_acmdln[i]==' ') while (_acmdln[i]==' ')
i++; i++;
afterlastspace=i; afterlastspace=i;
doexpand = flag; doexpand = expand_wildcards;
} else { }
else
{
i++; i++;
} }
} }
if (_acmdln[afterlastspace] != 0) { if (_acmdln[afterlastspace] != 0)
{
expand(strndup(_acmdln+afterlastspace, i - afterlastspace), doexpand); expand(strndup(_acmdln+afterlastspace, i - afterlastspace), doexpand);
} }
HeapValidate(hHeap, 0, NULL); HeapValidate(hHeap, 0, NULL);
*argc = __argc; *argc = __argc;
*argv = __argv; *argv = __argv;
*env = _environ; *env = _environ;
_pgmptr = _strdup((char*)argv[0]); _pgmptr = _strdup((char*)argv[0]);
return 0;
// if (new_mode) _set_new_mode(*new_mode);
} }
/* /*
@ -146,10 +171,11 @@ int __getmainargs(int* argc, char*** argv, char*** env, int flag)
void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv, void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv,
int expand_wildcards, int* new_mode) int expand_wildcards, int* new_mode)
{ {
extern wchar_t **__winitenv; *argc = __argc;
*argc = 0; *wargv = __wargv;
*wargv = NULL;
*wenv = __winitenv; *wenv = __winitenv;
// if (new_mode) _set_new_mode(*new_mode);
} }
/* /*