Example #1
0
/*
 * Read and allocate space for a string from a file.
 * This replaces db.c fread_string
 * This is modified version of Furey's fread_string from Merc
 */
char *_fread_string(FILE * fp, const char *caller)
{
    char buf[MAX_STRING_LENGTH * 4];
    char *ptr = buf;
    char c;

    do
    {
        c = getc(fp);
    }
    while (isspace(c));

    if ((*ptr++ = c) == '~')
        return &str_empty[0];

    for (;;)
    {
        switch (*ptr = getc(fp))
        {
        default:
            ptr++;
            break;

        case EOF:
            bugf("Fread_string: EOF");
            raise(SIGSEGV);
            break;

        case '\n':
            ptr++;
            *ptr++ = '\r';
            break;

        case '\r':
            break;

        case '~':
            *ptr = '\0';
            if (fBootDb)
            {
                int len = ptr - buf;

                ptr = temp_hash_find(buf, len);
                if (ptr)
                    return _str_dup(ptr, caller);

                ptr = _str_dup(buf, caller);
                temp_hash_add(ptr, len);
                return ptr;
            }

            ptr=_str_dup(buf, caller);
            tail_chain();
            return ptr;
        }
    }
}
Example #2
0
static int _find_file(udfread *udf, const char *path,
                      const struct udf_dir **p_dir,
                      const struct udf_file_identifier **p_fid)
{
    const struct udf_file_identifier *fid = NULL;
    struct udf_dir *current_dir;
    char *tmp_path, *save_ptr, *token;

    current_dir = udf->root_dir;
    if (!current_dir) {
        return -1;
    }

    tmp_path = _str_dup(path);
    if (!tmp_path) {
        return -1;
    }

    token = strtok_r(tmp_path, "/\\", &save_ptr);
    if (token == NULL) {
        udf_trace("_find_file: requested root dir\n");
    }

    while (token) {

        int index = _scan_dir(current_dir, token);
        if (index < 0) {
            udf_log("_find_file: entry %s not found\n", token);
            goto error;
        }
        fid = &current_dir->files[index];

        token = strtok_r(NULL, "/\\", &save_ptr);

        if (fid->characteristic & CHAR_FLAG_DIR) {
            current_dir = _read_subdir(udf, current_dir, index);
            if (!current_dir) {
                goto error;
            }
        } else if (token) {
            udf_log("_find_file: entry %s not found (parent is file, not directory)\n", token);
            goto error;
        } else {
            // found a file, make sure we won't return directory data
            current_dir = NULL;
        }
    }

    if (p_fid) {
        if (!fid) {
            udf_log("no file identifier found for %s\n", path);
            goto error;
        }
        *p_fid = fid;
    }
    if (p_dir) {
        *p_dir = current_dir;
    }

    free(tmp_path);
    return 0;

error:
    free(tmp_path);
    return -1;
}