Beispiel #1
0
/*
 * Invoke the "file" command on the file and match its output against PTR.
 * have_type is a flag that is set if we already have tried to determine
 * the type of that file.
 * Return 1 for match, 0 for no match, -1 errors.
 */
static int
regex_check_type (const char *filename, const char *ptr, int *have_type)
{
    int found = 0;

    /* Following variables are valid if *have_type is 1 */
    static char content_string[2048];
    static int content_shift = 0;
    static int got_data = 0;

    if (!use_file_to_check_type) {
	return 0;
    }

    if (!*have_type) {
	char *realname;		/* name used with "file" */
	char *localfile;
	/* Don't repeate even unsuccessful checks */
	*have_type = 1;

	localfile = mc_getlocalcopy (filename);
	if (!localfile)
	    return -1;

	realname = localfile;
	got_data =
	    get_file_type_local (localfile, content_string,
				 sizeof (content_string));
	mc_ungetlocalcopy (filename, localfile, 0);

	if (got_data > 0) {
	    char *pp;

	    /* Paranoid termination */
	    content_string[sizeof (content_string) - 1] = 0;

	    if ((pp = strchr (content_string, '\n')) != 0)
		*pp = 0;

	    if (!strncmp (content_string, realname, strlen (realname))) {
		/* Skip "realname: " */
		content_shift = strlen (realname);
		if (content_string[content_shift] == ':') {
		    /* Solaris' file prints tab(s) after ':' */
		    for (content_shift++;
			 content_string[content_shift] == ' '
			 || content_string[content_shift] == '\t';
			 content_shift++);
		}
	    }
	} else {
	    /* No data */
	    content_string[0] = 0;
	}
	g_free (realname);
    }

    if (got_data == -1) {
	return -1;
    }

    if (content_string[0]
	&& regexp_match (ptr, content_string + content_shift, match_regex)) {
	found = 1;
    }

    return found;
}
Beispiel #2
0
Datei: ext.c Projekt: inso/mc
static gboolean
regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type,
                  gboolean case_insense, GError ** error)
{
    gboolean found = FALSE;

    /* Following variables are valid if *have_type is 1 */
    static char content_string[2048];
#ifdef HAVE_CHARSET
    static char encoding_id[21];        /* CSISO51INISCYRILLIC -- 20 */
#endif
    static size_t content_shift = 0;
    static int got_data = 0;

    if (!use_file_to_check_type)
        return FALSE;

    if (*have_type == 0)
    {
        vfs_path_t *localfile_vpath;
        const char *realname;   /* name used with "file" */

#ifdef HAVE_CHARSET
        int got_encoding_data;
#endif /* HAVE_CHARSET */

        /* Don't repeate even unsuccessful checks */
        *have_type = 1;

        localfile_vpath = mc_getlocalcopy (filename_vpath);
        if (localfile_vpath == NULL)
        {
            g_propagate_error (error,
                               g_error_new (MC_ERROR, -1,
                                            _("Cannot fetch a local copy of %s"),
                                            vfs_path_as_str (filename_vpath)));
            return FALSE;
        }

        realname = vfs_path_get_last_path_str (localfile_vpath);

#ifdef HAVE_CHARSET
        got_encoding_data = is_autodetect_codeset_enabled
            ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;

        if (got_encoding_data > 0)
        {
            char *pp;
            int cp_id;

            pp = strchr (encoding_id, '\n');
            if (pp != NULL)
                *pp = '\0';

            cp_id = get_codepage_index (encoding_id);
            if (cp_id == -1)
                cp_id = default_source_codepage;

            do_set_codepage (cp_id);
        }
#endif /* HAVE_CHARSET */

        mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);

        got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));

        if (got_data > 0)
        {
            char *pp;
            size_t real_len;

            pp = strchr (content_string, '\n');
            if (pp != NULL)
                *pp = '\0';

            real_len = strlen (realname);

            if (strncmp (content_string, realname, real_len) == 0)
            {
                /* Skip "realname: " */
                content_shift = real_len;
                if (content_string[content_shift] == ':')
                {
                    /* Solaris' file prints tab(s) after ':' */
                    for (content_shift++;
                         content_string[content_shift] == ' '
                         || content_string[content_shift] == '\t'; content_shift++)
                        ;
                }
            }
        }
        else
        {
            /* No data */
            content_string[0] = '\0';
        }
        vfs_path_free (localfile_vpath);
    }

    if (got_data == -1)
    {
        g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Pipe failed")));
        return FALSE;
    }

    if (content_string[0] != '\0')
    {
        mc_search_t *search;

        search = mc_search_new (ptr, -1);
        if (search != NULL)
        {
            search->search_type = MC_SEARCH_T_REGEX;
            search->is_case_sensitive = !case_insense;
            found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
            mc_search_free (search);
        }
        else
        {
            g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Regular expression error")));
        }
    }

    return found;
}