Example #1
0
bool
find_command(const char *prog)
{
  struct varbuf filename = VARBUF_INIT;
  struct stat stab;
  const char *path_list;
  const char *path, *path_end;
  size_t path_len;

  path_list = getenv("PATH");
  if (!path_list)
    ohshit(_("PATH is not set"));

  for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
    path_end = strchr(path, ':');
    path_len = path_end ? (size_t)(path_end - path) : strlen(path);

    varbuf_reset(&filename);
    varbuf_add_buf(&filename, path, path_len);
    if (path_len)
      varbuf_add_char(&filename, '/');
    varbuf_add_str(&filename, prog);
    varbuf_end_str(&filename);

    if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111)) {
      varbuf_destroy(&filename);
      return true;
    }
  }

  varbuf_destroy(&filename);
  return false;
}
Example #2
0
static void
virt_source_version(struct varbuf *vb,
                    const struct pkginfo *pkg, const struct pkgbin *pkgbin,
                    enum fwriteflags flags, const struct fieldinfo *fip)
{
	const char *version;
	size_t len;

	if (pkgbin->source)
		version = strchr(pkgbin->source, '(');
	else
		version = NULL;

	if (version == NULL) {
		varbufversion(vb, &pkgbin->version, vdew_nonambig);
	} else {
		version++;

		len = strcspn(version, ")");
		if (len == 0)
			len = strlen(version);

		varbuf_add_buf(vb, version, len);
	}
}
Example #3
0
File: help.c Project: pexip/os-dpkg
/**
 * Verify that some programs can be found in the PATH.
 */
void checkpath(void) {
  static const char *const prog_list[] = {
    DEFAULTSHELL,
    RM,
    TAR,
    FIND,
    BACKEND,
    "ldconfig",
#if WITH_START_STOP_DAEMON
    "start-stop-daemon",
#endif
    NULL
  };

  const char *const *prog;
  const char *path_list;
  struct varbuf filename = VARBUF_INIT;
  int warned= 0;

  path_list = getenv("PATH");
  if (!path_list)
    ohshit(_("PATH is not set."));

  for (prog = prog_list; *prog; prog++) {
    struct stat stab;
    const char *path, *path_end;
    size_t path_len;

    for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
      path_end = strchr(path, ':');
      path_len = path_end ? (size_t)(path_end - path) : strlen(path);

      varbuf_reset(&filename);
      varbuf_add_buf(&filename, path, path_len);
      if (path_len)
        varbuf_add_char(&filename, '/');
      varbuf_add_str(&filename, *prog);
      varbuf_end_str(&filename);

      if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111))
        break;
    }
    if (!path) {
      warning(_("'%s' not found in PATH or not executable."), *prog);
      warned++;
    }
  }

  varbuf_destroy(&filename);

  if (warned)
    forcibleerr(fc_badpath,
                P_("%d expected program not found in PATH or not executable.\n%s",
                   "%d expected programs not found in PATH or not executable.\n%s",
                   warned),
                warned, _("Note: root's PATH should usually contain "
                          "/usr/local/sbin, /usr/sbin and /sbin."));
}
Example #4
0
static void
test_varbuf_addbuf(void)
{
	struct varbuf vb;

	varbuf_init(&vb, 5);

	varbuf_add_buf(&vb, "1234567890", 10);
	test_pass(vb.used == 10);
	test_pass(vb.size >= vb.used);
	test_mem(vb.buf, ==, "1234567890", 10);

	varbuf_add_buf(&vb, "abcde", 5);
	test_pass(vb.used == 15);
	test_pass(vb.size >= vb.used);
	test_mem(vb.buf, ==, "1234567890abcde", 15);

	varbuf_destroy(&vb);
}
Example #5
0
static void
test_varbuf_reset(void)
{
	struct varbuf vb;

	varbuf_init(&vb, 10);

	varbuf_add_buf(&vb, "1234567890", 10);

	varbuf_reset(&vb);
	test_pass(vb.used == 0);
	test_pass(vb.size >= vb.used);

	varbuf_add_buf(&vb, "abcdefghijklmno", 15);
	test_pass(vb.used == 15);
	test_pass(vb.size >= vb.used);
	test_mem(vb.buf, ==, "abcdefghijklmno", 15);

	varbuf_destroy(&vb);
}
Example #6
0
/**
 * Parse the field and value into the package being constructed.
 */
