+ char *linepos;
+ char *temp;
+
+ linepos = *line;
+ if (!linepos)
+ return -1;
+
+ if (strchr(linepos, '\\')) {
+ dbg("escaped characters are not supported, skip");
+ return -1;
+ }
+
+ /* skip whitespace */
+ while (isspace(linepos[0]))
+ linepos++;
+
+ /* get the key */
+ *key = linepos;
+ while (1) {
+ linepos++;
+ if (linepos[0] == '\0')
+ return -1;
+ if (isspace(linepos[0]))
+ break;
+ if (linepos[0] == '=')
+ break;
+ }
+
+ /* terminate key */
+ linepos[0] = '\0';
+ linepos++;
+
+ /* skip whitespace */
+ while (isspace(linepos[0]))
+ linepos++;
+
+ /* get the value*/
+ if (linepos[0] == '"') {
+ linepos++;
+ temp = strchr(linepos, '"');
+ if (!temp) {
+ dbg("missing closing quote");
+ return -1;
+ }
+ dbg("value is quoted");
+ temp[0] = '\0';
+ } else if (linepos[0] == '\'') {
+ linepos++;
+ temp = strchr(linepos, '\'');
+ if (!temp) {
+ dbg("missing closing quote");
+ return -1;
+ }
+ dbg("value is quoted");
+ temp[0] = '\0';
+ } else if (linepos[0] == '\0') {
+ dbg("value is empty");
+ } else {
+ temp = linepos;
+ while (temp[0] && !isspace(temp[0]))
+ temp++;
+ temp[0] = '\0';
+ }
+ *value = linepos;
+
+ return 0;
+}
+
+static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
+{
+ char line[LINE_SIZE];
+ const char *bufline;
+ char *linepos;
+ char *variable;
+ char *value;
+ size_t cur;
+ size_t count;
+ int lineno;
+
+ /* loop through the whole buffer */
+ lineno = 0;
+ cur = 0;
+ while (cur < bufsize) {
+ count = buf_get_line(buf, bufsize, cur);
+ bufline = &buf[cur];
+ cur += count+1;
+ lineno++;
+
+ if (count >= sizeof(line)) {
+ err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
+ continue;
+ }
+
+ /* eat the whitespace */
+ while ((count > 0) && isspace(bufline[0])) {
+ bufline++;
+ count--;
+ }
+ if (count == 0)
+ continue;
+
+ /* see if this is a comment */
+ if (bufline[0] == COMMENT_CHARACTER)
+ continue;
+
+ memcpy(line, bufline, count);
+ line[count] = '\0';
+
+ linepos = line;
+ if (get_key(&linepos, &variable, &value) == 0) {
+ dbg("import '%s=%s'", variable, value);
+ name_list_key_add(&udev->env_list, variable, value);
+ setenv(variable, value, 1);
+ }
+ }
+
+ return 0;
+}
+
+static int import_file_into_env(struct udevice *udev, const char *filename)
+{
+ char *buf;
+ size_t bufsize;
+
+ if (file_map(filename, &buf, &bufsize) != 0) {
+ err("can't open '%s': %s", filename, strerror(errno));
+ return -1;
+ }
+ import_keys_into_env(udev, buf, bufsize);
+ file_unmap(buf, bufsize);
+
+ return 0;
+}
+
+static int import_program_into_env(struct udevice *udev, const char *program)
+{
+ char result[1024];
+ size_t reslen;
+
+ if (run_program(program, udev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_INFO)) != 0)
+ return -1;
+ return import_keys_into_env(udev, result, reslen);
+}
+
+static int import_parent_into_env(struct udevice *udev, struct sysfs_class_device *class_dev, const char *filter)
+{
+ struct sysfs_class_device *parent = sysfs_get_classdev_parent(class_dev);
+ int rc = -1;
+
+ if (parent != NULL) {
+ struct udevice udev_parent;
+ struct name_entry *name_loop;
+
+ dbg("found parent '%s', get the node name", parent->path);
+ udev_init_device(&udev_parent, NULL, NULL, NULL);
+ /* import the udev_db of the parent */
+ if (udev_db_get_device(&udev_parent, &parent->path[strlen(sysfs_path)]) == 0) {
+ dbg("import stored parent env '%s'", udev_parent.name);
+ list_for_each_entry(name_loop, &udev_parent.env_list, node) {
+ char name[NAME_SIZE];
+ char *pos;
+
+ strlcpy(name, name_loop->name, sizeof(name));
+ pos = strchr(name, '=');
+ if (pos) {
+ pos[0] = '\0';
+ pos++;
+ if (strcmp_pattern(filter, name) == 0) {
+ dbg("import key '%s'", name_loop->name);
+ name_list_add(&udev->env_list, name_loop->name, 0);
+ setenv(name, pos, 1);
+ } else
+ dbg("skip key '%s'", name_loop->name);
+ }
+ }
+ rc = 0;
+ } else
+ dbg("parent not found in database");
+ udev_cleanup_device(&udev_parent);
+ }
+
+ return rc;
+}
+
+static int match_name_and_get_number(const char *base, const char *devname)
+{
+ size_t baselen;
+ char *endptr;
+ int num;
+
+ baselen = strlen(base);
+ if (strncmp(base, devname, baselen) != 0)
+ return -1;
+ if (devname[baselen] == '\0')
+ return 0;
+ if (!isdigit(devname[baselen]))
+ return -1;
+ num = strtoul(&devname[baselen], &endptr, 10);
+ if (endptr[0] != '\0')
+ return -1;
+ return num;
+}
+
+/* finds the lowest positive device number such that <name>N isn't present in the udevdb
+ * if <name> doesn't exist, 0 is returned, N otherwise */
+static int find_free_number(const char *base, const char *devpath)
+{
+ char db_devpath[PATH_SIZE];