Ejemplo n.º 1
0
static void
test_ctype(void)
{
	int c;

	for (c = -1; c < 256; c++) {
		/* Test blank. */
		if (c == '\t' || c == ' ')
			test_pass(c_isblank(c));
		else
			test_fail(c_isblank(c));

		/* Test white. */
		if (c == '\t' || c == ' ' || c == '\n')
			test_pass(c_iswhite(c));
		else
			test_fail(c_iswhite(c));

		/* Test space. */
		if (c == '\t' || c == '\v' || c == '\f' ||
		    c == '\r' || c == '\n' || c == ' ')
			test_pass(c_isspace(c));
		else
			test_fail(c_isspace(c));

		/* Test digit. */
		if (c >= '0' && c <= '9')
			test_pass(c_isdigit(c));
		else
			test_fail(c_isdigit(c));

		/* Test lower case. */
		if (c >= 'a' && c <= 'z')
			test_pass(c_islower(c));
		else
			test_fail(c_islower(c));

		/* Test upper case. */
		if (c >= 'A' && c <= 'Z')
			test_pass(c_isupper(c));
		else
			test_fail(c_isupper(c));

		/* Test alpha. */
		if (c_islower(c) || c_isupper(c))
			test_pass(c_isalpha(c));
		else
			test_fail(c_isalpha(c));

		/* Test alphanumeric. */
		if (c_isdigit(c) || c_isalpha(c))
			test_pass(c_isalnum(c));
		else
			test_fail(c_isalnum(c));
	}
}
Ejemplo n.º 2
0
/**
 * virConfParseName:
 * @ctxt: the parsing context
 *
 * Parse one name
 *
 * Returns a copy of the new string, NULL in case of error
 */
static char *
virConfParseName(virConfParserCtxtPtr ctxt)
{
    const char *base;
    char *ret;

    SKIP_BLANKS;
    base = ctxt->cur;
    /* TODO: probably need encoding support and UTF-8 parsing ! */
    if (!c_isalpha(CUR) &&
            !((ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) && (CUR == '.'))) {
        virConfError(ctxt, VIR_ERR_CONF_SYNTAX, _("expecting a name"));
        return NULL;
    }
    while ((ctxt->cur < ctxt->end) &&
            (c_isalnum(CUR) || (CUR == '_') ||
             ((ctxt->conf->flags & VIR_CONF_FLAG_VMX_FORMAT) &&
              ((CUR == ':') || (CUR == '.') || (CUR == '-'))) ||
             ((ctxt->conf->flags & VIR_CONF_FLAG_LXC_FORMAT) &&
              (CUR == '.'))))
        NEXT;
    if (VIR_STRNDUP(ret, base, ctxt->cur - base) < 0)
        return NULL;
    return ret;
}
Ejemplo n.º 3
0
/* Match a file suffix defined by this regular expression:
   /(\.[A-Za-z~][A-Za-z0-9~]*)*$/
   Scan the string *STR and return a pointer to the matching suffix, or
   NULL if not found.  Upon return, *STR points to terminating NUL.  */
static const char *
match_suffix (const char **str)
{
  const char *match = NULL;
  bool read_alpha = false;
  while (**str)
    {
      if (read_alpha)
        {
          read_alpha = false;
          if (!c_isalpha (**str) && '~' != **str)
            match = NULL;
        }
      else if ('.' == **str)
        {
          read_alpha = true;
          if (!match)
            match = *str;
        }
      else if (!c_isalnum (**str) && '~' != **str)
        match = NULL;
      (*str)++;
    }
  return match;
}
Ejemplo n.º 4
0
static int udevGenerateDeviceName(struct udev_device *device,
                                  virNodeDeviceDefPtr def,
                                  const char *s)
{
    int ret = 0, i = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    virBufferAsprintf(&buf, "%s_%s",
                      udev_device_get_subsystem(device),
                      udev_device_get_sysname(device));

    if (s != NULL) {
        virBufferAsprintf(&buf, "_%s", s);
    }

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        VIR_ERROR(_("Buffer error when generating device name for device "
                    "with sysname '%s'"), udev_device_get_sysname(device));
        ret = -1;
    }

    def->name = virBufferContentAndReset(&buf);

    for (i = 0; i < strlen(def->name) ; i++) {
        if (!(c_isalnum(*(def->name + i)))) {
            *(def->name + i) = '_';
        }
    }

    return ret;
}
Ejemplo n.º 5
0
const char *
pkg_name_is_illegal(const char *p)
{
  /* FIXME: _ is deprecated, remove sometime. */
  static const char alsoallowed[] = "-+._";
  static char buf[150];
  int c;

  if (!*p) return _("may not be empty string");
  if (!c_isalnum(*p))
    return _("must start with an alphanumeric character");
  while ((c = *p++) != '\0')
    if (!c_isalnum(c) && !strchr(alsoallowed, c))
      break;
  if (!c) return NULL;

  snprintf(buf, sizeof(buf), _(
	   "character '%c' not allowed (only letters, digits and characters '%s')"),
	   c, alsoallowed);
  return buf;
}
Ejemplo n.º 6
0
/**
 * virBufferURIEncodeString:
 * @buf: the buffer to append to
 * @str: the string argument which will be URI-encoded
 *
 * Append the string to the buffer.  The string will be URI-encoded
 * during the append (ie any non alpha-numeric characters are replaced
 * with '%xx' hex sequences).  Auto indentation may be applied.
 */
