2024-05-01 21:02:15 +03:00

47 lines
989 B
C

#include "util.h"
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define HUB_URL "https://configs.matterlinux.xyz"
bool url_is_name(char *name) {
for (char *c = name; *c != '\0'; c++) {
if (*c == '/' || *c == '.')
return false;
}
return true;
}
bool url_is_local(char *url) {
bool ret = false;
if (startswith(url, "http://") || startswith(url, "https://"))
return ret;
DIR *dir = NULL;
if (access(url, F_OK) == 0 && (dir = opendir(url)) != NULL)
ret = true;
closedir(dir);
return ret;
}
char *url_complete(char *name) {
char *url = malloc(strlen(name) + strlen(HUB_URL) + 1);
sprintf(url, "%s/%s", HUB_URL, name);
return url;
}
char *url_ensure_protocol(char *url) {
if (startswith(url, "http://") || startswith(url, "https://"))
return strdup(url);
char *full = malloc(strlen("https://") + strlen(url) + 1);
join(full, "https://", url);
return full;
}