Example #1
0
gboolean
utils_should_ignore_file (const char *filename, gboolean only_ifcfg)
{
	gs_free char *base = NULL;

	g_return_val_if_fail (filename != NULL, TRUE);

	base = g_path_get_basename (filename);

	/* Only handle ifcfg, keys, and routes files */
	if (strncmp (base, IFCFG_TAG, strlen (IFCFG_TAG)) != 0) {
		if (only_ifcfg)
			return TRUE;
		else if (   strncmp (base, KEYS_TAG, strlen (KEYS_TAG)) != 0
		         && strncmp (base, ROUTE_TAG, strlen (ROUTE_TAG)) != 0
		         && strncmp (base, ROUTE6_TAG, strlen (ROUTE6_TAG)) != 0)
			return TRUE;
	}

	/* But not those that have certain suffixes */
	if (   check_suffix (base, BAK_TAG)
	    || check_suffix (base, TILDE_TAG)
	    || check_suffix (base, ORIG_TAG)
	    || check_suffix (base, REJ_TAG)
	    || check_suffix (base, RPMNEW_TAG)
	    || check_suffix (base, AUGNEW_TAG)
	    || check_suffix (base, AUGTMP_TAG)
	    || check_rpm_temp_suffix (base))
		return TRUE;

	return FALSE;
}
gboolean
nms_keyfile_utils_should_ignore_file (const char *filename)
{
	gs_free char *base = NULL;

	g_return_val_if_fail (filename != NULL, TRUE);

	base = g_path_get_basename (filename);
	g_return_val_if_fail (base != NULL, TRUE);

	/* Ignore hidden and backup files */
	/* should_ignore_file() must mirror escape_filename() */
	if (check_prefix (base, ".") || check_suffix (base, "~"))
		return TRUE;
	/* Ignore temporary files */
	if (check_mkstemp_suffix (base))
		return TRUE;
	/* Ignore 802.1x certificates and keys */
	if (check_suffix (base, PEM_TAG) || check_suffix (base, DER_TAG))
		return TRUE;

	return FALSE;
}
char *
nms_keyfile_utils_escape_filename (const char *filename)
{
	GString *str;
	const char *f = filename;
	const char ESCAPE_CHAR = '*';

	/* keyfile used to escape with '*', do not change that behavior.
	 * But for newly added escapings, use '_' instead. */
	const char ESCAPE_CHAR2 = '_';

	g_return_val_if_fail (filename && filename[0], NULL);

	str = g_string_sized_new (60);

	/* Convert '/' to ESCAPE_CHAR */
	for (f = filename; f[0]; f++) {
		if (f[0] == '/')
			g_string_append_c (str, ESCAPE_CHAR);
		else
			g_string_append_c (str, f[0]);
	}

	/* escape_filename() must avoid anything that should_ignore_file() would reject.
	 * We can escape here more aggressivly then what we would read back. */
	if (check_prefix (str->str, "."))
		str->str[0] = ESCAPE_CHAR2;
	if (check_suffix (str->str, "~"))
		str->str[str->len - 1] = ESCAPE_CHAR2;
	if (   check_mkstemp_suffix (str->str)
	    || check_suffix (str->str, PEM_TAG)
	    || check_suffix (str->str, DER_TAG))
		g_string_append_c (str, ESCAPE_CHAR2);

	return g_string_free (str, FALSE);;
}
Example #4
0
int main(int argc, char **argv) {
    int i, j, found = 0;
    for (i = 1; i != argc; ++i) {
        if (!strcmp(argv[i], "-o")) {
            assert(i + 1 != argc);
            if (!check_suffix(argv[i + 1], ".o")) {
                found = i + 1;
            }
            break;
        }
    }
    if (found) {
        pid_t pid = fork();
        if (!pid) {
            int new_argc = argc - found + 2;
            char *new_argv[new_argc + 1];
            new_argv[0] = "llvm-link";
            new_argv[1] = "-o";
            for (i = 2, j = found; j != argc; ++j) {
                if(!strcmp(argv[j], "--start-group")||
                        !strcmp(argv[j], "--end-group")){
                    continue;
                }
                new_argv[i] = malloc(strlen(argv[j]) + 3 + 1);
                //seems that alloca does not work. Will investigate later
                sprintf(new_argv[i], "%s.bc", argv[j]);
                //add some check here
                if(i > 2 && !check_is_bc(new_argv[i])){
                    continue; //the file NOT a LLVM IR
                }
                ++i; //accept the current bc
            }
            new_argv[i] = NULL;
            if(i > 3){
                return execvp(new_argv[0], new_argv);
            }
            return 0;
        }
        int status;
        pid_t pid2 = wait(&status);
        assert(pid == pid2);
        if(status)
            return status;
    }
    argv[0] = "ld-real";
    return execvp(argv[0], argv);
}
Example #5
0
File: bin.c Project: bitursa/maos
/*
  Process the input file name and return file names that can be open to
  read/write. If the file name does not end with .bin or .bin.gz it will add to
  the end .bin or .bin.gz depending on the value of defaultgzip. For read only
  access, it will also look into the path for files.
*/
static char* procfn(const char *fn, const char *mod, const int defaultgzip) {
    char *fn2;
    if(fn[0]=='~') {
        fn2=malloc(strlen(HOME)+strlen(fn)+16);
        strcpy(fn2,HOME);
        strcat(fn2,fn+1);
    } else {
        fn2=malloc(strlen(fn)+16);
        strcpy(fn2,fn);
    }
    /*If there is no recognized suffix, add .bin in the end. */
    if(!check_suffix(fn2,".bin")  && !check_suffix(fn2, ".bin.gz")
            && !check_suffix(fn2,".fits") && !check_suffix(fn2, ".fits.gz")) {
        strncat(fn2, ".bin", 4);
    }
    if(mod[0]=='r') {
        char *fnr=NULL;
        if(!(fnr=search_file(fn2))) { /*If does not exist. */
            if(!check_suffix(fn2, ".gz")) {
                /*does not end with .gz, add gz*/
                strncat(fn2, ".gz", 3);
            } else if (check_suffix(fn, ".gz")) {
                /*ended with gz, remove .gz*/
                fn2[strlen(fn2)-3]='\0';
            }
            fnr=search_file(fn2);
        }
        free(fn2);
        fn2=fnr;
        if(!fnr) {

        }
    } else if (mod[0]=='w' || mod[0]=='a') {
        if(disable_save) { //When saving is disabled, allow writing to cache folder.
            char fncache[PATH_MAX];
            snprintf(fncache, PATH_MAX, "%s/.aos/cache", HOME);
            if(mystrcmp(fn2, fncache)) {
                warning("Saving is disabled for %s.\n", fn2);
                free(fn2);
                fn2=0;
            }
        }
        if(fn2 && islink(fn2)) { /*remove old file to avoid write over a symbolic link. */
            if(remove(fn2)) {
                error("Failed to remove %s\n", fn2);
            }
        }
    } else {
        error("Invalid mode\n");
    }
    return fn2;
}
Example #6
0
/*
 * Set outfile based on the suffix.  In most cases we just strip
 * off the suffix but things like .tgz and .taz are special.
 */
