commit a69df12310b56dd6a926c6dcf73f87f4deaff181
parent 23505c5e178c7197313b4a08ce61241c69bd7f9a
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Fri, 19 Feb 2021 21:56:53 -0600
pkg.c: replace strcats with snprintfs
Diffstat:
M | pkg.c | | | 88 | ++++++++++++++++++++++++++++++++----------------------------------------------- |
1 file changed, 36 insertions(+), 52 deletions(-)
diff --git a/pkg.c b/pkg.c
@@ -14,41 +14,36 @@
void
pkg_retrieve(char *repo, char *name, struct source_t *sources, int count)
{
- char cachepath[PATH_LEN], repopath[PATH_LEN];
- size_t clen = 0, rlen = 0;
-
- get_cache_dir(cachepath);
- strcat(cachepath, "/sources/");
- clen = strlen(cachepath);
-
- strcpy(repopath, repo);
- strcat(repopath, "/");
- strcat(repopath, name);
- strcat(repopath, "/");
- rlen = strlen(repopath);
+ char cachedir[PATH_LEN], path[PATH_LEN], dup[URL_LEN];
+ int size;
+ get_cache_dir(cachedir);
for (int i = 0; i < count; i++) {
if (sources[i].local) {
- strcat(repopath, sources[i].loc);
- if (access(cachepath, F_OK))
- die("pkg_retrieve: cannot find local source %s for %s",
- sources[i].loc, name);
+ size = snprintf(path, PATH_LEN, "%s/%s/%s", repo, name,
+ sources[i].loc);
+ if (size > PATH_LEN)
+ die("%s: path %s too long", __func__, path);
- repopath[rlen] = '\0';
+ if (access(path, F_OK) != 0)
+ die("%s: cannot find local source %s for %s", __func__,
+ sources[i].loc, name);
} else {
- strcat(cachepath, name);
- strcat(cachepath, "/");
- if (sources[i].path) {
- strcat(cachepath, sources[i].path);
- strcat(cachepath, "/");
- }
-
- mkdirs(cachepath);
- strcat(cachepath, basename(sources[i].loc));
-
- if (access(cachepath, F_OK))
- http_fetch(sources[i].loc, cachepath);
- cachepath[clen] = '\0';
+ strncpy(dup, sources[i].loc, URL_LEN);
+ size = snprintf(path, PATH_LEN, "%s/sources/%s/%s%s%s",
+ cachedir, name,
+ sources[i].path[0] ? sources[i].path : "",
+ sources[i].path[0] ? "/" : "",
+ basename(dup));
+
+ if (size > PATH_LEN)
+ die("%s: path %s too long", __func__, path);
+
+ strncpy(dup, path, PATH_LEN);
+ mkdirs(dirname(dup));
+
+ if (access(path, F_OK) != 0)
+ http_fetch(sources[i].loc, path);
}
}
}
@@ -57,14 +52,12 @@ int
pkg_sources(char *repo, char *name, struct source_t *sources)
{
FILE *sfile;
- char path[strlen(repo) + strlen(name) + 10];
+ char path[PATH_LEN];
char line[512], *p, *end;
int i = 0;
- strcpy(path, repo);
- strcat(path, "/");
- strcat(path, name);
- strcat(path, "/sources");
+ if (snprintf(path, PATH_LEN, "%s/%s/sources", repo, name) > PATH_LEN)
+ die("%s: path %s too long", __func__, path);
if ((sfile = fopen(path, "r")) == NULL)
die("%s: failed to open sources file for %s:", __func__, name);
@@ -117,13 +110,11 @@ pkg_sources(char *repo, char *name, struct source_t *sources)
void
pkg_version(char *repo, char *name, char *ver)
{
- char path[strlen(repo) + strlen(name) + 10];
+ char path[PATH_LEN];
FILE *vfile;
- strcpy(path, repo);
- strcat(path, "/");
- strcat(path, name);
- strcat(path, "/version");
+ if (snprintf(path, PATH_LEN, "%s/%s/version", repo, name) > PATH_LEN)
+ die("%s: path %s too long", __func__, path);
if ((vfile = fopen(path, "r")) == NULL)
die("%s: failed to open version file for %s", __func__, name);
@@ -204,12 +195,8 @@ pkg_exists(const char *repo, const char *name, char *path)
{
DIR *pkg;
- if (strlen(repo) + strlen(name) > PATH_LEN)
- die("%s: repo path length + package name > max path length", __func__);
-
- strcpy(path, repo);
- strcat(path, "/");
- strcat(path, name);
+ if (snprintf(path, PATH_LEN, "%s/%s", repo, name) > PATH_LEN)
+ die("%s: path %s too long", __func__, path);
if ((pkg = opendir(path)) == NULL) {
if (errno == ENOENT) {
@@ -223,7 +210,7 @@ pkg_exists(const char *repo, const char *name, char *path)
return 1;
}
-#define CACHE_DIR "/.cache/kiss"
+#define CACHE_DIR ".cache/kiss"
void
get_cache_dir(char *path)
{
@@ -232,9 +219,6 @@ get_cache_dir(char *path)
if ((home = getenv("HOME")) == NULL)
die("%s: $HOME unset", __func__);
- if (strlen(home) > PATH_LEN - LEN(CACHE_DIR))
- die("%s: $HOME too long", __func__);
-
- strcpy(path, home);
- strcat(path, CACHE_DIR);
+ if (snprintf(path, PATH_LEN, "%s/%s", home, CACHE_DIR) > PATH_LEN)
+ die("%s: path %s too long", __func__, path);
}