Forgot to remove files / run autogen.sh
[grub2/phcoder.git] / commands / ls.c
blob4257e026f0a4f04ad94cea57137ef857ff8f91d4
1 /* ls.c - command to list files and devices */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2007 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/normal.h>
26 #include <grub/arg.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},
38 {0, 0, 0, 0, 0, 0}
41 static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'};
43 static grub_err_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)
49 if (longlist)
50 grub_normal_print_device_info (name);
51 else
52 grub_printf ("(%s) ", name);
54 return 0;
57 grub_device_iterate (grub_ls_print_devices);
58 grub_putchar ('\n');
59 grub_refresh ();
61 return 0;
64 static grub_err_t
65 grub_ls_list_files (char *dirname, int longlist, int all, int human)
67 char *device_name;
68 grub_fs_t fs;
69 const char *path;
70 grub_device_t dev;
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 ? "/" : "");
79 return 0;
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] == '.'))
87 return 0;
89 if (! dir)
91 grub_file_t file;
93 if (dirname[grub_strlen (dirname) - 1] == '/')
94 grub_sprintf (pathname, "%s%s", dirname, filename);
95 else
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);
101 if (! file)
103 grub_errno = 0;
104 return 0;
107 if (! human)
108 grub_printf ("%-12llu", (unsigned long long) file->size);
109 else
111 grub_uint64_t fsize = file->size * 100ULL;
112 int fsz = file->size;
113 int units = 0;
114 char buf[20];
116 while (fsz / 1024)
118 fsize = (fsize + 512) / 1024;
119 fsz /= 1024;
120 units++;
123 if (units)
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);
132 else
133 grub_printf ("%-12llu", (unsigned long long) file->size);
136 grub_file_close (file);
138 else
139 grub_printf ("%-12s", "DIR");
141 grub_printf ("%s%s\n", filename, dir ? "/" : "");
143 return 0;
146 device_name = grub_file_get_device_name (dirname);
147 dev = grub_device_open (device_name);
148 if (! dev)
149 goto fail;
151 fs = grub_fs_probe (dev);
152 path = grub_strchr (dirname, ')');
153 if (! path)
154 path = dirname;
155 else
156 path++;
158 if (! path && ! device_name)
160 grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
161 goto fail;
164 if (! *path)
166 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
167 grub_errno = GRUB_ERR_NONE;
169 grub_normal_print_device_info (device_name);
171 else if (fs)
173 if (longlist)
174 (fs->dir) (dev, path, print_files_long);
175 else
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. */
182 char *p;
183 grub_file_t file;
185 grub_errno = 0;
187 file = grub_file_open (dirname);
188 if (! file)
189 goto fail;
191 grub_file_close (file);
193 p = grub_strrchr (dirname, '/') + 1;
194 dirname = grub_strndup (dirname, p - dirname);
195 if (! dirname)
196 goto fail;
198 all = 1;
199 if (longlist)
200 print_files_long (p, 0);
201 else
202 print_files (p, 0);
204 grub_free (dirname);
207 if (grub_errno == GRUB_ERR_NONE)
208 grub_putchar ('\n');
210 grub_refresh ();
213 fail:
214 if (dev)
215 grub_device_close (dev);
217 grub_free (device_name);
219 return 0;
222 static grub_err_t
223 grub_cmd_ls (struct grub_arg_list *state, int argc, char **args)
225 if (argc == 0)
226 grub_ls_list_devices (state[0].set);
227 else
228 grub_ls_list_files (args[0], state[0].set, state[2].set,
229 state[1].set);
231 return 0;
234 GRUB_MOD_INIT(ls)
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);
242 GRUB_MOD_FINI(ls)
244 grub_unregister_command ("ls");