static void
pkg_parse_field(struct parsedb_state *ps, struct field_state *fs,
                void *parse_obj)
{
  struct pkg_parse_object *pkg_obj = parse_obj;
  const struct nickname *nick;
  const struct fieldinfo *fip;
  int *ip;

  for (nick = nicknames; nick->nick; nick++)
    if (strncasecmp(nick->nick, fs->fieldstart, fs->fieldlen) == 0 &&
        nick->nick[fs->fieldlen] == '\0')
      break;
  if (nick->nick) {
    fs->fieldstart = nick->canon;
    fs->fieldlen = strlen(fs->fieldstart);
  }

  for (fip = fieldinfos, ip = fs->fieldencountered; fip->name; fip++, ip++)
    if (strncasecmp(fip->name, fs->fieldstart, fs->fieldlen) == 0)
      break;
  if (fip->name) {
    if ((*ip)++)
      parse_error(ps,
                  _("duplicate value for `%s' field"), fip->name);

    varbuf_reset(&fs->value);
    varbuf_add_buf(&fs->value, fs->valuestart, fs->valuelen);
    varbuf_end_str(&fs->value);

    fip->rcall(pkg_obj->pkg, pkg_obj->pkgbin, ps, fs->value.buf, fip);
  } else {
    struct arbitraryfield *arp, **larpp;

    if (fs->fieldlen < 2)
      parse_error(ps,
                  _("user-defined field name `%.*s' too short"),
                  fs->fieldlen, fs->fieldstart);
    larpp = &pkg_obj->pkgbin->arbs;
    while ((arp = *larpp) != NULL) {
      if (!strncasecmp(arp->name, fs->fieldstart, fs->fieldlen))
        parse_error(ps,
                   _("duplicate value for user-defined field `%.*s'"),
                   fs->fieldlen, fs->fieldstart);
      larpp = &arp->next;
    }
    arp = nfmalloc(sizeof(struct arbitraryfield));
    arp->name = nfstrnsave(fs->fieldstart, fs->fieldlen);
    arp->value = nfstrnsave(fs->valuestart, fs->valuelen);
    arp->next = NULL;
    *larpp = arp;
  }
}
Example #7
0
static void
virt_summary(struct varbuf *vb,
             const struct pkginfo *pkg, const struct pkgbin *pkgbin,
             enum fwriteflags flags, const struct fieldinfo *fip)
{
	const char *desc;
	int len;

	desc = pkg_summary(pkg, pkgbin, &len);

	varbuf_add_buf(vb, desc, len);
}
Example #8
0
static void
test_varbuf_map_char(void)
{
	struct varbuf vb;

	varbuf_init(&vb, 5);

	varbuf_add_buf(&vb, "1234a5678a9012a", 15);

	varbuf_map_char(&vb, 'a', 'z');
	test_pass(vb.used == 15);
	test_pass(vb.size >= vb.used);
	test_mem(vb.buf, ==, "1234z5678z9012z", 15);

	varbuf_destroy(&vb);
}
Example #9
0
static void
virt_source_package(struct varbuf *vb,
                    const struct pkginfo *pkg, const struct pkgbin *pkgbin,
                    enum fwriteflags flags, const struct fieldinfo *fip)
{
	const char *name;
	size_t len;

	name = pkgbin->source;
	if (name == NULL)
		name = pkg->set->name;

	len = strcspn(name, " ");
	if (len == 0)
		len = strlen(name);

	varbuf_add_buf(vb, name, len);
}
Example #10
0
static void
test_varbuf_detach(void)
{
	struct varbuf vb;
	char *str;

	varbuf_init(&vb, 0);

	varbuf_add_buf(&vb, "1234567890", 10);

	str = varbuf_detach(&vb);

	test_mem(str, ==, "1234567890", 10);
	test_pass(vb.used == 0);
	test_pass(vb.size == 0);
	test_pass(vb.buf == NULL);

	free(str);
}
Example #11
0
static void
test_varbuf_end_str(void)
{
	struct varbuf vb;

	varbuf_init(&vb, 10);

	varbuf_add_buf(&vb, "1234567890X", 11);
	test_pass(vb.used == 11);
	test_pass(vb.size >= vb.used);
	test_mem(vb.buf, ==, "1234567890X", 11);

	varbuf_trunc(&vb, 10);

	varbuf_end_str(&vb);
	test_pass(vb.used == 10);
	test_pass(vb.size >= vb.used + 1);
	test_pass(vb.buf[10] == '\0');
	test_str(vb.buf, ==, "1234567890");

	varbuf_destroy(&vb);
}
Example #12
0
static off_t
buffer_write(struct buffer_data *data, const void *buf, off_t length,
             struct dpkg_error *err)
{
	off_t ret = length;

