recognize new archive, audio, image formats; give audio files a separate color
[platform/upstream/coreutils.git] / lib / fdopendir-glibc.c
1 /* fdopendir implementation derived from glibc.
2
3    Copyright (C) 2006 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdbool.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30
31 #if _LIBC
32 # include <dirstream.h>
33 # include <not-cancel.h>
34
35 #else
36
37 # if __GNUC__ < 3
38 #  define __builtin_expect(expr, expected_val) expr
39 # endif
40
41 # include "openat.h"
42 # define stat64 stat
43 # define dirent64 dirent
44 # define __fxstat64(V, fd, sb) fstat(fd, sb)
45 # define __fcntl fcntl
46 # define __set_errno(Val) do { errno = (Val); } while (0)
47 # define __libc_lock_init(NAME) ((NAME) = 0, 0)
48 # define close_not_cancel_no_status(fd) close (fd)
49 # ifdef __i386__
50 #  define internal_function __attribute ((regparm (3), stdcall))
51 # else
52 #  define internal_function
53 # endif
54
55 struct __dirstream
56   {
57     int fd;                     /* File descriptor.  */
58
59     char *data;                 /* Directory block.  */
60     size_t allocation;          /* Space allocated for the block.  */
61     size_t size;                /* Total valid data in the block.  */
62     size_t offset;              /* Current offset into the block.  */
63
64     off_t filepos;              /* Position of next entry to read.  */
65     int lock;
66   };
67 #endif
68
69 #undef _STATBUF_ST_BLKSIZE
70
71 static DIR *
72 internal_function
73 __alloc_dir (int fd, bool close_fd)
74 {
75   if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
76     goto lose;
77
78   size_t allocation;
79 #ifdef _STATBUF_ST_BLKSIZE
80   if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
81                         1))
82     allocation = statp->st_blksize;
83   else
84 #endif
85     allocation = (BUFSIZ < sizeof (struct dirent64)
86                   ? sizeof (struct dirent64) : BUFSIZ);
87
88   const int pad = -sizeof (DIR) % __alignof__ (struct dirent64);
89
90   DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad);
91   if (dirp == NULL)
92   lose:
93     {
94       if (close_fd)
95         {
96           int save_errno = errno;
97           close_not_cancel_no_status (fd);
98           __set_errno (save_errno);
99         }
100       return NULL;
101     }
102   memset (dirp, '\0', sizeof (DIR));
103   dirp->data = (char *) (dirp + 1) + pad;
104   dirp->allocation = allocation;
105   dirp->fd = fd;
106
107   __libc_lock_init (dirp->lock);
108
109   return dirp;
110 }
111
112 DIR *
113 fdopendir (int fd)
114 {
115 #if 0
116   struct stat64 statbuf;
117
118   if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
119     return NULL;
120   if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
121     {
122       __set_errno (ENOTDIR);
123       return NULL;
124     }
125   /* Make sure the descriptor allows for reading.  */
126   int flags = __fcntl (fd, F_GETFL);
127   if (__builtin_expect (flags == -1, 0))
128     return NULL;
129   if (__builtin_expect ((flags & O_ACCMODE) == O_WRONLY, 0))
130     {
131       __set_errno (EINVAL);
132       return NULL;
133     }
134 #endif
135
136   return __alloc_dir (fd, false);
137 }