xref: /minix3/external/bsd/pkg_install/dist/lib/str.c (revision a824f5a1008ee67499d167f8c48e64aae26960ca)
1*a824f5a1SJean-Baptiste Boric /*	$NetBSD: str.c,v 1.1.1.2 2009/02/02 20:44:08 joerg Exp $	*/
2*a824f5a1SJean-Baptiste Boric 
3*a824f5a1SJean-Baptiste Boric #if HAVE_CONFIG_H
4*a824f5a1SJean-Baptiste Boric #include "config.h"
5*a824f5a1SJean-Baptiste Boric #endif
6*a824f5a1SJean-Baptiste Boric #include <nbcompat.h>
7*a824f5a1SJean-Baptiste Boric #if HAVE_SYS_CDEFS_H
8*a824f5a1SJean-Baptiste Boric #include <sys/cdefs.h>
9*a824f5a1SJean-Baptiste Boric #endif
10*a824f5a1SJean-Baptiste Boric __RCSID("$NetBSD: str.c,v 1.1.1.2 2009/02/02 20:44:08 joerg Exp $");
11*a824f5a1SJean-Baptiste Boric 
12*a824f5a1SJean-Baptiste Boric /*
13*a824f5a1SJean-Baptiste Boric  * FreeBSD install - a package for the installation and maintainance
14*a824f5a1SJean-Baptiste Boric  * of non-core utilities.
15*a824f5a1SJean-Baptiste Boric  *
16*a824f5a1SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
17*a824f5a1SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
18*a824f5a1SJean-Baptiste Boric  * are met:
19*a824f5a1SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
20*a824f5a1SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
21*a824f5a1SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
22*a824f5a1SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
23*a824f5a1SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
24*a824f5a1SJean-Baptiste Boric  *
25*a824f5a1SJean-Baptiste Boric  * Jordan K. Hubbard
26*a824f5a1SJean-Baptiste Boric  * 18 July 1993
27*a824f5a1SJean-Baptiste Boric  *
28*a824f5a1SJean-Baptiste Boric  * Miscellaneous string utilities.
29*a824f5a1SJean-Baptiste Boric  *
30*a824f5a1SJean-Baptiste Boric  */
31*a824f5a1SJean-Baptiste Boric 
32*a824f5a1SJean-Baptiste Boric #if HAVE_ASSERT_H
33*a824f5a1SJean-Baptiste Boric #include <assert.h>
34*a824f5a1SJean-Baptiste Boric #endif
35*a824f5a1SJean-Baptiste Boric #if HAVE_ERR_H
36*a824f5a1SJean-Baptiste Boric #include <err.h>
37*a824f5a1SJean-Baptiste Boric #endif
38*a824f5a1SJean-Baptiste Boric #if HAVE_FNMATCH_H
39*a824f5a1SJean-Baptiste Boric #include <fnmatch.h>
40*a824f5a1SJean-Baptiste Boric #endif
41*a824f5a1SJean-Baptiste Boric #include "lib.h"
42*a824f5a1SJean-Baptiste Boric #include "dewey.h"
43*a824f5a1SJean-Baptiste Boric 
44*a824f5a1SJean-Baptiste Boric /* pull in definitions and macros for resizing arrays as we go */
45*a824f5a1SJean-Baptiste Boric #include "defs.h"
46*a824f5a1SJean-Baptiste Boric 
47*a824f5a1SJean-Baptiste Boric /*
48*a824f5a1SJean-Baptiste Boric  * Return the suffix portion of a path
49*a824f5a1SJean-Baptiste Boric  */
50*a824f5a1SJean-Baptiste Boric const char *
suffix_of(const char * str)51*a824f5a1SJean-Baptiste Boric suffix_of(const char *str)
52*a824f5a1SJean-Baptiste Boric {
53*a824f5a1SJean-Baptiste Boric 	const char *dot;
54*a824f5a1SJean-Baptiste Boric 
55*a824f5a1SJean-Baptiste Boric 	return ((dot = strrchr(basename_of(str), '.')) == NULL) ? "" : dot + 1;
56*a824f5a1SJean-Baptiste Boric }
57*a824f5a1SJean-Baptiste Boric 
58*a824f5a1SJean-Baptiste Boric /*
59*a824f5a1SJean-Baptiste Boric  * Return the filename portion of a path
60*a824f5a1SJean-Baptiste Boric  */
61*a824f5a1SJean-Baptiste Boric const char *
basename_of(const char * str)62*a824f5a1SJean-Baptiste Boric basename_of(const char *str)
63*a824f5a1SJean-Baptiste Boric {
64*a824f5a1SJean-Baptiste Boric 	const char *slash;
65*a824f5a1SJean-Baptiste Boric 
66*a824f5a1SJean-Baptiste Boric 	return ((slash = strrchr(str, '/')) == NULL) ? str : slash + 1;
67*a824f5a1SJean-Baptiste Boric }
68*a824f5a1SJean-Baptiste Boric 
69*a824f5a1SJean-Baptiste Boric /*
70*a824f5a1SJean-Baptiste Boric  * Return the dirname portion of a path
71*a824f5a1SJean-Baptiste Boric  */
72*a824f5a1SJean-Baptiste Boric const char *
dirname_of(const char * path)73*a824f5a1SJean-Baptiste Boric dirname_of(const char *path)
74*a824f5a1SJean-Baptiste Boric {
75*a824f5a1SJean-Baptiste Boric 	size_t  cc;
76*a824f5a1SJean-Baptiste Boric 	char   *s;
77*a824f5a1SJean-Baptiste Boric 	static char buf[MaxPathSize];
78*a824f5a1SJean-Baptiste Boric 
79*a824f5a1SJean-Baptiste Boric 	if ((s = strrchr(path, '/')) == NULL) {
80*a824f5a1SJean-Baptiste Boric 		return ".";
81*a824f5a1SJean-Baptiste Boric 	}
82*a824f5a1SJean-Baptiste Boric 	if (s == path) {
83*a824f5a1SJean-Baptiste Boric 		/* "/foo" -> return "/" */
84*a824f5a1SJean-Baptiste Boric 		return "/";
85*a824f5a1SJean-Baptiste Boric 	}
86*a824f5a1SJean-Baptiste Boric 	cc = (size_t) (s - path);
87*a824f5a1SJean-Baptiste Boric 	if (cc >= sizeof(buf))
88*a824f5a1SJean-Baptiste Boric 		errx(EXIT_FAILURE, "dirname_of: too long dirname: '%s'", path);
89*a824f5a1SJean-Baptiste Boric 	(void) memcpy(buf, path, cc);
90*a824f5a1SJean-Baptiste Boric 	buf[cc] = 0;
91*a824f5a1SJean-Baptiste Boric 	return buf;
92*a824f5a1SJean-Baptiste Boric }
93*a824f5a1SJean-Baptiste Boric 
94*a824f5a1SJean-Baptiste Boric /*
95*a824f5a1SJean-Baptiste Boric  * Does the pkgname contain any of the special chars ("{[]?*<>")?
96*a824f5a1SJean-Baptiste Boric  * If so, return 1, else 0
97*a824f5a1SJean-Baptiste Boric  */
98*a824f5a1SJean-Baptiste Boric int
ispkgpattern(const char * pkg)99*a824f5a1SJean-Baptiste Boric ispkgpattern(const char *pkg)
100*a824f5a1SJean-Baptiste Boric {
101*a824f5a1SJean-Baptiste Boric 	return strpbrk(pkg, "<>[]?*{") != NULL;
102*a824f5a1SJean-Baptiste Boric }
103