Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
| 3 | * ls.c - list files |
| 4 | * |
| 5 | * Copyright 2012 Andre Renaud <andre@bluewatersys.com> |
| 6 | * |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 7 | * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 8 | |
Andre Renaud | 94d673d | 2012-02-21 20:48:52 -0600 | [diff] [blame] | 9 | USE_LS(NEWTOY(ls, "nRlF1a", TOYFLAG_BIN)) |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 10 | |
| 11 | config LS |
| 12 | bool "ls" |
Rob Landley | b0dcd66 | 2012-02-26 22:04:37 -0600 | [diff] [blame^] | 13 | default n |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 14 | help |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 15 | usage: ls [-l] [-F] [-a] [-1] [directory...] |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 16 | list files |
| 17 | |
| 18 | -F append a character as a file type indicator |
| 19 | -a list all files |
| 20 | -1 list one file per line |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 21 | -l show full details for each file |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 22 | */ |
| 23 | |
Andre Renaud | 94d673d | 2012-02-21 20:48:52 -0600 | [diff] [blame] | 24 | /* So that we can do 64-bit stat etc... */ |
| 25 | #define _FILE_OFFSET_BITS 64 |
| 26 | |
| 27 | #include <unistd.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <grp.h> |
| 30 | #include <pwd.h> |
| 31 | |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 32 | #include "toys.h" |
| 33 | |
| 34 | #define FLAG_a 1 |
| 35 | #define FLAG_1 2 |
| 36 | #define FLAG_F 4 |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 37 | #define FLAG_l 8 |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 38 | #define FLAG_R 16 |
Andre Renaud | 94d673d | 2012-02-21 20:48:52 -0600 | [diff] [blame] | 39 | #define FLAG_n 32 |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 40 | |
| 41 | static int dir_filter(const struct dirent *d) |
| 42 | { |
| 43 | /* Skip over the . & .. entries unless -a is given */ |
| 44 | if (!(toys.optflags & FLAG_a)) |
| 45 | if (d->d_name[0] == '.') |
| 46 | return 0; |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | static void do_ls(int fd, char *name) |
| 51 | { |
| 52 | struct dirent **entries; |
| 53 | int nentries; |
| 54 | int i; |
| 55 | int maxwidth = -1; |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 56 | int ncolumns = 1; |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 57 | struct dirent file_dirent; |
| 58 | struct dirent *file_direntp; |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 59 | |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 60 | if (!name || strcmp(name, "-") == 0) |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 61 | name = "."; |
| 62 | |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 63 | if (toys.optflags & FLAG_R) |
| 64 | xprintf("\n%s:\n", name); |
| 65 | |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 66 | /* Get all the files in this directory */ |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 67 | nentries = scandir(name, &entries, dir_filter, alphasort); |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 68 | if (nentries < 0) { |
| 69 | /* We've just selected a single file, so create a single-length list */ |
| 70 | /* FIXME: This means that ls *.x results in a whole bunch of single |
| 71 | * listings, not one combined listing. |
| 72 | */ |
| 73 | if (errno == ENOTDIR) { |
| 74 | nentries = 1; |
| 75 | strcpy(file_dirent.d_name, name); |
| 76 | file_direntp = &file_dirent; |
| 77 | entries = &file_direntp; |
| 78 | } else |
| 79 | perror_exit("ls: cannot access %s'", name); |
| 80 | } |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 81 | |
| 82 | |
| 83 | /* Determine the widest entry so we can flow them properly */ |
| 84 | if (!(toys.optflags & FLAG_1)) { |
| 85 | int columns; |
| 86 | char *columns_str; |
| 87 | |
| 88 | for (i = 0; i < nentries; i++) { |
| 89 | struct dirent *ent = entries[i]; |
| 90 | int width; |
| 91 | |
| 92 | width = strlen(ent->d_name); |
| 93 | if (width > maxwidth) |
| 94 | maxwidth = width; |
| 95 | } |
| 96 | /* We always want at least a single space for each entry */ |
| 97 | maxwidth++; |
| 98 | if (toys.optflags & FLAG_F) |
| 99 | maxwidth++; |
| 100 | |
| 101 | columns_str = getenv("COLUMNS"); |
| 102 | columns = columns_str ? atoi(columns_str) : 80; |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 103 | ncolumns = maxwidth ? columns / maxwidth : 1; |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | for (i = 0; i < nentries; i++) { |
| 107 | struct dirent *ent = entries[i]; |
| 108 | int len = strlen(ent->d_name); |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 109 | struct stat st; |
| 110 | int stat_valid = 0; |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 111 | |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 112 | sprintf(toybuf, "%s/%s", name, ent->d_name); |
| 113 | |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 114 | /* Provide the ls -l long output */ |
| 115 | if (toys.optflags & FLAG_l) { |
| 116 | char type; |
| 117 | char timestamp[64]; |
| 118 | struct tm mtime; |
| 119 | |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 120 | if (lstat(toybuf, &st)) |
| 121 | perror_exit("Can't stat %s", toybuf); |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 122 | stat_valid = 1; |
| 123 | if (S_ISDIR(st.st_mode)) |
| 124 | type = 'd'; |
| 125 | else if (S_ISCHR(st.st_mode)) |
| 126 | type = 'c'; |
| 127 | else if (S_ISBLK(st.st_mode)) |
| 128 | type = 'b'; |
| 129 | else if (S_ISLNK(st.st_mode)) |
| 130 | type = 'l'; |
| 131 | else |
| 132 | type = '-'; |
| 133 | |
| 134 | xprintf("%c%c%c%c%c%c%c%c%c%c ", type, |
| 135 | (st.st_mode & S_IRUSR) ? 'r' : '-', |
| 136 | (st.st_mode & S_IWUSR) ? 'w' : '-', |
| 137 | (st.st_mode & S_IXUSR) ? 'x' : '-', |
| 138 | (st.st_mode & S_IRGRP) ? 'r' : '-', |
| 139 | (st.st_mode & S_IWGRP) ? 'w' : '-', |
| 140 | (st.st_mode & S_IXGRP) ? 'x' : '-', |
| 141 | (st.st_mode & S_IROTH) ? 'r' : '-', |
| 142 | (st.st_mode & S_IWOTH) ? 'w' : '-', |
| 143 | (st.st_mode & S_IXOTH) ? 'x' : '-'); |
| 144 | |
| 145 | xprintf("%2d ", st.st_nlink); |
Andre Renaud | 94d673d | 2012-02-21 20:48:52 -0600 | [diff] [blame] | 146 | if (toys.optflags & FLAG_n) { |
| 147 | xprintf("%4d ", st.st_uid); |
| 148 | xprintf("%4d ", st.st_gid); |
| 149 | } else { |
| 150 | struct passwd *pwd = getpwuid(st.st_uid); |
| 151 | struct group *grp = getgrgid(st.st_gid); |
| 152 | if (!pwd) |
| 153 | xprintf("%4d ", st.st_uid); |
| 154 | else |
| 155 | xprintf("%-10s ", pwd->pw_name); |
| 156 | if (!grp) |
| 157 | xprintf("%4d ", st.st_gid); |
| 158 | else |
| 159 | xprintf("%-10s ", grp->gr_name); |
| 160 | } |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 161 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) |
| 162 | xprintf("%3d, %3d ", major(st.st_rdev), minor(st.st_rdev)); |
| 163 | else |
Andre Renaud | 94d673d | 2012-02-21 20:48:52 -0600 | [diff] [blame] | 164 | xprintf("%12lld ", st.st_size); |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 165 | |
| 166 | localtime_r(&st.st_mtime, &mtime); |
| 167 | |
| 168 | strftime(timestamp, sizeof(timestamp), "%b %e %H:%M", &mtime); |
| 169 | xprintf("%s ", timestamp); |
| 170 | } |
| 171 | |
| 172 | xprintf("%s", ent->d_name); |
| 173 | |
| 174 | /* Append the file-type indicator character */ |
| 175 | if (toys.optflags & FLAG_F) { |
| 176 | if (!stat_valid) { |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 177 | if (lstat(toybuf, &st)) |
| 178 | perror_exit("Can't stat %s", toybuf); |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 179 | stat_valid = 1; |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 180 | } |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 181 | if (S_ISDIR(st.st_mode)) { |
| 182 | xprintf("/"); |
| 183 | len++; |
| 184 | } else if (S_ISREG(st.st_mode) && |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 185 | (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) { |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 186 | xprintf("*"); |
| 187 | len++; |
| 188 | } else if (S_ISLNK(st.st_mode)) { |
| 189 | xprintf("@"); |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 190 | len++; |
| 191 | } |
| 192 | } |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 193 | if (toys.optflags & FLAG_1) { |
| 194 | xprintf("\n"); |
| 195 | } else { |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 196 | if (i % ncolumns == ncolumns - 1) |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 197 | xprintf("\n"); |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 198 | else |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 199 | xprintf("%*s", maxwidth - len, ""); |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | /* Make sure we put at a trailing new line in */ |
| 203 | if (!(toys.optflags & FLAG_1) && (i % ncolumns)) |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 204 | xprintf("\n"); |
Rob Landley | 4797bc2 | 2012-02-18 15:19:00 -0600 | [diff] [blame] | 205 | |
| 206 | if (toys.optflags & FLAG_R) { |
| 207 | for (i = 0; i < nentries; i++) { |
| 208 | struct dirent *ent = entries[i]; |
| 209 | struct stat st; |
| 210 | char dirname[PATH_MAX]; |
| 211 | |
| 212 | sprintf(dirname, "%s/%s", name, ent->d_name); |
| 213 | if (lstat(dirname, &st)) |
| 214 | perror_exit("Can't stat %s", dirname); |
| 215 | if (S_ISDIR(st.st_mode)) |
| 216 | do_ls(0, dirname); |
| 217 | } |
| 218 | } |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void ls_main(void) |
| 222 | { |
Andre Renaud | 94d673d | 2012-02-21 20:48:52 -0600 | [diff] [blame] | 223 | /* If the output is not a TTY, then just do one-file per line |
| 224 | * This makes ls easier to use with other command line tools (grep/awk etc...) |
| 225 | */ |
| 226 | if (!isatty(fileno(stdout))) |
| 227 | toys.optflags |= FLAG_1; |
Rob Landley | da5aa3a | 2012-02-13 21:27:50 -0600 | [diff] [blame] | 228 | /* Long output must be one-file per line */ |
| 229 | if (toys.optflags & FLAG_l) |
| 230 | toys.optflags |= FLAG_1; |
Rob Landley | 3d8685b | 2012-02-13 21:26:27 -0600 | [diff] [blame] | 231 | loopfiles(toys.optargs, do_ls); |
| 232 | } |