pkgtools

morpheus pkg tools
git clone git://git.2f30.org/pkgtools
Log | Files | Refs | README | LICENSE

common.c (1742B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "pkg.h"
      3 
      4 /* Extract the package name from a filename.  e.g. /tmp/pkg#version.pkg.tgz */
      5 void
      6 parse_name(const char *path, char **name)
      7 {
      8 	char tmp[PATH_MAX], filename[PATH_MAX], *p;
      9 
     10 	estrlcpy(tmp, path, sizeof(tmp));
     11 	estrlcpy(filename, basename(tmp), sizeof(filename));
     12 	/* strip extension */
     13 	p = strrchr(filename, '.');
     14 	if (!p)
     15 		goto err;
     16 	*p = '\0';
     17 	p = strrchr(filename, '.');
     18 	if (!p)
     19 		goto err;
     20 	*p = '\0';
     21 	/* extract name */
     22 	p = strchr(filename, '#');
     23 	if (p)
     24 		*p = '\0';
     25 	if (filename[0] == '\0')
     26 		goto err;
     27 	*name = estrdup(filename);
     28 	return;
     29 err:
     30 	eprintf("%s: invalid package filename\n",
     31 		path);
     32 }
     33 
     34 /* Extract the package version from a filename.  e.g. /tmp/pkg#version.pkg.tgz */
     35 void
     36 parse_version(const char *path, char **version)
     37 {
     38 	char tmp[PATH_MAX], filename[PATH_MAX], *p;
     39 
     40 	estrlcpy(tmp, path, sizeof(tmp));
     41 	estrlcpy(filename, basename(tmp), sizeof(filename));
     42 	/* strip extension */
     43 	p = strrchr(filename, '.');
     44 	if (!p)
     45 		goto err;
     46 	*p = '\0';
     47 	p = strrchr(filename, '.');
     48 	if (!p)
     49 		goto err;
     50 	*p = '\0';
     51 	/* extract version */
     52 	p = strchr(filename, '#');
     53 	if (!p) {
     54 		*version = NULL;
     55 		return;
     56 	}
     57 	p++;
     58 	if (*p == '\0')
     59 		goto err;
     60 	*version = estrdup(p);
     61 	return ;
     62 err:
     63 	eprintf("%s: invalid package filename\n",
     64 		path);
     65 }
     66 
     67 void
     68 parse_db_name(const char *path, char **name)
     69 {
     70 	char tmp[PATH_MAX], *p;
     71 
     72 	estrlcpy(tmp, path, sizeof(tmp));
     73 	p = strchr(tmp, '#');
     74 	if (p)
     75 		*p = '\0';
     76 	*name = estrdup(tmp);
     77 }
     78 
     79 void
     80 parse_db_version(const char *path, char **version)
     81 {
     82 	char tmp[PATH_MAX], *p;
     83 
     84 	estrlcpy(tmp, path, sizeof(tmp));
     85 	p = strchr(tmp, '#');
     86 	if (p)
     87 		*p = '\0';
     88 	if (p)
     89 		*version = estrdup(p + 1);
     90 	else
     91 		*version = NULL;
     92 }