From: Paul Eggert Date: Sat, 12 Aug 2023 16:54:08 +0000 (-0700) Subject: c-file-type: new module X-Git-Tag: v1.0~926 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=46decc1c8c159527f82cc2fb69715802f8c45a42;p=gnulib.git c-file-type: new module * lib/c-file-type.c: New file, containing all of the old file-type except for the gettext call. * lib/file-type.c (_): Remove; no longer used. (file_type): Call c_file_type for most of the work. * modules/c-file-type: New module. * modules/file-type (Depends-on): Add c-file-type. --- diff --git a/ChangeLog b/ChangeLog index 31aec050a8..d407d7b4c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2023-08-12 Paul Eggert + + c-file-type: new module + * lib/c-file-type.c: New file, containing all of the + old file-type except for the gettext call. + * lib/file-type.c (_): Remove; no longer used. + (file_type): Call c_file_type for most of the work. + * modules/c-file-type: New module. + * modules/file-type (Depends-on): Add c-file-type. + 2023-08-12 Bruno Haible readutmp, boot-time: Use the BSD sysctl as fallback. diff --git a/lib/c-file-type.c b/lib/c-file-type.c new file mode 100644 index 0000000000..efc17c5ccc --- /dev/null +++ b/lib/c-file-type.c @@ -0,0 +1,111 @@ +/* Return a string describing the type of a file. + + Copyright (C) 1993-1994, 2001-2002, 2004-2006, 2009-2023 Free Software + Foundation, Inc. + + 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 . */ + +/* Written by Paul Eggert. */ + +#include + +#include "file-type.h" + +#define N_(msgid) (msgid) + +char const * +c_file_type (struct stat const *st) +{ + /* For some of these formats, see POSIX 1003.1-2017 "file" command, + STDOUT section, Table: File Utility Output Strings + . + + To keep diagnostics grammatical in English, the returned string + must start with a consonant. */ + + /* Do these three first, as they're the most common. */ + + if (S_ISREG (st->st_mode)) + return st->st_size == 0 ? N_("regular empty file") : N_("regular file"); + + if (S_ISDIR (st->st_mode)) + return N_("directory"); + + if (S_ISLNK (st->st_mode)) + return N_("symbolic link"); + + /* Do the S_TYPEIS* macros next, as they may be implemented in terms + of S_ISNAM, and we want the more-specialized interpretation. */ + + if (S_TYPEISMQ (st)) + return N_("message queue"); + + if (S_TYPEISSEM (st)) + return N_("semaphore"); + + if (S_TYPEISSHM (st)) + return N_("shared memory object"); + + if (S_TYPEISTMO (st)) + return N_("typed memory object"); + + /* The remaining are in alphabetical order. */ + + if (S_ISBLK (st->st_mode)) + return N_("block special file"); + + if (S_ISCHR (st->st_mode)) + return N_("character special file"); + + if (S_ISCTG (st->st_mode)) + return N_("contiguous data"); + + if (S_ISFIFO (st->st_mode)) + return N_("fifo"); + + if (S_ISDOOR (st->st_mode)) + return N_("door"); + + if (S_ISMPB (st->st_mode)) + return N_("multiplexed block special file"); + + if (S_ISMPC (st->st_mode)) + return N_("multiplexed character special file"); + + if (S_ISMPX (st->st_mode)) + return N_("multiplexed file"); + + if (S_ISNAM (st->st_mode)) + return N_("named file"); + + if (S_ISNWK (st->st_mode)) + return N_("network special file"); + + if (S_ISOFD (st->st_mode)) + return N_("migrated file with data"); + + if (S_ISOFL (st->st_mode)) + return N_("migrated file without data"); + + if (S_ISPORT (st->st_mode)) + return N_("port"); + + if (S_ISSOCK (st->st_mode)) + return N_("socket"); + + if (S_ISWHT (st->st_mode)) + return N_("whiteout"); + + return N_("weird file"); +} diff --git a/lib/file-type.c b/lib/file-type.c index 359b4b8ebd..b4a2bdb915 100644 --- a/lib/file-type.c +++ b/lib/file-type.c @@ -23,89 +23,9 @@ #include "file-type.h" #include -#define _(text) gettext (text) char const * file_type (struct stat const *st) { - /* See POSIX 1003.1-2001 XCU Table 4-8 lines 17093-17107 for some of - these formats. - - To keep diagnostics grammatical in English, the returned string - must start with a consonant. */ - - /* Do these three first, as they're the most common. */ - - if (S_ISREG (st->st_mode)) - return st->st_size == 0 ? _("regular empty file") : _("regular file"); - - if (S_ISDIR (st->st_mode)) - return _("directory"); - - if (S_ISLNK (st->st_mode)) - return _("symbolic link"); - - /* Do the S_TYPEIS* macros next, as they may be implemented in terms - of S_ISNAM, and we want the more-specialized interpretation. */ - - if (S_TYPEISMQ (st)) - return _("message queue"); - - if (S_TYPEISSEM (st)) - return _("semaphore"); - - if (S_TYPEISSHM (st)) - return _("shared memory object"); - - if (S_TYPEISTMO (st)) - return _("typed memory object"); - - /* The remaining are in alphabetical order. */ - - if (S_ISBLK (st->st_mode)) - return _("block special file"); - - if (S_ISCHR (st->st_mode)) - return _("character special file"); - - if (S_ISCTG (st->st_mode)) - return _("contiguous data"); - - if (S_ISFIFO (st->st_mode)) - return _("fifo"); - - if (S_ISDOOR (st->st_mode)) - return _("door"); - - if (S_ISMPB (st->st_mode)) - return _("multiplexed block special file"); - - if (S_ISMPC (st->st_mode)) - return _("multiplexed character special file"); - - if (S_ISMPX (st->st_mode)) - return _("multiplexed file"); - - if (S_ISNAM (st->st_mode)) - return _("named file"); - - if (S_ISNWK (st->st_mode)) - return _("network special file"); - - if (S_ISOFD (st->st_mode)) - return _("migrated file with data"); - - if (S_ISOFL (st->st_mode)) - return _("migrated file without data"); - - if (S_ISPORT (st->st_mode)) - return _("port"); - - if (S_ISSOCK (st->st_mode)) - return _("socket"); - - if (S_ISWHT (st->st_mode)) - return _("whiteout"); - - return _("weird file"); + return gettext (c_file_type (st)); } diff --git a/lib/file-type.h b/lib/file-type.h index 2c5a8e9afd..5b1e447cb9 100644 --- a/lib/file-type.h +++ b/lib/file-type.h @@ -29,6 +29,7 @@ # include # include +char const *c_file_type (struct stat const *) _GL_ATTRIBUTE_PURE; char const *file_type (struct stat const *) _GL_ATTRIBUTE_PURE; #endif /* FILE_TYPE_H */ diff --git a/modules/c-file-type b/modules/c-file-type new file mode 100644 index 0000000000..2abc601ed8 --- /dev/null +++ b/modules/c-file-type @@ -0,0 +1,23 @@ +Description: +Return a string describing the type of a file. + +Files: +lib/file-type.h +lib/c-file-type.c + +Depends-on: +sys_stat + +configure.ac: + +Makefile.am: +lib_SOURCES += c-file-type.c + +Include: +"file-type.h" + +License: +GPL + +Maintainer: +Paul Eggert, Jim Meyering diff --git a/modules/file-type b/modules/file-type index 47150aeeca..c2bc9f59ba 100644 --- a/modules/file-type +++ b/modules/file-type @@ -6,6 +6,7 @@ lib/file-type.h lib/file-type.c Depends-on: +c-file-type gettext-h sys_stat