(file_type): New file/function. Extracted from diffutils.
[gnulib.git] / lib / file-type.c
1 char const *
2 file_type (struct stat const *st)
3 {
4   /* See POSIX 1003.1-2001 for these formats.
5
6      To keep diagnostics grammatical in English, the returned string
7      must start with a consonant.  */
8
9   if (S_ISREG (st->st_mode))
10     return st->st_size == 0 ? _("regular empty file") : _("regular file");
11
12   if (S_ISDIR (st->st_mode))
13     return _("directory");
14
15   if (S_ISBLK (st->st_mode))
16     return _("block special file");
17
18   if (S_ISCHR (st->st_mode))
19     return _("character special file");
20
21   if (S_ISFIFO (st->st_mode))
22     return _("fifo");
23
24   if (S_ISLNK (st->st_mode))
25     return _("symbolic link");
26
27   if (S_ISSOCK (st->st_mode))
28     return _("socket");
29
30   if (S_TYPEISMQ (st))
31     return _("message queue");
32
33   if (S_TYPEISSEM (st))
34     return _("semaphore");
35
36   if (S_TYPEISSHM (st))
37     return _("shared memory object");
38
39   if (S_TYPEISTMO (st))
40     return _("typed memory object");
41
42   return _("weird file");
43 }