4 * System utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003-2005
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.
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.
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
27 * sysfs_remove_trailing_slash: Removes any trailing '/' in the given path
28 * @path: Path to look for the trailing '/'
29 * Returns 0 on success 1 on error
31 int sysfs_remove_trailing_slash(char *path)
41 while (len > 0 && path[len-1] == '/')
47 * sysfs_get_mnt_path: Gets the sysfs mount point.
48 * @mnt_path: place to put "sysfs" mount point
49 * @len: size of mnt_path
50 * returns 0 with success and -1 with error.
52 int sysfs_get_mnt_path(char *mnt_path, size_t len)
54 static char sysfs_path[SYSFS_PATH_MAX] = "";
55 const char *sysfs_path_env;
57 /* evaluate only at the first call */
58 if (sysfs_path[0] == '\0') {
59 /* possible overrride of real mount path */
60 sysfs_path_env = getenv(SYSFS_PATH_ENV);
61 if (sysfs_path_env != NULL) {
62 safestrcpymax(mnt_path, sysfs_path_env, len);
63 sysfs_remove_trailing_slash(mnt_path);
66 safestrcpymax(mnt_path, SYSFS_MNT_PATH, len);
73 * sysfs_get_name_from_path: returns last name from a "/" delimited path
74 * @path: path to get name from
75 * @name: where to put name
78 int sysfs_get_name_from_path(const char *path, char *name, size_t len)
80 char tmp[SYSFS_PATH_MAX];
83 if (!path || !name || len == 0) {
87 memset(tmp, 0, SYSFS_PATH_MAX);
88 safestrcpy(tmp, path);
89 n = strrchr(tmp, '/');
96 n = strrchr(tmp, '/');
103 safestrcpymax(name, n, len);
108 * sysfs_get_link: returns link source
109 * @path: symbolic link's path
110 * @target: where to put name
113 int sysfs_get_link(const char *path, char *target, size_t len)
115 char devdir[SYSFS_PATH_MAX];
116 char linkpath[SYSFS_PATH_MAX];
117 char temp_path[SYSFS_PATH_MAX];
118 char *d = NULL, *s = NULL;
119 int slashes = 0, count = 0;
121 if (!path || !target || len == 0) {
126 memset(devdir, 0, SYSFS_PATH_MAX);
127 memset(linkpath, 0, SYSFS_PATH_MAX);
128 memset(temp_path, 0, SYSFS_PATH_MAX);
129 safestrcpy(devdir, path);
131 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
137 * 1. relative path => format ../..
138 * 2. absolute path => format /abcd/efgh
139 * 3. relative path _from_ this dir => format abcd/efgh
144 * handle the case where link is of type ./abcd/xxx
146 safestrcpy(temp_path, devdir);
149 else if (*(d+1) == '.')
151 s = strrchr(temp_path, '/');
154 safestrcat(temp_path, d);
156 safestrcpy(temp_path, d);
158 safestrcpymax(target, temp_path, len);
161 * relative path, getting rid of leading "../.."
164 while (*d == '/' || *d == '.') {
170 s = &devdir[strlen(devdir)-1];
171 while (s != NULL && count != (slashes+1)) {
176 safestrcpymax(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
177 safestrcpymax(target, devdir, len);
180 /* absolute path - copy as is */
181 safestrcpymax(target, linkpath, len);
184 /* relative path from this directory */
185 safestrcpy(temp_path, devdir);
186 s = strrchr(temp_path, '/');
189 safestrcat(temp_path, linkpath);
191 safestrcpy(temp_path, linkpath);
193 safestrcpymax(target, temp_path, len);
199 * sysfs_close_list: generic list free routine
200 * @list: dlist to free
203 void sysfs_close_list(struct dlist *list)
210 * sysfs_open_directory_list: gets a list of all directories under "path"
211 * @path: path to read
212 * Returns a dlist of supported names or NULL no directories (errno is set
215 struct dlist *sysfs_open_directory_list(const char *path)
220 return (read_dir_subdirs(path));
224 * sysfs_path_is_dir: Check if the path supplied points to a directory
225 * @path: path to validate
226 * Returns 0 if path points to dir, 1 otherwise
228 int sysfs_path_is_dir(const char *path)
236 if ((lstat(path, &astats)) != 0) {
237 dprintf("stat() failed\n");
240 if (S_ISDIR(astats.st_mode))
247 * sysfs_path_is_link: Check if the path supplied points to a link
248 * @path: path to validate
249 * Returns 0 if path points to link, 1 otherwise
251 int sysfs_path_is_link(const char *path)
259 if ((lstat(path, &astats)) != 0) {
260 dprintf("stat() failed\n");
263 if (S_ISLNK(astats.st_mode))
270 * sysfs_path_is_file: Check if the path supplied points to a file
271 * @path: path to validate
272 * Returns 0 if path points to file, 1 otherwise
274 int sysfs_path_is_file(const char *path)
282 if ((lstat(path, &astats)) != 0) {
283 dprintf("stat() failed\n");
286 if (S_ISREG(astats.st_mode))