<commit_before>/*
 * Copyright © 2012-2015 <NAME> <<EMAIL>>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <config.h>
#include <ugpio.h>
#include <ugpio-internal.h>

ugpio_t *ugpio_request(unsigned int gpio, const char *label)
{
    ugpio_t *ctx;
    int is_requested, val;

    if ((ctx = malloc(sizeof(*ctx))) == NULL)
        return NULL;

    ctx->gpio = gpio;
    ctx->flags = GPIOF_CLOEXEC | GPIOF_DIRECTION_UNKNOWN;
    ctx->label = label;
    ctx->fd_value = -1;
    ctx->fd_active_low = -1;
    ctx->fd_direction = -1;
    ctx->fd_edge = -1;

    if ((is_requested = gpio_is_requested(ctx->gpio)) < 0)
        goto error_free;

    if (!is_requested) {
        if (gpio_request(ctx->gpio, ctx->label) < 0)
            goto error_free;

        ctx->flags |= GPIOF_REQUESTED;
    }

    if (gpio_alterable_direction(ctx->gpio)) {
        ctx->flags |= GPIOF_ALTERABLE_DIRECTION;

        if ((val = gpio_get_direction(ctx->gpio)) != -1) {
            ctx->flags &= ~GPIOF_DIRECTION_UNKNOWN;
            ctx->flags |= val;
        }
    }

    if (gpio_alterable_edge(ctx->gpio)) {
        ctx->flags |= GPIOF_ALTERABLE_EDGE;

        if ((val = gpio_get_edge(ctx->gpio)) != -1) {
            ctx->flags &= ~GPIOF_TRIGGER_MASK;
            ctx->flags |= val;
        }
    }

    return ctx;

error_free:
    free(ctx);
    return NULL;
}

ugpio_t *ugpio_request_one(unsigned int gpio, unsigned int flags, const char *label)
{
    ugpio_t *ctx;
    int is_requested;

    if ((ctx = malloc(sizeof(*ctx))) == NULL)
        return NULL;

    ctx->gpio = gpio;
    ctx->flags = flags;
    ctx->label = label;
    ctx->fd_value = -1;
    ctx->fd_active_low = -1;
    ctx->fd_direction = -1;
    ctx->fd_edge = -1;

    if ((is_requested = gpio_is_requested(ctx->gpio)) < 0)
        goto error_free;

    is_requested = gpio_request_one(ctx->gpio, ctx->flags, ctx->label);
    if (is_requested < 0)
        goto error_free;
    else if (is_requested)
        ctx->flags |= GPIOF_REQUESTED;

    return ctx;

error_free:
    free(ctx);
    return NULL;
}

void ugpio_free(ugpio_t *ctx)
{
    if (ctx == NULL)
        return;

    if (ctx->flags & GPIOF_REQUESTED)
        gpio_free(ctx->gpio);

    free(ctx);
}

int ugpio_open(ugpio_t *ctx)
{
    int flags;

    if (ctx->fd_value != -1)
        return ctx->fd_value;

    flags  = (ctx->flags & GPIOF_DIR_IN) ? O_RDONLY : O_RDWR;
    flags |= (ctx->flags & GPIOF_CLOEXEC) ? O_CLOEXEC : 0;

    ctx->fd_value = gpio_fd_open(ctx->gpio, GPIO_VALUE, flags);

    return ctx->fd_value;
}

int ugpio_full_open(ugpio_t *ctx)
{
    int flags;

    if (ugpio_open(ctx) == -1)
        return -1;

    flags  = O_RDWR;
    flags |= (ctx->flags & GPIOF_CLOEXEC) ? O_CLOEXEC : 0;

    if (ctx->fd_active_low == -1) {
        ctx->fd_active_low = gpio_fd_open(ctx->gpio, GPIO_ACTIVELOW, flags);
        if (ctx->fd_active_low == -1) {
            return -1;
        }
    }

    if (ctx->fd_direction == -1 && ctx->flags & GPIOF_ALTERABLE_DIRECTION) {
        ctx->fd_direction = gpio_fd_open(ctx->gpio, GPIO_DIRECTION, flags);
        if (ctx->fd_direction == -1) {
            return -1;<commit_msg>Always open value file using O_RDWR flag<commit_after>/*
 * Copyright © 2012-2015 <NAME> <<EMAIL>>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <config.h>
#include <ugpio.h>
#include <ugpio-internal.h>

ugpio_t *ugpio_request(unsigned int gpio, const char *label)
{
    ugpio_t *ctx;
    int is_requested, val;

    if ((ctx = malloc(sizeof(*ctx))) == NULL)
        return NULL;

    ctx->gpio = gpio;
    ctx->flags = GPIOF_CLOEXEC | GPIOF_DIRECTION_UNKNOWN;
    ctx->label = label;
    ctx->fd_value = -1;
    ctx->fd_active_low = -1;
    ctx->fd_direction = -1;
    ctx->fd_edge = -1;

    if ((is_requested = gpio_is_requested(ctx->gpio)) < 0)
        goto error_free;

    if (!is_requested) {
        if (gpio_request(ctx->gpio, ctx->label) < 0)
            goto error_free;

        ctx->flags |= GPIOF_REQUESTED;
    }

    if (gpio_alterable_direction(ctx->gpio)) {
        ctx->flags |= GPIOF_ALTERABLE_DIRECTION;

        if ((val = gpio_get_direction(ctx->gpio)) != -1) {
            ctx->flags &= ~GPIOF_DIRECTION_UNKNOWN;
            ctx->flags |= val;
        }
    }

    if (gpio_alterable_edge(ctx->gpio)) {
        ctx->flags |= GPIOF_ALTERABLE_EDGE;

        if ((val = gpio_get_edge(ctx->gpio)) != -1) {
            ctx->flags &= ~GPIOF_TRIGGER_MASK;
            ctx->flags |= val;
        }
    }

    return ctx;

error_free:
    free(ctx);
    return NULL;
}

ugpio_t *ugpio_request_one(unsigned int gpio, unsigned int flags, const char *label)
{
    ugpio_t *ctx;
    int is_requested;

    if ((ctx = malloc(sizeof(*ctx))) == NULL)
        return NULL;

    ctx->gpio = gpio;
    ctx->flags = flags;
    ctx->label = label;
    ctx->fd_value = -1;
    ctx->fd_active_low = -1;
    ctx->fd_direction = -1;
    ctx->fd_edge = -1;

    if ((is_requested = gpio_is_requested(ctx->gpio)) < 0)
        goto error_free;

    is_requested = gpio_request_one(ctx->gpio, ctx->flags, ctx->label);
    if (is_requested < 0)
        goto error_free;
    else if (is_requested)
        ctx->flags |= GPIOF_REQUESTED;

    return ctx;

error_free:
    free(ctx);
    return NULL;
}

void ugpio_free(ugpio_t *ctx)
{
    if (ctx == NULL)
        return;

    if (ctx->flags & GPIOF_REQUESTED)
        gpio_free(ctx->gpio);

    free(ctx);
}

int ugpio_open(ugpio_t *ctx)
{
    int flags;

    if (ctx->fd_value != -1)
        return ctx->fd_value;

    flags  = O_RDWR;
    flags |= (ctx->flags & GPIOF_CLOEXEC) ? O_CLOEXEC : 0;

    ctx->fd_value = gpio_fd_open(ctx->gpio, GPIO_VALUE, flags);

    return ctx->fd_value;
}

int ugpio_full_open(ugpio_t *ctx)
{
    int flags;

    if (ugpio_open(ctx) == -1)
        return -1;

    flags  = O_RDWR;
    flags |= (ctx->flags & GPIOF_CLOEXEC) ? O_CLOEXEC : 0;

    if (ctx->fd_active_low == -1) {
        ctx->fd_active_low = gpio_fd_open(ctx->gpio, GPIO_ACTIVELOW, flags);
        if (ctx->fd_active_low == -1) {
            return -1;
        }
    }

    if (ctx->fd_direction == -1 && ctx->flags & GPIOF_ALTERABLE_DIRECTION) {
        ctx->fd_direction = gpio_fd_open(ctx->gpio, GPIO_DIRECTION, flags);
        if (ctx->fd_direction == -1) {
            return -1;
{'fraction_non_alphanumeric': 0.148714069591528, 'fraction_numerical': 0.01059001512859304, 'mean_word_length': 2.468520461699895, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 132, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2517795', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2524, 'n_words': 502}