Rev 4874 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4874 | Rev 4921 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 1990, 2007 The Regents of the University of California. |
2 | * Copyright (c) 1990, 2007 The Regents of the University of California. |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms are permitted |
5 | * Redistribution and use in source and binary forms are permitted |
6 | * provided that the above copyright notice and this paragraph are |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, |
7 | * duplicated in all such forms and that any documentation, |
8 | * advertising materials, and other materials related to such |
8 | * advertising materials, and other materials related to such |
9 | * distribution and use acknowledge that the software was developed |
9 | * distribution and use acknowledge that the software was developed |
10 | * by the University of California, Berkeley. The name of the |
10 | * by the University of California, Berkeley. The name of the |
11 | * University may not be used to endorse or promote products derived |
11 | * University may not be used to endorse or promote products derived |
12 | * from this software without specific prior written permission. |
12 | * from this software without specific prior written permission. |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
16 | */ |
16 | */ |
17 | /* No user fns here. Pesch 15apr92. */ |
17 | /* No user fns here. Pesch 15apr92. */ |
18 | 18 | ||
19 | #include <_ansi.h> |
19 | #include <_ansi.h> |
20 | #include |
20 | #include |
21 | #include |
21 | #include |
22 | #include |
22 | #include |
23 | #include |
23 | #include |
24 | #include |
24 | #include |
25 | #include "local.h" |
25 | #include "local.h" |
26 | 26 | ||
27 | #define _DEFAULT_ASPRINTF_BUFSIZE 64 |
27 | #define _DEFAULT_ASPRINTF_BUFSIZE 64 |
28 | 28 | ||
29 | /* |
29 | /* |
30 | * Allocate a file buffer, or switch to unbuffered I/O. |
30 | * Allocate a file buffer, or switch to unbuffered I/O. |
31 | * Per the ANSI C standard, ALL tty devices default to line buffered. |
31 | * Per the ANSI C standard, ALL tty devices default to line buffered. |
32 | * |
32 | * |
33 | * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek |
33 | * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek |
34 | * optimization) right after the _fstat() that finds the buffer size. |
34 | * optimization) right after the _fstat() that finds the buffer size. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | _VOID |
37 | _VOID |
38 | _DEFUN(__smakebuf_r, (ptr, fp), |
38 | _DEFUN(__smakebuf_r, (ptr, fp), |
39 | struct _reent *ptr _AND |
39 | struct _reent *ptr _AND |
40 | register FILE *fp) |
40 | register FILE *fp) |
41 | { |
41 | { |
42 | register size_t size, couldbetty; |
42 | register size_t size, couldbetty; |
43 | register _PTR p; |
43 | register _PTR p; |
44 | #ifdef __USE_INTERNAL_STAT64 |
44 | #ifdef __USE_INTERNAL_STAT64 |
45 | struct stat64 st; |
45 | struct stat64 st; |
46 | #else |
46 | #else |
47 | struct stat st; |
47 | struct stat st; |
48 | #endif |
48 | #endif |
49 | 49 | ||
50 | if (fp->_flags & __SNBF) |
50 | if (fp->_flags & __SNBF) |
51 | { |
51 | { |
52 | fp->_bf._base = fp->_p = fp->_nbuf; |
52 | fp->_bf._base = fp->_p = fp->_nbuf; |
53 | fp->_bf._size = 1; |
53 | fp->_bf._size = 1; |
54 | return; |
54 | return; |
55 | } |
55 | } |
56 | #ifdef __USE_INTERNAL_STAT64 |
56 | #ifdef __USE_INTERNAL_STAT64 |
57 | if (fp->_file < 0 || _fstat64_r (ptr, fp->_file, &st) < 0) |
57 | if (fp->_file < 0 || _fstat64_r (ptr, fp->_file, &st) < 0) |
58 | #else |
58 | #else |
59 | if (fp->_file < 0 || _fstat_r (ptr, fp->_file, &st) < 0) |
59 | if (fp->_file < 0 || _fstat_r (ptr, fp->_file, &st) < 0) |
60 | #endif |
60 | #endif |
61 | { |
61 | { |
62 | couldbetty = 0; |
62 | couldbetty = 0; |
63 | /* Check if we are be called by asprintf family for initial buffer. */ |
63 | /* Check if we are be called by asprintf family for initial buffer. */ |
64 | if (fp->_flags & __SMBF) |
64 | if (fp->_flags & __SMBF) |
65 | size = _DEFAULT_ASPRINTF_BUFSIZE; |
65 | size = _DEFAULT_ASPRINTF_BUFSIZE; |
66 | else |
66 | else |
67 | size = BUFSIZ; |
67 | size = BUFSIZ; |
- | 68 | #ifdef _FSEEK_OPTIMIZATION |
|
68 | /* do not try to optimise fseek() */ |
69 | /* do not try to optimise fseek() */ |
69 | fp->_flags |= __SNPT; |
70 | fp->_flags |= __SNPT; |
- | 71 | #endif |
|
70 | } |
72 | } |
71 | else |
73 | else |
72 | { |
74 | { |
73 | couldbetty = (st.st_mode & S_IFMT) == S_IFCHR; |
75 | couldbetty = (st.st_mode & S_IFMT) == S_IFCHR; |
74 | #ifdef HAVE_BLKSIZE |
76 | #ifdef HAVE_BLKSIZE |
75 | size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize; |
77 | size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize; |
76 | #else |
78 | #else |
77 | size = BUFSIZ; |
79 | size = BUFSIZ; |
78 | #endif |
80 | #endif |
- | 81 | #ifdef _FSEEK_OPTIMIZATION |
|
79 | /* |
82 | /* |
80 | * Optimize fseek() only if it is a regular file. |
83 | * Optimize fseek() only if it is a regular file. |
81 | * (The test for __sseek is mainly paranoia.) |
84 | * (The test for __sseek is mainly paranoia.) |
82 | */ |
85 | */ |
83 | if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek) |
86 | if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek) |
84 | { |
87 | { |
85 | fp->_flags |= __SOPT; |
88 | fp->_flags |= __SOPT; |
86 | #ifdef HAVE_BLKSIZE |
89 | #ifdef HAVE_BLKSIZE |
87 | fp->_blksize = st.st_blksize; |
90 | fp->_blksize = st.st_blksize; |
88 | #else |
91 | #else |
89 | fp->_blksize = 1024; |
92 | fp->_blksize = 1024; |
90 | #endif |
93 | #endif |
91 | } |
94 | } |
92 | else |
95 | else |
93 | fp->_flags |= __SNPT; |
96 | fp->_flags |= __SNPT; |
- | 97 | #endif |
|
94 | } |
98 | } |
95 | if ((p = _malloc_r (ptr, size)) == NULL) |
99 | if ((p = _malloc_r (ptr, size)) == NULL) |
96 | { |
100 | { |
97 | if (!(fp->_flags & __SSTR)) |
101 | if (!(fp->_flags & __SSTR)) |
98 | { |
102 | { |
99 | fp->_flags |= __SNBF; |
103 | fp->_flags |= __SNBF; |
100 | fp->_bf._base = fp->_p = fp->_nbuf; |
104 | fp->_bf._base = fp->_p = fp->_nbuf; |
101 | fp->_bf._size = 1; |
105 | fp->_bf._size = 1; |
102 | } |
106 | } |
103 | } |
107 | } |
104 | else |
108 | else |
105 | { |
109 | { |
106 | ptr->__cleanup = _cleanup_r; |
110 | ptr->__cleanup = _cleanup_r; |
107 | fp->_flags |= __SMBF; |
111 | fp->_flags |= __SMBF; |
108 | fp->_bf._base = fp->_p = (unsigned char *) p; |
112 | fp->_bf._base = fp->_p = (unsigned char *) p; |
109 | fp->_bf._size = size; |
113 | fp->_bf._size = size; |
110 | if (couldbetty && _isatty_r (ptr, fp->_file)) |
114 | if (couldbetty && _isatty_r (ptr, fp->_file)) |
111 | fp->_flags |= __SLBF; |
115 | fp->_flags |= __SLBF; |
112 | } |
116 | } |
113 | }=>>>>> |
117 | }=>>>>> |