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>
35 GRUB_MOD_LICENSE ("GPLv3+");
37 static const struct grub_arg_option options
[] =
39 {"long", 'l', 0, N_("Show a long list with more detailed information."), 0, 0},
40 {"human-readable", 'h', 0, N_("Print sizes in a human readable format."), 0, 0},
41 {"all", 'a', 0, N_("List all files."), 0, 0},
45 static const char grub_human_sizes
[] = {' ', 'K', 'M', 'G', 'T'};
48 grub_ls_list_devices (int longlist
)
50 auto int grub_ls_print_devices (const char *name
);
51 int grub_ls_print_devices (const char *name
)
54 grub_normal_print_device_info (name
);
56 grub_printf ("(%s) ", name
);
61 grub_device_iterate (grub_ls_print_devices
);
69 grub_ls_list_files (char *dirname
, int longlist
, int all
, int human
)
76 auto int print_files (const char *filename
,
77 const struct grub_dirhook_info
*info
);
78 auto int print_files_long (const char *filename
,
79 const struct grub_dirhook_info
*info
);
81 int print_files (const char *filename
, const struct grub_dirhook_info
*info
)
83 if (all
|| filename
[0] != '.')
84 grub_printf ("%s%s ", filename
, info
->dir
? "/" : "");
89 int print_files_long (const char *filename
,
90 const struct grub_dirhook_info
*info
)
92 if ((! all
) && (filename
[0] == '.'))
100 if (dirname
[grub_strlen (dirname
) - 1] == '/')
101 pathname
= grub_xasprintf ("%s%s", dirname
, filename
);
103 pathname
= grub_xasprintf ("%s/%s", dirname
, filename
);
108 /* XXX: For ext2fs symlinks are detected as files while they
109 should be reported as directories. */
110 grub_file_filter_disable_compression ();
111 file
= grub_file_open (pathname
);
115 grub_free (pathname
);
120 grub_printf ("%-12llu", (unsigned long long) file
->size
);
123 grub_uint64_t fsize
= file
->size
* 100ULL;
124 int fsz
= file
->size
;
130 fsize
= (fsize
+ 512) / 1024;
137 grub_uint64_t whole
, fraction
;
139 whole
= grub_divmod64 (fsize
, 100, &fraction
);
140 grub_snprintf (buf
, sizeof (buf
),
141 "%" PRIuGRUB_UINT64_T
142 ".%02" PRIuGRUB_UINT64_T
"%c", whole
, fraction
,
143 grub_human_sizes
[units
]);
144 grub_printf ("%-12s", buf
);
147 grub_printf ("%-12llu", (unsigned long long) file
->size
);
150 grub_file_close (file
);
151 grub_free (pathname
);
154 grub_printf ("%-12s", "DIR");
158 struct grub_datetime datetime
;
159 grub_unixtime2datetime (info
->mtime
, &datetime
);
161 grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
162 datetime
.year
, datetime
.month
, datetime
.day
,
163 datetime
.hour
, datetime
.minute
,
165 grub_get_weekday_name (&datetime
));
167 grub_printf (" %04d%02d%02d%02d%02d%02d ",
168 datetime
.year
, datetime
.month
,
169 datetime
.day
, datetime
.hour
,
170 datetime
.minute
, datetime
.second
);
172 grub_printf ("%s%s\n", filename
, info
->dir
? "/" : "");
177 device_name
= grub_file_get_device_name (dirname
);
178 dev
= grub_device_open (device_name
);
182 fs
= grub_fs_probe (dev
);
183 path
= grub_strchr (dirname
, ')');
189 if (! path
&& ! device_name
)
191 grub_error (GRUB_ERR_BAD_ARGUMENT
, "invalid argument");
197 if (grub_errno
== GRUB_ERR_UNKNOWN_FS
)
198 grub_errno
= GRUB_ERR_NONE
;
200 grub_normal_print_device_info (device_name
);
205 (fs
->dir
) (dev
, path
, print_files_long
);
207 (fs
->dir
) (dev
, path
, print_files
);
209 if (grub_errno
== GRUB_ERR_BAD_FILE_TYPE
210 && path
[grub_strlen (path
) - 1] != '/')
212 /* PATH might be a regular file. */
215 struct grub_dirhook_info info
;
218 grub_file_filter_disable_compression ();
219 file
= grub_file_open (dirname
);
223 grub_file_close (file
);
225 p
= grub_strrchr (dirname
, '/') + 1;
226 dirname
= grub_strndup (dirname
, p
- dirname
);
231 grub_memset (&info
, 0, sizeof (info
));
233 print_files_long (p
, &info
);
235 print_files (p
, &info
);
240 if (grub_errno
== GRUB_ERR_NONE
)
248 grub_device_close (dev
);
250 grub_free (device_name
);
256 grub_cmd_ls (grub_extcmd_context_t ctxt
, int argc
, char **args
)
258 struct grub_arg_list
*state
= ctxt
->state
;
262 grub_ls_list_devices (state
[0].set
);
264 for (i
= 0; i
< argc
; i
++)
265 grub_ls_list_files (args
[i
], state
[0].set
, state
[2].set
,
271 static grub_extcmd_t cmd
;
275 cmd
= grub_register_extcmd ("ls", grub_cmd_ls
, 0,
276 N_("[-l|-h|-a] [FILE ...]"),
277 N_("List devices and files."), options
);
282 grub_unregister_extcmd (cmd
);