change proc_args to use CommandLineToArgvW parser

This commit is contained in:
Doug MacEachern 2004-08-03 03:06:25 +00:00
parent 042c38ae95
commit bb02117ef4
3 changed files with 24 additions and 16 deletions

View File

@ -7,6 +7,7 @@
#include "sigar.h" #include "sigar.h"
#include "sigar_private.h" #include "sigar_private.h"
#include "sigar_os.h" #include "sigar_os.h"
#include <shellapi.h>
#define PAGE_START 0x00020000 #define PAGE_START 0x00020000
#define CWD_OFFSET PAGE_START + 0x0290 #define CWD_OFFSET PAGE_START + 0x0290
@ -79,12 +80,16 @@ int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
return SIGAR_OK; return SIGAR_OK;
} }
int sigar_proc_cmdline_get(sigar_t *sigar, HANDLE proc, char *cmdline) int sigar_proc_args_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_args_t *procargs)
{ {
int status; int status;
LPBYTE scratch; LPBYTE scratch;
DWORD base; DWORD base;
WCHAR buf[SIGAR_CMDLINE_MAX]; WCHAR buf[SIGAR_CMDLINE_MAX];
char arg[SIGAR_CMDLINE_MAX];
LPWSTR *args;
int num, i;
if ((status = sigar_peb_get(sigar, proc, &base)) != SIGAR_OK) { if ((status = sigar_peb_get(sigar, proc, &base)) != SIGAR_OK) {
return status; return status;
@ -99,7 +104,20 @@ int sigar_proc_cmdline_get(sigar_t *sigar, HANDLE proc, char *cmdline)
wcsncpy(buf, (LPWSTR)scratch, SIGAR_CMDLINE_MAX); wcsncpy(buf, (LPWSTR)scratch, SIGAR_CMDLINE_MAX);
buf[SIGAR_CMDLINE_MAX-1] = L'\0'; buf[SIGAR_CMDLINE_MAX-1] = L'\0';
SIGAR_W2A(buf, cmdline, SIGAR_CMDLINE_MAX); args = CommandLineToArgvW(buf, &num);
sigar_proc_args_create(procargs);
for (i=0; i<=num; i++) {
if (!iswprint(*args[i])) {
continue; /* seen trailing garbage */
}
SIGAR_PROC_ARGS_GROW(procargs);
SIGAR_W2A(args[i], arg, SIGAR_CMDLINE_MAX);
procargs->data[procargs->number++] = strdup(arg);
}
GlobalFree(args);
return SIGAR_OK; return SIGAR_OK;
} }

View File

@ -124,7 +124,8 @@ int sigar_wsa_init(sigar_t *sigar);
int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc, int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_exe_t *procexe); sigar_proc_exe_t *procexe);
int sigar_proc_cmdline_get(sigar_t *sigar, HANDLE proc, char *cmdline); int sigar_proc_args_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_args_t *procargs);
unsigned int sigar_cpu_count(sigar_t *sigar); unsigned int sigar_cpu_count(sigar_t *sigar);

View File

@ -820,24 +820,13 @@ static int sigar_remote_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
return GetLastError(); return GetLastError();
} }
status = sigar_proc_cmdline_get(sigar, proc, cmdline); status = sigar_proc_args_peb_get(sigar, proc, procargs);
CloseHandle(proc); CloseHandle(proc);
if (status != SIGAR_OK) {
return status; return status;
} }
sigar_proc_args_create(procargs);
while (*ptr && (arg = getarg(&ptr))) {
SIGAR_PROC_ARGS_GROW(procargs);
procargs->data[procargs->number++] = strdup(arg);
}
return SIGAR_OK;
}
static int sigar_local_proc_args_get(sigar_t *sigar, sigar_pid_t pid, static int sigar_local_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs) sigar_proc_args_t *procargs)
{ {