FreeNOS
TimeCommand.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <string.h>
24#include <sys/wait.h>
25#include <Log.h>
26#include <SystemClock.h>
27#include "Shell.h"
28#include "TimeCommand.h"
29
31 : ShellCommand("time", 1)
32 , m_shell(shell)
33{
34 m_help = "Measure the execution time of a program";
35}
36
37int TimeCommand::execute(const Size nparams, const char **params)
38{
39 SystemClock t1, t2;
40
41 // Get timestamp before
42 t1.now();
43
44 // Run command
45 int result = m_shell->executeInput(nparams, params, false);
46 if (result != EXIT_SUCCESS)
47 {
48 return result;
49 }
50
51 // Get timestamp after
52 t2.now();
53
54 // Print time measured
55 printf("\r\nTime: ");
56 t1.printDiff(t2);
57 return result;
58}
Builtin command for the Shell.
const char * m_help
Command help text.
System command shell interpreter.
Definition Shell.h:34
int executeInput(const Size argc, const char **argv, const bool background)
Executes the given input.
Definition Shell.cpp:161
Provides an abstract interface to the system clock.
Definition SystemClock.h:35
Result now()
Get the current time.
void printDiff(const SystemClock &clock) const
Print difference between two clocks to stdout.
Shell * m_shell
Shell object.
Definition TimeCommand.h:58
TimeCommand(Shell *shell)
Constructor function.
virtual int execute(const Size nparams, const char **params)
Executes the command.
#define EXIT_SUCCESS
Successful termination.
Definition stdlib.h:33
C int printf(const char *format,...)
Output a formatted string to standard output.
Definition printf.cpp:22
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128