Serenity Operating System
at hosted 134 lines 4.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include <AK/String.h> 29#include <LibCore/ArgsParser.h> 30#include <LibCore/DateTime.h> 31#include <grp.h> 32#include <pwd.h> 33#include <stdio.h> 34#include <sys/stat.h> 35#include <time.h> 36#include <unistd.h> 37 38static int stat(const char* file, bool should_follow_links) 39{ 40 struct stat st; 41 int rc = should_follow_links ? stat(file, &st) : lstat(file, &st); 42 if (rc < 0) { 43 perror("lstat"); 44 return 1; 45 } 46 printf(" File: %s\n", file); 47 printf(" Inode: %u\n", st.st_ino); 48 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) 49 printf(" Device: %u,%u\n", major(st.st_rdev), minor(st.st_rdev)); 50 else 51 printf(" Size: %u\n", st.st_size); 52 printf(" Links: %u\n", st.st_nlink); 53 printf(" Blocks: %u\n", st.st_blocks); 54 printf(" UID: %u", st.st_uid); 55 if (auto* pwd = getpwuid(st.st_uid)) { 56 printf(" (%s)", pwd->pw_name); 57 } 58 printf("\n"); 59 printf(" GID: %u", st.st_gid); 60 if (auto* grp = getgrgid(st.st_gid)) { 61 printf(" (%s)", grp->gr_name); 62 } 63 printf("\n"); 64 printf(" Mode: (%o/", st.st_mode); 65 66 if (S_ISDIR(st.st_mode)) 67 printf("d"); 68 else if (S_ISLNK(st.st_mode)) 69 printf("l"); 70 else if (S_ISBLK(st.st_mode)) 71 printf("b"); 72 else if (S_ISCHR(st.st_mode)) 73 printf("c"); 74 else if (S_ISFIFO(st.st_mode)) 75 printf("f"); 76 else if (S_ISSOCK(st.st_mode)) 77 printf("s"); 78 else if (S_ISREG(st.st_mode)) 79 printf("-"); 80 else 81 printf("?"); 82 83 printf("%c%c%c%c%c%c%c%c", 84 st.st_mode & S_IRUSR ? 'r' : '-', 85 st.st_mode & S_IWUSR ? 'w' : '-', 86 st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'), 87 st.st_mode & S_IRGRP ? 'r' : '-', 88 st.st_mode & S_IWGRP ? 'w' : '-', 89 st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'), 90 st.st_mode & S_IROTH ? 'r' : '-', 91 st.st_mode & S_IWOTH ? 'w' : '-'); 92 93 if (st.st_mode & S_ISVTX) 94 printf("t"); 95 else 96 printf("%c", st.st_mode & S_IXOTH ? 'x' : '-'); 97 98 printf(")\n"); 99 100 auto print_time = [](time_t t) { 101 printf("%s\n", Core::DateTime::from_timestamp(t).to_string().characters()); 102 }; 103 104 printf("Accessed: "); 105 print_time(st.st_atime); 106 printf("Modified: "); 107 print_time(st.st_mtime); 108 printf(" Changed: "); 109 print_time(st.st_ctime); 110 111 return 0; 112} 113 114int main(int argc, char** argv) 115{ 116 if (pledge("stdio rpath", nullptr) < 0) { 117 perror("pledge"); 118 return 1; 119 } 120 121 bool should_follow_links = false; 122 Vector<const char*> files; 123 124 auto args_parser = Core::ArgsParser(); 125 args_parser.add_option(should_follow_links, "Follow links to files", nullptr, 'L'); 126 args_parser.add_positional_argument(files, "File(s) to stat", "file", Core::ArgsParser::Required::Yes); 127 args_parser.parse(argc, argv); 128 129 int ret = 0; 130 for (auto& file : files) 131 ret |= stat(file, should_follow_links); 132 133 return ret; 134}