added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / arch / common / grub2 / commands / search.c
blob61b35c45aa4bc96ffe4fffff66c04d6fe969f635
1 /* search.c - search devices based on a file or a filesystem label */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 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/device.h>
28 #include <grub/file.h>
29 #include <grub/env.h>
31 static const struct grub_arg_option options[] =
33 {"file", 'f', 0, "search devices by a file (default)", 0, 0},
34 {"label", 'l', 0, "search devices by a filesystem label", 0, 0},
35 {"set", 's', GRUB_ARG_OPTION_OPTIONAL, "set a variable to the first device found", "VAR", ARG_TYPE_STRING},
36 {0, 0, 0, 0, 0, 0}
39 static void
40 search_label (const char *key, const char *var)
42 int count = 0;
43 auto int iterate_device (const char *name);
45 int iterate_device (const char *name)
47 grub_device_t dev;
49 dev = grub_device_open (name);
50 if (dev)
52 grub_fs_t fs;
54 fs = grub_fs_probe (dev);
55 if (fs && fs->label)
57 char *label;
59 (fs->label) (dev, &label);
60 if (grub_errno == GRUB_ERR_NONE && label)
62 if (grub_strcmp (label, key) == 0)
64 /* Found! */
65 grub_printf (" %s", name);
66 if (count++ == 0 && var)
67 grub_env_set (var, name);
70 grub_free (label);
74 grub_device_close (dev);
77 grub_errno = GRUB_ERR_NONE;
78 return 0;
81 grub_device_iterate (iterate_device);
83 if (count == 0)
84 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
87 static void
88 search_file (const char *key, const char *var)
90 int count = 0;
91 char *buf = 0;
92 auto int iterate_device (const char *name);
94 int iterate_device (const char *name)
96 grub_size_t len;
97 char *p;
98 grub_file_t file;
100 len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
101 p = grub_realloc (buf, len);
102 if (! p)
103 return 1;
105 buf = p;
106 grub_sprintf (buf, "(%s)%s", name, key);
108 file = grub_file_open (buf);
109 if (file)
111 /* Found! */
112 grub_printf (" %s", name);
113 if (count++ == 0 && var)
114 grub_env_set (var, name);
116 grub_file_close (file);
119 grub_errno = GRUB_ERR_NONE;
120 return 0;
123 grub_device_iterate (iterate_device);
125 grub_free (buf);
127 if (grub_errno == GRUB_ERR_NONE && count == 0)
128 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device");
131 static grub_err_t
132 grub_cmd_search (struct grub_arg_list *state, int argc, char **args)
134 const char *var = 0;
136 if (argc == 0)
137 return grub_error (GRUB_ERR_INVALID_COMMAND, "no argument specified");
139 if (state[2].set)
140 var = state[2].arg ? : "root";
142 if (state[1].set)
143 search_label (args[0], var);
144 else
145 search_file (args[0], var);
147 return grub_errno;
150 GRUB_MOD_INIT(search)
152 (void) mod; /* To stop warning. */
153 grub_register_command ("search", grub_cmd_search, GRUB_COMMAND_FLAG_BOTH,
154 "search [-f|-l|-s] NAME",
155 "Search devices by a file or a filesystem label."
156 " If --set is specified, the first device found is"
157 " set to a variable. If no variable name is"
158 " specified, \"root\" is used.",
159 options);
162 GRUB_MOD_FINI(search)
164 grub_unregister_command ("search");