	switch (data->type) {
	case BUFFER_WRITE_VBUF:
		varbuf_add_buf((struct varbuf *)data->arg.ptr, buf, length);
		break;
	case BUFFER_WRITE_FD:
		ret = fd_write(data->arg.i, buf, length);
		if (ret < 0)
			dpkg_put_errno(err, _("failed to write"));
		break;
	case BUFFER_WRITE_NULL:
		break;
	default:
		internerr("unknown data type %i", data->type);
	}

	return ret;
}
Example #13
0
/**
 * Verify that some programs can be found in the PATH.
 */
void checkpath(void) {
  static const char *const prog_list[] = {
    DEFAULTSHELL,
    RM,
    TAR,
    FIND,
    BACKEND,
    /* Mac OS X uses dyld (Mach-O) instead of ld.so (ELF), and does not have
     * an ldconfig. */
#if defined(__APPLE__) && defined(__MACH__)
    "update_dyld_shared_cache",
#else
    "ldconfig",
#endif
#if BUILD_START_STOP_DAEMON
    "start-stop-daemon",
#endif
    NULL
  };

  const char *const *prog;
  const char *path_list;
  struct varbuf filename = VARBUF_INIT;
  int warned= 0;

  path_list = getenv("PATH");
  if (!path_list)
    ohshit(_("PATH is not set"));

  for (prog = prog_list; *prog; prog++) {
    struct stat stab;
    const char *path, *path_end;
    size_t path_len;

    for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
      path_end = strchr(path, ':');
      path_len = path_end ? (size_t)(path_end - path) : strlen(path);

      varbuf_reset(&filename);
      varbuf_add_buf(&filename, path, path_len);
      if (path_len)
        varbuf_add_char(&filename, '/');
      varbuf_add_str(&filename, *prog);
      varbuf_end_str(&filename);

      if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111))
        break;
    }
    if (!path) {
      warning(_("'%s' not found in PATH or not executable"), *prog);
      warned++;
    }
  }

  varbuf_destroy(&filename);

  if (warned)
    forcibleerr(fc_badpath,
                P_("%d expected program not found in PATH or not executable\n%s",
                   "%d expected programs not found in PATH or not executable\n%s",
                   warned),
                warned, _("Note: root's PATH should usually contain "
                          "/usr/local/sbin, /usr/sbin and /sbin"));
}
Example #14
0
void
pkg_format_show(const struct pkg_format_node *head,
                struct pkginfo *pkg, struct pkgbin *pkgbin)
{
	struct varbuf vb = VARBUF_INIT, fb = VARBUF_INIT, wb = VARBUF_INIT;

	while (head) {
		bool ok;
		char fmt[16];

		ok = false;

		if (head->width > 0)
			snprintf(fmt, 16, "%%%s%zus",
			         ((head->pad) ? "-" : ""), head->width);
		else
			strcpy(fmt, "%s");

		if (head->type == PKG_FORMAT_STRING) {
			varbuf_printf(&fb, fmt, head->data);
			ok = true;
		} else if (head->type == PKG_FORMAT_FIELD) {
			const struct fieldinfo *fip;

			fip = find_field_info(fieldinfos, head);

			if (fip->name == NULL)
				fip = find_field_info(virtinfos, head);

			if (fip->name) {
				fip->wcall(&wb, pkg, pkgbin, 0, fip);

				varbuf_end_str(&wb);
				varbuf_printf(&fb, fmt, wb.buf);
				varbuf_reset(&wb);
				ok = true;
			} else {
				const struct arbitraryfield *afp;

				for (afp = pkgbin->arbs; afp; afp = afp->next)
					if (strcasecmp(head->data, afp->name) == 0) {
						varbuf_printf(&fb, fmt, afp->value);
						ok = true;
						break;
					}
			}
		}

		if (ok) {
			size_t len = strlen(fb.buf);
			if ((head->width > 0) && (len > head->width))
				len = head->width;
			varbuf_add_buf(&vb, fb.buf, len);
		}

		varbuf_reset(&fb);
		head = head->next;
	}

	if (vb.buf) {
		varbuf_end_str(&vb);
		fputs(vb.buf, stdout);
	}

	varbuf_destroy(&wb);
	varbuf_destroy(&fb);
	varbuf_destroy(&vb);
}