void
virBufferURIEncodeString(virBufferPtr buf, const char *str)
{
    int grow_size = 0;
    const char *p;
    unsigned char uc;
    const char *hex = "0123456789abcdef";

    if ((buf == NULL) || (str == NULL))
        return;

    if (buf->error)
        return;

    virBufferAddLit(buf, ""); /* auto-indent */

    for (p = str; *p; ++p) {
        if (c_isalnum(*p))
            grow_size++;
        else
            grow_size += 3; /* %ab */
    }

    if (virBufferGrow(buf, grow_size) < 0)
        return;

    for (p = str; *p; ++p) {
        if (c_isalnum(*p)) {
            buf->content[buf->use++] = *p;
        } else {
            uc = (unsigned char) *p;
            buf->content[buf->use++] = '%';
            buf->content[buf->use++] = hex[uc >> 4];
            buf->content[buf->use++] = hex[uc & 0xf];
        }
    }

    buf->content[buf->use] = '\0';
}
Ejemplo n.º 7
0
/* Specialized PV UUID comparison function.
 * pvuuid1: from vgpvuuids, this may contain '-' characters which
 *   should be ignored.
 * pvuuid2: from pvs-full, this is 32 characters long and NOT
 *   terminated by \0
 */
static int
compare_pvuuids (const char *pvuuid1, const char *pvuuid2)
{
  size_t i;
  const char *p = pvuuid1;

  for (i = 0; i < 32; ++i) {
    while (*p && !c_isalnum (*p))
      p++;
    if (!*p)
      return 0;
    if (*p != pvuuid2[i])
      return 0;
  }

  return 1;
}
Ejemplo n.º 8
0
/**
 * Check that the $TERM environment variable is reasonable before
 * we pass it through to the appliance.
 */
static bool
valid_term (const char *term)
{
  size_t len = strlen (term);

  if (len == 0 || len > 16)
    return false;

  while (len > 0) {
    char c = *term++;
    len--;
    if (!c_isalnum (c) && c != '-' && c != '_')
      return false;
  }

  return true;
}
Ejemplo n.º 9
0
static int
valid_config_filename(const struct dirent *dent)
{
  const char *c;

  if (dent->d_name[0] == '.')
    return 0;

  for (c = dent->d_name; *c; c++)
    if (!c_isalnum(*c) && *c != '_' && *c != '-')
      return 0;

  if (*c == '\0')
    return 1;
  else
    return 0;
}
Ejemplo n.º 10
0
int
guestfs_impl_set_identifier (guestfs_h *g, const char *identifier)
{
  size_t i, len;

  /* Check the identifier contains only permitted characters. */
  len = strlen (identifier);
  for (i = 0; i < len; ++i) {
    char c = identifier[i];

    if (!c_isalnum (c) && c != '_' && c != '-') {
      error (g, _("identifier must contain only alphanumeric characters, underscore or minus sign"));
      return -1;
    }
  }

  free (g->identifier);
  g->identifier = safe_strdup (g, identifier);

  return 0;
}
Ejemplo n.º 11
0
int
do_filesystem_available (const char *filesystem)
{
  size_t i, len = strlen (filesystem);
  int r;

  for (i = 0; i < len; ++i) {
    if (!c_isalnum (filesystem[i]) && filesystem[i] != '_') {
      reply_with_error ("filesystem name contains non-alphanumeric characters");
      return -1;
    }
  }

  r = filesystem_available (filesystem);
  if (r == -1) {
    reply_with_error ("error testing for filesystem availability; "
                      "enable verbose mode and look at preceding output");
    return -1;
  }

  return r;
}
Ejemplo n.º 12
0
/* Make a LUKS map name from the partition name,
 * eg "/dev/vda2" => "luksvda2"
 */
static void
make_mapname (const char *device, char *mapname, size_t len)
{
  size_t i = 0;

  if (len < 5)
    abort ();
  strcpy (mapname, "luks");
  mapname += 4;
  len -= 4;

  if (STRPREFIX (device, "/dev/"))
    i = 5;

  for (; device[i] != '\0' && len >= 1; ++i) {
    if (c_isalnum (device[i])) {
      *mapname++ = device[i];
      len--;
    }
  }

  *mapname = '\0';
}
Ejemplo n.º 13
0
struct robot_specs *
res_parse (const char *source, int length)
{
  int line_count = 1;

  const char *p   = source;
  const char *end = source + length;

  /* true if last applicable user-agent field matches Wget. */
  bool user_agent_applies = false;

  /* true if last applicable user-agent field *exactly* matches
     Wget.  */
  bool user_agent_exact = false;

  /* whether we ever encountered exact user agent. */
  bool found_exact = false;

  /* count of allow/disallow lines in the current "record", i.e. after
     the last `user-agent' instructions.  */
  int record_count = 0;

  struct robot_specs *specs = xnew0 (struct robot_specs);

  while (1)
    {
      const char *lineend, *lineend_real;
      const char *field_b, *field_e;
      const char *value_b, *value_e;

      if (p == end)
        break;
      lineend_real = memchr (p, '\n', end - p);
      if (lineend_real)
        ++lineend_real;
      else
        lineend_real = end;
      lineend = lineend_real;

      /* Before doing anything else, check whether the line is empty
         or comment-only. */
      SKIP_SPACE (p);
      if (EOL (p) || *p == '#')
        goto next;

      /* Make sure the end-of-line comments are respected by setting
         lineend to a location preceding the first comment.  Real line
         ending remains in lineend_real.  */
      for (lineend = p; lineend < lineend_real; lineend++)
        if ((lineend == p || c_isspace (*(lineend - 1)))
            && *lineend == '#')
          break;

      /* Ignore trailing whitespace in the same way. */
      while (lineend > p && c_isspace (*(lineend - 1)))
        --lineend;

      assert (!EOL (p));

      field_b = p;
      while (!EOL (p) && (c_isalnum (*p) || *p == '-'))
        ++p;
      field_e = p;

      SKIP_SPACE (p);
      if (field_b == field_e || EOL (p) || *p != ':')
        {
          DEBUGP (("Ignoring malformed line %d\n", line_count));
          goto next;
        }
      ++p;                      /* skip ':' */
      SKIP_SPACE (p);

      value_b = p;
      while (!EOL (p))
        ++p;
      value_e = p;

      /* Finally, we have a syntactically valid line. */
      if (FIELD_IS ("user-agent"))
        {
          /* We have to support several cases:

             --previous records--

             User-Agent: foo
             User-Agent: Wget
             User-Agent: bar
             ... matching record ...

             User-Agent: baz
             User-Agent: qux
             ... non-matching record ...

             User-Agent: *
             ... matching record, but will be pruned later ...

             We have to respect `User-Agent' at the beginning of each
             new record simply because we don't know if we're going to
             encounter "Wget" among the agents or not.  Hence,
             match_user_agent is called when record_count != 0.

             But if record_count is 0, we have to keep calling it
             until it matches, and if that happens, we must not call
             it any more, until the next record.  Hence the other part
             of the condition.  */
          if (record_count != 0 || user_agent_applies == false)
            match_user_agent (value_b, value_e - value_b,
                              &user_agent_applies, &user_agent_exact);
          if (user_agent_exact)
            found_exact = true;
          record_count = 0;
        }
      else if (FIELD_IS ("allow"))
        {
          if (user_agent_applies)
            {
              add_path (specs, value_b, value_e, true, user_agent_exact);
            }
          ++record_count;
        }
      else if (FIELD_IS ("disallow"))
        {
          if (user_agent_applies)
            {
              bool allowed = false;
              if (value_b == value_e)
                /* Empty "disallow" line means everything is *allowed*!  */
                allowed = true;
              add_path (specs, value_b, value_e, allowed, user_agent_exact);
            }
          ++record_count;
        }
      else
        {
          DEBUGP (("Ignoring unknown field at line %d\n", line_count));
          goto next;
        }

    next:
      p = lineend_real;
      ++line_count;
    }

  if (found_exact)
    {
      /* We've encountered an exactly matching user-agent.  Throw out
         all the stuff with user-agent: *.  */
      prune_non_exact (specs);
    }
  else if (specs->size > specs->count)
    {
      /* add_path normally over-allocates specs->paths.  Reallocate it
         to the correct size in order to conserve some memory.  */
      specs->paths = xrealloc (specs->paths,
                               specs->count * sizeof (struct path_info));
      specs->size = specs->count;
    }

  return specs;
}
Ejemplo n.º 14
0
Archivo: strtod.c Proyecto: 4solo/cs35
/* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
   character after the last one used in the number is put in *ENDPTR.  */
double
strtod (const char *nptr, char **endptr)
{
  const unsigned char *s;
  bool negative = false;

  /* The number so far.  */
  double num;

  bool got_dot;			/* Found a decimal point.  */
  bool got_digit;		/* Seen any digits.  */
  bool hex = false;		/* Look for hex float exponent.  */

  /* The exponent of the number.  */
  long int exponent;

  if (nptr == NULL)
    {
      errno = EINVAL;
      goto noconv;
    }

  /* Use unsigned char for the ctype routines.  */
  s = (unsigned char *) nptr;

  /* Eat whitespace.  */
  while (isspace (*s))
    ++s;

  /* Get the sign.  */
  negative = *s == '-';
  if (*s == '-' || *s == '+')
    ++s;

  num = 0.0;
  got_dot = false;
  got_digit = false;
  exponent = 0;

  /* Check for hex float.  */
  if (*s == '0' && c_tolower (s[1]) == 'x'
      && (c_isxdigit (s[2]) || ('.' == s[2] && c_isxdigit (s[3]))))
    {
      hex = true;
      s += 2;
      for (;; ++s)
	{
	  if (c_isxdigit (*s))
	    {
	      got_digit = true;

	      /* Make sure that multiplication by 16 will not overflow.  */
	      if (num > DBL_MAX / 16)
		/* The value of the digit doesn't matter, since we have already
		   gotten as many digits as can be represented in a `double'.
		   This doesn't necessarily mean the result will overflow.
		   The exponent may reduce it to within range.

		   We just need to record that there was another
		   digit so that we can multiply by 16 later.  */
		++exponent;
	      else
		num = ((num * 16.0)
		       + (c_tolower (*s) - (c_isdigit (*s) ? '0' : 'a' - 10)));

	      /* Keep track of the number of digits after the decimal point.
		 If we just divided by 16 here, we would lose precision.  */
	      if (got_dot)
		--exponent;
	    }
	  else if (!got_dot && *s == '.')
	    /* Record that we have found the decimal point.  */
	    got_dot = true;
	  else
	    /* Any other character terminates the number.  */
	    break;
	}
    }

  /* Not a hex float.  */
  else
    {
      for (;; ++s)
	{
	  if (c_isdigit (*s))
	    {
	      got_digit = true;

	      /* Make sure that multiplication by 10 will not overflow.  */
	      if (num > DBL_MAX * 0.1)
		/* The value of the digit doesn't matter, since we have already
		   gotten as many digits as can be represented in a `double'.
		   This doesn't necessarily mean the result will overflow.
		   The exponent may reduce it to within range.

		   We just need to record that there was another
		   digit so that we can multiply by 10 later.  */
		++exponent;
	      else
		num = (num * 10.0) + (*s - '0');

	      /* Keep track of the number of digits after the decimal point.
		 If we just divided by 10 here, we would lose precision.  */
	      if (got_dot)
		--exponent;
	    }
	  else if (!got_dot && *s == '.')
	    /* Record that we have found the decimal point.  */
	    got_dot = true;
	  else
	    /* Any other character terminates the number.  */
	    break;
	}
    }

  if (!got_digit)
    {
      /* Check for infinities and NaNs.  */
      if (c_tolower (*s) == 'i'
	  && c_tolower (s[1]) == 'n'
	  && c_tolower (s[2]) == 'f')
	{
	  s += 3;
	  num = HUGE_VAL;
	  if (c_tolower (*s) == 'i'
	      && c_tolower (s[1]) == 'n'
	      && c_tolower (s[2]) == 'i'
	      && c_tolower (s[3]) == 't'
	      && c_tolower (s[4]) == 'y')
	    s += 5;
	  goto valid;
	}
#ifdef NAN
      else if (c_tolower (*s) == 'n'
	       && c_tolower (s[1]) == 'a'
	       && c_tolower (s[2]) == 'n')
	{
	  s += 3;
	  num = NAN;
	  /* Since nan(<n-char-sequence>) is implementation-defined,
	     we define it by ignoring <n-char-sequence>.  A nicer
	     implementation would populate the bits of the NaN
	     according to interpreting n-char-sequence as a
	     hexadecimal number, but the result is still a NaN.  */
	  if (*s == '(')
	    {
	      const unsigned char *p = s + 1;
	      while (c_isalnum (*p))
		p++;
	      if (*p == ')')
		s = p + 1;
	    }
	  goto valid;
	}
#endif
      goto noconv;
    }

  if (c_tolower (*s) == (hex ? 'p' : 'e') && !isspace (s[1]))
    {
      /* Get the exponent specified after the `e' or `E'.  */
      int save = errno;
      char *end;
      long int value;

      errno = 0;
      ++s;
      value = strtol ((char *) s, &end, 10);
      if (errno == ERANGE && num)
	{
	  /* The exponent overflowed a `long int'.  It is probably a safe
	     assumption that an exponent that cannot be represented by
	     a `long int' exceeds the limits of a `double'.  */
	  if (endptr != NULL)
	    *endptr = end;
	  if (value < 0)
	    goto underflow;
	  else
	    goto overflow;
	}
      else if (end == (char *) s)
	/* There was no exponent.  Reset END to point to
	   the 'e' or 'E', so *ENDPTR will be set there.  */
	end = (char *) s - 1;
      errno = save;
      s = (unsigned char *) end;
      exponent += value;
    }

  if (num == 0.0)
    goto valid;

  if (hex)
    {
      /* ldexp takes care of range errors.  */
      num = ldexp (num, exponent);
      goto valid;
    }

  /* Multiply NUM by 10 to the EXPONENT power,
     checking for overflow and underflow.  */

  if (exponent < 0)
    {
      if (num < DBL_MIN * pow (10.0, (double) -exponent))
	goto underflow;
    }
  else if (exponent > 0)
    {
      if (num > DBL_MAX * pow (10.0, (double) -exponent))
	goto overflow;
    }

  num *= pow (10.0, (double) exponent);

 valid:
  if (endptr != NULL)
    *endptr = (char *) s;
  return negative ? -num : num;

 overflow:
  /* Return an overflow error.  */
  if (endptr != NULL)
    *endptr = (char *) s;
  errno = ERANGE;
  return negative ? -HUGE_VAL : HUGE_VAL;

 underflow:
  /* Return an underflow error.  */
  if (endptr != NULL)
    *endptr = (char *) s;
  errno = ERANGE;
  return negative ? -0.0 : 0.0;

 noconv:
  /* There was no number.  */
  if (endptr != NULL)
    *endptr = (char *) nptr;
  errno = EINVAL;
  return 0.0;
}
Ejemplo n.º 15
0
static int decode_tpmkey_url(const char *url, struct tpmkey_url_st *s)
{
	char *p;
	size_t size;
	int ret;
	unsigned int i, j;

	if (strstr(url, "tpmkey:") == NULL)
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);

	memset(s, 0, sizeof(*s));

	p = strstr(url, "file=");
	if (p != NULL) {
		p += sizeof("file=") - 1;
		size = strlen(p);
		s->filename = gnutls_malloc(size + 1);
		if (s->filename == NULL)
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

		ret = unescape_string(s->filename, p, &size, ';');
		if (ret < 0) {
			gnutls_assert();
			goto cleanup;
		}
		s->filename[size] = 0;
	} else if ((p = strstr(url, "uuid=")) != NULL) {
		char tmp_uuid[33];
		uint8_t raw_uuid[16];

		p += sizeof("uuid=") - 1;
		size = strlen(p);

		for (j = i = 0; i < size; i++) {
			if (j == sizeof(tmp_uuid) - 1) {
				break;
			}
			if (c_isalnum(p[i]))
				tmp_uuid[j++] = p[i];
		}
		tmp_uuid[j] = 0;

		size = sizeof(raw_uuid);
		ret =
		    _gnutls_hex2bin(tmp_uuid, strlen(tmp_uuid), raw_uuid,
				    &size);
		if (ret < 0) {
			gnutls_assert();
			goto cleanup;
		}

		memcpy(&s->uuid.ulTimeLow, raw_uuid, 4);
		memcpy(&s->uuid.usTimeMid, &raw_uuid[4], 2);
		memcpy(&s->uuid.usTimeHigh, &raw_uuid[6], 2);
		s->uuid.bClockSeqHigh = raw_uuid[8];
		s->uuid.bClockSeqLow = raw_uuid[9];
		memcpy(&s->uuid.rgbNode, &raw_uuid[10], 6);
		s->uuid_set = 1;
	} else {
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
	}

	if ((p = strstr(url, "storage=user")) != NULL)
		s->storage = TSS_PS_TYPE_USER;
	else
		s->storage = TSS_PS_TYPE_SYSTEM;

	return 0;

      cleanup:
	clear_tpmkey_url(s);
	return ret;
}
Ejemplo n.º 16
0
static void *
format_parse (const char *format, bool translated, char **invalid_reason)
{
  struct spec spec;
  struct spec *result;

  spec.directives = 0;
  spec.named_arg_count = 0;
  spec.allocated = 0;
  spec.named = NULL;

  for (; *format != '\0';)
    if (*format++ == '$')
      {
	/* A variable substitution.  */
	char *name;

	spec.directives++;

	if (*format == '{')
	  {
	    const char *name_start;
	    const char *name_end;
	    size_t n;

	    name_start = ++format;
	    for (; *format != '\0'; format++)
	      {
		if (*format == '}')
		  break;
		if (!c_isascii (*format))
		  {
		    *invalid_reason = INVALID_NON_ASCII_VARIABLE ();
		    goto bad_format;
		  }
		if (format > name_start
		    && (*format == '-' || *format == '=' || *format == '+'
			|| *format == '?' || *format == ':'))
		  {
		    *invalid_reason = INVALID_SHELL_SYNTAX ();
		    goto bad_format;
		  }
		if (!(c_isalnum (*format) || *format == '_')
		    || (format == name_start && c_isdigit (*format)))
		  {
		    *invalid_reason = INVALID_CONTEXT_DEPENDENT_VARIABLE ();
		    goto bad_format;
		  }
	      }
	    if (*format == '\0')
	      {
		*invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
		goto bad_format;
	      }
	    name_end = format++;

	    n = name_end - name_start;
	    if (n == 0)
	      {
		*invalid_reason = INVALID_EMPTY_VARIABLE ();
		goto bad_format;
	      }
	    name = (char *) xmalloc (n + 1);
	    memcpy (name, name_start, n);
	    name[n] = '\0';
	  }
	else if (c_isalpha (*format) || *format == '_')
	  {
	    const char *name_start;
	    const char *name_end;
	    size_t n;

	    name_start = format;
	    do
	      format++;
	    while (*format != '\0' && (c_isalnum (*format) || *format == '_'));
	    name_end = format;

	    n = name_end - name_start;
	    name = (char *) xmalloc (n + 1);
	    memcpy (name, name_start, n);
	    name[n] = '\0';
	  }
	else if (*format != '\0')
	  {
	    if (!c_isascii (*format))
	      {
		*invalid_reason = INVALID_NON_ASCII_VARIABLE ();
		goto bad_format;
	      }
	    else
	      {
		*invalid_reason = INVALID_CONTEXT_DEPENDENT_VARIABLE ();
		goto bad_format;
	      }
	  }
	else
	  {
	    *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
	    goto bad_format;
	  }

	/* Named argument.  */
	if (spec.allocated == spec.named_arg_count)
	  {
	    spec.allocated = 2 * spec.allocated + 1;
	    spec.named = (struct named_arg *) xrealloc (spec.named, spec.allocated * sizeof (struct named_arg));
	  }
	spec.named[spec.named_arg_count].name = name;
	spec.named_arg_count++;
      }

  /* Sort the named argument array, and eliminate duplicates.  */
  if (spec.named_arg_count > 1)
    {
      unsigned int i, j;

      qsort (spec.named, spec.named_arg_count, sizeof (struct named_arg),
	     named_arg_compare);

      /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i.  */
      for (i = j = 0; i < spec.named_arg_count; i++)
	if (j > 0 && strcmp (spec.named[i].name, spec.named[j-1].name) == 0)
	  free (spec.named[i].name);
	else
	  {
	    if (j < i)
	      spec.named[j].name = spec.named[i].name;
	    j++;
	  }
      spec.named_arg_count = j;
    }

  result = (struct spec *) xmalloc (sizeof (struct spec));
  *result = spec;
  return result;

 bad_format:
  if (spec.named != NULL)
    {
      unsigned int i;
      for (i = 0; i < spec.named_arg_count; i++)
	free (spec.named[i].name);
      free (spec.named);
    }
  return NULL;
}
Ejemplo n.º 17
0
/* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
   character after the last one used in the number is put in *ENDPTR.  */
