@@ -1221,31 +1221,52 @@ static inline int chomp_trailing_dir_sep(const char *path, int len)
12211221}
12221222
12231223/*
1224- * If path ends with suffix (complete path components), returns the
1225- * part before suffix (sans trailing directory separators).
1226- * Otherwise returns NULL .
1224+ * If path ends with suffix (complete path components), returns the offset of
1225+ * the last character in the path before the suffix (sans trailing directory
1226+ * separators), and -1 otherwise .
12271227 */
1228- char * strip_path_suffix (const char * path , const char * suffix )
1228+ static ssize_t stripped_path_suffix_offset (const char * path , const char * suffix )
12291229{
12301230 int path_len = strlen (path ), suffix_len = strlen (suffix );
12311231
12321232 while (suffix_len ) {
12331233 if (!path_len )
1234- return NULL ;
1234+ return -1 ;
12351235
12361236 if (is_dir_sep (path [path_len - 1 ])) {
12371237 if (!is_dir_sep (suffix [suffix_len - 1 ]))
1238- return NULL ;
1238+ return -1 ;
12391239 path_len = chomp_trailing_dir_sep (path , path_len );
12401240 suffix_len = chomp_trailing_dir_sep (suffix , suffix_len );
12411241 }
12421242 else if (path [-- path_len ] != suffix [-- suffix_len ])
1243- return NULL ;
1243+ return -1 ;
12441244 }
12451245
12461246 if (path_len && !is_dir_sep (path [path_len - 1 ]))
1247- return NULL ;
1248- return xstrndup (path , chomp_trailing_dir_sep (path , path_len ));
1247+ return -1 ;
1248+ return chomp_trailing_dir_sep (path , path_len );
1249+ }
1250+
1251+ /*
1252+ * Returns true if the path ends with components, considering only complete path
1253+ * components, and false otherwise.
1254+ */
1255+ int ends_with_path_components (const char * path , const char * components )
1256+ {
1257+ return stripped_path_suffix_offset (path , components ) != -1 ;
1258+ }
1259+
1260+ /*
1261+ * If path ends with suffix (complete path components), returns the
1262+ * part before suffix (sans trailing directory separators).
1263+ * Otherwise returns NULL.
1264+ */
1265+ char * strip_path_suffix (const char * path , const char * suffix )
1266+ {
1267+ ssize_t offset = stripped_path_suffix_offset (path , suffix );
1268+
1269+ return offset == -1 ? NULL : xstrndup (path , offset );
12491270}
12501271
12511272int daemon_avoid_alias (const char * p )
0 commit comments