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
26FileStatus::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
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}
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
const Vector< Argument * > & getPositionals() const
Get positional arguments.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
virtual ~FileStatus()
Destructor.
FileStatus(int argc, char **argv)
Constructor.
Result printStatus(const String &file) const
Concatenate a file.
virtual Result exec()
Execute the application.
POSIX-compatible application.
Abstraction of strings.
Definition String.h:42
Vectors are dynamically resizeable Arrays.
Definition Vector.h:42
virtual Size count() const
Returns the number of items inside the Vector.
Definition Vector.h:204
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 S_ISDIR(m)
Test for a directory.
Definition stat.h:155
#define S_ISBLK(m)
Test for a block special file.
Definition stat.h:149
#define S_ISCHR(m)
Test for a character special file.
Definition stat.h:152
C int printf(const char *format,...)
Output a formatted string to standard output.
Definition printf.cpp:22
#define S_ISREG(m)
Test for a regular file.
Definition stat.h:161
#define ERROR(msg)
Output an error message.
Definition Log.h:61
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
ProcessID major
Major device ID number is a PID.
Definition Types.h:148
u16 minor
Device specific minor ID number.
Definition Types.h:151
The <sys/stat.h> header shall define the stat structure.
Definition stat.h:177
off_t st_size
For regular files, the file size in bytes.
Definition stat.h:226
uid_t st_uid
User ID of file.
Definition stat.h:209
mode_t st_mode
Mode of file.
Definition stat.h:203
ino_t st_ino
File inode number.
Definition stat.h:200
gid_t st_gid
Group ID of file.
Definition stat.h:212
dev_t st_dev
Device ID of device containing file.
Definition stat.h:197