Exemplo n.º 1
0
/* Compare version strings.
 *
 * @s1 first string to compare
 * @s2 second string to compare
 *
 * @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
 */
int
filevercmp (const char *s1, const char *s2)
{
    const char *s1_pos, *s2_pos;
    const char *s1_suffix, *s2_suffix;
    size_t s1_len, s2_len;
    int simple_cmp, result;

    /* easy comparison to see if strings are identical */
    simple_cmp = strcmp (s1, s2);
    if (simple_cmp == 0)
        return 0;

    /* special handle for "", "." and ".." */
    if (*s1 == '\0')
        return -1;
    if (*s2 == '\0')
        return 1;
    if (DIR_IS_DOT (s1))
        return -1;
    if (DIR_IS_DOT (s2))
        return 1;
    if (DIR_IS_DOTDOT (s1))
        return -1;
    if (DIR_IS_DOTDOT (s2))
        return 1;

    /* special handle for other hidden files */
    if (*s1 == '.' && *s2 != '.')
        return -1;
    if (*s1 != '.' && *s2 == '.')
        return 1;
    if (*s1 == '.' && *s2 == '.')
    {
        s1++;
        s2++;
    }

    /* "cut" file suffixes */
    s1_pos = s1;
    s2_pos = s2;
    s1_suffix = match_suffix (&s1_pos);
    s2_suffix = match_suffix (&s2_pos);
    s1_len = (s1_suffix != NULL ? s1_suffix : s1_pos) - s1;
    s2_len = (s2_suffix != NULL ? s2_suffix : s2_pos) - s2;

    /* restore file suffixes if strings are identical after "cut" */
    if ((s1_suffix != NULL || s2_suffix != NULL) && (s1_len == s2_len)
        && strncmp (s1, s2, s1_len) == 0)
    {
        s1_len = s1_pos - s1;
        s2_len = s2_pos - s2;
    }

    result = verrevcmp (s1, s1_len, s2, s2_len);

    return result == 0 ? simple_cmp : result;
}
Exemplo n.º 2
0
compress_info *comp_from_ext(const char *name, compress_info *ci)
{
    for (;ci->name;ci++)
        if (match_suffix(name, ci->ext, 0))
            return ci;
    return 0;
}
Exemplo n.º 3
0
int open_output(char *name, char *oname, char **ofn)
{
	int fd;
	char *file;

	if (oname) {
		file = oname;
	} else {
		int len = strlen(name);

		file = xmalloc(len + 6);
		if (mode == compress)
			snprintf(file, len + 6, "%s.snp", name);
		else if (match_suffix(oname, ".snp")) {
			strcpy(file, oname);
			file[len - 4] = 0;			
		} else {
			fprintf(stderr, "Please specify output name\n");
			exit(1);
		}
	}

	*ofn = file;		
	fd = open(file, O_WRONLY|O_CREAT|O_TRUNC, 0644);
	if (fd < 0) { 
		fprintf(stderr, "Cannot create %s: %s\n", file,
			strerror(errno));
		exit(1);
	}		
	return fd;
}
Exemplo n.º 4
0
static int find_w_format(char *format, char *filename, char *fallback)
{
    int nf;

    // given explicitely
    if (format)
    {
        for (nf=0; rec[nf].name; nf++)
            if (!strcasecmp(format, rec[nf].name))
                return nf;
        return -1;	// no fallback to default
    }
    // guess from the filename
    else if (filename)
    {
        compress_info *ci;
        int skip;

        ci=comp_from_ext(filename, decompressors);
        if (ci)
            skip=strlen(ci->ext);
        else
            skip=0;
        for (nf=0; rec[nf].name; nf++)
            if (rec[nf].ext && match_suffix(filename, rec[nf].ext, skip))
                return nf;
    }
    // is the default valid?
    for (nf=0; rec[nf].name; nf++)
        if (!strcasecmp(fallback, rec[nf].name))
            return nf;
    return -1;
}
Exemplo n.º 5
0
static bool should_optimise(const char *name)
{
    return (match_suffix(name, ".html") ||
            match_suffix(name, ".css") ||
            match_suffix(name, ".js"));
}
Exemplo n.º 6
0
int main(int ac, char **av)
{
	int opt;
	int to_stdout = 0;

	while ((opt = getopt(ac, av, "dcs")) != -1) {
		switch (opt) { 
		case 'd':
			mode = uncompress;
			break;
		case 'c':
			mode = compress;
			break;
		case 's':
			to_stdout = 1;
			break;
		default:
			usage();
		}
	}

	char *map;
	size_t size;
	if (!av[optind])
		usage();

	if (mode == undef && match_suffix(av[optind], ".snp"))
		mode = uncompress;
	else
		mode = compress;

	map = mapfile(av[optind], O_RDONLY, &size);
	if (!map) { 
		fprintf(stderr, "Cannot open %s: %s\n", av[1], strerror(errno));
		exit(1);
	}
		
	int err;
	char *out;	
	size_t outlen;
	if (mode == uncompress) {
		if (!snappy_uncompressed_length(map, size, &outlen)) {
			fprintf(stderr, "Cannot read length in %s\n", 
				av[optind]);
			exit(1);
		}
	} else {	
		outlen = snappy_max_compressed_length(size);
	}
	
	out = xmalloc(outlen);

	if (mode == compress) {
		struct snappy_env env;
		snappy_init_env(&env);
		err = snappy_compress(&env, map, size, out, &outlen);
	} else
		err = snappy_uncompress(map, size, out);

	if (err) {
		fprintf(stderr, "Cannot process %s: %s\n", av[optind], 
			strerror(-err));
		exit(1);
	}

	char *file;
	int fd;
	if (to_stdout) {
		if(av[optind + 1])
			usage();
		fd = 1;
		file = "<stdout>";
	} else {
		if (av[optind + 1] && av[optind + 2])
			usage();
		fd = open_output(av[optind], av[optind + 1], &file);
	}

	err = 0;
	if (write(fd, out, outlen) != outlen) {
		fprintf(stderr, "Cannot write to %s: %s\n", 
			file,
			strerror(errno));
		err = 1;
	}

	return err;
}