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
struct sysfs_attribute* sysfs_open_attribute(const char* path)
{
    struct sysfs_attribute* sysattr = NULL;
    struct stat fileinfo;

    if (!path)
    {
        errno = EINVAL;
        return NULL;
    }
    sysattr = alloc_attribute();
    if (!sysattr)
    {
        dprintf("Error allocating attribute at %s\n", path);
        return NULL;
    }
    if (sysfs_get_name_from_path(path, sysattr->name, SYSFS_NAME_LEN) != 0)
    {
        dprintf("Error retrieving attrib name from path: %s\n", path);
        sysfs_close_attribute(sysattr);
        return NULL;
    }
    safestrcpy(sysattr->path, path);
    if ((stat(sysattr->path, &fileinfo)) != 0)
    {
        dprintf("Stat failed: No such attribute?\n");
        sysattr->method = 0;
        free(sysattr);
        sysattr = NULL;
    }
    else
    {
        if (fileinfo.st_mode & S_IRUSR)
            sysattr->method |= SYSFS_METHOD_SHOW;
        if (fileinfo.st_mode & S_IWUSR)
            sysattr->method |= SYSFS_METHOD_STORE;
    }

    return sysattr;
}
/**
 * sysfs_open_attribute: creates sysfs_attribute structure
 * @path: path to attribute.
 * returns sysfs_attribute struct with success and NULL with error.
 */
creates sysfs_attribute structure
@path: path to attribute.
returns sysfs_attribute struct with success and NULL with error.
creates sysfs_attribute structure @path : path to attribute . returns sysfs_attribute struct with success and NULL with error .
struct sysfs_attribute* sysfs_open_attribute(const char* path)
{
    struct sysfs_attribute* sysattr = NULL;
    struct stat fileinfo;
    if (!path)
    {
        errno = EINVAL;
        return NULL;
    }
    sysattr = alloc_attribute();
    if (!sysattr)
    {
        dprintf("Error allocating attribute at %s\n", path);
        return NULL;
    }
    if (sysfs_get_name_from_path(path, sysattr->name, SYSFS_NAME_LEN) != 0)
    {
        dprintf("Error retrieving attrib name from path: %s\n", path);
        sysfs_close_attribute(sysattr);
        return NULL;
    }
    safestrcpy(sysattr->path, path);
    if ((stat(sysattr->path, &fileinfo)) != 0)
    {
        dprintf("Stat failed: No such attribute?\n");
        sysattr->method = 0;
        free(sysattr);
        sysattr = NULL;
    }
    else
    {
        if (fileinfo.st_mode & S_IRUSR)
            sysattr->method |= SYSFS_METHOD_SHOW;
        if (fileinfo.st_mode & S_IWUSR)
            sysattr->method |= SYSFS_METHOD_STORE;
    }
    return sysattr;
}
struct sysfs_attribute * sysfs_open_attribute ( const char * path ) { struct sysfs_attribute * sysattr = NULL ; struct stat fileinfo ; if ( ! path ) { errno = EINVAL ; return NULL ; } sysattr = alloc_attribute ( ) ; if ( ! sysattr ) { dprintf ( " \n " , path ) ; return NULL ; } if ( sysfs_get_name_from_path ( path , sysattr -> name , SYSFS_NAME_LEN ) != 0 ) { dprintf ( " \n " , path ) ; sysfs_close_attribute ( sysattr ) ; return NULL ; } safestrcpy ( sysattr -> path , path ) ; if ( ( stat ( sysattr -> path , & fileinfo ) ) != 0 ) { dprintf ( " \n " ) ; sysattr -> method = 0 ; free ( sysattr ) ; sysattr = NULL ; } else { if ( fileinfo . st_mode & S_IRUSR ) sysattr -> method |= SYSFS_METHOD_SHOW ; if ( fileinfo . st_mode & S_IWUSR ) sysattr -> method |= SYSFS_METHOD_STORE ; } return sysattr ; }
sysfs_open_attribute: creates sysfs_attribute structure
 @path: path to attribute.
sysfs_open_attribute : creates sysfs_attribute structure @path : path to attribute .