| | 114 | } |
| | 115 | |
| | 116 | static int opensc_get_conf_entry(const char *config) |
| | 117 | { |
| | 118 | scconf_block *conf_block = NULL, **blocks; |
| | 119 | char *buffer = NULL; |
| | 120 | char *section = NULL; |
| | 121 | char *name = NULL; |
| | 122 | char *key = NULL; |
| | 123 | int r = 0; |
| | 124 | |
| | 125 | if (ctx->conf == NULL) { |
| | 126 | r = ENOENT; |
| | 127 | goto cleanup; |
| | 128 | } |
| | 129 | |
| | 130 | if ((buffer = strdup(config)) == NULL) { |
| | 131 | r = ENOMEM; |
| | 132 | goto cleanup; |
| | 133 | } |
| | 134 | |
| | 135 | section = buffer; |
| | 136 | name = section == NULL ? NULL : strchr(section+1, ':'); |
| | 137 | key = name == NULL ? NULL : strchr(name+1, ':'); |
| | 138 | if (key == NULL) { |
| | 139 | r = EINVAL; |
| | 140 | goto cleanup; |
| | 141 | } |
| | 142 | *name = '\0'; |
| | 143 | name++; |
| | 144 | *key = '\0'; |
| | 145 | key++; |
| | 146 | |
| | 147 | blocks = scconf_find_blocks(ctx->conf, NULL, section, name); |
| | 148 | if (blocks[0]) |
| | 149 | conf_block = blocks[0]; |
| | 150 | free(blocks); |
| | 151 | if (conf_block != NULL) { |
| | 152 | const char *value = scconf_get_str(conf_block, key, NULL); |
| | 153 | |
| | 154 | if (value != NULL) { |
| | 155 | printf ("%s\n", value); |
| | 156 | } |
| | 157 | } |
| | 158 | |
| | 159 | r = 0; |
| | 160 | |
| | 161 | cleanup: |
| | 162 | |
| | 163 | if (buffer != NULL) |
| | 164 | free(buffer); |
| | 165 | |
| | 166 | return r; |
| | 167 | } |
| | 168 | |
| | 169 | static int opensc_set_conf_entry(const char *config) |
| | 170 | { |
| | 171 | scconf_block *conf_block = NULL, **blocks; |
| | 172 | char *buffer = NULL; |
| | 173 | char *section = NULL; |
| | 174 | char *name = NULL; |
| | 175 | char *key = NULL; |
| | 176 | char *value = NULL; |
| | 177 | int r = 0; |
| | 178 | |
| | 179 | if (ctx->conf == NULL) { |
| | 180 | r = ENOENT; |
| | 181 | goto cleanup; |
| | 182 | } |
| | 183 | |
| | 184 | if ((buffer = strdup(config)) == NULL) { |
| | 185 | r = ENOMEM; |
| | 186 | goto cleanup; |
| | 187 | } |
| | 188 | |
| | 189 | section = buffer; |
| | 190 | name = section == NULL ? NULL : strchr(section+1, ':'); |
| | 191 | key = name == NULL ? NULL : strchr(name+1, ':'); |
| | 192 | value = key == NULL ? NULL : strchr(key+1, ':'); |
| | 193 | if (value == NULL) { |
| | 194 | r = EINVAL; |
| | 195 | goto cleanup; |
| | 196 | } |
| | 197 | *name = '\0'; |
| | 198 | name++; |
| | 199 | *key = '\0'; |
| | 200 | key++; |
| | 201 | *value = '\0'; |
| | 202 | value++; |
| | 203 | |
| | 204 | blocks = scconf_find_blocks(ctx->conf, NULL, section, name); |
| | 205 | if (blocks[0]) |
| | 206 | conf_block = blocks[0]; |
| | 207 | free(blocks); |
| | 208 | if (conf_block != NULL) { |
| | 209 | scconf_item *item; |
| | 210 | |
| | 211 | for (item = conf_block->items; item != NULL; item = item->next) { |
| | 212 | scconf_list *list; |
| | 213 | |
| | 214 | if ((item->type != SCCONF_ITEM_TYPE_VALUE) |
| | 215 | || (strcmp(item->key, key) != 0)) |
| | 216 | continue; |
| | 217 | list = item->value.list; |
| | 218 | scconf_list_destroy(list); |
| | 219 | list = NULL; |
| | 220 | scconf_list_add(&list, value); |
| | 221 | item->value.list = list; |
| | 222 | break; |
| | 223 | } |
| | 224 | if (item == NULL) |
| | 225 | scconf_put_str(conf_block, key, value); |
| | 226 | } |
| | 227 | |
| | 228 | /* Write */ |
| | 229 | if ((r = scconf_write(ctx->conf, ctx->conf->filename)) != 0) { |
| | 230 | fprintf(stderr, "scconf_write(): %s\n", strerror(r)); |
| | 231 | goto cleanup; |
| | 232 | } |
| | 233 | |
| | 234 | r = 0; |
| | 235 | |
| | 236 | cleanup: |
| | 237 | |
| | 238 | if (buffer != NULL) |
| | 239 | free(buffer); |
| | 240 | |
| | 241 | return r; |