FreeNOS
FileStatus.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 <sys/stat.h>
23 #include <Log.h>
24 #include "FileStatus.h"
25 
26 FileStatus::FileStatus(int argc, char **argv)
27  : POSIXApplication(argc, argv)
28 {
29  parser().setDescription("Retrieve file status from the filesystem");
30  parser().registerPositional("FILE", "Name of the file(s) to stat", 0);
31 }
32 
34 {
35 }
36 
38 {
39  const Vector<Argument *> & positionals = arguments().getPositionals();
40  Result result = Success;
41  Result ret = Success;
42 
43  // Perform a stat for each file
44  for (Size i = 0; i < positionals.count(); i++)
45  {
46  // Stat the file immediately
47  result = printStatus(positionals[i]->getValue());
48 
49  // Update exit status, if needed
50  if (result != Success)
51  {
52  ret = result;
53  }
54  }
55 
56  // Done
57  return ret;
58 }
59 
61 {
62  struct stat st;
63 
64  // Try to stat the file
65  if ((stat(*file, &st)) < 0)
66  {
67  ERROR("failed to stat `" << *file << "': " << strerror(errno));
68  return IOError;
69  }
70 
71  // Output file statistics
72  printf("File: %s\r\n", *file);
73  printf("Type: ");
74 
75  // Print the right file type
76  if (S_ISREG(st.st_mode))
77  {
78  printf("Regular File\r\n");
79  }
80  else if (S_ISDIR(st.st_mode))
81  {
82  printf("Directory\r\n");
83  }
84  else if (S_ISCHR(st.st_mode))
85  {
86  printf("Character Device\r\n");
87  printf("Major ID: %u\r\n", st.st_dev.major);
88  printf("Minor ID: %u\r\n", st.st_dev.minor);
89  }
90  else if (S_ISBLK(st.st_mode))
91  {
92  printf("Block Device\r\n");
93  printf("Major ID: %u\r\n", st.st_dev.major);
94  printf("Minor ID: %u\r\n", st.st_dev.minor);
95  }
96  else
97  {
98  printf("Unknown\r\n");
99  }
100 
101  // Print additional file information fields
102  printf("Inode: %u\r\n", st.st_ino);
103  printf("Mode: %u\r\n", st.st_mode);
104  printf("Size: %u\r\n", st.st_size);
105  printf("Uid: %u\r\n", st.st_uid);
106  printf("Gid: %u\r\n", st.st_gid);
107 
108  // Done
109  return Success;
110 }
S_ISBLK
#define S_ISBLK(m)
Test for a block special file.
Definition: stat.h:149
stat::st_size
off_t st_size
For regular files, the file size in bytes.
Definition: stat.h:226
S_ISDIR
#define S_ISDIR(m)
Test for a directory.
Definition: stat.h:155
stat
The <sys/stat.h> header shall define the stat structure.
Definition: stat.h:176
DeviceID::minor
u16 minor
Device specific minor ID number.
Definition: Types.h:151
errno
C int errno
The lvalue errno is used by many functions to return error values.
stat::st_uid
uid_t st_uid
User ID of file.
Definition: stat.h:209
string.h
String
Abstraction of strings.
Definition: String.h:41
FileStatus::exec
virtual Result exec()
Execute the application.
Definition: FileStatus.cpp:37
stat::st_dev
dev_t st_dev
Device ID of device containing file.
Definition: stat.h:197
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
Log.h
Application::IOError
@ IOError
Definition: Application.h:57
S_ISCHR
#define S_ISCHR(m)
Test for a character special file.
Definition: stat.h:152
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
Vector::count
virtual Size count() const
Returns the number of items inside the Vector.
Definition: Vector.h:204
stdio.h
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
stat::st_ino
ino_t st_ino
File inode number.
Definition: stat.h:200
S_ISREG
#define S_ISREG(m)
Test for a regular file.
Definition: stat.h:161
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
stat::st_mode
mode_t st_mode
Mode of file.
Definition: stat.h:203
Application::Result
Result
Result codes.
Definition: Application.h:53
FileStatus::FileStatus
FileStatus(int argc, char **argv)
Constructor.
Definition: FileStatus.cpp:26
DeviceID::major
ProcessID major
Major device ID number is a PID.
Definition: Types.h:148
stat
int stat(const char *path, struct stat *buf)
Get file status.
Definition: stat.cpp:25
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
stat.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
stat::st_gid
gid_t st_gid
Group ID of file.
Definition: stat.h:212
stdlib.h
FileStatus::~FileStatus
virtual ~FileStatus()
Destructor.
Definition: FileStatus.cpp:33
FileStatus::printStatus
Result printStatus(const String &file) const
Concatenate a file.
Definition: FileStatus.cpp:60
Vector< Argument * >
errno.h
FileStatus.h
ArgumentContainer::getPositionals
const Vector< Argument * > & getPositionals() const
Get positional arguments.
Definition: ArgumentContainer.cpp:39