double
strtod (const char *nptr, char **endptr)
{
  bool negative = false;

  /* The number so far.  */
  double num;

  const char *s = nptr;
  const char *end;
  char *endbuf;
  int saved_errno;

  /* Eat whitespace.  */
  while (locale_isspace (*s))
    ++s;

  /* Get the sign.  */
  negative = *s == '-';
  if (*s == '-' || *s == '+')
    ++s;

  saved_errno = errno;
  num = underlying_strtod (s, &endbuf);
  end = endbuf;

  if (c_isdigit (s[*s == '.']))
    {
      /* If a hex float was converted incorrectly, do it ourselves.
         If the string starts with "0x" but does not contain digits,
         consume the "0" ourselves.  If a hex float is followed by a
         'p' but no exponent, then adjust the end pointer.  */
      if (*s == '0' && c_tolower (s[1]) == 'x')
        {
          if (! c_isxdigit (s[2 + (s[2] == '.')]))
            end = s + 1;
          else if (end <= s + 2)
            {
              num = parse_number (s + 2, 16, 2, 4, 'p', &endbuf);
              end = endbuf;
            }
          else
            {
              const char *p = s + 2;
              while (p < end && c_tolower (*p) != 'p')
                p++;
              if (p < end && ! c_isdigit (p[1 + (p[1] == '-' || p[1] == '+')]))
                end = p;
            }
        }
      else
        {
          /* If "1e 1" was misparsed as 10.0 instead of 1.0, re-do the
             underlying strtod on a copy of the original string
             truncated to avoid the bug.  */
          const char *e = s + 1;
          while (e < end && c_tolower (*e) != 'e')
            e++;
          if (e < end && ! c_isdigit (e[1 + (e[1] == '-' || e[1] == '+')]))
            {
              char *dup = strdup (s);
              errno = saved_errno;
              if (!dup)
                {
                  /* Not really our day, is it.  Rounding errors are
                     better than outright failure.  */
                  num = parse_number (s, 10, 10, 1, 'e', &endbuf);
                }
              else
                {
                  dup[e - s] = '\0';
                  num = underlying_strtod (dup, &endbuf);
                  saved_errno = errno;
                  free (dup);
                  errno = saved_errno;
                }
              end = e;
            }
        }

      s = end;
    }

  /* Check for infinities and NaNs.  */
  else if (c_tolower (*s) == 'i'
           && c_tolower (s[1]) == 'n'
           && c_tolower (s[2]) == 'f')
    {
      s += 3;
      if (c_tolower (*s) == 'i'
          && c_tolower (s[1]) == 'n'
          && c_tolower (s[2]) == 'i'
          && c_tolower (s[3]) == 't'
          && c_tolower (s[4]) == 'y')
        s += 5;
      num = HUGE_VAL;
      errno = saved_errno;
    }
  else if (c_tolower (*s) == 'n'
           && c_tolower (s[1]) == 'a'
           && c_tolower (s[2]) == 'n')
    {
      s += 3;
      if (*s == '(')
        {
          const char *p = s + 1;
          while (c_isalnum (*p))
            p++;
          if (*p == ')')
            s = p + 1;
        }

      /* If the underlying implementation misparsed the NaN, assume
         its result is incorrect, and return a NaN.  Normally it's
         better to use the underlying implementation's result, since a
         nice implementation populates the bits of the NaN according
         to interpreting n-char-sequence as a hexadecimal number.  */
      if (s != end)
        num = NAN;
      errno = saved_errno;
    }
  else
    {
      /* No conversion could be performed.  */
      errno = EINVAL;
      s = nptr;
    }

  if (endptr != NULL)
    *endptr = (char *) s;
  /* Special case -0.0, since at least ICC miscompiles negation.  We
     can't use copysign(), as that drags in -lm on some platforms.  */
  if (!num && negative)
    return minus_zero;
  return negative ? -num : num;
}
Ejemplo n.º 18
0
static void
test_all (void)
{
  int c;

  for (c = -0x80; c < 0x100; c++)
    {
      ASSERT (c_isascii (c) == (c >= 0 && c < 0x80));

      switch (c)
        {
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
          ASSERT (c_isalnum (c) == 1);
          break;
        default:
          ASSERT (c_isalnum (c) == 0);
          break;
        }

      switch (c)
        {
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
          ASSERT (c_isalpha (c) == 1);
          break;
        default:
          ASSERT (c_isalpha (c) == 0);
          break;
        }

      switch (c)
        {
        case '\t': case ' ':
          ASSERT (c_isblank (c) == 1);
          break;
        default:
          ASSERT (c_isblank (c) == 0);
          break;
        }

      ASSERT (c_iscntrl (c) == ((c >= 0 && c < 0x20) || c == 0x7f));

      switch (c)
        {
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
          ASSERT (c_isdigit (c) == 1);
          break;
        default:
          ASSERT (c_isdigit (c) == 0);
          break;
        }

      switch (c)
        {
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
        case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
        case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
        case 's': case 't': case 'u': case 'v': case 'w': case 'x':
        case 'y': case 'z':
          ASSERT (c_islower (c) == 1);
          break;
        default:
          ASSERT (c_islower (c) == 0);
          break;
        }

      ASSERT (c_isgraph (c) == ((c >= 0x20 && c < 0x7f) && c != ' '));

      ASSERT (c_isprint (c) == (c >= 0x20 && c < 0x7f));

      ASSERT (c_ispunct (c) == (c_isgraph (c) && !c_isalnum (c)));

      switch (c)
        {
        case ' ': case '\t': case '\n': case '\v': case '\f': case '\r':
          ASSERT (c_isspace (c) == 1);
          break;
        default:
          ASSERT (c_isspace (c) == 0);
          break;
        }

      switch (c)
        {
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
        case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
        case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
        case 'Y': case 'Z':
          ASSERT (c_isupper (c) == 1);
          break;
        default:
          ASSERT (c_isupper (c) == 0);
          break;
        }

      switch (c)
        {
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
          ASSERT (c_isxdigit (c) == 1);
          break;
        default:
          ASSERT (c_isxdigit (c) == 0);
          break;
        }

      switch (c)
        {
        case 'A':
          ASSERT (c_tolower (c) == 'a');
          ASSERT (c_toupper (c) == c);
          break;
        case 'B':
          ASSERT (c_tolower (c) == 'b');
          ASSERT (c_toupper (c) == c);
          break;
        case 'C':
          ASSERT (c_tolower (c) == 'c');
          ASSERT (c_toupper (c) == c);
          break;
        case 'D':
          ASSERT (c_tolower (c) == 'd');
          ASSERT (c_toupper (c) == c);
          break;
        case 'E':
          ASSERT (c_tolower (c) == 'e');
          ASSERT (c_toupper (c) == c);
          break;
        case 'F':
          ASSERT (c_tolower (c) == 'f');
          ASSERT (c_toupper (c) == c);
          break;
        case 'G':
          ASSERT (c_tolower (c) == 'g');
          ASSERT (c_toupper (c) == c);
          break;
        case 'H':
          ASSERT (c_tolower (c) == 'h');
          ASSERT (c_toupper (c) == c);
          break;
        case 'I':
          ASSERT (c_tolower (c) == 'i');
          ASSERT (c_toupper (c) == c);
          break;
        case 'J':
          ASSERT (c_tolower (c) == 'j');
          ASSERT (c_toupper (c) == c);
          break;
        case 'K':
          ASSERT (c_tolower (c) == 'k');
          ASSERT (c_toupper (c) == c);
          break;
        case 'L':
          ASSERT (c_tolower (c) == 'l');
          ASSERT (c_toupper (c) == c);
          break;
        case 'M':
          ASSERT (c_tolower (c) == 'm');
          ASSERT (c_toupper (c) == c);
          break;
        case 'N':
          ASSERT (c_tolower (c) == 'n');
          ASSERT (c_toupper (c) == c);
          break;
        case 'O':
          ASSERT (c_tolower (c) == 'o');
          ASSERT (c_toupper (c) == c);
          break;
        case 'P':
          ASSERT (c_tolower (c) == 'p');
          ASSERT (c_toupper (c) == c);
          break;
        case 'Q':
          ASSERT (c_tolower (c) == 'q');
          ASSERT (c_toupper (c) == c);
          break;
        case 'R':
          ASSERT (c_tolower (c) == 'r');
          ASSERT (c_toupper (c) == c);
          break;
        case 'S':
          ASSERT (c_tolower (c) == 's');
          ASSERT (c_toupper (c) == c);
          break;
        case 'T':
          ASSERT (c_tolower (c) == 't');
          ASSERT (c_toupper (c) == c);
          break;
        case 'U':
          ASSERT (c_tolower (c) == 'u');
          ASSERT (c_toupper (c) == c);
          break;
        case 'V':
          ASSERT (c_tolower (c) == 'v');
          ASSERT (c_toupper (c) == c);
          break;
        case 'W':
          ASSERT (c_tolower (c) == 'w');
          ASSERT (c_toupper (c) == c);
          break;
        case 'X':
          ASSERT (c_tolower (c) == 'x');
          ASSERT (c_toupper (c) == c);
          break;
        case 'Y':
          ASSERT (c_tolower (c) == 'y');
          ASSERT (c_toupper (c) == c);
          break;
        case 'Z':
          ASSERT (c_tolower (c) == 'z');
          ASSERT (c_toupper (c) == c);
          break;
        case 'a':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'A');
          break;
        case 'b':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'B');
          break;
        case 'c':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'C');
          break;
        case 'd':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'D');
          break;
        case 'e':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'E');
          break;
        case 'f':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'F');
          break;
        case 'g':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'G');
          break;
        case 'h':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'H');
          break;
        case 'i':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'I');
          break;
        case 'j':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'J');
          break;
        case 'k':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'K');
          break;
        case 'l':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'L');
          break;
        case 'm':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'M');
          break;
        case 'n':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'N');
          break;
        case 'o':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'O');
          break;
        case 'p':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'P');
          break;
        case 'q':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'Q');
          break;
        case 'r':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'R');
          break;
        case 's':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'S');
          break;
        case 't':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'T');
          break;
        case 'u':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'U');
          break;
        case 'v':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'V');
          break;
        case 'w':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'W');
          break;
        case 'x':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'X');
          break;
        case 'y':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'Y');
          break;
        case 'z':
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == 'Z');
          break;
        default:
          ASSERT (c_tolower (c) == c);
          ASSERT (c_toupper (c) == c);
          break;
        }
    }
}
Ejemplo n.º 19
0
static void
dpkg_options_load_file(const char *fn, const struct cmdinfo *cmdinfos)
{
  FILE *file;
  int line_num = 0;
  char linebuf[MAX_CONFIG_LINE];

  file= fopen(fn, "r");
  if (!file) {
    if (errno==ENOENT)
      return;
    warning(_("failed to open configuration file '%.255s' for reading: %s"),
            fn, strerror(errno));
    return;
  }

  while (fgets(linebuf, sizeof(linebuf), file)) {
    char *opt;
    const struct cmdinfo *cip;
    int l;

    line_num++;

    l = strlen(linebuf);
    while (l && c_isspace(linebuf[l - 1]))
      l--;
    linebuf[l] = '\0';

    if ((linebuf[0] == '#') || (linebuf[0] == '\0'))
      continue;
    for (opt = linebuf; c_isalnum(*opt) || *opt == '-'; opt++) ;
    if (*opt == '\0')
      opt=NULL;
    else {
      *opt++ = '\0';
      if (*opt=='=') opt++;
      while (c_isspace(*opt))
        opt++;

      opt = str_strip_quotes(opt);
      if (opt == NULL)
        config_error(fn, line_num, _("unbalanced quotes in '%s'"), linebuf);
    }

    for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
      if (!cip->olong) continue;
      if (strcmp(cip->olong, linebuf) == 0)
        break;
      l=strlen(cip->olong);
      if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
          !opt && strncmp(linebuf, cip->olong, l) == 0) {
	opt=linebuf+l+1;
	break;
      }
    }

    if (!cip->olong)
      config_error(fn, line_num, _("unknown option '%s'"), linebuf);

    if (cip->takesvalue) {
      if (!opt)
        config_error(fn, line_num, _("'%s' needs a value"), linebuf);
      if (cip->call) cip->call(cip,opt);
      else
        *cip->sassignto = m_strdup(opt);
    } else {
      if (opt)
        config_error(fn, line_num, _("'%s' does not take a value"), linebuf);
      if (cip->call) cip->call(cip,NULL);
      else
        *cip->iassignto = cip->arg_int;
    }
  }
  if (ferror(file))
    ohshite(_("read error in configuration file '%.255s'"), fn);
  if (fclose(file))
    ohshite(_("error closing configuration file '%.255s'"), fn);
}