4 * System utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003
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)
39 c = strrchr(path, '/');
41 dprintf("Invalid path %s\n", path);
51 * sysfs_get_mnt_path: Gets the sysfs mount point.
52 * @mnt_path: place to put "sysfs" mount point
53 * @len: size of mnt_path
54 * returns 0 with success and -1 with error.
56 int sysfs_get_mnt_path(char *mnt_path, size_t len)
58 static char sysfs_path[SYSFS_PATH_MAX] = "";
59 const char *sysfs_path_env;
61 /* evaluate only at the first call */
62 if (sysfs_path[0] == '\0') {
63 /* possible overrride of real mount path */
64 sysfs_path_env = getenv(SYSFS_PATH_ENV);
65 if (sysfs_path_env != NULL) {
66 safestrcpymax(mnt_path, sysfs_path_env, len);
69 safestrcpymax(mnt_path, SYSFS_MNT_PATH, len);
76 * sysfs_get_name_from_path: returns last name from a "/" delimited path
77 * @path: path to get name from
78 * @name: where to put name
81 int sysfs_get_name_from_path(const char *path, char *name, size_t len)
83 char tmp[SYSFS_PATH_MAX];
86 if (!path || !name || len == 0) {
90 memset(tmp, 0, SYSFS_PATH_MAX);
91 safestrcpy(tmp, path);
92 n = strrchr(tmp, '/');
99 n = strrchr(tmp, '/');
106 safestrcpymax(name, n, len);
111 * sysfs_get_link: returns link source
112 * @path: symbolic link's path
113 * @target: where to put name
116 int sysfs_get_link(const char *path, char *target, size_t len)
118 char devdir[SYSFS_PATH_MAX];
119 char linkpath[SYSFS_PATH_MAX];
120 char temp_path[SYSFS_PATH_MAX];
121 char *d = NULL, *s = NULL;
122 int slashes = 0, count = 0;
124 if (!path || !target || len == 0) {
129 memset(devdir, 0, SYSFS_PATH_MAX);
130 memset(linkpath, 0, SYSFS_PATH_MAX);
131 memset(temp_path, 0, SYSFS_PATH_MAX);
132 safestrcpy(devdir, path);
134 if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
140 * 1. relative path => format ../..
141 * 2. absolute path => format /abcd/efgh
142 * 3. relative path _from_ this dir => format abcd/efgh
147 * handle the case where link is of type ./abcd/xxx
149 safestrcpy(temp_path, devdir);
152 else if (*(d+1) == '.')
154 s = strrchr(temp_path, '/');
157 safestrcat(temp_path, d);
159 safestrcpy(temp_path, d);
161 safestrcpymax(target, temp_path, len);
164 * relative path, getting rid of leading "../.."
167 while (*d == '/' || *d == '.') {
173 s = &devdir[strlen(devdir)-1];
174 while (s != NULL && count != (slashes+1)) {
179 safestrcpymax(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
180 safestrcpymax(target, devdir, len);
183 /* absolute path - copy as is */
184 safestrcpymax(target, linkpath, len);
187 /* relative path from this directory */
188 safestrcpy(temp_path, devdir);
189 s = strrchr(temp_path, '/');
192 safestrcat(temp_path, linkpath);
194 safestrcpy(temp_path, linkpath);
196 safestrcpymax(target, temp_path, len);
202 * sysfs_close_list: generic list free routine
203 * @list: dlist to free
206 void sysfs_close_list(struct dlist *list)
213 * sysfs_open_directory_list: gets a list of all directories under "path"
214 * @path: path to read
215 * Returns a dlist of supported names or NULL no directories (errno is set
218 struct dlist *sysfs_open_directory_list(const char *path)
223 return (read_dir_subdirs(path));
227 * sysfs_path_is_dir: Check if the path supplied points to a directory
228 * @path: path to validate
229 * Returns 0 if path points to dir, 1 otherwise
231 int sysfs_path_is_dir(const char *path)
239 if ((lstat(path, &astats)) != 0) {
240 dprintf("stat() failed\n");
243 if (S_ISDIR(astats.st_mode))
250 * sysfs_path_is_link: Check if the path supplied points to a link
251 * @path: path to validate
252 * Returns 0 if path points to link, 1 otherwise
254 int sysfs_path_is_link(const char *path)
262 if ((lstat(path, &astats)) != 0) {
263 dprintf("stat() failed\n");
266 if (S_ISLNK(astats.st_mode))
273 * sysfs_path_is_file: Check if the path supplied points to a file
274 * @path: path to validate
275 * Returns 0 if path points to file, 1 otherwise
277 int sysfs_path_is_file(const char *path)
285 if ((lstat(path, &astats)) != 0) {
286 dprintf("stat() failed\n");
289 if (S_ISREG(astats.st_mode))