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 
37 int 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 }
TimeCommand::TimeCommand
TimeCommand(Shell *shell)
Constructor function.
Definition: TimeCommand.cpp:30
TimeCommand.h
SystemClock.h
TimeCommand::m_shell
Shell * m_shell
Shell object.
Definition: TimeCommand.h:58
TimeCommand::execute
virtual int execute(const Size nparams, const char **params)
Executes the command.
Definition: TimeCommand.cpp:37
fcntl.h
string.h
ShellCommand
Builtin command for the Shell.
Definition: ShellCommand.h:38
ShellCommand::m_help
const char * m_help
Command help text.
Definition: ShellCommand.h:91
Shell
System command shell interpreter.
Definition: Shell.h:33
SystemClock::printDiff
void printDiff(const SystemClock &clock) const
Print difference between two clocks to stdout.
Definition: SystemClock.cpp:49
Shell::executeInput
int executeInput(const Size argc, const char **argv, const bool background)
Executes the given input.
Definition: Shell.cpp:161
wait.h
Log.h
SystemClock
Provides an abstract interface to the system clock.
Definition: SystemClock.h:34
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
stdio.h
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
EXIT_SUCCESS
#define EXIT_SUCCESS
Successful termination.
Definition: stdlib.h:33
unistd.h
SystemClock::now
Result now()
Get the current time.
Definition: SystemClock.cpp:35
stdlib.h
Shell.h
errno.h