From 0759887c2ecac33a1307b9f1355a2926e2d2a147 Mon Sep 17 00:00:00 2001 From: "blizzard%redhat.com" Date: Thu, 17 Feb 2000 15:35:54 +0000 Subject: [PATCH] change nsIFile::Spawn to take an array of arguments instead of just a single flat string. bug #27843. r=dougt, a=chofmann. also, implement nsIFile::Normalize for unix. bug #17948. r=shaver, a=chofmann git-svn-id: svn://10.0.0.236/trunk@61201 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/xpcom/io/nsIFile.idl | 3 +- mozilla/xpcom/io/nsLocalFileMac.cpp | 2 +- mozilla/xpcom/io/nsLocalFileUnix.cpp | 53 ++++++++++++++++++++++++++-- mozilla/xpcom/io/nsLocalFileWin.cpp | 38 ++++++++++++++------ 4 files changed, 80 insertions(+), 16 deletions(-) diff --git a/mozilla/xpcom/io/nsIFile.idl b/mozilla/xpcom/io/nsIFile.idl index de0edbe1e05..a91ef126bb7 100644 --- a/mozilla/xpcom/io/nsIFile.idl +++ b/mozilla/xpcom/io/nsIFile.idl @@ -20,6 +20,7 @@ * * Contributor(s): * Doug Turner + * Christopher Blizzard */ @@ -150,7 +151,7 @@ interface nsIFile : nsISupports * execution. 'args' will be passed through on the command line * if the OS supports that. */ - void spawn([const] in string args); + void spawn([array, size_is(count)] in string args, in unsigned long count); /** * This will try to delete this file. The 'recursive' flag diff --git a/mozilla/xpcom/io/nsLocalFileMac.cpp b/mozilla/xpcom/io/nsLocalFileMac.cpp index 286a876f558..ef0d86ea341 100644 --- a/mozilla/xpcom/io/nsLocalFileMac.cpp +++ b/mozilla/xpcom/io/nsLocalFileMac.cpp @@ -1026,7 +1026,7 @@ nsLocalFile::MoveTo(nsIFile *newParentDir, const char *newName) } NS_IMETHODIMP -nsLocalFile::Spawn(const char *args) +nsLocalFile::Spawn(const char **args, PRUint32 count) { PRBool isFile; nsresult rv = IsFile(&isFile); diff --git a/mozilla/xpcom/io/nsLocalFileUnix.cpp b/mozilla/xpcom/io/nsLocalFileUnix.cpp index 0f418dc55a7..63ce909f0a5 100644 --- a/mozilla/xpcom/io/nsLocalFileUnix.cpp +++ b/mozilla/xpcom/io/nsLocalFileUnix.cpp @@ -20,6 +20,7 @@ * * Contributor(s): * Mike Shaver + * Christopher Blizzard */ /* @@ -34,6 +35,7 @@ #include #include #include +#include #include "nsCRT.h" #include "nsCOMPtr.h" @@ -42,6 +44,7 @@ #include "nsILocalFile.h" #include "nsLocalFileUnix.h" #include "nsIComponentManager.h" +#include "prproces.h" // On some platforms file/directory name comparisons need to // be case-blind. @@ -404,7 +407,17 @@ nsLocalFile::Append(const char *fragment) NS_IMETHODIMP nsLocalFile::Normalize() { - return NS_ERROR_NOT_IMPLEMENTED; + char resolved_path[PATH_MAX]; + char *resolved_path_ptr = NULL; + CHECK_mPath(); + resolved_path_ptr = realpath(mPath, resolved_path); + // if there is an error, the return is null. + if (resolved_path_ptr == NULL) { + return NSRESULT_FOR_ERRNO(); + } + // nsXPIDLCString will copy. + mPath = resolved_path; + return NS_OK; } nsresult @@ -1065,9 +1078,43 @@ nsLocalFile::GetTarget(char **_retval) } NS_IMETHODIMP -nsLocalFile::Spawn(const char *args) +nsLocalFile::Spawn(const char **args, PRUint32 count) { - return NS_ERROR_NOT_IMPLEMENTED; + CHECK_mPath(); + + // status from our create process + PRStatus rv = PR_FAILURE; + + // this is the argv that will hold the args that + char ** my_argv = NULL; + + // make sure that when we allocate we have 1 greater than the + // count since we need to null terminate the list for the argv to + // pass into PR_CreateProcessDetached + my_argv = (char **)malloc(sizeof(char *) * (count + 2) ); + if (!my_argv) { + return NS_ERROR_OUT_OF_MEMORY; + } + // copy the args + PRUint32 i; + for (i=0; i < count; i++) { + my_argv[i+1] = (char *)args[i]; + } + // we need to set argv[0] to the program name. + my_argv[0] = (char *)(const char *)mPath; + + // null terminate the array + my_argv[count+1] = NULL; + + rv = PR_CreateProcessDetached(mPath, my_argv, NULL, NULL); + + // free up our argv + free(my_argv); + + if (PR_SUCCESS == rv) + return NS_OK; + else + return NS_ERROR_FILE_EXECUTION_FAILED; } NS_IMETHODIMP diff --git a/mozilla/xpcom/io/nsLocalFileWin.cpp b/mozilla/xpcom/io/nsLocalFileWin.cpp index 19fcc419960..25054e7e79a 100644 --- a/mozilla/xpcom/io/nsLocalFileWin.cpp +++ b/mozilla/xpcom/io/nsLocalFileWin.cpp @@ -1077,7 +1077,7 @@ nsLocalFile::MoveTo(nsIFile *newParentDir, const char *newName) } NS_IMETHODIMP -nsLocalFile::Spawn(const char *args) +nsLocalFile::Spawn(const char **args, PRUint32 count) { PRBool isFile; nsresult rv = IsFile(&isFile); @@ -1085,19 +1085,35 @@ nsLocalFile::Spawn(const char *args) if (NS_FAILED(rv)) return rv; - nsCString fileNameWithArgs(mResolvedPath); - - if(args) - { - fileNameWithArgs.Append(" "); - fileNameWithArgs.Append(args); + // make sure that when we allocate we have 1 greater than the + // count since we need to null terminate the list for the argv to + // pass into PR_CreateProcessDetached + char **my_argv = NULL; + + my_argv = (char **)malloc(sizeof(char *) * (count + 2) ); + if (!my_argv) { + return NS_ERROR_OUT_OF_MEMORY; } - int execResult = WinExec( fileNameWithArgs, SW_NORMAL ); - if (execResult > 31) - return NS_OK; + // copy the args + PRUint32 i; + for (i=0; i < count; i++) { + my_argv[i+1] = (char *)args[i]; + } + // we need to set argv[0] to the program name. + my_argv[0] = mResolvedPath; + + // null terminate the array + my_argv[count+1] = NULL; + rv = PR_CreateProcessDetached(mResolvedPath, my_argv, NULL, NULL); - return NS_ERROR_FILE_EXECUTION_FAILED; + // free up our argv + nsAllocator::Free(my_argv); + + if (PR_SUCCESS == rv) + return NS_OK; + else + return NS_ERROR_FILE_EXECUTION_FAILED; } NS_IMETHODIMP