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