[PATCH] savedefault --once
Leandro Dorileo
ldorileo at gmail.com
Wed Mar 8 09:12:27 UTC 2006
Hi Otavio
I cleaned up the patch.
--- stage2/builtins.c-orig 2006-03-08 04:44:59.000000000 +0000
+++ stage2/builtins.c 2006-03-08 04:42:02.000000000 +0000
@@ -82,6 +82,10 @@
inside other functions. */
static int configfile_func (char *arg, int flags);
+static int savedefault_helper (char *arg, int flags);
+
+static int savedefault_shell (char *arg, int flags);
+
/* Initialize the data for builtins. */
void
init_builtins (void)
@@ -3221,7 +3225,90 @@
static int
savedefault_func (char *arg, int flags)
{
-#if !defined(SUPPORT_DISKLESS) && !defined(GRUB_UTIL)
+#if !defined(SUPPORT_DISKLESS)
+ #if !defined(GRUB_UTIL)
+ savedefault_helper(arg, flags);
+ #else
+ savedefault_shell(arg, flags);
+ #endif
+#else /* !SUPPORT_DISKLESS */
+ errnum = ERR_UNRECOGNIZED;
+ return 1;
+#endif /* !SUPPORT_DISKLESS */
+}
+
+#if !defined(SUPPORT_DISKLESS) && defined(GRUB_UTIL)
+/* savedefault_shell */
+static int
+savedefault_shell(char *arg, int flags)
+ {
+ int once_only = 0;
+ int new_default;
+ int curr_default = -1;
+ int curr_prev_default = -1;
+ int new_prev_default = -1;
+ FILE *fp;
+ size_t bytes = 10;
+ char line[bytes];
+ char *default_file = (char *) DEFAULT_FILE_BUF;
+ char buf[bytes];
+
+ while (1)
+ {
+ if (grub_memcmp ("--default=", arg, sizeof ("--default=") - 1) == 0)
+ {
+ char *p = arg + sizeof ("--default=") - 1;
+ if (! safe_parse_maxint (&p, &new_default))
+ return 1;
+ arg = skip_to (0, arg);
+ }
+ else if (grub_memcmp ("--once", arg, sizeof ("--once") - 1) == 0)
+ {
+ once_only = 1;
+ arg = skip_to (0, arg);
+ }
+ else
+ break;
+ }
+
+ if(!(fp = fopen(default_file,"w")))
+ {
+ errnum = ERR_READ;
+ goto fail;
+ }
+
+ read(&line, -1);
+
+ sscanf(line, "%d:%d", &curr_prev_default, &curr_default);
+
+ if(curr_default != -1)
+ new_prev_default = curr_default;
+ else
+ {
+ if(curr_prev_default != -1)
+ new_prev_default = curr_prev_default;
+ else
+ new_prev_default = 0;
+ }
+
+ if(once_only)
+ sprintf(buf, "%d:%d\n", new_prev_default, new_default);
+ else
+ sprintf(buf, "%d\n", new_default);
+
+ fprintf(fp, buf);
+
+fail:
+ fclose(fp);
+ return errnum;
+}
+#endif
+
+/* savedefault_helper */
+static int
+savedefault_helper (char *arg, int flags)
+{
+#if !defined(SUPPORT_DISKLESS)
unsigned long tmp_drive = saved_drive;
unsigned long tmp_partition = saved_partition;
char *default_file = (char *) DEFAULT_FILE_BUF;
@@ -3300,19 +3387,23 @@
disk_read_hook = 0;
grub_close ();
- if (len != sizeof (buf))
- {
- /* This is too small. Do not modify the file manually, please! */
- errnum = ERR_READ;
- goto fail;
- }
-
if (sector_count > 2)
{
/* Is this possible?! Too fragmented! */
errnum = ERR_FSYS_CORRUPT;
goto fail;
}
+
+ char *tmp;
+ if((tmp = grub_strstr(buf, ":")) != NULL)
+ {
+ int f_len = grub_strlen(buf) - grub_strlen(tmp);
+ char *def;
+ int a;
+ for(a = 0; a < f_len; a++)
+ grub_memcpy(&def[a], &buf[a], sizeof(char));
+ safe_parse_maxint (&def, &entryno);
+ }
/* Set up a string to be written. */
grub_memset (buf, '\n', sizeof (buf));
--- stage2/stage2.c-orig 2005-03-19 17:51:57.000000000 +0000
+++ stage2/stage2.c 2006-03-08 04:38:39.000000000 +0000
@@ -891,8 +891,18 @@
len = grub_read (buf, sizeof (buf));
if (len > 0)
{
+ char *tmp;
+ char *def;
buf[sizeof (buf) - 1] = 0;
- safe_parse_maxint (&p, &saved_entryno);
+
+ if((tmp = grub_strstr(p, ":")) != NULL)
+ {
+ *tmp++;
+ grub_memcpy(&def, &tmp, sizeof(p));
+ }else
+ grub_memcpy(&def, &p, sizeof(p));
+
+ safe_parse_maxint (&def, &saved_entryno);
}
grub_close ();
--
Dorileo
More information about the Pkg-grub-devel
mailing list