Alternative yang spec option -y added to all applications
This commit is contained in:
parent
236b661e43
commit
c59869a44e
10 changed files with 86 additions and 167 deletions
|
|
@ -62,108 +62,6 @@
|
|||
#include "clixon_string.h"
|
||||
#include "clixon_file.h"
|
||||
|
||||
/*! Resolve the real path of a given 'path', following symbolic links and '../'.
|
||||
* If 'path' relative, it will be resolved based on the currnt working
|
||||
* directory 'cwd'. The response is a 2 entry vector of strings. The first
|
||||
* entry is the resolved path and the second is the part of the path which
|
||||
* actually exist.
|
||||
* @retval vec
|
||||
*/
|
||||
char **
|
||||
clicon_realpath(const char *cwd,
|
||||
char *path,
|
||||
const char *label)
|
||||
{
|
||||
char **ret = NULL;
|
||||
char *rest;
|
||||
char **vec = NULL;
|
||||
char **vec2;
|
||||
int nvec, nvec2;
|
||||
char *p;
|
||||
char *rp = NULL;
|
||||
char *ptr;
|
||||
int i;
|
||||
struct passwd *pwd;
|
||||
char cwdbuf[PATH_MAX];
|
||||
|
||||
/* Prepend 'cwd' if not absolute */
|
||||
if (path[0] == '/')
|
||||
p = path;
|
||||
else {
|
||||
if (cwd == NULL || strlen(cwd) == 0)
|
||||
cwd = getcwd(cwdbuf, sizeof(cwdbuf));
|
||||
else if (cwd[0] == '~') {
|
||||
if((pwd = getpwuid(getuid())) == NULL)
|
||||
goto catch;
|
||||
cwd = pwd->pw_dir;
|
||||
}
|
||||
p = chunk_sprintf(__FUNCTION__, "%s%s/%s",
|
||||
(cwd[0]=='/' ? "" : "/"), cwd, path);
|
||||
}
|
||||
if (p == NULL)
|
||||
goto catch;
|
||||
|
||||
/* Make a local copy of 'path' */
|
||||
if ((path = chunkdup(p, strlen(p)+1, __FUNCTION__)) == NULL)
|
||||
goto catch;
|
||||
|
||||
/* Find the smallest portion of the path that exist and run realpath() */
|
||||
while(strlen(p) && ((rp = realpath(p, NULL)) == NULL)) {
|
||||
if((ptr = strrchr(p, '/')) == NULL)
|
||||
break;
|
||||
*ptr = '\0';
|
||||
}
|
||||
if(rp == NULL)
|
||||
goto catch;
|
||||
|
||||
/* Use the result of realpath() and the rest of 'path' untouched, to
|
||||
form a new path */
|
||||
rest = path + strlen(p);
|
||||
ptr = chunk_sprintf(__FUNCTION__, "%s%s", rp, rest);
|
||||
p = ptr;
|
||||
|
||||
/* Split path based on '/'. Loop through vector from the end and copy
|
||||
each entry into a new vector, skipping '..' and it's previous directory
|
||||
as well as all '.' */
|
||||
if ((vec = clicon_strsep(p, "/", &nvec)) == NULL)
|
||||
goto catch;
|
||||
if ((vec2 = malloc(nvec * sizeof(char *))) == NULL)
|
||||
goto catch;
|
||||
nvec2 = i = nvec;
|
||||
while(--i >= 0) {
|
||||
if(strcmp(vec[i], "..") == 0)
|
||||
i--; /* Skip previous */
|
||||
else if(strcmp(vec[i], ".") == 0)
|
||||
/* do nothing */ ;
|
||||
else
|
||||
vec2[--nvec2] = vec[i];
|
||||
}
|
||||
|
||||
/* Create resulting vector */
|
||||
if ((ret = malloc(sizeof(char *) * 2)) != NULL) {
|
||||
if((ret[0] = clicon_strjoin(nvec-nvec2, &vec2[nvec2], "/")) == NULL) {
|
||||
free(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
if ((ret[1] = strdup(rp)) == NULL) {
|
||||
free(ret[0]);
|
||||
free(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
catch:
|
||||
if(rp)
|
||||
free(rp);
|
||||
if(vec)
|
||||
free(vec);
|
||||
if(vec2)
|
||||
free(vec2);
|
||||
unchunk_group(__FUNCTION__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* qsort function
|
||||
*/
|
||||
|
|
@ -354,24 +252,3 @@ clicon_file_copy(char *src,
|
|||
}
|
||||
|
||||
|
||||
#ifdef NOTUSED
|
||||
/*
|
||||
* (un)lock a whole file.
|
||||
* Arguments:
|
||||
* fd - File descriptor
|
||||
* cmd - F_GETLK, F_SETLK, F_SETLKW
|
||||
* type - F_RDLCK, F_WRLCK, F_UNLCK
|
||||
*/
|
||||
int
|
||||
file_lock(int fd, int cmd, int type)
|
||||
{
|
||||
struct flock lock;
|
||||
|
||||
lock.l_type = type;
|
||||
lock.l_whence = SEEK_SET;
|
||||
lock.l_start = 0;
|
||||
lock.l_len = 0;
|
||||
|
||||
return fcntl(fd, cmd, &lock);
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue