Author: cfinck
Date: Sat Jan 30 15:37:45 2010
New Revision: 45346

URL: http://svn.reactos.org/svn/reactos?rev=45346&view=rev
Log:
- Pass through the return value of the program called by buildtime.
- Make the Windows version of buildtime fully Unicode-aware.

Modified:
    trunk/tools/RosBE/RosBE-Unix/Base-i386/tools/buildtime.c
    trunk/tools/RosBE/RosBE-Windows/Tools/buildtime.c

Modified: trunk/tools/RosBE/RosBE-Unix/Base-i386/tools/buildtime.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-Unix/Base-i386/tools/buildtime.c?rev=45346&r1=45345&r2=45346&view=diff
==============================================================================
--- trunk/tools/RosBE/RosBE-Unix/Base-i386/tools/buildtime.c [iso-8859-1] 
(original)
+++ trunk/tools/RosBE/RosBE-Unix/Base-i386/tools/buildtime.c [iso-8859-1] Sat 
Jan 30 15:37:45 2010
@@ -1,28 +1,20 @@
 /* Program for computing the build time.
 
-   Developed by Colin Finck <[email protected]>
+   Developed by Colin Finck <[email protected]>
    Derived from "buildtime.c" of RosBE for Windows
    Released under GNU GPL v2 or any later version.
 */
 
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <time.h>
+#include <unistd.h>
 
 int
 main(int argc, char* argv[])
 {
-       char* CommandLine;
-       double TotalTime;
-       int i;
-       int CommandLineLength = 0;
-       int Hour;
-       int Minute;
-       int Ret;
-       int Second;
-       time_t StartTime;
-       time_t FinishTime;
+       pid_t ProcessId;
 
        /* Do we have command line arguments? */
        if(argc <= 1)
@@ -31,50 +23,52 @@
                return 1;
        }
 
-       /* First compute the memory size to allocate */
-       for(i = 1; i < argc; i++)
+       ProcessId = fork();
+       if(ProcessId < 0)
        {
-               /* Every argument is enclosed between quotes and followed by a 
space.
-                  The last argument is followed by a terminating null 
character instead of a space. */
-               CommandLineLength += 3 + strlen(argv[i]);
-       }
-
-       /* Now allocate the needed memory */
-       CommandLine = malloc(CommandLineLength + 1);
-       if(!CommandLine)
-       {
-               fprintf(stderr, "buildtime: Unable to allocate memory!\n");
+               /* Error */
+               fprintf(stderr, "buildtime: fork() failed!\n");
                return 1;
        }
+       else if(ProcessId == 0)
+       {
+               /* Child process */
+               execvp(argv[1], &argv[1]);
+               
+               /* If this function returned, an error occured */
+               fprintf(stderr, "execvp() failed!\n");
+               return 1;
+       }
+       else
+       {
+               /* Parent process */
+               double TotalTime;
+               int Hour;
+               int Minute;
+               int Ret;
+               int Second;
+               time_t StartTime;
+               time_t FinishTime;
+               
+               time(&StartTime);
 
-       memset(CommandLine, 0, CommandLineLength + 1);
+               if(wait(&Ret) != ProcessId)
+               {
+                       fprintf(stderr, "buildtime: wait() failed!\n");
+                       return 1;
+               }
+               
+               time(&FinishTime);
 
-       /* Put the command line into the variable */
-       for(i = 1; i < argc; i++)
-       {
-               strcat(CommandLine, "\"");
-               strcat(CommandLine, argv[i]);
-               strcat(CommandLine, "\" ");
+               /* Compute the needed time and print it */
+               TotalTime = difftime(FinishTime, StartTime);
+
+               Second = (int)TotalTime % 60;
+               TotalTime = TotalTime / 60;
+               Minute = (int)TotalTime % 60;
+               Hour = TotalTime / 60;
+
+               printf("\nTotal Build Time: %02d:%02d:%02d\n", Hour, Minute, 
Second);
+               return WEXITSTATUS(Ret);
        }
-
-       CommandLine[CommandLineLength] = 0;
-
-       /* Get the timestamps and execute the command line */
-       time(&StartTime);
-       Ret = system(CommandLine);
-       time(&FinishTime);
-
-       /* Compute the needed time and print it */
-       TotalTime = difftime(FinishTime, StartTime);
-
-       Second = (int)TotalTime % 60;
-       TotalTime = TotalTime / 60;
-       Minute = (int)TotalTime % 60;
-       Hour = TotalTime / 60;
-
-       printf("\nTotal Build Time: %02d:%02d:%02d\n", Hour, Minute, Second);
-
-       /* Final actions */
-       free(CommandLine);
-       return Ret;
 }

