1 /* ls.c - command to list files and devices */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2007,2008 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/normal.h>
27 #include <grub/disk.h>
28 #include <grub/device.h>
29 #include <grub/term.h>
30 #include <grub/partition.h>
31 #include <grub/file.h>
33 static const struct grub_arg_option options
[] =
35 {"long", 'l', 0, "show a long list with more detailed information", 0, 0},
36 {"human-readable", 'h', 0, "print sizes in a human readable format", 0, 0},
37 {"all", 'a', 0, "list all files", 0, 0},
41 static const char grub_human_sizes
[] = {' ', 'K', 'M', 'G', 'T'};
44 grub_ls_list_devices (int longlist
)
46 auto int grub_ls_print_devices (const char *name
);
47 int grub_ls_print_devices (const char *name
)
50 grub_normal_print_device_info (name
);
52 grub_printf ("(%s) ", name
);
57 grub_device_iterate (grub_ls_print_devices
);
65 grub_ls_list_files (char *dirname
, int longlist
, int all
, int human
)
71 auto int print_files (const char *filename
, int dir
);
72 auto int print_files_long (const char *filename
, int dir
);
74 int print_files (const char *filename
, int dir
)
76 if (all
|| filename
[0] != '.')
77 grub_printf ("%s%s ", filename
, dir
? "/" : "");
82 int print_files_long (const char *filename
, int dir
)
84 char pathname
[grub_strlen (dirname
) + grub_strlen (filename
) + 1];
86 if ((! all
) && (filename
[0] == '.'))
93 if (dirname
[grub_strlen (dirname
) - 1] == '/')
94 grub_sprintf (pathname
, "%s%s", dirname
, filename
);
96 grub_sprintf (pathname
, "%s/%s", dirname
, filename
);
98 /* XXX: For ext2fs symlinks are detected as files while they
99 should be reported as directories. */
100 file
= grub_file_open (pathname
);
108 grub_printf ("%-12llu", (unsigned long long) file
->size
);
111 grub_uint64_t fsize
= file
->size
* 100ULL;
112 int fsz
= file
->size
;
118 fsize
= (fsize
+ 512) / 1024;
125 grub_uint32_t whole
, fraction
;
127 whole
= grub_divmod64 (fsize
, 100, &fraction
);
128 grub_sprintf (buf
, "%u.%02u%c", whole
, fraction
,
129 grub_human_sizes
[units
]);
130 grub_printf ("%-12s", buf
);
133 grub_printf ("%-12llu", (unsigned long long) file
->size
);
136 grub_file_close (file
);
139 grub_printf ("%-12s", "DIR");
141 grub_printf ("%s%s\n", filename
, dir
? "/" : "");
146 device_name
= grub_file_get_device_name (dirname
);
147 dev
= grub_device_open (device_name
);
151 fs
= grub_fs_probe (dev
);
152 path
= grub_strchr (dirname
, ')');
158 if (! path
&& ! device_name
)
160 grub_error (GRUB_ERR_BAD_ARGUMENT
, "invalid argument");
166 if (grub_errno
== GRUB_ERR_UNKNOWN_FS
)
167 grub_errno
= GRUB_ERR_NONE
;
169 grub_normal_print_device_info (device_name
);
174 (fs
->dir
) (dev
, path
, print_files_long
);
176 (fs
->dir
) (dev
, path
, print_files
);
178 if (grub_errno
== GRUB_ERR_BAD_FILE_TYPE
179 && path
[grub_strlen (path
) - 1] != '/')
181 /* PATH might be a regular file. */
187 file
= grub_file_open (dirname
);
191 grub_file_close (file
);
193 p
= grub_strrchr (dirname
, '/') + 1;
194 dirname
= grub_strndup (dirname
, p
- dirname
);
200 print_files_long (p
, 0);
207 if (grub_errno
== GRUB_ERR_NONE
)
215 grub_device_close (dev
);
217 grub_free (device_name
);
223 grub_cmd_ls (struct grub_arg_list
*state
, int argc
, char **args
)
226 grub_ls_list_devices (state
[0].set
);
228 grub_ls_list_files (args
[0], state
[0].set
, state
[2].set
,
236 (void)mod
; /* To stop warning. */
237 grub_register_command ("ls", grub_cmd_ls
, GRUB_COMMAND_FLAG_BOTH
,
238 "ls [-l|-h|-a] [FILE]",
239 "List devices and files.", options
);
244 grub_unregister_command ("ls");