Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / ls.c
blob913bb65462eba672e4b954bcdf31eb5a20c57dfe
1 /* ls.c - command to list files and devices */
2 /*
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>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.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>
34 #include <grub/net.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},
43 {0, 0, 0, 0, 0, 0}
46 static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'};
48 static grub_err_t
49 grub_ls_list_devices (int longlist)
51 auto int grub_ls_print_devices (const char *name);
52 int grub_ls_print_devices (const char *name)
54 if (longlist)
55 grub_normal_print_device_info (name);
56 else
57 grub_printf ("(%s) ", name);
59 return 0;
62 grub_device_iterate (grub_ls_print_devices);
63 grub_xputs ("\n");
65 #if 0
67 grub_net_app_level_t proto;
68 int first = 1;
69 FOR_NET_APP_LEVEL (proto)
71 if (first)
72 grub_puts_ (N_ ("Network protocols:"));
73 first = 0;
74 grub_printf ("%s ", proto->name);
76 grub_xputs ("\n");
78 #endif
80 grub_refresh ();
82 return 0;
85 static grub_err_t
86 grub_ls_list_files (char *dirname, int longlist, int all, int human)
88 char *device_name;
89 grub_fs_t fs;
90 const char *path;
91 grub_device_t dev;
93 auto int print_files (const char *filename,
94 const struct grub_dirhook_info *info);
95 auto int print_files_long (const char *filename,
96 const struct grub_dirhook_info *info);
98 int print_files (const char *filename, const struct grub_dirhook_info *info)
100 if (all || filename[0] != '.')
101 grub_printf ("%s%s ", filename, info->dir ? "/" : "");
103 return 0;
106 int print_files_long (const char *filename,
107 const struct grub_dirhook_info *info)
109 if ((! all) && (filename[0] == '.'))
110 return 0;
112 if (! info->dir)
114 grub_file_t file;
115 char *pathname;
117 if (dirname[grub_strlen (dirname) - 1] == '/')
118 pathname = grub_xasprintf ("%s%s", dirname, filename);
119 else
120 pathname = grub_xasprintf ("%s/%s", dirname, filename);
122 if (!pathname)
123 return 1;
125 /* XXX: For ext2fs symlinks are detected as files while they
126 should be reported as directories. */
127 grub_file_filter_disable_compression ();
128 file = grub_file_open (pathname);
129 if (! file)
131 grub_errno = 0;
132 grub_free (pathname);
133 return 0;
136 if (! human)
137 grub_printf ("%-12llu", (unsigned long long) file->size);
138 else
140 grub_uint64_t fsize = file->size * 100ULL;
141 grub_uint64_t fsz = file->size;
142 int units = 0;
143 char buf[20];
145 while (fsz / 1024)
147 fsize = (fsize + 512) / 1024;
148 fsz /= 1024;
149 units++;
152 if (units)
154 grub_uint64_t whole, fraction;
156 whole = grub_divmod64 (fsize, 100, &fraction);
157 grub_snprintf (buf, sizeof (buf),
158 "%" PRIuGRUB_UINT64_T
159 ".%02" PRIuGRUB_UINT64_T "%c", whole, fraction,
160 grub_human_sizes[units]);
161 grub_printf ("%-12s", buf);
163 else
164 grub_printf ("%-12llu", (unsigned long long) file->size);
167 grub_file_close (file);
168 grub_free (pathname);
170 else
171 grub_printf ("%-12s", _("DIR"));
173 if (info->mtimeset)
175 struct grub_datetime datetime;
176 grub_unixtime2datetime (info->mtime, &datetime);
177 if (human)
178 grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
179 datetime.year, datetime.month, datetime.day,
180 datetime.hour, datetime.minute,
181 datetime.second,
182 grub_get_weekday_name (&datetime));
183 else
184 grub_printf (" %04d%02d%02d%02d%02d%02d ",
185 datetime.year, datetime.month,
186 datetime.day, datetime.hour,
187 datetime.minute, datetime.second);
189 grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
191 return 0;
194 device_name = grub_file_get_device_name (dirname);
195 dev = grub_device_open (device_name);
196 if (! dev)
197 goto fail;
199 fs = grub_fs_probe (dev);
200 path = grub_strchr (dirname, ')');
201 if (! path)
202 path = dirname;
203 else
204 path++;
206 if (! path && ! device_name)
208 grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
209 goto fail;
212 if (! *path)
214 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
215 grub_errno = GRUB_ERR_NONE;
217 grub_normal_print_device_info (device_name);
219 else if (fs)
221 if (longlist)
222 (fs->dir) (dev, path, print_files_long);
223 else
224 (fs->dir) (dev, path, print_files);
226 if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
227 && path[grub_strlen (path) - 1] != '/')
229 /* PATH might be a regular file. */
230 char *p;
231 grub_file_t file;
232 struct grub_dirhook_info info;
233 grub_errno = 0;
235 grub_file_filter_disable_compression ();
236 file = grub_file_open (dirname);
237 if (! file)
238 goto fail;
240 grub_file_close (file);
242 p = grub_strrchr (dirname, '/') + 1;
243 dirname = grub_strndup (dirname, p - dirname);
244 if (! dirname)
245 goto fail;
247 all = 1;
248 grub_memset (&info, 0, sizeof (info));
249 if (longlist)
250 print_files_long (p, &info);
251 else
252 print_files (p, &info);
254 grub_free (dirname);
257 if (grub_errno == GRUB_ERR_NONE)
258 grub_xputs ("\n");
260 grub_refresh ();
263 fail:
264 if (dev)
265 grub_device_close (dev);
267 grub_free (device_name);
269 return 0;
272 static grub_err_t
273 grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
275 struct grub_arg_list *state = ctxt->state;
276 int i;
278 if (argc == 0)
279 grub_ls_list_devices (state[0].set);
280 else
281 for (i = 0; i < argc; i++)
282 grub_ls_list_files (args[i], state[0].set, state[2].set,
283 state[1].set);
285 return 0;
288 static grub_extcmd_t cmd;
290 GRUB_MOD_INIT(ls)
292 cmd = grub_register_extcmd ("ls", grub_cmd_ls, 0,
293 N_("[-l|-h|-a] [FILE ...]"),
294 N_("List devices and files."), options);
297 GRUB_MOD_FINI(ls)
299 grub_unregister_extcmd (cmd);