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
25Sleep::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}
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
POSIX-compatible application.
virtual ~Sleep()
Destructor.
Definition Sleep.cpp:32
virtual Result exec()
Execute the application.
Definition Sleep.cpp:36
Sleep(int argc, char **argv)
Constructor.
Definition Sleep.cpp:25
C int atoi(const char *nptr)
Convert a string to an integer.
Definition atoi.cpp:21
C unsigned int sleep(unsigned int seconds)
Sleep for the specified number of seconds.
Definition sleep.cpp:23
C char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition strerror.cpp:20
C int errno
The lvalue errno is used by many functions to return error values.
#define ERROR(msg)
Output an error message.
Definition Log.h:61