- Pass through the return value of the program called by buildtime.

- Make the Windows version of buildtime fully Unicode-aware.

svn path=/trunk/tools/RosBE/; revision=1173
This commit is contained in:
Colin Finck 2010-01-30 14:37:45 +00:00
parent 61873d735e
commit 54500bb306
2 changed files with 68 additions and 80 deletions

View File

@ -1,28 +1,20 @@
/* Program for computing the build time.
Developed by Colin Finck <mail@colinfinck.de>
Developed by Colin Finck <colin@reactos.org>
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 @@ main(int argc, char* argv[])
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;
}
memset(CommandLine, 0, CommandLineLength + 1);
/* Put the command line into the variable */
for(i = 1; i < argc; i++)
else if(ProcessId == 0)
{
strcat(CommandLine, "\"");
strcat(CommandLine, argv[i]);
strcat(CommandLine, "\" ");
/* 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);
CommandLine[CommandLineLength] = 0;
if(wait(&Ret) != ProcessId)
{
fprintf(stderr, "buildtime: wait() failed!\n");
return 1;
}
time(&FinishTime);
/* 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);
/* 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;
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;
printf("\nTotal Build Time: %02d:%02d:%02d\n", Hour, Minute, Second);
return WEXITSTATUS(Ret);
}
}

View File

@ -5,25 +5,22 @@
* PURPOSE: Buildtime Counter
* COPYRIGHT: Copyright 2007 KJK::Hyperion
* Copyright 2007 Peter Ward <dralnix@gmail.com>
* Copyright 2010 Colin Finck <colin@reactos.org>
*
*/
#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 @@ static LPTSTR SkipSelfArgument(LPTSTR lpszCommandLine)
// 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 @@ static LPTSTR SkipSelfArgument(LPTSTR lpszCommandLine)
++ 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 @@ static LPTSTR SkipSelfArgument(LPTSTR lpszCommandLine)
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 @@ int main()
return 1;
}
CommandLineBuffer = malloc((strlen(CommandLine) + 2 + 1));
if (!CommandLineBuffer)
{
fprintf(stderr, "buildtime: unable to allocate memory\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);
// 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: CreateProcessW() failed!\n");
return 1;
}
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, &ExitCode);
// Grab the finishing timestamp.
time(&FinishTime);
@ -101,8 +98,5 @@ int main()
// 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;
}