char *
set_outfile(const char *infile, char *outfile, size_t osize)
{
	const char *s;
	char *cp;

	if ((s = check_suffix(infile)) == NULL)
		return (NULL);

	(void)strlcpy(outfile, infile, osize);
	cp = outfile + (s - infile) + 1;
	/*
	 * Convert tgz and taz -> tar, else drop the suffix.
	 */
	if (strcmp(cp, "tgz") == 0) {
		cp[1] = 'a';
		cp[2] = 'r';
	} else if (strcmp(cp, "taz") == 0)
		cp[2] = 'r';
	else
		cp[-1] = '\0';
	return (outfile);
}
gboolean
utils_should_ignore_file (const char *filename, gboolean only_ifcfg)
{
	char *base;
	gboolean ignore = TRUE;
	gboolean is_ifcfg = FALSE;
	gboolean is_other = FALSE;

	g_return_val_if_fail (filename != NULL, TRUE);

	base = g_path_get_basename (filename);
	g_return_val_if_fail (base != NULL, TRUE);

	/* Only handle ifcfg, keys, and routes files */
	if (!strncmp (base, IFCFG_TAG, strlen (IFCFG_TAG)))
		is_ifcfg = TRUE;

	if (only_ifcfg == FALSE) {
		if (   !strncmp (base, KEYS_TAG, strlen (KEYS_TAG))
		    || !strncmp (base, ROUTE_TAG, strlen (ROUTE_TAG))
		    || !strncmp (base, ROUTE6_TAG, strlen (ROUTE6_TAG)))
				is_other = TRUE;
	}

	/* But not those that have certain suffixes */
	if (   (is_ifcfg || is_other)
	    && !check_suffix (base, BAK_TAG)
	    && !check_suffix (base, TILDE_TAG)
	    && !check_suffix (base, ORIG_TAG)
	    && !check_suffix (base, REJ_TAG)
	    && !check_suffix (base, RPMNEW_TAG)
	    && !check_suffix (base, AUGNEW_TAG)
	    && !check_suffix (base, AUGTMP_TAG)
	    && !check_rpm_temp_suffix (base))
		ignore = FALSE;

	g_free (base);
	return ignore;
}
/* uncompress the given file and remove the original */
static off_t
file_uncompress(char *file, char *outfile, size_t outsize)
{
	struct stat isb, osb;
	off_t size;
	ssize_t rbytes;
	unsigned char header1[4];
	enum filetype method;
	int fd, ofd, zfd = -1;
#ifndef SMALL
	ssize_t rv;
	time_t timestamp = 0;
	char name[PATH_MAX + 1];
#endif

	/* gather the old name info */

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		maybe_warn("can't open %s", file);
		goto lose;
	}

	strlcpy(outfile, file, outsize);
	if (check_suffix(outfile, 1) == NULL && !(cflag || lflag)) {
		maybe_warnx("%s: unknown suffix -- ignored", file);
		goto lose;
	}

	rbytes = read(fd, header1, sizeof header1);
	if (rbytes != sizeof header1) {
		/* we don't want to fail here. */
#ifndef SMALL
		if (fflag)
			goto lose;
#endif
		if (rbytes == -1)
			maybe_warn("can't read %s", file);
		else
			goto unexpected_EOF;
		goto lose;
	}

	method = file_gettype(header1);
