1 /* ls.c - command to list files and devices */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/types.h>
21 #include <grub/misc.h>
25 #include <grub/disk.h>
26 #include <grub/device.h>
27 #include <grub/term.h>
28 #include <grub/partition.h>
29 #include <grub/file.h>
30 #include <grub/normal.h>
31 #include <grub/extcmd.h>
32 #include <grub/datetime.h>
33 #include <grub/i18n.h>
36 GRUB_MOD_LICENSE ("GPLv3+");
38 static const struct grub_arg_option options[] =
40 {"long", 'l', 0, N_("Show a long list with more detailed information."), 0, 0},
41 {"human-readable", 'h', 0, N_("Print sizes in a human readable format."), 0, 0},
42 {"all", 'a', 0, N_("List all files."), 0, 0},
46 /* Helper for grub_ls_list_devices. */
48 grub_ls_print_devices (const char *name, void *data)
53 grub_normal_print_device_info (name);
55 grub_printf ("(%s) ", name);
61 grub_ls_list_devices (int longlist)
63 grub_device_iterate (grub_ls_print_devices, &longlist);
68 grub_net_app_level_t proto;
70 FOR_NET_APP_LEVEL (proto)
73 grub_puts_ (N_ ("Network protocols:"));
75 grub_printf ("%s ", proto->name);
86 /* Context for grub_ls_list_files. */
87 struct grub_ls_list_files_ctx
94 /* Helper for grub_ls_list_files. */
96 print_files (const char *filename, const struct grub_dirhook_info *info,
99 struct grub_ls_list_files_ctx *ctx = data;
101 if (ctx->all || filename[0] != '.')
102 grub_printf ("%s%s ", filename, info->dir ? "/" : "");
107 /* Helper for grub_ls_list_files. */
109 print_files_long (const char *filename, const struct grub_dirhook_info *info,
112 struct grub_ls_list_files_ctx *ctx = data;
114 if ((! ctx->all) && (filename[0] == '.'))
122 if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/')
123 pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
125 pathname = grub_xasprintf ("%s/%s", ctx->dirname, filename);
130 /* XXX: For ext2fs symlinks are detected as files while they
131 should be reported as directories. */
132 grub_file_filter_disable_compression ();
133 file = grub_file_open (pathname);
137 grub_free (pathname);
142 grub_printf ("%-12llu", (unsigned long long) file->size);
144 grub_printf ("%-12s", grub_get_human_size (file->size,
145 GRUB_HUMAN_SIZE_SHORT));
146 grub_file_close (file);
147 grub_free (pathname);
150 grub_printf ("%-12s", _("DIR"));
154 struct grub_datetime datetime;
155 grub_unixtime2datetime (info->mtime, &datetime);
157 grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
158 datetime.year, datetime.month, datetime.day,
159 datetime.hour, datetime.minute,
161 grub_get_weekday_name (&datetime));
163 grub_printf (" %04d%02d%02d%02d%02d%02d ",
164 datetime.year, datetime.month,
165 datetime.day, datetime.hour,
166 datetime.minute, datetime.second);
168 grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
174 grub_ls_list_files (char *dirname, int longlist, int all, int human)
181 device_name = grub_file_get_device_name (dirname);
182 dev = grub_device_open (device_name);
186 fs = grub_fs_probe (dev);
187 path = grub_strchr (dirname, ')');
193 if (! path && ! device_name)
195 grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
201 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
202 grub_errno = GRUB_ERR_NONE;
204 grub_normal_print_device_info (device_name);
208 struct grub_ls_list_files_ctx ctx = {
215 (fs->dir) (dev, path, print_files_long, &ctx);
217 (fs->dir) (dev, path, print_files, &ctx);
219 if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
220 && path[grub_strlen (path) - 1] != '/')
222 /* PATH might be a regular file. */
225 struct grub_dirhook_info info;
228 grub_file_filter_disable_compression ();
229 file = grub_file_open (dirname);
233 grub_file_close (file);
235 p = grub_strrchr (dirname, '/') + 1;
236 dirname = grub_strndup (dirname, p - dirname);
241 grub_memset (&info, 0, sizeof (info));
243 print_files_long (p, &info, &ctx);
245 print_files (p, &info, &ctx);
250 if (grub_errno == GRUB_ERR_NONE)
258 grub_device_close (dev);
260 grub_free (device_name);
266 grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
268 struct grub_arg_list *state = ctxt->state;
272 grub_ls_list_devices (state[0].set);
274 for (i = 0; i < argc; i++)
275 grub_ls_list_files (args[i], state[0].set, state[2].set,
281 static grub_extcmd_t cmd;
285 cmd = grub_register_extcmd ("ls", grub_cmd_ls, 0,
286 N_("[-l|-h|-a] [FILE ...]"),
287 N_("List devices and files."), options);
292 grub_unregister_extcmd (cmd);