LISysNotify*
lisys_notify_new ()
{
#ifdef HAVE_INOTIFY
	LISysNotify* self;

	/* Allocate self. */
	self = calloc (1, sizeof (LISysNotify));
	if (self == NULL)
	{
		error_memory ();
		return NULL;
	}

	/* Find symbols. */
	self->calls.inotify_init = lisys_module_global_symbol ("c", "inotify_init");
	self->calls.inotify_add_watch = lisys_module_global_symbol ("c", "inotify_add_watch");
	self->calls.inotify_rm_watch = lisys_module_global_symbol ("c", "inotify_rm_watch");
	if (self->calls.inotify_init == NULL ||
	    self->calls.inotify_add_watch == NULL ||
	    self->calls.inotify_rm_watch == NULL)
	{
		error_support ();
		free (self);
		return NULL;
	}

	/* Allocate buffer. */
	self->buffer.capacity = sizeof (struct inotify_event) + 1024;
	self->buffer.buffer = malloc (self->buffer.capacity);
	if (self->buffer.buffer == NULL)
	{
		error_memory ();
		free (self);
		return NULL;
	}

	/* Initialize monitor. */
	self->fd = self->calls.inotify_init ();
	if (self->fd == -1)
	{
		error_open ();
		free (self->buffer.buffer);
		free (self);
		return NULL;
	}

	return self;
#else
	error_support ();
	return NULL;
#endif
}
Beispiel #2
0
    /** Open the channel. */
    ach_status_t open(const char* channel_name,
                      ach_attr_t* attr = NULL,
                      int allow_mask=ACH_MASK_OK,
                      int warn_mask=ACH_MASK_NONE) {
        ach_status_t r = ach_open(&channel, channel_name, attr);

        switch(check_status(r, allow_mask, warn_mask)) {
        case STATUS_WARN: warn_open(r);  break;
        case STATUS_ERR:  error_open(r); break;
        case STATUS_OK: break;
        }

        return r;
    }
Beispiel #3
0
ConfigFile *
config_read(const char *path)
{
    FILE *in = NULL;
    ConfigFile *cfg = NULL;
    char *k = NULL, *v = NULL, *str = NULL;
    if (!(in = fopen(path, "r"))) {
        error_open(path);
        goto fail;
    }
    cfg = (ConfigFile *) calloc(1, sizeof(*cfg));
    cfg->used = 0;
    cfg->alc = 1;
    cfg->v = (ConfigEntry *) calloc(1, sizeof(cfg->v[0]));
    cfg->used = 0;
    int line = 0, len = 0;
    while ((str = getline2(in)) != NULL) {
        line++;
        char *p = NULL;
        if ((p = strchr(str, '#'))) {
            *p = '\0';
        }
        p = str;
        len = strlen(p);
        for (; isspace(p[len - 1]) && len > 0; len--) {}
        if (len <= 0) {
            free(str);
            continue;
        }
        p[len] = '\0';
        k = str;
        for (; isspace(*k); k++) {}
        if (*k == '\0' || *k == '\n') {
            free(str);
            continue;
        }
        v = k;
        if (!isalpha(*v) && *v != '_') {
            parse_error(path, line);
            goto fail;
        }
        for (; isalpha(*v) || isdigit(*v) || 
               *v == '_' || *v == '-'; v++){}
        if (!isspace(*v) && *v != '=') {
            parse_error(path, line);
            goto fail;
        }
        if (*v != '=') {
            *v = '\0';
            v++;
            for (; isspace(*v); v++){}
            if (*v != '=') {
                parse_error(path, line);
                goto fail;
            }
        } else {
            *v = '\0';
        }
        v++;
        for (; isspace(*v); v++){}
        char *cur = v;
        for (; *cur != '\n' && *cur != '\0'; cur++){}
        *cur = '\0';
        if (cfg->used == cfg->alc) {
            cfg->alc = (cfg->alc * 1.618) + 1;
            cfg->v = (ConfigEntry *) realloc(cfg->v, 
                cfg->alc * sizeof(cfg->v[0]));
        }
        cfg->v[cfg->used].name = strdup(k);
        cfg->v[cfg->used].value = strdup(v);
        free(str);
        str = NULL;
        cfg->used++;
    }
    fclose(in);
    in = NULL;
    qsort(cfg->v, cfg->used, sizeof(cfg->v[0]), sort_func);
    int i;
    for (i = 1; i < cfg->used; i++) {
        if (!strcmp(cfg->v[i].name, cfg->v[i - 1].name)) {
            fprintf(stderr, "Duplicate parameter %s in %s\n", 
                cfg->v[i].name, path);
            goto fail;
        }
    }
    
    return cfg;
fail:
    if (in) {
        fclose(in);
        in = NULL;
    }
    cfg = config_free(cfg);
    free(str);
    return NULL;
}