#ifndef SMALL
	if (fflag == 0 && method == FT_UNKNOWN) {
		maybe_warnx("%s: not in gzip format", file);
		goto lose;
	}

#endif

#ifndef SMALL
	if (method == FT_GZIP && Nflag) {
		unsigned char ts[4];	/* timestamp */

		rv = pread(fd, ts, sizeof ts, GZIP_TIMESTAMP);
		if (rv >= 0 && rv < (ssize_t)(sizeof ts))
			goto unexpected_EOF;
		if (rv == -1) {
			if (!fflag)
				maybe_warn("can't read %s", file);
			goto lose;
		}
		timestamp = ts[3] << 24 | ts[2] << 16 | ts[1] << 8 | ts[0];

		if (header1[3] & ORIG_NAME) {
			rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME);
			if (rbytes < 0) {
				maybe_warn("can't read %s", file);
				goto lose;
			}
			if (name[0] != '\0') {
				char *dp, *nf;

				/* Make sure that name is NUL-terminated */
				name[rbytes] = '\0';

				/* strip saved directory name */
				nf = strrchr(name, '/');
				if (nf == NULL)
					nf = name;
				else
					nf++;

				/* preserve original directory name */
				dp = strrchr(file, '/');
				if (dp == NULL)
					dp = file;
				else
					dp++;
				snprintf(outfile, outsize, "%.*s%.*s",
						(int) (dp - file), 
						file, (int) rbytes, nf);
			}
		}
	}
#endif
	lseek(fd, 0, SEEK_SET);

	if (cflag == 0 || lflag) {
		if (fstat(fd, &isb) != 0)
			goto lose;
#ifndef SMALL
		if (isb.st_nlink > 1 && lflag == 0 && fflag == 0) {
			maybe_warnx("%s has %d other links -- skipping",
			    file, isb.st_nlink - 1);
			goto lose;
		}
		if (nflag == 0 && timestamp)
			isb.st_mtime = timestamp;
		if (check_outfile(outfile) == 0)
			goto lose;
#endif
	}

	if (cflag == 0 && lflag == 0) {
		zfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0600);
		if (zfd == STDOUT_FILENO) {
			/* We won't close STDOUT_FILENO later... */
			zfd = dup(zfd);
			close(STDOUT_FILENO);
		}
		if (zfd == -1) {
			maybe_warn("can't open %s", outfile);
			goto lose;
		}
	} else
		zfd = STDOUT_FILENO;

	switch (method) {
#ifndef NO_BZIP2_SUPPORT
	case FT_BZIP2:
		/* XXX */
		if (lflag) {
			maybe_warnx("no -l with bzip2 files");
			goto lose;
		}

		size = unbzip2(fd, zfd, NULL, 0, NULL);
		break;
#endif

#ifndef NO_COMPRESS_SUPPORT
	case FT_Z: {
		FILE *in, *out;

		/* XXX */
		if (lflag) {
			maybe_warnx("no -l with Lempel-Ziv files");
			goto lose;
		}

		if ((in = zdopen(fd)) == NULL) {
			maybe_warn("zdopen for read: %s", file);
			goto lose;
		}

		out = fdopen(dup(zfd), "w");
		if (out == NULL) {
			maybe_warn("fdopen for write: %s", outfile);
			fclose(in);
			goto lose;
		}

		size = zuncompress(in, out, NULL, 0, NULL);
		/* need to fclose() if ferror() is true... */
		if (ferror(in) | fclose(in)) {
			maybe_warn("failed infile fclose");
			unlink(outfile);
			(void)fclose(out);
		}
		if (fclose(out) != 0) {
			maybe_warn("failed outfile fclose");
			unlink(outfile);
			goto lose;
		}
		break;
	}
#endif

#ifndef NO_PACK_SUPPORT
	case FT_PACK:
		if (lflag) {
			maybe_warnx("no -l with packed files");
			goto lose;
		}

		size = unpack(fd, zfd, NULL, 0, NULL);
		break;
#endif

#ifndef NO_XZ_SUPPORT
	case FT_XZ:
		if (lflag) {
			maybe_warnx("no -l with xz files");
			goto lose;
		}

		size = unxz(fd, zfd, NULL, 0, NULL);
		break;
#endif

#ifndef SMALL
	case FT_UNKNOWN:
		if (lflag) {
			maybe_warnx("no -l for unknown filetypes");
			goto lose;
		}
		size = cat_fd(NULL, 0, NULL, fd);
		break;
#endif
	default:
		if (lflag) {
			print_list(fd, isb.st_size, outfile, isb.st_mtime);
			close(fd);
			return -1;	/* XXX */
		}

		size = gz_uncompress(fd, zfd, NULL, 0, NULL, file);
		break;
	}

	if (close(fd) != 0)
		maybe_warn("couldn't close input");
	if (zfd != STDOUT_FILENO && close(zfd) != 0)
		maybe_warn("couldn't close output");

	if (size == -1) {
		if (cflag == 0)
			unlink(outfile);
		maybe_warnx("%s: uncompress failed", file);
		return -1;
	}

	/* if testing, or we uncompressed to stdout, this is all we need */
