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