openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/sysfsutils/2.1.0-r5/sysfsutils-2.1.0/lib/sysfs_attr.c
int sysfs_read_attribute(struct sysfs_attribute* sysattr)
{
    char* fbuf = NULL;
    char* vbuf = NULL;
    ssize_t length = 0;
    long pgsize = 0;
    int fd;

    if (!sysattr)
    {
        errno = EINVAL;
        return -1;
    }
    if (!(sysattr->method & SYSFS_METHOD_SHOW))
    {
        dprintf("Show method not supported for attribute %s\n", sysattr->path);
        errno = EACCES;
        return -1;
    }
    pgsize = getpagesize();
    fbuf = (char*)calloc(1, pgsize + 1);
    if (!fbuf)
    {
        dprintf("calloc failed\n");
        return -1;
    }
    if ((fd = open(sysattr->path, O_RDONLY)) < 0)
    {
        dprintf("Error reading attribute %s\n", sysattr->path);
        free(fbuf);
        return -1;
    }
    length = read(fd, fbuf, pgsize);
    if (length < 0)
    {
        dprintf("Error reading from attribute %s\n", sysattr->path);
        close(fd);
        free(fbuf);
        return -1;
    }
    if (sysattr->len > 0)
    {
        if ((sysattr->len == length) &&
            (!(strncmp(sysattr->value, fbuf, length))))
        {
            close(fd);
            free(fbuf);
            return 0;
        }
        free(sysattr->value);
    }
    sysattr->len = length;
    close(fd);
    vbuf = (char*)realloc(fbuf, length + 1);
    if (!vbuf)
    {
        dprintf("realloc failed\n");
        free(fbuf);
        return -1;
    }
    sysattr->value = vbuf;

    return 0;
}
/**
 * sysfs_read_attribute: reads value from attribute
 * @sysattr: attribute to read
 * returns 0 with success and -1 with error.
 */
reads value from attribute
@sysattr: attribute to read
returns 0 with success and -1 with error.
reads value from attribute @sysattr : attribute to read returns 0 with success and - 1 with error .
int sysfs_read_attribute(struct sysfs_attribute* sysattr)
{
    char* fbuf = NULL;
    char* vbuf = NULL;
    ssize_t length = 0;
    long pgsize = 0;
    int fd;
    if (!sysattr)
    {
        errno = EINVAL;
        return -1;
    }
    if (!(sysattr->method & SYSFS_METHOD_SHOW))
    {
        dprintf("Show method not supported for attribute %s\n", sysattr->path);
        errno = EACCES;
        return -1;
    }
    pgsize = getpagesize();
    fbuf = (char*)calloc(1, pgsize + 1);
    if (!fbuf)
    {
        dprintf("calloc failed\n");
        return -1;
    }
    if ((fd = open(sysattr->path, O_RDONLY)) < 0)
    {
        dprintf("Error reading attribute %s\n", sysattr->path);
        free(fbuf);
        return -1;
    }
    length = read(fd, fbuf, pgsize);
    if (length < 0)
    {
        dprintf("Error reading from attribute %s\n", sysattr->path);
        close(fd);
        free(fbuf);
        return -1;
    }
    if (sysattr->len > 0)
    {
        if ((sysattr->len == length) &&
            (!(strncmp(sysattr->value, fbuf, length))))
        {
            close(fd);
            free(fbuf);
            return 0;
        }
        free(sysattr->value);
    }
    sysattr->len = length;
    close(fd);
    vbuf = (char*)realloc(fbuf, length + 1);
    if (!vbuf)
    {
        dprintf("realloc failed\n");
        free(fbuf);
        return -1;
    }
    sysattr->value = vbuf;
    return 0;
}
int sysfs_read_attribute ( struct sysfs_attribute * sysattr ) { char * fbuf = NULL ; char * vbuf = NULL ; ssize_t length = 0 ; long pgsize = 0 ; int fd ; if ( ! sysattr ) { errno = EINVAL ; return -1 ; } if ( ! ( sysattr -> method & SYSFS_METHOD_SHOW ) ) { dprintf ( " \n " , sysattr -> path ) ; errno = EACCES ; return -1 ; } pgsize = getpagesize ( ) ; fbuf = ( char * ) calloc ( 1 , pgsize + 1 ) ; if ( ! fbuf ) { dprintf ( " \n " ) ; return -1 ; } if ( ( fd = open ( sysattr -> path , O_RDONLY ) ) < 0 ) { dprintf ( " \n " , sysattr -> path ) ; free ( fbuf ) ; return -1 ; } length = read ( fd , fbuf , pgsize ) ; if ( length < 0 ) { dprintf ( " \n " , sysattr -> path ) ; close ( fd ) ; free ( fbuf ) ; return -1 ; } if ( sysattr -> len > 0 ) { if ( ( sysattr -> len == length ) && ( ! ( strncmp ( sysattr -> value , fbuf , length ) ) ) ) { close ( fd ) ; free ( fbuf ) ; return 0 ; } free ( sysattr -> value ) ; } sysattr -> len = length ; close ( fd ) ; vbuf = ( char * ) realloc ( fbuf , length + 1 ) ; if ( ! vbuf ) { dprintf ( " \n " ) ; free ( fbuf ) ; return -1 ; } sysattr -> value = vbuf ; return 0 ; }
sysfs_read_attribute: reads value from attribute
 @sysattr: attribute to read
 returns 0 with success and -1 with error.
sysfs_read_attribute : reads value from attribute @sysattr : attribute to read returns 0 with success and - 1 with error .