FreeNOS
Sleep.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 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 <string.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include "Sleep.h"
24 
25 Sleep::Sleep(int argc, char **argv)
26  : POSIXApplication(argc, argv)
27 {
28  parser().setDescription("Stop executing for some time");
29  parser().registerPositional("SECONDS", "Stop executing for the given number of seconds");
30 }
31 
33 {
34 }
35 
37 {
38  int sec = 0;
39 
40  // Convert input to seconds
41  if ((sec = atoi(arguments().get("SECONDS"))) <= 0)
42  {
43  ERROR("invalid sleep time `" << arguments().get("SECONDS") << "'");
44  return InvalidArgument;
45  }
46 
47  // Sleep now
48  if (sleep(sec) != 0)
49  {
50  ERROR("failed to sleep: " << strerror(errno));
51  return IOError;
52  }
53 
54  // Done
55  return Success;
56 }
Sleep.h
errno
C int errno
The lvalue errno is used by many functions to return error values.
Sleep::exec
virtual Result exec()
Execute the application.
Definition: Sleep.cpp:36
string.h
atoi
C int atoi(const char *nptr)
Convert a string to an integer.
Definition: atoi.cpp:21
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
Application::Success
@ Success
Definition: Application.h:55
Application::arguments
const ArgumentContainer & arguments() const
Get program arguments.
Definition: Application.cpp:112
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
Application::IOError
@ IOError
Definition: Application.h:57
Application::InvalidArgument
@ InvalidArgument
Definition: Application.h:58
Sleep::~Sleep
virtual ~Sleep()
Destructor.
Definition: Sleep.cpp:32
sleep
unsigned int sleep(unsigned int seconds)
Sleep for the specified number of seconds.
Definition: sleep.cpp:23
stdio.h
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
Application::Result
Result
Result codes.
Definition: Application.h:53
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
unistd.h
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
stdlib.h
Sleep::Sleep
Sleep(int argc, char **argv)
Constructor.
Definition: Sleep.cpp:25
errno.h