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_device.c
struct sysfs_device* sysfs_get_device_parent(struct sysfs_device* dev)
{
    char ppath[SYSFS_PATH_MAX], dpath[SYSFS_PATH_MAX], *tmp;

    if (!dev)
    {
        errno = EINVAL;
        return NULL;
    }

    if (dev->parent)
        return (dev->parent);

    memset(ppath, 0, SYSFS_PATH_MAX);
    memset(dpath, 0, SYSFS_PATH_MAX);
    safestrcpy(ppath, dev->path);
    tmp = strrchr(ppath, '/');
    if (!tmp)
    {
        dprintf("Invalid path to device %s\n", ppath);
        return NULL;
    }
    if (*(tmp + 1) == '\0')
    {
        *tmp = '\0';
        tmp = strrchr(tmp, '/');
        if (tmp == NULL)
        {
            dprintf("Invalid path to device %s\n", ppath);
            return NULL;
        }
    }
    *tmp = '\0';

    /* Make sure we're at the top of the device tree */
    if (sysfs_get_mnt_path(dpath, SYSFS_PATH_MAX) != 0)
    {
        dprintf("Sysfs not supported on this system\n");
        return NULL;
    }
    safestrcat(dpath, "/");
    safestrcat(dpath, SYSFS_DEVICES_NAME);

    if (strcmp(dpath, ppath) == 0)
    {
        dprintf("Device at %s does not have a parent\n", dev->path);
        return NULL;
    }

    dev->parent = sysfs_open_device_path(ppath);
    if (!dev->parent)
    {
        dprintf("Error opening device %s's parent at %s\n", dev->bus_id, ppath);
        return NULL;
    }
    return (dev->parent);
}
/**
 * sysfs_get_device_parent: opens up given device's parent and returns a
 * 	reference to its sysfs_device
 * @dev: sysfs_device whose parent is requested
 * Returns sysfs_device of the parent on success and NULL on failure
 */
opens up given device's parent and returns a
reference to its sysfs_device
@dev: sysfs_device whose parent is requested
Returns sysfs_device of the parent on success and NULL on failure
opens up given device ' s parent and returns a reference to its sysfs_device @dev : sysfs_device whose parent is requested Returns sysfs_device of the parent on success and NULL on failure
struct sysfs_device* sysfs_get_device_parent(struct sysfs_device* dev)
{
    char ppath[SYSFS_PATH_MAX], dpath[SYSFS_PATH_MAX], *tmp;
    if (!dev)
    {
        errno = EINVAL;
        return NULL;
    }
    if (dev->parent)
        return (dev->parent);
    memset(ppath, 0, SYSFS_PATH_MAX);
    memset(dpath, 0, SYSFS_PATH_MAX);
    safestrcpy(ppath, dev->path);
    tmp = strrchr(ppath, '/');
    if (!tmp)
    {
        dprintf("Invalid path to device %s\n", ppath);
        return NULL;
    }
    if (*(tmp + 1) == '\0')
    {
        *tmp = '\0';
        tmp = strrchr(tmp, '/');
        if (tmp == NULL)
        {
            dprintf("Invalid path to device %s\n", ppath);
            return NULL;
        }
    }
    *tmp = '\0';
    if (sysfs_get_mnt_path(dpath, SYSFS_PATH_MAX) != 0)
    {
        dprintf("Sysfs not supported on this system\n");
        return NULL;
    }
    safestrcat(dpath, "/");
    safestrcat(dpath, SYSFS_DEVICES_NAME);
    if (strcmp(dpath, ppath) == 0)
    {
        dprintf("Device at %s does not have a parent\n", dev->path);
        return NULL;
    }
    dev->parent = sysfs_open_device_path(ppath);
    if (!dev->parent)
    {
        dprintf("Error opening device %s's parent at %s\n", dev->bus_id, ppath);
        return NULL;
    }
    return (dev->parent);
}
struct sysfs_device * sysfs_get_device_parent ( struct sysfs_device * dev ) { char ppath [ SYSFS_PATH_MAX ] , dpath [ SYSFS_PATH_MAX ] , * tmp ; if ( ! dev ) { errno = EINVAL ; return NULL ; } if ( dev -> parent ) return ( dev -> parent ) ; memset ( ppath , 0 , SYSFS_PATH_MAX ) ; memset ( dpath , 0 , SYSFS_PATH_MAX ) ; safestrcpy ( ppath , dev -> path ) ; tmp = strrchr ( ppath , ' ' ) ; if ( ! tmp ) { dprintf ( " \n " , ppath ) ; return NULL ; } if ( * ( tmp + 1 ) == ' \0 ' ) { * tmp = ' \0 ' ; tmp = strrchr ( tmp , ' ' ) ; if ( tmp == NULL ) { dprintf ( " \n " , ppath ) ; return NULL ; } } * tmp = ' \0 ' ; if ( sysfs_get_mnt_path ( dpath , SYSFS_PATH_MAX ) != 0 ) { dprintf ( " \n " ) ; return NULL ; } safestrcat ( dpath , " " ) ; safestrcat ( dpath , SYSFS_DEVICES_NAME ) ; if ( strcmp ( dpath , ppath ) == 0 ) { dprintf ( " \n " , dev -> path ) ; return NULL ; } dev -> parent = sysfs_open_device_path ( ppath ) ; if ( ! dev -> parent ) { dprintf ( " \n " , dev -> bus_id , ppath ) ; return NULL ; } return ( dev -> parent ) ; }
sysfs_get_device_parent: opens up given device's parent and returns a
      reference to its sysfs_device
 @dev: sysfs_device whose parent is requested
 Returns sysfs_device of the parent on success and NULL on failure
sysfs_get_device_parent : opens up given device ' s parent and returns a reference to its sysfs_device @dev : sysfs_device whose parent is requested Returns sysfs_device of the parent on success and NULL on failure
/* Make sure we're at the top of the device tree */