lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ls.c --- list directories |
| 3 | * |
| 4 | * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | #include "config.h" |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <ctype.h> |
| 13 | #include <string.h> |
| 14 | #include <time.h> |
| 15 | #ifdef HAVE_ERRNO_H |
| 16 | #include <errno.h> |
| 17 | #endif |
| 18 | #include <sys/types.h> |
| 19 | #ifdef HAVE_GETOPT_H |
| 20 | #include <getopt.h> |
| 21 | #else |
| 22 | extern int optind; |
| 23 | extern char *optarg; |
| 24 | #endif |
| 25 | |
| 26 | #include "debugfs.h" |
| 27 | |
| 28 | /* |
| 29 | * list directory |
| 30 | */ |
| 31 | |
| 32 | #define LONG_OPT 0x0001 |
| 33 | #define DELETED_OPT 0x0002 |
| 34 | #define PARSE_OPT 0x0004 |
| 35 | |
| 36 | struct list_dir_struct { |
| 37 | FILE *f; |
| 38 | int col; |
| 39 | int options; |
| 40 | }; |
| 41 | |
| 42 | static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 43 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
| 44 | |
| 45 | static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), |
| 46 | int entry, |
| 47 | struct ext2_dir_entry *dirent, |
| 48 | int offset EXT2FS_ATTR((unused)), |
| 49 | int blocksize EXT2FS_ATTR((unused)), |
| 50 | char *buf EXT2FS_ATTR((unused)), |
| 51 | void *private) |
| 52 | { |
| 53 | struct ext2_inode inode; |
| 54 | ext2_ino_t ino; |
| 55 | struct tm *tm_p; |
| 56 | time_t modtime; |
| 57 | char name[EXT2_NAME_LEN + 1]; |
| 58 | char tmp[EXT2_NAME_LEN + 16]; |
| 59 | char datestr[80]; |
| 60 | char lbr, rbr; |
| 61 | int thislen; |
| 62 | struct list_dir_struct *ls = (struct list_dir_struct *) private; |
| 63 | |
| 64 | thislen = dirent->name_len & 0xFF; |
| 65 | strncpy(name, dirent->name, thislen); |
| 66 | name[thislen] = '\0'; |
| 67 | ino = dirent->inode; |
| 68 | |
| 69 | if (entry == DIRENT_DELETED_FILE) { |
| 70 | lbr = '<'; |
| 71 | rbr = '>'; |
| 72 | ino = 0; |
| 73 | } else { |
| 74 | lbr = rbr = ' '; |
| 75 | } |
| 76 | if (ls->options & PARSE_OPT) { |
| 77 | if (ino) { |
| 78 | if (debugfs_read_inode(ino, &inode, name)) |
| 79 | return 0; |
| 80 | } else |
| 81 | memset(&inode, 0, sizeof(struct ext2_inode)); |
| 82 | fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name); |
| 83 | if (LINUX_S_ISDIR(inode.i_mode)) |
| 84 | fprintf(ls->f, "/"); |
| 85 | else |
| 86 | fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode)); |
| 87 | fprintf(ls->f, "\n"); |
| 88 | } else if (ls->options & LONG_OPT) { |
| 89 | if (ino) { |
| 90 | if (debugfs_read_inode(ino, &inode, name)) |
| 91 | return 0; |
| 92 | modtime = inode.i_mtime; |
| 93 | tm_p = localtime(&modtime); |
| 94 | sprintf(datestr, "%2d-%s-%4d %02d:%02d", |
| 95 | tm_p->tm_mday, monstr[tm_p->tm_mon], |
| 96 | 1900 + tm_p->tm_year, tm_p->tm_hour, |
| 97 | tm_p->tm_min); |
| 98 | } else { |
| 99 | strcpy(datestr, " "); |
| 100 | memset(&inode, 0, sizeof(struct ext2_inode)); |
| 101 | } |
| 102 | fprintf(ls->f, "%c%6u%c %6o (%d) %5d %5d ", lbr, ino, rbr, |
| 103 | inode.i_mode, dirent->name_len >> 8, |
| 104 | inode_uid(inode), inode_gid(inode)); |
| 105 | if (LINUX_S_ISDIR(inode.i_mode)) |
| 106 | fprintf(ls->f, "%5d", inode.i_size); |
| 107 | else |
| 108 | fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode)); |
| 109 | fprintf (ls->f, " %s %s\n", datestr, name); |
| 110 | } else { |
| 111 | sprintf(tmp, "%c%u%c (%d) %s ", lbr, dirent->inode, rbr, |
| 112 | dirent->rec_len, name); |
| 113 | thislen = strlen(tmp); |
| 114 | |
| 115 | if (ls->col + thislen > 80) { |
| 116 | fprintf(ls->f, "\n"); |
| 117 | ls->col = 0; |
| 118 | } |
| 119 | fprintf(ls->f, "%s", tmp); |
| 120 | ls->col += thislen; |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | void do_list_dir(int argc, char *argv[]) |
| 126 | { |
| 127 | ext2_ino_t inode; |
| 128 | int retval; |
| 129 | int c; |
| 130 | int flags; |
| 131 | struct list_dir_struct ls; |
| 132 | |
| 133 | ls.options = 0; |
| 134 | if (check_fs_open(argv[0])) |
| 135 | return; |
| 136 | |
| 137 | reset_getopt(); |
| 138 | while ((c = getopt (argc, argv, "dlp")) != EOF) { |
| 139 | switch (c) { |
| 140 | case 'l': |
| 141 | ls.options |= LONG_OPT; |
| 142 | break; |
| 143 | case 'd': |
| 144 | ls.options |= DELETED_OPT; |
| 145 | break; |
| 146 | case 'p': |
| 147 | ls.options |= PARSE_OPT; |
| 148 | break; |
| 149 | default: |
| 150 | goto print_usage; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (argc > optind+1) { |
| 155 | print_usage: |
| 156 | com_err(0, 0, "Usage: ls [-l] [-d] [-p] file"); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (argc == optind) |
| 161 | inode = cwd; |
| 162 | else |
| 163 | inode = string_to_inode(argv[optind]); |
| 164 | if (!inode) |
| 165 | return; |
| 166 | |
| 167 | ls.f = open_pager(); |
| 168 | ls.col = 0; |
| 169 | flags = DIRENT_FLAG_INCLUDE_EMPTY; |
| 170 | if (ls.options & DELETED_OPT) |
| 171 | flags |= DIRENT_FLAG_INCLUDE_REMOVED; |
| 172 | |
| 173 | retval = ext2fs_dir_iterate2(current_fs, inode, flags, |
| 174 | 0, list_dir_proc, &ls); |
| 175 | fprintf(ls.f, "\n"); |
| 176 | close_pager(ls.f); |
| 177 | if (retval) |
| 178 | com_err(argv[1], retval, 0); |
| 179 | |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | |