Ejemplo n.º 1
0
int main(void)
{
    printf(" %d\n", atoi("  +000309afcc 3d"));
    printf(" isdigit ret = %d\n", isdigit('a'));
    printf(" %d, %d\n", u_atoi("00123"), 3*u_atoi("00-124"));
    printf_libmacro();
    return 0;
}
Ejemplo n.º 2
0
/**
 * \brief   create a \c klog_args_t object with configuration parameters read
 *          from a log subsection of a kloned configuration file
 *
 * \param ls        a log configuration record
 * \param pka       the corresponding \c klog_args_t object as a value-result 
 *                  argument
 * \return
 * - \c 0  success
 * - \c ~0 on failure (\p pka MUST not be referenced)
 */
int klog_args (u_config_t *ls, klog_args_t **pka)
{
    const char *cs;
    klog_args_t *ka = NULL;

    dbg_return_if (ls == NULL, ~0);
    dbg_return_if (pka == NULL, ~0);

    /* here defaults are set */
    dbg_err_if (klog_args_new(&ka));

    /* read in config values */
    ka->type = klog_type(u_config_get_subkey_value(ls, "type"));

    if ((cs = u_config_get_subkey_value(ls, "ident")) != NULL)
        ka->ident = u_strdup(cs);

    ka->threshold = klog_threshold(u_config_get_subkey_value(ls, "threshold"));

    if ((cs = u_config_get_subkey_value(ls, "memory.limit")) != NULL)
    {
        dbg_err_ifm (u_atoi(cs, &ka->mlimit), 
                "'memory.limit' bad value: %s", cs);
    }

    if ((cs = u_config_get_subkey_value(ls, "file.basename")) != NULL) 
        ka->fbasename = u_strdup(cs);

    if ((cs = u_config_get_subkey_value(ls, "file.limit")) != NULL)
    {
        dbg_err_ifm (u_atoi(cs, &ka->flimit), 
                "'file.limit' bad value: %s", cs);
    }

    if ((cs = u_config_get_subkey_value(ls, "file.splits")) != NULL)
    {
        dbg_err_ifm (u_atoi(cs, &ka->fsplits),
                "'file.splits' bad value: %s", cs);
    }

    #ifdef HAVE_SYSLOG
    ka->sfacility = 
        klog_facility(ka, u_config_get_subkey_value(ls, "syslog.facility"));

    ka->soptions = klog_logopt(u_config_get_subkey_value(ls, "syslog.options"));
    #endif

    dbg_return_if (klog_args_check(ka), ~0);

    *pka = ka;
    
    return 0;
err:
    if (ka)
        klog_args_free(ka);
    return ~0;
}
Ejemplo n.º 3
0
int parse_addr(const char *ap, char *a, size_t a_sz, uint16_t *p)
{
    int tmp;
    char *ptr;
    size_t alen;

    dbg_return_if(ap == NULL, -1);
    dbg_return_if(a == NULL, -1);
    dbg_return_if(a_sz == 0, -1);
    dbg_return_if(p == NULL, -1);

    /* Extract port, if specified. */
    if ((ptr = strchr(ap, '+')) != NULL && ptr[1] != '\0')
    {
        dbg_err_ifm(u_atoi(++ptr, &tmp), "could not parse port %s", ptr);
        *p = (uint16_t) tmp;
    }
    else
    {
        ptr = (char *)(ap + strlen(ap) + 1);
        *p = EC_COAP_DEFAULT_PORT;
    }

    alen = (size_t)(ptr - ap - 1);

    dbg_err_ifm(alen >= a_sz,
            "not enough bytes (%zu vs %zu) to copy %s", alen, a_sz, ap);

    (void) strncpy(a, ap, alen);
    a[alen] = '\0';

    return 0;
err:
    return -1;
}
Ejemplo n.º 4
0
int
U_EXPORT main (int argc, char* argv[], char* env[])
{
   U_ULIB_INIT(argv);

   U_TRACE(5,"main(%d)",argc)

   u_init_ulib_hostname();
   u_init_ulib_username();

   ULog y(U_STRING_FROM_CONSTANT("$PWD/test_log.log"), 1024, "tmp");

   y.setPrefix(U_CONSTANT_TO_PARAM(U_SERVER_LOG_PREFIX));

   uint32_t i, n = (argc > 1 ? u_atoi(argv[1]) : 10);

   for (i = 0; i < n; ++i)
      {
      y.log(U_CONSTANT_TO_PARAM("message %6d - %H %U %w"), i+1);

      y.msync();
      }

   cout << "ok" << '\n';
}
Ejemplo n.º 5
0
/**
 * Create a range from XML parsed attributes
 * @param name the name of the range
 * @param atts a NULL-terminated array of attribute name/value pairs
 * @return a range object or NULL
 */
