Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 6F1116DE035A for ; Fri, 15 Apr 2016 12:30:55 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: -0.557 X-Spam-Level: X-Spam-Status: No, score=-0.557 tagged_above=-999 required=5 tests=[AWL=0.163, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jsX6pIGSbfgP for ; Fri, 15 Apr 2016 12:30:46 -0700 (PDT) Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com [74.125.82.65]) by arlo.cworth.org (Postfix) with ESMTPS id E38866DE0159 for ; Fri, 15 Apr 2016 12:30:40 -0700 (PDT) Received: by mail-wm0-f65.google.com with SMTP id l6so8591801wml.3 for ; Fri, 15 Apr 2016 12:30:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nikula-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=OwZp1jhzj6ccv8nCLEkiKlcpEGmQOwjr0f0aUY8xTt0=; b=0ELWerzEVCyKvP7xDo6V4tlOTwtcXzSrFRtJ5ce5//qwddoPu7EIIYY2FLRrpfZokS rzldAk6aDnWCKDbjcUeRuvigkwssyYgo7EQT6P1CToSLBfZ7m8uINe6pIExXN6OQvUaB 6Z+DE+Jl6M9eDveg2KH+Jzc/zj34zr8As0CZNu0jSs7jBlJxzp69tNWx6903MnB4dnrJ eftOSa7Y519rv0BZ3JZUPiMH7mo+7sFkcCHet89IQi7UousVKVIM0qdcUXVe1K/Em/PO RO7Wmtw11CQBJXcGAAoUo4nGyoMTfXhBAWMB9CH5N1OkFQ6AC1N0Ct97X8r6tXCqUF5S uCDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=OwZp1jhzj6ccv8nCLEkiKlcpEGmQOwjr0f0aUY8xTt0=; b=Al2dUGqdfMbQ7RuoE/IUBLO0/qwxmshRR6kt4sXX0GNTQ4DXNURBVNpygudmKUCl+R WKs1LcSciN/xjh+mUF4AOhr0JkGWM/1bvnWAxXNXFMr6sYH7UATzvwG+wnGMR4EJL2a/ giksRNB6uSC3e0LOSpH7I3wEQnTWlh4ZmtFQteLv6mD8PiIShnkTZjudsrTfjxXmhiIG 7Wp6VVBc1Yv6c4zYbBwfH0GtIaaUUwaV47aoRZMyWvySyH1S804PnRDXE/YLU9xmsQJ+ PcYSb/044NXDFQTJTaxFqNhRNz0XH+8ajBF4qxSHAqJxOl249vW8/AvoIz4Unpkew+ss /Anw== X-Gm-Message-State: AOPr4FWoZBoRLpmYxJB1DW10DTodzwlKv+4WU0QWwMo3KZexgTUIXBUOPUaDF9wUvyLJDA== X-Received: by 10.28.20.198 with SMTP id 189mr6202754wmu.103.1460748639613; Fri, 15 Apr 2016 12:30:39 -0700 (PDT) Received: from localhost (mobile-access-bcee7f-102.dhcp.inet.fi. [188.238.127.102]) by smtp.gmail.com with ESMTPSA id x2sm50403985wjr.33.2016.04.15.12.30.38 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 15 Apr 2016 12:30:39 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [RFC PATCH 3/5] util: add a homebrew scandir implementation Date: Fri, 15 Apr 2016 22:29:17 +0300 Message-Id: <8db6698a2e3ed8378b6a8bcc2fb9935ed27d917d.1460748142.git.jani@nikula.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Apr 2016 19:30:55 -0000 Add support for a filter callback with a context parameter, propagate errors from the filter callback, generate a list of filenames instead of dirents. --- util/Makefile.local | 2 +- util/scandir.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++ util/scandir.h | 11 +++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 util/scandir.c create mode 100644 util/scandir.h diff --git a/util/Makefile.local b/util/Makefile.local index 905f23763468..8893209f320e 100644 --- a/util/Makefile.local +++ b/util/Makefile.local @@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir) libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \ $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \ - $(dir)/util.c + $(dir)/util.c $(dir)/scandir.c libutil_modules := $(libutil_c_srcs:.c=.o) diff --git a/util/scandir.c b/util/scandir.c new file mode 100644 index 000000000000..c69717724235 --- /dev/null +++ b/util/scandir.c @@ -0,0 +1,87 @@ +/* scandir.c - Dedicated scandir implementation. + * + * Copyright (c) 2016 Jani Nikula + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/ . + * + * Author: Jani Nikula + */ + +#include "scandir.h" + +#include +#include +#include +#include + +int scandirx (const char *path, char ***namelist, + int (*filter)(const struct dirent *dirent, void *context), + int (*compar)(const void *a, const void *b), + void *context) +{ + DIR *dir; + struct dirent *d; + char **array = NULL; + int i, count = 0, array_size = 0; + char *d_name; + + dir = opendir (path); + if (!dir) + return -1; + + while ((d = readdir (dir)) != NULL) { + if (filter) { + int selected = filter (d, context); + if (selected < 0) + goto err; + else if (! selected) + continue; + } + + if (count == array_size) { + char **new_array; + + array_size = array_size ? 2 * array_size : 16; + + new_array = realloc (array, array_size * sizeof (*array)); + if (! new_array) + goto err; + + array = new_array; + } + + d_name = strdup (d->d_name); + if (! d_name) + goto err; + + array[count++] = d_name; + } + + closedir (dir); + + if (compar) + qsort (array, count, sizeof (*array), compar); + + *namelist = array; + + return count; + +err: + for (i = 0; i < count; i++) { + free (array[i]); + } + free (array); + + return -1; +} diff --git a/util/scandir.h b/util/scandir.h new file mode 100644 index 000000000000..cc5ed95a8b1b --- /dev/null +++ b/util/scandir.h @@ -0,0 +1,11 @@ +#ifndef _SCANDIR_H +#define _SCANDIR_H + +#include + +int scandirx (const char *path, char ***namelist, + int (*filter)(const struct dirent *dirent, void *context), + int (*compar)(const void *a, const void *b), + void *context); + +#endif /* _SCANDIR_H */ -- 2.1.4