~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to kern/env.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* env.c - Environment variables */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2003,2005  Free Software Foundation, Inc.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 */
 
20
 
 
21
#include <grub/env.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
 
 
25
/* The size of the hash table.  */
 
26
#define HASHSZ  13
 
27
 
 
28
/* A hashtable for quick lookup of variables.  */
 
29
static struct grub_env_var *grub_env[HASHSZ];
 
30
 
 
31
/* The variables in a sorted list.  */
 
32
static struct grub_env_var *grub_env_sorted;
 
33
 
 
34
/* Return the hash representation of the string S.  */
 
35
static unsigned int grub_env_hashval (const char *s)
 
36
{
 
37
  unsigned int i = 0;
 
38
 
 
39
  /* XXX: This can be done much more effecient.  */
 
40
  while (*s)
 
41
    i += 5 * *(s++);
 
42
 
 
43
  return i % HASHSZ;
 
44
}
 
45
 
 
46
static struct grub_env_var *
 
47
grub_env_find (const char *name)
 
48
{
 
49
  struct grub_env_var *var;
 
50
  int idx = grub_env_hashval (name);
 
51
 
 
52
  for (var = grub_env[idx]; var; var = var->next)
 
53
    if (! grub_strcmp (var->name, name))
 
54
      return var;
 
55
  return 0;
 
56
}
 
57
 
 
58
grub_err_t
 
59
grub_env_set (const char *var, const char *val)
 
60
{
 
61
  int idx = grub_env_hashval (var);
 
62
  struct grub_env_var *env;
 
63
  struct grub_env_var *sort;
 
64
  struct grub_env_var **sortp;
 
65
  
 
66
  /* If the variable does already exist, just update the variable.  */
 
67
  env = grub_env_find (var);
 
68
  if (env)
 
69
    {
 
70
      char *old = env->value;
 
71
 
 
72
      if (env->write_hook)
 
73
        env->value = env->write_hook (env, val);
 
74
      else
 
75
        env->value = grub_strdup (val);
 
76
      
 
77
      if (! env->value)
 
78
        {
 
79
          env->value = old;
 
80
          return grub_errno;
 
81
        }
 
82
 
 
83
      grub_free (old);
 
84
      return 0;
 
85
    }
 
86
 
 
87
  /* The variable does not exist, create it.  */
 
88
  env = grub_malloc (sizeof (struct grub_env_var));
 
89
  if (! env)
 
90
    return grub_errno;
 
91
  
 
92
  grub_memset (env, 0, sizeof (struct grub_env_var));
 
93
  
 
94
  env->name = grub_strdup (var);
 
95
  if (! env->name)
 
96
    goto fail;
 
97
  
 
98
  env->value = grub_strdup (val);
 
99
  if (! env->value)
 
100
    goto fail;
 
101
  
 
102
  /* Insert it in the hashtable.  */
 
103
  env->prevp = &grub_env[idx];
 
104
  env->next = grub_env[idx];
 
105
  if (grub_env[idx])
 
106
    grub_env[idx]->prevp = &env->next;
 
107
  grub_env[idx] = env;
 
108
  
 
109
  /* Insert it in the sorted list.  */
 
110
  sortp = &grub_env_sorted;
 
111
  sort = grub_env_sorted;
 
112
  while (sort)
 
113
    {
 
114
      if (grub_strcmp (sort->name, var) > 0)
 
115
        break;
 
116
      
 
117
      sortp = &sort->sort_next;
 
118
      sort = sort->sort_next;
 
119
    }
 
120
  env->sort_prevp = sortp;
 
121
  env->sort_next = sort;
 
122
  if (sort)
 
123
    sort->sort_prevp = &env->sort_next;
 
124
  *sortp = env;
 
125
 
 
126
  return 0;
 
127
 
 
128
 fail:
 
129
  grub_free (env->name);
 
130
  grub_free (env->value);
 
131
  grub_free (env);
 
132
 
 
133
  return grub_errno;
 
134
}
 
135
 
 
136
char *
 
137
grub_env_get (const char *name)
 
138
{
 
139
  struct grub_env_var *env;
 
140
  env = grub_env_find (name);
 
141
  if (! env)
 
142
    return 0;
 
143
 
 
144
  if (env->read_hook)
 
145
    return env->read_hook (env, env->value);
 
146
 
 
147
  return env->value;
 
148
}
 
149
 
 
150
void
 
151
grub_env_unset (const char *name)
 
152
{
 
153
  struct grub_env_var *env;
 
154
  env = grub_env_find (name);
 
155
  if (! env)
 
156
    return;
 
157
 
 
158
  /* XXX: It is not possible to unset variables with a read or write
 
159
     hook.  */
 
160
  if (env->read_hook || env->write_hook)
 
161
    return;
 
162
 
 
163
  *env->prevp = env->next;
 
164
  if (env->next)
 
165
    env->next->prevp = env->prevp;
 
166
 
 
167
  *env->sort_prevp = env->sort_next;
 
168
  if (env->sort_next)
 
169
    env->sort_next->sort_prevp = env->sort_prevp;
 
170
 
 
171
  grub_free (env->name);
 
172
  grub_free (env->value);
 
173
  grub_free (env);
 
174
  return;
 
175
}
 
176
 
 
177
void
 
178
grub_env_iterate (int (* func) (struct grub_env_var *var))
 
179
{
 
180
  struct grub_env_var *env;
 
181
  
 
182
  for (env = grub_env_sorted; env; env = env->sort_next)
 
183
    if (func (env))
 
184
      return;
 
185
}
 
186
 
 
187
grub_err_t
 
188
grub_register_variable_hook (const char *var,
 
189
                             grub_env_read_hook_t read_hook,
 
190
                             grub_env_write_hook_t write_hook)
 
191
{
 
192
  struct grub_env_var *env = grub_env_find (var);
 
193
 
 
194
  if (! env)
 
195
    {
 
196
      char *val = grub_strdup ("");
 
197
 
 
198
      if (! val)
 
199
        return grub_errno;
 
200
      
 
201
      if (grub_env_set (var, val) != GRUB_ERR_NONE)
 
202
        return grub_errno;
 
203
    }
 
204
  
 
205
  env = grub_env_find (var);
 
206
  /* XXX Insert an assertion?  */
 
207
  
 
208
  env->read_hook = read_hook;
 
209
  env->write_hook = write_hook;
 
210
 
 
211
  return GRUB_ERR_NONE;
 
212
}