range *range_create_atts( const UChar **atts )
{
    range *r = calloc( 1, sizeof(range) );
    if ( r != NULL )
    {
        int i = 0;
        r->rightmost = 1;
        while ( atts[i] != NULL )
        {
            if ( u_strcmp(atts[i],U_RELOFF)== 0 )
                r->reloff = u_atoi((UChar*)atts[i+1]);
            else if ( u_strcmp(atts[i],U_NAME)==0 )
            {
                r->name = u_strdup((UChar*)atts[i+1]);
                if ( r->name == NULL )
                {
                    warning("range: failed to duplicate name %s\n",
                        atts[i+1]);
                    free( r );
                    r = NULL;
                    break;
                }
            }
            else if ( u_strcmp(atts[i],U_LEN)==0 )
                r->len = u_atoi( (UChar*)atts[i+1] );
            else if ( u_strcmp(atts[i],U_REMOVED)==0 )
            {
                warning( "range: attempt to create removed range\n");
                free( r );
                r = NULL;
            }
            else
                warning( "range: invalid attribute %s ignored\n",atts[i]);
            i += 2;
        }
    }
    else
        warning("range: failed to allocate range");
    return r;
}
Ejemplo n.º 6
0
int vhost_load_resource(u_config_t *resource, const char *origin)
{
    int tmp;
    size_t i, val_sz;
    uint32_t ma;
    const char *path, *max_age, *val, *meth;
    ec_res_t *res = NULL;
    ec_mt_t mt;
    char uri[512];
    ec_method_mask_t methods;
    u_config_t *repr, *attrs;

    dbg_return_if(resource == NULL, -1);

    /* Get resource path. */
    dbg_err_ifm((path = u_config_get_subkey_value(resource, "path")) == NULL,
            "missing mandatory \'path\' in resource");

    /* Get resource max age (default to 60 secs if not specified.) */
    if ((max_age = u_config_get_subkey_value(resource, "max-age")) == NULL)
        ma = 60;
    else
    {
        dbg_err_ifm(u_atoi(max_age, &tmp), "conversion error for %s", max_age);
        ma = (uint32_t) tmp;
    }

    /* Get allowed methods. */
    if ((meth = u_config_get_subkey_value(resource, "allowed-methods")) == NULL)
        methods = EC_GET_MASK; /* Default is read-only. */
    else
    {
        dbg_err_ifm(vhost_load_allowed_methods(meth, &methods),
                "bad allowed-methods in %s%s", origin, path);
    }

    /* Create complete resource name. */
    dbg_err_ifm(u_snprintf(uri, sizeof uri, "%s%s", origin, path),
            "could not create uri for path %s and origin %s", path, origin);

    CHAT("adding resource %s", uri);

    /* Create FS resource. */
    dbg_err_ifm((res = ec_resource_new(uri, methods, ma)) == NULL,
            "resource creation failed");

    /* Load each resource representation. */
    for (i = 0; (repr = u_config_get_child_n(resource,
            "representation", i)) != NULL; ++i)
    {
        /* Retrieve representation type and value. */
        dbg_err_ifm(ec_mt_from_string(u_config_get_subkey_value(repr, "t:"),
                &mt), "media type map error");

        dbg_err_ifm((val = u_config_get_subkey_value(repr, "v:")) == NULL,
                "no value for resource %s", uri);
        val_sz = strlen(val);

        dbg_err_ifm(ec_resource_add_rep(res, (const uint8_t *) val,
                val_sz, mt, NULL),
                "error adding representation for %s", uri);
    }
    dbg_err_ifm(i == 0, "no resources in virtual host");

    /* Add fixed link-format attributes. */
    if (u_config_get_subkey(resource, "link-attrs", &attrs) == 0)
    {
        dbg_err_ifm(vhost_load_resource_attrs(res, attrs),
                "error loading link-attrs for resource %s%s", origin, path);
    }

    /* Put resource into the file system. */
    dbg_err_ifm(ec_filesys_put_resource(g_ctx.fs, res),
            "adding resource failed");
    res = NULL; /* ownership lost */

    /* Register the callback that will serve this URI. */
    dbg_err_ifm(ec_register_cb(g_ctx.coap, uri, serve, NULL),
            "registering callback for %s failed", uri);

    return 0;
err:
    if (res)
        ec_resource_free(res);
    return -1;
}