1 : /*
2 : +----------------------------------------------------------------------+
3 : | Zend Engine |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1998-2007 Zend Technologies Ltd. (http://www.zend.com) |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 2.00 of the Zend license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.zend.com/license/2_00.txt. |
11 : | If you did not receive a copy of the Zend license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@zend.com so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Wez Furlong <wez@thebrainroom.com> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: zend_stream.c,v 1.13.2.1.2.1 2007/01/01 09:35:47 sebastian Exp $ */
20 :
21 :
22 : #include "zend.h"
23 : #include "zend_compile.h"
24 :
25 : ZEND_DLIMPORT int isatty(int fd);
26 :
27 : static size_t zend_stream_stdio_reader(void *handle, char *buf, size_t len TSRMLS_DC)
28 444 : {
29 444 : return fread(buf, 1, len, (FILE*)handle);
30 : }
31 :
32 : static void zend_stream_stdio_closer(void *handle TSRMLS_DC)
33 0 : {
34 0 : if ((FILE*)handle != stdin)
35 0 : fclose((FILE*)handle);
36 0 : }
37 :
38 : static long zend_stream_stdio_fteller(void *handle TSRMLS_DC)
39 0 : {
40 0 : return ftell((FILE*) handle);
41 : }
42 :
43 :
44 : ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle TSRMLS_DC)
45 116 : {
46 116 : if (zend_stream_open_function) {
47 116 : return zend_stream_open_function(filename, handle TSRMLS_CC);
48 : }
49 0 : handle->type = ZEND_HANDLE_FP;
50 0 : handle->opened_path = NULL;
51 0 : handle->handle.fp = zend_fopen(filename, &handle->opened_path);
52 0 : handle->filename = (char *)filename;
53 0 : handle->free_filename = 0;
54 :
55 0 : return (handle->handle.fp) ? SUCCESS : FAILURE;
56 : }
57 :
58 : ZEND_API int zend_stream_fixup(zend_file_handle *file_handle TSRMLS_DC)
59 335 : {
60 335 : switch (file_handle->type) {
61 : case ZEND_HANDLE_FILENAME:
62 116 : if (FAILURE == zend_stream_open(file_handle->filename, file_handle TSRMLS_CC)) {
63 0 : return FAILURE;
64 : }
65 116 : break;
66 :
67 : case ZEND_HANDLE_FD:
68 0 : file_handle->handle.fp = fdopen(file_handle->handle.fd, "rb");
69 0 : file_handle->type = ZEND_HANDLE_FP;
70 0 : break;
71 :
72 : case ZEND_HANDLE_FP:
73 219 : file_handle->handle.fp = file_handle->handle.fp;
74 219 : break;
75 :
76 : case ZEND_HANDLE_STREAM:
77 : /* nothing to do */
78 0 : return SUCCESS;
79 :
80 : default:
81 0 : return FAILURE;
82 : }
83 335 : if (file_handle->type == ZEND_HANDLE_FP) {
84 219 : if (!file_handle->handle.fp) {
85 0 : return FAILURE;
86 : }
87 :
88 : /* make compatible with stream */
89 219 : file_handle->handle.stream.handle = file_handle->handle.fp;
90 219 : file_handle->handle.stream.reader = zend_stream_stdio_reader;
91 219 : file_handle->handle.stream.closer = zend_stream_stdio_closer;
92 219 : file_handle->handle.stream.fteller = zend_stream_stdio_fteller;
93 :
94 219 : file_handle->handle.stream.interactive = isatty(fileno((FILE *)file_handle->handle.stream.handle));
95 : }
96 335 : return SUCCESS;
97 : }
98 :
99 : ZEND_API size_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t len TSRMLS_DC)
100 676 : {
101 676 : if (file_handle->handle.stream.interactive) {
102 0 : int c = '*';
103 : size_t n;
104 :
105 : #ifdef NETWARE
106 : /*
107 : c != 4 check is there as fread of a character in NetWare LibC gives 4 upon ^D character.
108 : Ascii value 4 is actually EOT character which is not defined anywhere in the LibC
109 : or else we can use instead of hardcoded 4.
110 : */
111 : for ( n = 0; n < len && (c = zend_stream_getc( file_handle TSRMLS_CC)) != EOF && c != 4 && c != '\n'; ++n )
112 : #else
113 0 : for ( n = 0; n < len && (c = zend_stream_getc( file_handle TSRMLS_CC)) != EOF && c != '\n'; ++n )
114 : #endif
115 0 : buf[n] = (char) c;
116 0 : if ( c == '\n' )
117 0 : buf[n++] = (char) c;
118 :
119 0 : return n;
120 : }
121 676 : return file_handle->handle.stream.reader(file_handle->handle.stream.handle, buf, len TSRMLS_CC);
122 : }
123 :
124 : ZEND_API int zend_stream_getc(zend_file_handle *file_handle TSRMLS_DC)
125 0 : {
126 : char buf;
127 :
128 0 : if (file_handle->handle.stream.reader(file_handle->handle.stream.handle, &buf, sizeof(buf) TSRMLS_CC)) {
129 0 : return (int)buf;
130 : }
131 0 : return EOF;
132 : }
133 :
134 : ZEND_API int zend_stream_ferror(zend_file_handle *file_handle TSRMLS_DC)
135 335 : {
136 335 : return 0;
137 : }
138 :
139 : ZEND_API long zend_stream_ftell(zend_file_handle *file_handle TSRMLS_DC)
140 0 : {
141 0 : return file_handle->handle.stream.fteller(file_handle->handle.stream.handle TSRMLS_CC);
142 : }
|