56 lines
985 B
C
56 lines
985 B
C
#include <stdbool.h>
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include "util.h"
|
|
|
|
bool joinhome(char* res, char* path){
|
|
char* homedir = getenv("HOME");
|
|
|
|
if(NULL==homedir)
|
|
return false;
|
|
|
|
snprintf(res, PATH_MAX, "%s/%s", homedir, path);
|
|
return true;
|
|
}
|
|
|
|
bool exists(char* path){
|
|
return access(path, F_OK)==0;
|
|
}
|
|
|
|
bool endswith(const char *str, const char *suf){
|
|
int strl = strlen(str);
|
|
int sufl = strlen(suf);
|
|
|
|
if (sufl > strl)
|
|
return false;
|
|
|
|
return strncmp(str + strl - sufl, suf, sufl) == 0;
|
|
}
|
|
|
|
char* check_path(char* bin) {
|
|
char* path = getenv("PATH");
|
|
char* res = NULL;
|
|
|
|
if(NULL == path)
|
|
return NULL;
|
|
path = strdup(path);
|
|
|
|
char* p = strtok(path, ":");
|
|
while(NULL != p) {
|
|
char* fp = malloc(PATH_MAX);
|
|
snprintf(fp, PATH_MAX, "%s/%s", p, bin);
|
|
if(exists(fp)){
|
|
res = fp;
|
|
break;
|
|
}
|
|
p = strtok(NULL, ":");
|
|
}
|
|
|
|
free(path);
|
|
return res;
|
|
}
|