#ifndef SMALL
	if (tflag)
		return size;
#endif
	/* if we are uncompressing to stdin, don't remove the file. */
	if (cflag)
		return size;

	/*
	 * if we create a file...
	 */
	/*
	 * if we can't stat the file don't remove the file.
	 */

	ofd = open(outfile, O_RDWR, 0);
	if (ofd == -1) {
		maybe_warn("couldn't open (leaving original): %s",
			   outfile);
		return -1;
	}
	if (fstat(ofd, &osb) != 0) {
		maybe_warn("couldn't stat (leaving original): %s",
			   outfile);
		close(ofd);
		return -1;
	}
	if (osb.st_size != size) {
		maybe_warnx("stat gave different size: %" PRIdOFF
				" != %" PRIdOFF " (leaving original)",
				size, osb.st_size);
		close(ofd);
		unlink(outfile);
		return -1;
	}
	unlink_input(file, &isb);
#ifndef SMALL
	copymodes(ofd, &isb, outfile);
#endif
	close(ofd);
	return size;

    unexpected_EOF:
	maybe_warnx("%s: unexpected end of file", file);
    lose:
	if (fd != -1)
		close(fd);
	if (zfd != -1 && zfd != STDOUT_FILENO)
		close(fd);
	return -1;
}
/*
 * compress the given file: create a corresponding .gz file and remove the
 * original.
 */
static off_t
file_compress(char *file, char *outfile, size_t outsize)
{
	int in;
	int out;
	off_t size, insize;
#ifndef SMALL
	struct stat isb, osb;
	const suffixes_t *suff;
#endif

	in = open(file, O_RDONLY);
	if (in == -1) {
		maybe_warn("can't open %s", file);
		return -1;
	}

	if (cflag == 0) {
#ifndef SMALL
		if (fstat(in, &isb) == 0) {
			if (isb.st_nlink > 1 && fflag == 0) {
				maybe_warnx("%s has %d other link%s -- "
					    "skipping", file, isb.st_nlink - 1,
					    isb.st_nlink == 1 ? "" : "s");
				close(in);
				return -1;
			}
		}

		if (fflag == 0 && (suff = check_suffix(file, 0))
		    && suff->zipped[0] != 0) {
			maybe_warnx("%s already has %s suffix -- unchanged",
				    file, suff->zipped);
			close(in);
			return -1;
		}
#endif

		/* Add (usually) .gz to filename */
		if ((size_t)snprintf(outfile, outsize, "%s%s",
					file, suffixes[0].zipped) >= outsize)
			memcpy(outfile + outsize - suffixes[0].ziplen - 1,
				suffixes[0].zipped, suffixes[0].ziplen + 1);

#ifndef SMALL
		if (check_outfile(outfile) == 0) {
			close(in);
			return -1;
		}
#endif
	}

	if (cflag == 0) {
		out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
		if (out == -1) {
			maybe_warn("could not create output: %s", outfile);
			fclose(stdin);
			return -1;
		}
	} else
		out = STDOUT_FILENO;

	insize = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime);

	(void)close(in);

	/*
	 * If there was an error, insize will be -1.
	 * If we compressed to stdout, just return the size.
	 * Otherwise stat the file and check it is the correct size.
	 * We only blow away the file if we can stat the output and it
	 * has the expected size.
	 */
	if (cflag != 0)
		return insize == -1 ? -1 : size;

#ifndef SMALL
	if (fstat(out, &osb) != 0) {
		maybe_warn("couldn't stat: %s", outfile);
		goto bad_outfile;
	}

	if (osb.st_size != size) {
		maybe_warnx("output file: %s wrong size (%" PRIdOFF
				" != %" PRIdOFF "), deleting",
				outfile, osb.st_size, size);
		goto bad_outfile;
	}

	copymodes(out, &isb, outfile);
#endif
	if (close(out) == -1)
		maybe_warn("couldn't close output");

	/* output is good, ok to delete input */
	unlink_input(file, &isb);
	return size;

#ifndef SMALL
    bad_outfile:
	if (close(out) == -1)
		maybe_warn("couldn't close output");

	maybe_warnx("leaving original %s", file);
	unlink(outfile);
	return size;
#endif
}
Example #10
0
/*
 * Note: info->size == 6 is special; this means a base size 4 bytes,
 * and secondiory (high) size of 2 bytes.  This is needed for the
 * special case of i_blocks_high and i_file_acl_high.
 */
static errcode_t parse_uint(struct field_set_info *info, char *field,
			    char *arg)
{
	unsigned long long n, num, mask, limit;
	int suffix = check_suffix(field);
	char *tmp;
	void *field1 = info->ptr, *field2 = info->ptr2;
	int size = (info->size == 6) ? 4 : info->size;
	union {
		__u64	*ptr64;
		__u32	*ptr32;
		__u16	*ptr16;
		__u8	*ptr8;
	} u;

	if (suffix == 1)
		field2 = 0;
	if (suffix == 2) {
		field1 = field2;
		field2 = 0;
	}

	u.ptr8 = (__u8 *) field1;
	if (info->flags & FLAG_ARRAY)
		u.ptr8 += array_idx * info->size;

	errno = 0;
	num = STRTOULL(arg, &tmp, 0);
	if (*tmp || errno) {
		fprintf(stderr, "Couldn't parse '%s' for field %s.\n",
			arg, info->name);
		return EINVAL;
	}
	mask = ~0ULL >> ((8 - size) * 8);
	limit = ~0ULL >> ((8 - info->size) * 8);
	if (field2 && info->size != 6)
		limit = ~0ULL >> ((8 - info->size*2) * 8);

	if (num > limit) {
		fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n",
			arg, info->name, limit);
		return EINVAL;
	}
	n = num & mask;
	switch (size) {
	case 8:
		*u.ptr64 = n;
		break;
	case 4:
		*u.ptr32 = n;
		break;
	case 2:
		*u.ptr16 = n;
		break;
	case 1:
		*u.ptr8 = n;
		break;
	}
	if (!field2)
		return 0;
	n = num >> (size*8);
	u.ptr8 = (__u8 *) field2;
	if (info->size == 6)
		size = 2;
	switch (size) {
	case 8:
		*u.ptr64 = n;
		break;
	case 4:
		*u.ptr32 = n;
		break;
	case 2:
		*u.ptr16 = n;
		break;
	case 1:
		*u.ptr8 = n;
		break;
	}
	return 0;
}
Example #11
0
static struct field_set_info *find_field(struct field_set_info *fields,
					 char *field)
{
	struct field_set_info *ss;
	const char	*prefix;
	char		*arg, *delim, *idx, *tmp;
	int		suffix, prefix_len;

	if (fields == super_fields)
		prefix = "s_";
	else if (fields == inode_fields)
		prefix = "i_";
	else
		prefix = "bg_";
	prefix_len = strlen(prefix);
	if (strncmp(field, prefix, prefix_len) == 0)
		field += prefix_len;

	arg = malloc(strlen(field)+1);
	if (!arg)
		return NULL;
	strcpy(arg, field);

	idx = strchr(arg, '[');
	if (idx) {
		*idx++ = 0;
		delim = idx + strlen(idx) - 1;
		if (!*idx || *delim != ']')
			idx = 0;
		else
			*delim = 0;
	}
	/*
	 * Can we parse the number?
	 */
	if (idx) {
		array_idx = strtol(idx, &tmp, 0);
		if (*tmp)
			idx = 0;
	}

	/*
	 * If there is a valid _hi or a _lo suffix, strip it off
	 */
	suffix = check_suffix(arg);
	if (suffix > 0)
		arg[strlen(arg)-3] = 0;

	for (ss = fields ; ss->name ; ss++) {
		if (suffix && ss->ptr2 == 0)
			continue;
		if (ss->flags & FLAG_ARRAY) {
			if (!idx || (strcmp(ss->name, arg) != 0))
				continue;
			if (ss->max_idx > 0 && array_idx >= ss->max_idx)
				continue;
		} else {
			if (strcmp(ss->name, arg) != 0)
				continue;
		}
		free(arg);
		return ss;
	}
	free(arg);
	return NULL;
}
Example #12
0
void
zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
{
  if (plugin_manager == NULL || plugin_manager->path == NULL) {
    return;
  }

  GIRARA_LIST_FOREACH(plugin_manager->path, char*, iter, plugindir)
  /* read all files in the plugin directory */
  GDir* dir = g_dir_open(plugindir, 0, NULL);
  if (dir == NULL) {
    girara_error("could not open plugin directory: %s", plugindir);
    girara_list_iterator_next(iter);
    continue;
  }

  char* name = NULL;
  while ((name = (char*) g_dir_read_name(dir)) != NULL) {
    char* path = g_build_filename(plugindir, name, NULL);
    if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == 0) {
      girara_debug("%s is not a regular file. Skipping.", path);
      g_free(path);
      continue;
    }

    if (check_suffix(path) == false) {
      girara_debug("%s is not a plugin file. Skipping.", path);
      g_free(path);
      continue;
    }

    zathura_plugin_t* plugin = NULL;

    /* load plugin */
    GModule* handle = g_module_open(path, G_MODULE_BIND_LOCAL);
    if (handle == NULL) {
      girara_error("could not load plugin %s (%s)", path, g_module_error());
      g_free(path);
      continue;
    }

    /* resolve symbols and check API and ABI version*/
    zathura_plugin_api_version_t api_version = NULL;
    if (g_module_symbol(handle, PLUGIN_API_VERSION_FUNCTION, (gpointer*) &api_version) == FALSE ||
        api_version == NULL) {
      girara_error("could not find '%s' function in plugin %s", PLUGIN_API_VERSION_FUNCTION, path);
      g_free(path);
      g_module_close(handle);
      continue;
    }

    if (api_version() != ZATHURA_API_VERSION) {
      girara_error("plugin %s has been built againt zathura with a different API version (plugin: %d, zathura: %d)",
                   path, api_version(), ZATHURA_API_VERSION);
      g_free(path);
      g_module_close(handle);
      continue;
    }

    zathura_plugin_abi_version_t abi_version = NULL;
    if (g_module_symbol(handle, PLUGIN_ABI_VERSION_FUNCTION, (gpointer*) &abi_version) == FALSE ||
        abi_version == NULL) {
      girara_error("could not find '%s' function in plugin %s", PLUGIN_ABI_VERSION_FUNCTION, path);
      g_free(path);
      g_module_close(handle);
      continue;
    }

    if (abi_version() != ZATHURA_ABI_VERSION) {
      girara_error("plugin %s has been built againt zathura with a different ABI version (plugin: %d, zathura: %d)",
                   path, abi_version(), ZATHURA_ABI_VERSION);
      g_free(path);
      g_module_close(handle);
      continue;
    }

    zathura_plugin_register_service_t register_service = NULL;
    if (g_module_symbol(handle, PLUGIN_REGISTER_FUNCTION, (gpointer*) &register_service) == FALSE ||
        register_service == NULL) {
      girara_error("could not find '%s' function in plugin %s", PLUGIN_REGISTER_FUNCTION, path);
      g_free(path);
      g_module_close(handle);
      continue;
    }

    plugin = g_try_malloc0(sizeof(zathura_plugin_t));
    if (plugin == NULL) {
      girara_error("Failed to allocate memory for plugin.");
      g_free(path);
      g_module_close(handle);
      continue;
    }

    plugin->content_types = girara_list_new2(g_free);
    plugin->handle = handle;

    register_service(plugin);

    /* register functions */
    if (plugin->register_function == NULL) {
      girara_error("plugin has no document functions register function");
      g_free(path);
      g_free(plugin);
      g_module_close(handle);
      continue;
    }

    plugin->register_function(&(plugin->functions));
    plugin->path = path;

    bool ret = register_plugin(plugin_manager, plugin);
    if (ret == false) {
      girara_error("could not register plugin %s", path);
      zathura_plugin_free(plugin);
    } else {
      girara_debug("successfully loaded plugin %s", path);

      zathura_plugin_version_function_t plugin_major = NULL, plugin_minor = NULL, plugin_rev = NULL;
      g_module_symbol(handle, PLUGIN_VERSION_MAJOR_FUNCTION,    (gpointer*) &plugin_major);
      g_module_symbol(handle, PLUGIN_VERSION_MINOR_FUNCTION,    (gpointer*) &plugin_minor);
      g_module_symbol(handle, PLUGIN_VERSION_REVISION_FUNCTION, (gpointer*) &plugin_rev);
      if (plugin_major != NULL && plugin_minor != NULL && plugin_rev != NULL) {
        plugin->version.major = plugin_major();
        plugin->version.minor = plugin_minor();
        plugin->version.rev   = plugin_rev();
        girara_debug("plugin '%s': version %u.%u.%u", path,
                     plugin->version.major, plugin->version.minor,
                     plugin->version.rev);
      }
    }
  }
  g_dir_close(dir);
  GIRARA_LIST_FOREACH_END(zathura->plugins.path, char*, iter, plugindir);
}
Example #13
0
int
main(int argc, char *argv[])
{
	FTS *ftsp;
	FTSENT *entry;
	const struct compressor *method;
	const char *s;
	char *p, *infile;
	char outfile[MAXPATHLEN], _infile[MAXPATHLEN], suffix[16];
	char *nargv[512];	/* some estimate based on ARG_MAX */
	int bits, ch, error, i, rc, cflag, oflag;
	static const char *optstr[3] = {
		"123456789ab:cdfghlLnNOo:qrS:tvV",
		"cfhlNno:qrtv",
		"fghqr"
	};

	bits = cflag = oflag = 0;
	storename = -1;
	p = __progname;
	if (p[0] == 'g') {
		method = M_DEFLATE;
		bits = 6;
		p++;
	} else
#ifdef SMALL
		method = M_DEFLATE;
#else
		method = M_COMPRESS;
#endif /* SMALL */

	decomp = 0;
	pmode = MODE_COMP;
	if (!strcmp(p, "zcat")) {
		decomp++;
		cflag = 1;
		pmode = MODE_CAT;
	} else {
		if (p[0] == 'u' && p[1] == 'n') {
			p += 2;
			decomp++;
			pmode = MODE_DECOMP;
		}

		if (strcmp(p, "zip") &&
		    strcmp(p, "compress"))
			errx(1, "unknown program name");
	}

	strlcpy(suffix, method->suffix, sizeof(suffix));

	nargv[0] = NULL;
	if (method == M_DEFLATE && (p = getenv("GZIP")) != NULL) {
		char *last;

		nargv[0] = *argv++;
		for (i = 1, (p = strtok_r(p, " ", &last)); p != NULL;
		    (p = strtok_r(NULL, " ", &last)), i++)
			if (i < sizeof(nargv)/sizeof(nargv[1]) - argc - 1)
				nargv[i] = p;
			else
				errx(1, "GZIP is too long");
		argc += i - 1;
		while ((nargv[i++] = *argv++))
			;
		argv = nargv;
	}

	while ((ch = getopt_long(argc, argv, optstr[pmode], longopts, NULL)) != -1)
		switch(ch) {
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			method = M_DEFLATE;
			strlcpy(suffix, method->suffix, sizeof(suffix));
			bits = ch - '0';
			break;
		case 'a':
			warnx("option -a is ignored on this system");
			break;
		case 'b':
			bits = strtol(optarg, &p, 10);
			/*
			 * POSIX 1002.3 says 9 <= bits <= 14 for portable
			 * apps, but says the implementation may allow
			 * greater.
			 */
			if (*p)
				errx(1, "illegal bit count -- %s", optarg);
			break;
		case 'c':
			cflag = 1;
			break;
		case 'd':		/* Backward compatible. */
			decomp++;
			break;
		case 'f':
			force++;
			break;
		case 'g':
			method = M_DEFLATE;
			strlcpy(suffix, method->suffix, sizeof(suffix));
			bits = 6;
			break;
		case 'l':
			list++;
			testmode = 1;
			decomp++;
			break;
		case 'n':
			storename = 0;
			break;
		case 'N':
			storename = 1;
			break;
#ifndef SMALL
		case 'O':
			method = M_COMPRESS;
			strlcpy(suffix, method->suffix, sizeof(suffix));
			break;
#endif /* SMALL */
		case 'o':
			if (strlcpy(outfile, optarg,
			    sizeof(outfile)) >= sizeof(outfile))
				errx(1, "-o argument is too long");
			oflag = 1;
			break;
		case 'q':
			verbose = -1;
			break;
		case 'S':
			p = suffix;
			if (optarg[0] != '.')
				*p++ = '.';
			strlcpy(p, optarg, sizeof(suffix) - (p - suffix));
			p = optarg;
			break;
		case 't':
			testmode = 1;
			decomp++;
			break;
#ifndef SMALL
		case 'V':
			printf("%s\n%s\n", main_rcsid, gz_rcsid);
			printf("%s\n%s\n", z_rcsid, null_rcsid);
#endif
			exit (0);
		case 'v':
			verbose++;
			break;
#ifndef SMALL
		case 'L':
			fputs(copyright, stderr);
			fputs(license, stderr);
#endif
			exit (0);
		case 'r':
			recurse++;
			break;

		case 'h':
			usage(0);
			break;
		default:
			usage(1);
		}
	argc -= optind;
	argv += optind;

	if (argc == 0) {
		if (nargv[0] == NULL)
			argv = nargv;
		/* XXX - make sure we don't oflow nargv in $GZIP case (millert) */
		argv[0] = "-";
		argv[1] = NULL;
	}
	if (oflag && (recurse || argc > 1))
		errx(1, "-o option may only be used with a single input file");

	if ((cat && argc) + testmode + oflag > 1)
		errx(1, "may not mix -o, -c, or -t options");
	/*
	 * By default, when compressing store the original name and timestamp
	 * in the header.  Do not restore these when decompressing unless
	 * the -N option is given.
	 */
	if (storename == -1)
		storename = !decomp;

	if ((ftsp = fts_open(argv, FTS_PHYSICAL|FTS_NOCHDIR, 0)) == NULL)
		err(1, NULL);
	for (rc = SUCCESS; (entry = fts_read(ftsp)) != NULL;) {
		cat = cflag;
		pipin = 0;
		infile = entry->fts_path;
		if (infile[0] == '-' && infile[1] == '\0') {
			infile = "stdin";
			pipin++;
			if (!oflag)
				cat = 1;
		}
		else
			switch (entry->fts_info) {
			case FTS_D:
				if (!recurse) {
					warnx("%s is a directory: ignored",
					    infile);
					fts_set(ftsp, entry, FTS_SKIP);
				}
				continue;
			case FTS_DP:
				continue;
			case FTS_NS:
				/*
				 * If file does not exist and has no suffix,
				 * tack on the default suffix and try that.
				 */
				if (entry->fts_errno == ENOENT) {
					p = strrchr(entry->fts_accpath, '.');
					if ((p == NULL ||
					    strcmp(p, suffix) != 0) &&
					    snprintf(_infile, sizeof(_infile),
					    "%s%s", infile, suffix) <
					    sizeof(_infile) &&
					    stat(_infile, entry->fts_statp) ==
					    0 &&
					    S_ISREG(entry->fts_statp->st_mode)) {
						infile = _infile;
						break;
					}
				}
			case FTS_ERR:
			case FTS_DNR:
				warnx("%s: %s", infile,
				    strerror(entry->fts_errno));
				rc = rc ? rc : WARNING;
				continue;
			default:
				if (!S_ISREG(entry->fts_statp->st_mode) &&
				    !(S_ISLNK(entry->fts_statp->st_mode) &&
				    cat)) {
					warnx("%s not a regular file%s",
					    infile, cat ? "" : ": unchanged");
					rc = rc ? rc : WARNING;
					continue;
				}
				break;
			}

		if (!decomp && !pipin && (s = check_suffix(infile)) != NULL) {
			warnx("%s already has %s suffix -- unchanged",
			    infile, s);
			rc = rc ? rc : WARNING;
			continue;
		}

		if (!oflag) {
			if (cat)
				strlcpy(outfile, "stdout", sizeof(outfile));
			else if (decomp) {
				if (set_outfile(infile, outfile,
				    sizeof outfile) == NULL) {
					if (!recurse) {
						warnx("%s: unknown suffix: "
						    "ignored", infile);
						rc = rc ? rc : WARNING;
					}
					continue;
				}
			} else {
				if (snprintf(outfile, sizeof(outfile),
				    "%s%s", infile, suffix) >= sizeof(outfile)) {
					warnx("%s%s: name too long",
					    infile, suffix);
					rc = rc ? rc : WARNING;
					continue;
				}
			}
		}

		if (verbose > 0 && !pipin && !list)
			fprintf(stderr, "%s:\t", infile);

		error = (decomp ? dodecompress : docompress)
		    (infile, outfile, method, bits, entry->fts_statp);

		switch (error) {
		case SUCCESS:
			if (!cat && !testmode) {
				if (!pipin && unlink(infile) && verbose >= 0)
					warn("input: %s", infile);
			}
			break;
		case WARNING:
			rc = rc ? rc : WARNING;
			break;
		default:
			rc = FAILURE;
			break;
		}
	}
	if (list)
		list_stats(NULL, NULL, NULL);

	exit(rc);
}
Example #14
0
File: bin.c Project: bitursa/maos
/**
   Open a bin file for read/write access.

   Whether the file is gzipped or not is automatically determined when open for
   reading. When open for writing, if the file name ends with .bin or .fits,
   will not be gzipped. If the file has no suffix, the file will be gzipped and
   .bin is appened to file name.
*/
file_t* zfopen_try(const char *fn, const char *mod) {
    LOCK(lock);
    file_t* fp=calloc(1, sizeof(file_t));
    const char* fn2=fp->fn=procfn(fn,mod,1);
    if(!fn2) {
        if(mod[0]=='r') {
            error("%s does not exist for read\n", fn);
        }
        free(fp);
        fp=0;
        goto end;
    }
    /*Now open the file to get a fd number that we can use to lock on the
      file.*/
    switch(mod[0]) {
    case 'r':/*read only */
        if((fp->fd=open(fn2, O_RDONLY))==-1) {
            perror("open for read");
        } else {
            if(futimes(fp->fd, NULL)) {
                warning("change access/modification time failed for %s.\n", fp->fn);
            }
        }
        break;
    case 'w':/*write */
    case 'a':
        if((fp->fd=open(fn2, O_RDWR | O_CREAT, 0666))==-1) {
            perror("open for write");
        } else {
            if(flock(fp->fd, LOCK_EX|LOCK_NB)) {
                perror("flock");
                close(fp->fd);
                fp->fd=-1;
            } else if(mod[0]=='w' && ftruncate(fp->fd, 0)) { /*Need to manually truncate the file. */
                perror("ftruncate");
            }
        }
        break;
    default:
        error("Unknown mod=%s\n", mod);
    }
    if(fp->fd==-1) {
        error("Unable to open file %s for %s\n", fn2, mod[0]=='r'?"reading":"writing");
        free(fp->fn);
        free(fp);
        fp=0;
        goto end;
    }
    /*check fn instead of fn2. if end of .bin or .fits, disable compressing.*/
    if(mod[0]=='w') {
        if(check_suffix(fn, ".bin") || check_suffix(fn, ".fits")) {
            fp->isgzip=0;
        } else if (check_suffix(fn, ".gz")) {
            fp->isgzip=1;
        } else {
            fp->isgzip=0;
        }
    } else {
        uint16_t magic;
        if(read(fp->fd, &magic, sizeof(uint16_t))!=sizeof(uint16_t)) {
            error("Unable to read %s.\n", fp->fn);
        }
        if(magic==0x8b1f) {
            fp->isgzip=1;
        } else {
            fp->isgzip=0;
        }
        lseek(fp->fd, 0, SEEK_SET);
    }
    if(fp->isgzip) {
        if(!(fp->p=gzdopen(fp->fd,mod))) {
            error("Error gzdopen for %s\n",fn2);
        }
    } else {
        if(!(fp->p=fdopen(fp->fd,mod))) {
            error("Error fdopen for %s\n",fn2);
        }
    }
    if(check_suffix(fn, ".fits") || check_suffix(fn, ".fits.gz")) {
        fp->isfits=1;
    }
    if(mod[0]=='w' && !fp->isfits) {
        write_timestamp(fp);
    }
end:
    UNLOCK(lock);
    return fp;
}