1 /* vi: set sw=8 ts=8: */
5 * Copyright (C) many different people.
6 * If you wrote this, please acknowledge your work.
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 #include <linux/stat.h>
17 #include <environment.h>
19 /* concatenate path and file name to new allocation buffer,
20 * not adding '/' if path name already has '/'
22 char *concat_path_file(const char *path
, const char *filename
)
28 lc
= last_char_is(path
, '/');
29 while (*filename
== '/')
32 str
= xmalloc(strlen(path
) + (lc
==0 ? 1 : 0) + strlen(filename
) + 1);
33 sprintf(str
, "%s%s%s", path
, (lc
==NULL
? "/" : ""), filename
);
37 EXPORT_SYMBOL(concat_path_file
);
40 * This function make special for recursive actions with usage
41 * concat_path_file(path, filename)
42 * and skipping "." and ".." directory entries
45 char *concat_subpath_file(const char *path
, const char *f
)
47 if (f
&& DOT_OR_DOTDOT(f
))
49 return concat_path_file(path
, f
);
51 EXPORT_SYMBOL(concat_subpath_file
);
53 /* check if path points to an executable file;
57 int execable_file(const char *name
)
60 return (!stat(name
, &s
) && S_ISREG(s
.st_mode
));
62 EXPORT_SYMBOL(execable_file
);
65 /* search $PATH for an executable file;
66 * return allocated string containing full path if found;
67 * return NULL otherwise;
69 char *find_execable(const char *filename
)
73 p
= path
= strdup(getenv("PATH"));
78 if (*p
!= '\0') { /* it's not a PATH="foo::bar" situation */
79 p
= concat_path_file(p
, filename
);
80 if (execable_file(p
)) {
91 EXPORT_SYMBOL(find_execable
);
93 /* Find out if the last character of a string matches the one given.
94 * Don't underrun the buffer if the string length is 0.
96 char* last_char_is(const char *s
, int c
)
99 size_t sz
= strlen(s
) - 1;
101 if ( (unsigned char)*s
== c
)
106 EXPORT_SYMBOL(last_char_is
);
108 /* Like strncpy but make sure the resulting string is always 0 terminated. */
109 char * safe_strncpy(char *dst
, const char *src
, size_t size
)
111 if (!size
) return dst
;
113 return strncpy(dst
, src
, size
);
115 EXPORT_SYMBOL(safe_strncpy
);