mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-27 13:21:11 +03:00
Added ssh_basename() and ssh_dirname().
This commit is contained in:
@@ -443,6 +443,10 @@ LIBSSH_API int ssh_userauth_kbdint_setanswer(SSH_SESSION *session, unsigned int
|
|||||||
LIBSSH_API int ssh_init(void);
|
LIBSSH_API int ssh_init(void);
|
||||||
LIBSSH_API int ssh_finalize(void);
|
LIBSSH_API int ssh_finalize(void);
|
||||||
|
|
||||||
|
/* misc.c */
|
||||||
|
char *ssh_dirname (const char *path);
|
||||||
|
char *ssh_basename (const char *path);
|
||||||
|
|
||||||
/* messages.c */
|
/* messages.c */
|
||||||
typedef struct ssh_message SSH_MESSAGE;
|
typedef struct ssh_message SSH_MESSAGE;
|
||||||
|
|
||||||
|
|||||||
108
libssh/misc.c
108
libssh/misc.c
@@ -238,6 +238,114 @@ const void *_ssh_list_get_head(struct ssh_list *list){
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Parse directory component.
|
||||||
|
*
|
||||||
|
* dirname breaks a null-terminated pathname string into a directory component.
|
||||||
|
* In the usual case, ssh_dirname() returns the string up to, but not including,
|
||||||
|
* the final '/'. Trailing '/' characters are not counted as part of the
|
||||||
|
* pathname. The caller must free the memory.
|
||||||
|
*
|
||||||
|
* @param path The path to parse.
|
||||||
|
*
|
||||||
|
* @return The dirname of path or NULL if we can't allocate memory. If path
|
||||||
|
* does not contain a slash, c_dirname() returns the string ".". If
|
||||||
|
* path is the string "/", it returns the string "/". If path is
|
||||||
|
* NULL or an empty string, "." is returned.
|
||||||
|
*/
|
||||||
|
char *ssh_dirname (const char *path) {
|
||||||
|
char *new = NULL;
|
||||||
|
unsigned int len;
|
||||||
|
|
||||||
|
if (path == NULL || *path == '\0') {
|
||||||
|
return strdup(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
len = strlen(path);
|
||||||
|
|
||||||
|
/* Remove trailing slashes */
|
||||||
|
while(len > 0 && path[len - 1] == '/') --len;
|
||||||
|
|
||||||
|
/* We have only slashes */
|
||||||
|
if (len == 0) {
|
||||||
|
return strdup("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* goto next slash */
|
||||||
|
while(len > 0 && path[len - 1] != '/') --len;
|
||||||
|
|
||||||
|
if (len == 0) {
|
||||||
|
return strdup(".");
|
||||||
|
} else if (len == 1) {
|
||||||
|
return strdup("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Remove slashes again */
|
||||||
|
while(len > 0 && path[len - 1] == '/') --len;
|
||||||
|
|
||||||
|
new = malloc(len + 1);
|
||||||
|
if (new == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(new, path, len);
|
||||||
|
new[len] = '\0';
|
||||||
|
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief basename - parse filename component.
|
||||||
|
*
|
||||||
|
* basename breaks a null-terminated pathname string into a filename component.
|
||||||
|
* ssh_basename() returns the component following the final '/'. Trailing '/'
|
||||||
|
* characters are not counted as part of the pathname.
|
||||||
|
*
|
||||||
|
* @param path The path to parse.
|
||||||
|
*
|
||||||
|
* @return The filename of path or NULL if we can't allocate memory. If path
|
||||||
|
* is a the string "/", basename returns the string "/". If path is
|
||||||
|
* NULL or an empty string, "." is returned.
|
||||||
|
*/
|
||||||
|
char *ssh_basename (const char *path) {
|
||||||
|
char *new = NULL;
|
||||||
|
const char *s;
|
||||||
|
unsigned int len;
|
||||||
|
|
||||||
|
if (path == NULL || *path == '\0') {
|
||||||
|
return strdup(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
len = strlen(path);
|
||||||
|
/* Remove trailing slashes */
|
||||||
|
while(len > 0 && path[len - 1] == '/') --len;
|
||||||
|
|
||||||
|
/* We have only slashes */
|
||||||
|
if (len == 0) {
|
||||||
|
return strdup("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
while(len > 0 && path[len - 1] != '/') --len;
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
s = path + len;
|
||||||
|
len = strlen(s);
|
||||||
|
|
||||||
|
while(len > 0 && s[len - 1] == '/') --len;
|
||||||
|
} else {
|
||||||
|
return strdup(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
new = malloc(len + 1);
|
||||||
|
if (new == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(new, s, len);
|
||||||
|
new[len] = '\0';
|
||||||
|
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
/* vim: set ts=2 sw=2 et cindent: */
|
/* vim: set ts=2 sw=2 et cindent: */
|
||||||
|
|||||||
Reference in New Issue
Block a user