Modified: trunk/tools/RosBE/RosBE-Windows/Tools/buildtime.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-Windows/Tools/buildtime.c?rev=45346&r1=45345&r2=45346&view=diff
==============================================================================
--- trunk/tools/RosBE/RosBE-Windows/Tools/buildtime.c [iso-8859-1] (original)
+++ trunk/tools/RosBE/RosBE-Windows/Tools/buildtime.c [iso-8859-1] Sat Jan 30 
15:37:45 2010
@@ -5,25 +5,22 @@
  * PURPOSE:     Buildtime Counter
  * COPYRIGHT:   Copyright 2007 KJK::Hyperion
  *              Copyright 2007 Peter Ward <[email protected]>
+ *              Copyright 2010 Colin Finck <[email protected]>
  *
  */
 
 #include <windows.h>
 #include <time.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include <ctype.h>
-#include <wctype.h>
-#include <tchar.h>
 
-static LPTSTR SkipSelfArgument(LPTSTR lpszCommandLine)
+static PWSTR SkipSelfArgument(PWSTR lpszCommandLine)
 {
-    LPTSTR p = lpszCommandLine;
+    PWSTR p = lpszCommandLine;
     int quote = 0;
 
     // Skip leading whitespace
-    while(*p != 0 && _istspace(*p))
+    while(*p != 0 && iswspace(*p))
         ++ p;
 
     if(*p == 0)
@@ -33,7 +30,7 @@
     // BUGBUG: the assumption is that argument 0 never contains escaped quotes
     do
     {
-        if(*p == TEXT('\"'))
+        if(*p == L'\"')
         {
             quote = !quote;
             ++ p;
@@ -42,10 +39,10 @@
 
         ++ p;
     }
-    while(*p != 0 && (quote || !_istspace(*p)));
+    while(*p != 0 && (quote || !iswspace(*p)));
 
     // Skip trailing whitespace
-    while(*p != 0 && _istspace(*p))
+    while(*p != 0 && iswspace(*p))
         ++ p;
 
     return p;
@@ -53,15 +50,16 @@
 
 int main()
 {
-    LPTSTR CommandLine, FullCommandLine, CommandLineBuffer;
+    DWORD ExitCode;
+    PWSTR CommandLine;
     time_t StartTime, FinishTime;
     double TotalTime;
     int Hour, Minute, Second;
-    int Status;
+    PROCESS_INFORMATION ProcessInfo;
+    STARTUPINFOW StartupInfo = {0};
 
     // Get the command line to pass on.
-    FullCommandLine = GetCommandLine();
-    CommandLine = SkipSelfArgument(FullCommandLine);
+    CommandLine = SkipSelfArgument(GetCommandLineW());
 
     // If nothing is on the command-line exit
     if (CommandLine[0] == 0)
@@ -70,21 +68,20 @@
         return 1;
     }
 
-    CommandLineBuffer = malloc((strlen(CommandLine) + 2 + 1));
-    if (!CommandLineBuffer)
+    // Grab the starting timestamp.
+    time(&StartTime);
+
+    // Run the program and get its exit code.
+    StartupInfo.cb = sizeof(StartupInfo);
+
+    if(!CreateProcessW(NULL, CommandLine, NULL, NULL, TRUE, 
NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInfo))
     {
-        fprintf(stderr, "buildtime: unable to allocate memory\n");
+        fprintf(stderr, "buildtime: CreateProcessW() failed!\n");
         return 1;
     }
 
-    CommandLineBuffer[0] = 0;
-    strcat(CommandLineBuffer, CommandLine);
-
-    // Grab the starting timestamp.
-    time(&StartTime);
-
-    // Run the program (Status is 1 on failure).
-    Status = system(CommandLineBuffer);
+    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
+    GetExitCodeProcess(ProcessInfo.hProcess, &ExitCode);
 
     // Grab the finishing timestamp.
     time(&FinishTime);
@@ -101,8 +98,5 @@
     // Print the total build time.
     printf("\nTotal Build Time: %02d:%02d:%02d", Hour, Minute, Second);
 
-    // Free the memory we allocated for the command line.
-    free(CommandLineBuffer);
-
-    return Status;
+    return ExitCode;
 }


Reply via email to