Ejemplo n.º 1
0
void xlocale2_check_functions(nl_item ni, locale_t l)
{
    /* ctype.h */
    (void)isalnum_l(0, l);
    (void)isdigit_l(0, l);
    (void)isxdigit_l(0, l);
    /* inttypes.h */
    (void)strtoimax_l("", (char**)1234, 10, l);
    /* langinfo.h */
    (void)nl_langinfo_l(ni, l);
    /* monetary.h */
    (void)strfmon_l((char*)1234, (size_t)0, l, "%n", 0.0);
    /* stdio.h */
    (void)printf_l(l, "%d", 0);
    /* stdlib.h */
    (void)strtol_l("", (char**)1234, 10, l);
    /* string.h */
    (void)strcoll_l("", "", l);
    /* time.h */
    (void)strftime_l((char*)1234, (size_t)0, "%s", (const struct tm *)1234, l);
    /* wchar.h */
    (void)wcstol_l(L"", (wchar_t**)1234, 10, l);
    /* wctype.h */
    (void)iswalnum_l((wint_t)0, l);
    (void)iswdigit_l((wint_t)0, l);
    (void)iswxdigit_l((wint_t)0, l);
}
Ejemplo n.º 2
0
size_t c_format_unix_time(char *fmt, time_t src, char* dst, int siz) {
    struct tm tim;
    init_locale();
    localtime_r(&src, &tim);
#if THREAD_SAFE
    return strftime_l(dst, siz, fmt, &tim, c_locale);
#else
    return strftime(dst, siz, fmt, &tim);
#endif
}
Ejemplo n.º 3
0
size_t
wcsftime_l(wchar_t *wcs, size_t maxsize,
    const wchar_t *format, const struct tm *timeptr, locale_t loc)
{
	char *dst, *dstp, *sformat;
	size_t n, sflen;
	int sverrno;

	sformat = dst = NULL;

	/*
	 * Convert the supplied format string to a multibyte representation
	 * for strftime(), which only handles single-byte characters.
	 */
	sflen = wcstombs_l(NULL, format, 0, loc);
	if (sflen == (size_t)-1)
		goto error;
	if ((sformat = malloc(sflen + 1)) == NULL)
		goto error;
	wcstombs_l(sformat, format, sflen + 1, loc);

	/*
	 * Allocate memory for longest multibyte sequence that will fit
	 * into the caller's buffer and call strftime() to fill it.
	 * Then, copy and convert the result back into wide characters in
	 * the caller's buffer.
	 */
	if (SIZE_T_MAX / MB_CUR_MAX_L(loc) <= maxsize) {
		/* maxsize is preposterously large - avoid int. overflow. */
		errno = EINVAL;
		goto error;
	}
	dst = malloc(maxsize * MB_CUR_MAX_L(loc));
	if (dst == NULL)
		goto error;
	if (strftime_l(dst, maxsize, sformat, timeptr, loc) == 0)
		goto error;
	dstp = dst;
	n = mbstowcs_l(wcs, dstp, maxsize, loc);
	if (n == (size_t)-2 || n == (size_t)-1)
		goto error;

	free(sformat);
	free(dst);
	return n;

error:
	sverrno = errno;
	free(sformat);
	free(dst);
	errno = sverrno;
	return 0;
}
Ejemplo n.º 4
0
size_t c_format_unix_time_gmt(char *fmt, time_t src, char* dst, int siz) {
    struct tm tim;
    char *local_tz;
    size_t dst_size;

    init_locale();
    gmtime_r(&src, &tim);

    local_tz = set_tz_utc();
#if THREAD_SAFE
    dst_size = strftime_l(dst, siz, fmt, &tim, c_locale);
#else
    dst_size = strftime(dst, siz, fmt, &tim);
#endif
    set_tz(local_tz);
    return dst_size;
}
Ejemplo n.º 5
0
static char *
__mrss_atom_prepare_date (mrss_t * data, char *datestr)
{
  struct tm stm;

  if (!datestr)
    return NULL;

  memset (&stm, 0, sizeof (stm));

  /* format: 2007-01-17T07:45:50Z */
  if (sscanf
      (datestr, "%04d-%02d-%02dT%02d:%02d:%02dZ", &stm.tm_year,
       &stm.tm_mon, &stm.tm_mday, &stm.tm_hour, &stm.tm_min,
       &stm.tm_sec) == 6)
    {
      char datebuf[256];
      stm.tm_year -= 1900;
      stm.tm_mon -= 1;

#ifdef USE_LOCALE
      if (!data->c_locale
	  && !(data->c_locale = newlocale (LC_ALL_MASK, "C", NULL)))
	return NULL;

      strftime_l (datebuf, sizeof (datebuf), "%a, %d %b %Y %H:%M:%S %z", &stm,
		  data->c_locale);
#else
      strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %H:%M:%S %z", &stm);
#endif

      return strdup (datebuf);
    }

  return NULL;
}
int
main (void)
{
  locale_t l;
  locale_t old;
  struct tm tm;
  char buf[1000];
  wchar_t wbuf[1000];
  int result = 0;
  size_t n;

  l = newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL);
  if (l == NULL)
    {
      puts ("newlocale failed");
      exit (1);
    }

  memset (&tm, '\0', sizeof (tm));

  tm.tm_year = 102;
  tm.tm_mon = 2;
  tm.tm_mday = 1;

  if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0)
    {
      puts ("initial strftime failed");
      exit (1);
    }
  if (strcmp (buf, " 1 MARCH 2002") != 0)
    {
      printf ("initial strftime: expected \"%s\", got \"%s\"\n",
	      " 1 MARCH 2002", buf);
      result = 1;
    }
  else
    printf ("got \"%s\"\n", buf);

  /* Now using the extended locale model.  */
  if (strftime_l (buf, sizeof (buf), "%e %^B %Y", &tm, l) == 0)
    {
      puts ("strftime_l failed");
      result = 1;
    }
  else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0)
    {
      printf ("strftime_l: expected \"%s\", got \"%s\"\n",
	      " 1 M\xc4RZ 2002", buf);
      result = 1;
    }
  else
    {
      setlocale (LC_ALL, "de_DE.ISO-8859-1");
      printf ("got \"%s\"\n", buf);
      setlocale (LC_ALL, "C");
    }

  /* And the wide character version.  */
  if (wcsftime_l (wbuf, sizeof (wbuf) / sizeof (wbuf[0]), L"%e %^B %Y", &tm, l)
      == 0)
    {
      puts ("wcsftime_l failed");
      result = 1;
    }
  else if (wcscmp (wbuf, L" 1 M\x00c4RZ 2002") != 0)
    {
      printf ("wcsftime_l: expected \"%ls\", got \"%ls\"\n",
	      L" 1 M\x00c4RZ 2002", wbuf);
      result = 1;
    }
  else
    {
      setlocale (LC_ALL, "de_DE.ISO-8859-1");
      printf ("got \"%ls\"\n", wbuf);
      setlocale (LC_ALL, "C");
    }

  old = uselocale (l);

  n = strftime (buf, sizeof (buf), "%e %^B %Y", &tm);

  /* Switch back.  */
  (void) uselocale (old);

  if (n == 0)
    {
      puts ("strftime after first uselocale failed");
      result = 1;
    }
  else if (strcmp (buf, " 1 M\xc4RZ 2002") != 0)
    {
      printf ("strftime in non-C locale: expected \"%s\", got \"%s\"\n",
	      " 1 M\xc4RZ 2002", buf);
      result = 1;
    }
  else
    {
      setlocale (LC_ALL, "de_DE.ISO-8859-1");
      printf ("got \"%s\"\n", buf);
      setlocale (LC_ALL, "C");
    }

  if (strftime (buf, sizeof (buf), "%e %^B %Y", &tm) == 0)
    {
      puts ("strftime after second uselocale failed");
      result = 1;
    }
  else if (strcmp (buf, " 1 MARCH 2002") != 0)
    {
      printf ("initial strftime: expected \"%s\", got \"%s\"\n",
	      " 1 MARCH 2002", buf);
      result = 1;
    }
  else
    printf ("got \"%s\"\n", buf);

  return result;
}
Ejemplo n.º 7
0
Archivo: v1.c Proyecto: fahlgren/kore
int
v1(struct http_request *http_req)
{
	struct jsonrpc_request	req;
	int			ret;
	
	/* We only allow POST/PUT methods. */
	if (http_req->method != HTTP_METHOD_POST &&
	    http_req->method != HTTP_METHOD_PUT) {
		http_response_header(http_req, "allow", "POST, PUT");
		http_response(http_req, HTTP_STATUS_METHOD_NOT_ALLOWED, NULL, 0);
		return (KORE_RESULT_OK);
	}
	
	/* Read JSON-RPC request. */
	if ((ret = jsonrpc_read_request(http_req, &req)) != 0)
		return jsonrpc_error(&req, ret, NULL);
	
	/* Echo command takes and gives back params. */
	if (strcmp(req.method, "echo") == 0) {
		if (!YAJL_IS_ARRAY(req.params)) {
			jsonrpc_log(&req, LOG_ERR,
			    "Echo only accepts positional params");
			return jsonrpc_error(&req, JSONRPC_INVALID_PARAMS, NULL);
		}
		for (size_t i = 0; i < req.params->u.array.len; i++) {
			yajl_val v = req.params->u.array.values[i];
			if (!YAJL_IS_STRING(v)) {
				jsonrpc_log(&req, -3,
				    "Echo only accepts strings");
				return jsonrpc_error(&req,
				    JSONRPC_INVALID_PARAMS, NULL);
			}
		}
		return jsonrpc_result(&req, write_string_array_params, NULL);
	}
	
	/* Date command displays date and time according to parameters. */
	if (strcmp(req.method, "date") == 0) {
		time_t		time_value;
		struct tm	time_info;
		char		timestamp[33];
		struct tm	*(*gettm)(const time_t *, struct tm *) =
				    localtime_r;
		
		if (YAJL_IS_OBJECT(req.params)) {
			const char	*path[] = {"local", NULL};
			yajl_val	bf;

			bf = yajl_tree_get(req.params, path, yajl_t_false);
			if (bf != NULL)
				gettm = gmtime_r;
		} else if (req.params != NULL) {
			jsonrpc_log(&req, LOG_ERR,
			    "Date only accepts named params");
			return jsonrpc_error(&req, JSONRPC_INVALID_PARAMS, NULL);
		}

		if ((time_value = time(NULL)) == -1)
			return jsonrpc_error(&req, -2,
			    "Failed to get date time");
		
		if (gettm(&time_value, &time_info) == NULL)
			return jsonrpc_error(&req, -3,
			    "Failed to get date time info");
		
		memset(timestamp, 0, sizeof(timestamp));
		if (strftime_l(timestamp, sizeof(timestamp) - 1, "%c",
		    &time_info, LC_GLOBAL_LOCALE) == 0)
			return jsonrpc_error(&req, -4,
			    "Failed to get printable date time");
		
		return jsonrpc_result(&req, write_string, timestamp);
	}
	
	return jsonrpc_error(&req, JSONRPC_METHOD_NOT_FOUND, NULL);
}