]>
git.proxmox.com Git - grub2.git/blob - commands/ls.c
1 /* ls.c - command to list files and devices */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005 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 2 of the License, or
9 * (at your option) any later version.
11 * This program 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <grub/types.h>
22 #include <grub/misc.h>
26 #include <grub/normal.h>
28 #include <grub/disk.h>
29 #include <grub/device.h>
30 #include <grub/term.h>
31 #include <grub/partition.h>
32 #include <grub/file.h>
34 static const struct grub_arg_option options
[] =
36 {"long", 'l', 0, "show a long list with more detailed information", 0, 0},
37 {"human-readable", 'h', 0, "print sizes in a human readable format", 0, 0},
38 {"all", 'a', 0, "list all files", 0, 0},
42 static const char grub_human_sizes
[] = {' ', 'K', 'M', 'G', 'T'};
45 grub_ls_list_devices (int longlist
)
47 auto int grub_ls_print_devices (const char *name
);
48 int grub_ls_print_devices (const char *name
)
51 grub_normal_print_device_info (name
);
53 grub_printf ("(%s) ", name
);
58 grub_device_iterate (grub_ls_print_devices
);
66 grub_ls_list_files (char *dirname
, int longlist
, int all
, int human
)
72 auto int print_files (const char *filename
, int dir
);
73 auto int print_files_long (const char *filename
, int dir
);
75 int print_files (const char *filename
, int dir
)
77 if (all
|| filename
[0] != '.')
78 grub_printf ("%s%s ", filename
, dir
? "/" : "");
83 int print_files_long (const char *filename
, int dir
)
85 char pathname
[grub_strlen (dirname
) + grub_strlen (filename
) + 1];
87 if ((! all
) && (filename
[0] == '.'))
94 if (dirname
[grub_strlen (dirname
) - 1] == '/')
95 grub_sprintf (pathname
, "%s%s", dirname
, filename
);
97 grub_sprintf (pathname
, "%s/%s", dirname
, filename
);
99 /* XXX: For ext2fs symlinks are detected as files while they
100 should be reported as directories. */
101 file
= grub_file_open (pathname
);
109 grub_printf ("%-12d", file
->size
);
112 float fsize
= file
->size
;
113 int fsz
= file
->size
;
126 grub_sprintf (buf
, "%0.2f%c", fsize
, grub_human_sizes
[units
]);
127 grub_printf ("%-12s", buf
);
130 grub_printf ("%-12d", file
->size
);
133 grub_file_close (file
);
136 grub_printf ("%-12s", "DIR");
138 grub_printf ("%s%s\n", filename
, dir
? "/" : "");
143 device_name
= grub_file_get_device_name (dirname
);
144 dev
= grub_device_open (device_name
);
148 fs
= grub_fs_probe (dev
);
149 path
= grub_strchr (dirname
, ')');
155 if (! path
&& ! device_name
)
157 grub_error (GRUB_ERR_BAD_ARGUMENT
, "invalid argument");
163 if (grub_errno
== GRUB_ERR_UNKNOWN_FS
)
164 grub_errno
= GRUB_ERR_NONE
;
166 grub_normal_print_device_info (device_name
);
171 (fs
->dir
) (dev
, path
, print_files_long
);
173 (fs
->dir
) (dev
, path
, print_files
);
175 if (grub_errno
== GRUB_ERR_BAD_FILE_TYPE
176 && path
[grub_strlen (path
) - 1] != '/')
178 /* PATH might be a regular file. */
184 file
= grub_file_open (dirname
);
188 grub_file_close (file
);
190 p
= grub_strrchr (dirname
, '/') + 1;
191 dirname
= grub_strndup (dirname
, p
- dirname
);
197 print_files_long (p
, 0);
204 if (grub_errno
== GRUB_ERR_NONE
)
212 grub_device_close (dev
);
214 grub_free (device_name
);
220 grub_cmd_ls (struct grub_arg_list
*state
, int argc
, char **args
)
223 grub_ls_list_devices (state
[0].set
);
225 grub_ls_list_files (args
[0], state
[0].set
, state
[2].set
,
233 (void)mod
; /* To stop warning. */
234 grub_register_command ("ls", grub_cmd_ls
, GRUB_COMMAND_FLAG_BOTH
,
235 "ls [-l|-h|-a] [FILE]",
236 "List devices and files.", options
);
241 grub_unregister_command ("ls");