commit 7c35cd356126506cd73be907b968dc377cea8ac3
parent e1a2b450a0386ad48a93612a5b76bed790d45bec
Author: sin <sin@2f30.org>
Date: Wed, 2 Jul 2014 00:12:31 +0100
Factor out parsing of pkg db name and version
Diffstat:
3 files changed, 36 insertions(+), 9 deletions(-)
diff --git a/common.c b/common.c
@@ -63,3 +63,30 @@ err:
eprintf("%s: invalid package filename\n",
path);
}
+
+void
+parse_db_name(const char *path, char **name)
+{
+ char tmp[PATH_MAX], *p;
+
+ estrlcpy(tmp, path, sizeof(tmp));
+ p = strchr(tmp, '#');
+ if (p)
+ *p = '\0';
+ *name = estrdup(tmp);
+}
+
+void
+parse_db_version(const char *path, char **version)
+{
+ char tmp[PATH_MAX], *p;
+
+ estrlcpy(tmp, path, sizeof(tmp));
+ p = strchr(tmp, '#');
+ if (p)
+ *p = '\0';
+ if (p)
+ *version = estrdup(p + 1);
+ else
+ *version = NULL;
+}
diff --git a/pkg.c b/pkg.c
@@ -5,21 +5,17 @@
struct pkg *
pkg_load(struct db *db, const char *file)
{
- char path[PATH_MAX], tmp[PATH_MAX], *p;
- char *name, *version;
struct pkg *pkg;
struct pkgentry *pe;
FILE *fp;
+ char path[PATH_MAX];
+ char *name, *version;
char *buf = NULL;
size_t sz = 0;
ssize_t len;
- estrlcpy(tmp, file, sizeof(tmp));
- p = strchr(tmp, '#');
- if (p)
- *p = '\0';
- name = tmp;
- version = p ? p + 1 : NULL;
+ parse_db_name(file, &name);
+ parse_db_version(file, &version);
estrlcpy(path, db->path, sizeof(path));
estrlcat(path, "/", sizeof(path));
@@ -30,6 +26,8 @@ pkg_load(struct db *db, const char *file)
}
pkg = pkg_new(path, name, version);
+ free(name);
+ free(version);
if (!(fp = fopen(pkg->path, "r"))) {
weprintf("fopen %s:", pkg->path);
diff --git a/pkg.h b/pkg.h
@@ -58,8 +58,10 @@ extern int vflag;
extern char *argv0;
/* common.c */
-void parse_version(const char *, char **);
+void parse_db_name(const char *, char **);
+void parse_db_version(const char *, char **);
void parse_name(const char *, char **);
+void parse_version(const char *, char **);
/* db.c */
struct db *db_new(const char *);