chiark / gitweb /
[PATCH] added libsysfs code from sysutils-0.1.1-071803 release
[elogind.git] / libsysfs / sysfs_driver.c
1 /*
2  * sysfs_driver.c
3  *
4  * Driver utility functions for libsysfs
5  *
6  * Copyright (C) 2003 International Business Machines, Inc.
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23 #include "libsysfs.h"
24 #include "sysfs.h"
25
26 /**
27  * sysfs_close_driver: closes and cleans up driver structure
28  * @driver: driver to close
29  */
30 void sysfs_close_driver(struct sysfs_driver *driver)
31 {
32         if (driver != NULL) {
33                 if (driver->directory != NULL)
34                         sysfs_close_directory(driver->directory);
35                 free(driver);
36         }
37 }
38
39 /**
40  * alloc_driver: allocates and initializes driver
41  * returns struct sysfs_driver with success and NULL with error.
42  */
43 static struct sysfs_driver *alloc_driver(void)
44 {
45         return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
46 }
47
48 /**
49  * sysfs_open_driver: opens and initializes driver structure
50  * @path: path to driver directory
51  * returns struct sysfs_driver with success and NULL with error
52  */
53 struct sysfs_driver *sysfs_open_driver(const char *path)
54 {
55         struct sysfs_driver *driver = NULL;
56         struct sysfs_directory *sdir = NULL;
57         char devname[SYSFS_NAME_LEN];
58
59         if (path == NULL) {
60                 errno = EINVAL;
61                 return NULL;
62         }
63         sdir = sysfs_open_directory(path);
64         if (sdir == NULL) {
65                 dprintf (stderr, "Error opening directory %s\n", path);
66                 return NULL;
67         }
68         if ((sysfs_read_directory(sdir)) != 0) {
69                 dprintf (stderr, "Error reading directory %s\n", path);
70                 sysfs_close_directory(sdir);
71                 return NULL;
72         }
73         driver = alloc_driver();
74         if (driver == NULL) {
75                 dprintf(stderr, "Error allocating driver at %s\n", path);
76                 sysfs_close_directory(sdir);
77                 return NULL;
78         }
79         if ((sysfs_get_name_from_path(path, devname, SYSFS_NAME_LEN)) != 0) {
80                 dprintf (stderr, "Error reading directory %s\n", path);
81                 sysfs_close_directory(sdir);
82                 free(driver);
83                 return NULL;
84         }
85         strncpy(driver->name, devname, sizeof(driver->name));
86         driver->directory = sdir;       
87         
88         return driver;
89 }