Debian Codegraph @

lib/c-file-type.c

raw
1 /* Return a string describing the type of a file.
2
3 Copyright (C) 1993-1994, 2001-2002, 2004-2006, 2009-2025 Free Software
4 Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 /* Written by Paul Eggert. */
20
21 #include <config.h>
22
23 #include "file-type.h"
24
25 #define N_(msgid) (msgid)
26
27 char const *
28 c_file_type (struct stat const *st)
29 {
30 /* For some of these formats, see POSIX 1003.1-2017 "file" command,
31 STDOUT section, Table: File Utility Output Strings
32 <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html#tagtcjh_27>.
33
34 To keep diagnostics grammatical in English, the returned string
35 must start with a consonant. */
36
37 /* Do these three first, as they're the most common. */
38
39 if (S_ISREG (st->st_mode))
40 return st->st_size == 0 ? N_("regular empty file") : N_("regular file");
41
42 if (S_ISDIR (st->st_mode))
43 return N_("directory");
44
45 if (S_ISLNK (st->st_mode))
46 return N_("symbolic link");
47
48 /* Do the S_TYPEIS* macros next, as they may be implemented in terms
49 of S_ISNAM, and we want the more-specialized interpretation. */
50
51 if (S_TYPEISMQ (st))
52 return N_("message queue");
53
54 if (S_TYPEISSEM (st))
55 return N_("semaphore");
56
57 if (S_TYPEISSHM (st))
58 return N_("shared memory object");
59
60 if (S_TYPEISTMO (st))
61 return N_("typed memory object");
62
63 /* The remaining are in alphabetical order. */
64
65 if (S_ISBLK (st->st_mode))
66 return N_("block special file");
67
68 if (S_ISCHR (st->st_mode))
69 return N_("character special file");
70
71 if (S_ISCTG (st->st_mode))
72 return N_("contiguous data");
73
74 if (S_ISFIFO (st->st_mode))
75 return N_("fifo");
76
77 if (S_ISDOOR (st->st_mode))
78 return N_("door");
79
80 if (S_ISMPB (st->st_mode))
81 return N_("multiplexed block special file");
82
83 if (S_ISMPC (st->st_mode))
84 return N_("multiplexed character special file");
85
86 if (S_ISMPX (st->st_mode))
87 return N_("multiplexed file");
88
89 if (S_ISNAM (st->st_mode))
90 return N_("named file");
91
92 if (S_ISNWK (st->st_mode))
93 return N_("network special file");
94
95 if (S_ISOFD (st->st_mode))
96 return N_("migrated file with data");
97
98 if (S_ISOFL (st->st_mode))
99 return N_("migrated file without data");
100
101 if (S_ISPORT (st->st_mode))
102 return N_("port");
103
104 if (S_ISSOCK (st->st_mode))
105 return N_("socket");
106
107 if (S_ISWHT (st->st_mode))
108 return N_("whiteout");
109
110 return N_("weird file");
111 }