/[dynamips]/trunk/rommon_var.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/rommon_var.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Sat Oct 6 16:45:40 2007 UTC (11 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 3404 byte(s)
make working copy

1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2007 Christophe Fillot (cf@utc.fr)
4 *
5 * ROMMON Environment Variables.
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <errno.h>
12
13 #include "utils.h"
14 #include "rommon_var.h"
15
16 /* Load file containing ROMMON variables */
17 int rommon_load_file(struct rommon_var_list *rvl)
18 {
19 char buffer[512];
20 FILE *fd;
21
22 if (!rvl->filename)
23 return(-1);
24
25 if (!(fd = fopen(rvl->filename,"r"))) {
26 fprintf(stderr,"%s: unable to create file %s (%s)\n",
27 __func__,rvl->filename,strerror(errno));
28 return(-1);
29 }
30
31 while(!feof(fd)) {
32 if (m_fgets(buffer,sizeof(buffer),fd))
33 rommon_var_add_str(rvl,buffer);
34 }
35
36 fclose(fd);
37 return(0);
38 }
39
40 /* Write a file with all ROMMON variables */
41 int rommon_var_update_file(struct rommon_var_list *rvl)
42 {
43 struct rommon_var *var;
44 FILE *fd;
45
46 if (!rvl->filename)
47 return(-1);
48
49 if (!(fd = fopen(rvl->filename,"w"))) {
50 fprintf(stderr,"%s: unable to create file %s (%s)\n",
51 __func__,rvl->filename,strerror(errno));
52 return(-1);
53 }
54
55 for(var=rvl->var_list;var;var=var->next)
56 fprintf(fd,"%s=%s\n",var->name,var->value ? var->value : "");
57
58 fclose(fd);
59 return(0);
60 }
61
62 /* Find the specified variable */
63 struct rommon_var *rommon_var_find(struct rommon_var_list *rvl,char *name)
64 {
65 struct rommon_var *var;
66
67 for(var=rvl->var_list;var;var=var->next)
68 if (!strcmp(var->name,name))
69 return var;
70
71 return NULL;
72 }
73
74 /* Create a new variable */
75 static struct rommon_var *rommon_var_create(char *name)
76 {
77 struct rommon_var *var;
78
79 if (!(var = malloc(sizeof(*var))))
80 return NULL;
81
82 var->next = NULL;
83 var->value = NULL;
84 var->name = strdup(name);
85
86 if (!var->name) {
87 free(var);
88 return NULL;
89 }
90
91 return var;
92 }
93
94 /* Set value for a variable */
95 static int rommon_var_set(struct rommon_var *var,char *value)
96 {
97 char *new_value;
98
99 if (!(new_value = strdup(value)))
100 return(-1);
101
102 /* free old value */
103 if (var->value)
104 free(var->value);
105
106 var->value = new_value;
107 return(0);
108 }
109
110 /* Add a new variable */
111 int rommon_var_add(struct rommon_var_list *rvl,char *name,char *value)
112 {
113 struct rommon_var *var;
114
115 /* if the variable already exists, overwrite it */
116 if (!(var = rommon_var_find(rvl,name))) {
117 var = rommon_var_create(name);
118 if (!var) return(-1);
119
120 if (rommon_var_set(var,value) == -1)
121 return(-1);
122
123 var->next = rvl->var_list;
124 rvl->var_list = var;
125 } else {
126 rommon_var_set(var,value);
127 }
128
129 /* synchronize disk file */
130 return(rommon_var_update_file(rvl));
131 }
132
133 /*
134 * Add a new variable, specified at the format: var=value.
135 * The string is modified.
136 */
137 int rommon_var_add_str(struct rommon_var_list *rvl,char *str)
138 {
139 char *eq_sym;
140
141 if (!(eq_sym = strchr(str,'=')))
142 return(-1);
143
144 /* The variable cannot be null */
145 if (str == eq_sym)
146 return(-1);
147
148 *eq_sym = 0;
149 return(rommon_var_add(rvl,str,eq_sym+1));
150 }
151
152 /* Get the specified variable */
153 int rommon_var_get(struct rommon_var_list *rvl,char *name,
154 char *buffer,size_t len)
155 {
156 struct rommon_var *var;
157
158 if (!(var = rommon_var_find(rvl,name)) || !var->value)
159 return(-1);
160
161 strncpy(buffer,var->value,len-1);
162 buffer[len-1] = '\0';
163 return(0);
164 }

  ViewVC Help
Powered by ViewVC 1.1.26