]> Savannah Git Hosting - gnulib.git/commitdiff
c-file-type: new module
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Aug 2023 16:54:08 +0000 (09:54 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Aug 2023 16:56:53 +0000 (09:56 -0700)
* 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.

ChangeLog
lib/c-file-type.c [new file with mode: 0644]
lib/file-type.c
lib/file-type.h
modules/c-file-type [new file with mode: 0644]
modules/file-type

index 31aec050a8acb24b9ecbb739e7d7d5aa16aba2c4..d407d7b4c6c1105e283c62300140409ac827a394 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2023-08-12  Paul Eggert  <eggert@cs.ucla.edu>
+
+       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  <bruno@clisp.org>
 
        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 (file)
index 0000000..efc17c5
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert.  */
+
+#include <config.h>
+
+#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
+     <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html#tagtcjh_27>.
+
+     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");
+}
index 359b4b8ebd1eca2406d2d1a9a7d3ed9fbaaba03b..b4a2bdb915410603b1e5ce0e80e53fe48f4ee93a 100644 (file)
 #include "file-type.h"
 
 #include <gettext.h>
-#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));
 }
index 2c5a8e9afd63c7690227509492c1e8aeb46fd0b5..5b1e447cb9cd5ca5f6e76b08050b18a64e20820c 100644 (file)
@@ -29,6 +29,7 @@
 # include <sys/types.h>
 # include <sys/stat.h>
 
+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 (file)
index 0000000..2abc601
--- /dev/null
@@ -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
index 47150aeecac18bc6368213c25d755e5b2733cc44..c2bc9f59ba48f682623f854bde28b817865a5897 100644 (file)
@@ -6,6 +6,7 @@ lib/file-type.h
 lib/file-type.c
 
 Depends-on:
+c-file-type
 gettext-h
 sys_stat