3 Copyright (C) 2005, 2007 Red Hat, Inc. All rights reserved.
4 This copyrighted material is made available to anyone wishing to use, modify,
5 copy, or redistribute it subject to the terms and conditions of the GNU General
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10 PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License along with
13 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
14 Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 Author: Miloslav Trmac <mitr@redhat.com>
18 plocate modifications: Copyright (C) 2020 Steinar H. Gunderson.
19 plocate parts and modifications are licensed under the GPLv2 or, at your option,
26 #include <arpa/inet.h>
39 /* Compare two path names using the database directory order. This is not
40 exactly strcmp () order: "a" < "a.b", so "a/z" < "a.b". */
41 int dir_path_cmp(const string &a, const string &b)
43 auto [ai, bi] = mismatch(a.begin(), a.end(), b.begin(), b.end());
44 if (ai == a.end() && bi == b.end()) {
62 return int((unsigned char)*ai) - int((unsigned char)*bi);
65 /* Sort LIST using dir_path_cmp () */
66 void string_list_dir_path_sort(vector<string> *list)
68 sort(list->begin(), list->end(), [](const string &a, const string &b) {
69 return dir_path_cmp(a, b) < 0;
73 /* Is PATH included in LIST? Update *IDX to move within LIST.
75 LIST is assumed to be sorted using dir_path_cmp (), successive calls to this
76 function are assumed to use PATH values increasing in dir_path_cmp (). */
77 bool string_list_contains_dir_path(const vector<string> *list, size_t *idx,
81 while (*idx < list->size() && (cmp = dir_path_cmp((*list)[*idx], path)) < 0) {
84 if (*idx < list->size() && cmp == 0) {