FreeNOS
UnixName.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 <errno.h>
21 #include <string.h>
22 #include <sys/utsname.h>
23 #include "UnixName.h"
24 
25 UnixName::UnixName(int argc, char **argv)
26  : POSIXApplication(argc, argv)
27 {
28  parser().setDescription("Print the operating system name");
29  parser().registerFlag('s', "system", "Print the system name");
30  parser().registerFlag('n', "node", "Print the node name");
31  parser().registerFlag('r', "release", "Print the operating system release");
32  parser().registerFlag('m', "machine", "Print the machine name");
33  parser().registerFlag('a', "all", "Print all available information");
34 }
35 
37 {
38 }
39 
41 {
42  struct utsname info;
43 
44  // Retrieve version information
45  if (uname(&info) < 0)
46  {
47  ERROR("uname() failed: " << strerror(errno));
48  return IOError;
49  }
50 
51  // If no arguments given, just show the system name. */
52  if (arguments().getFlags().count() == 0)
53  {
54  printf("%s", info.sysname);
55  }
56  // Print everything?
57  else if (arguments().get("all"))
58  {
59  printf("%s %s %s %s %s ",
60  info.sysname,
61  info.nodename,
62  info.release,
63  info.version,
64  info.machine);
65  }
66  // Otherwise, interpret argument(s).
67  else
68  {
69  // System name
70  if (arguments().get("system"))
71  printf("%s ", info.sysname);
72 
73  // Node name
74  if (arguments().get("node"))
75  printf("%s ", info.nodename);
76 
77  // Release
78  if (arguments().get("release"))
79  printf("%s ", info.release);
80 
81  // Machine name
82  if (arguments().get("machine"))
83  printf("%s ", info.machine);
84  }
85 
86  // Terminate with a newline
87  printf("\r\n");
88 
89  // Done
90  return Success;
91 }
UnixName::exec
virtual Result exec()
Execute the application.
Definition: UnixName.cpp:40
UnixName::~UnixName
virtual ~UnixName()
Destructor.
Definition: UnixName.cpp:36
utsname::machine
char machine[UTSBUF]
Name of the hardware type on which the system is running.
Definition: utsname.h:56
errno
C int errno
The lvalue errno is used by many functions to return error values.
utsname::version
char version[UTSBUF]
Current version level of this release.
Definition: utsname.h:53
string.h
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
UnixName::UnixName
UnixName(int argc, char **argv)
Constructor.
Definition: UnixName.cpp:25
Application::IOError
@ IOError
Definition: Application.h:57
uname
int uname(struct utsname *name)
Get the name of the current system.
Definition: utsname.cpp:23
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
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::registerFlag
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
Definition: ArgumentParser.cpp:100
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
utsname::sysname
char sysname[UTSBUF]
Name of this implementation of the operating system.
Definition: utsname.h:41
utsname
System name structure.
Definition: utsname.h:38
utsname::nodename
char nodename[UTSBUF]
Name of this node within the communications network to which this node is attached,...
Definition: utsname.h:47
stdlib.h
utsname.h
UnixName.h
utsname::release
char release[UTSBUF]
Current release level of this implementation.
Definition: utsname.h:50
errno.h