[packages] php5: add PECL dio extension
[openwrt/svn-archive/archive.git] / lang / php5 / patches / 010-PECL-dio.patch
1 --- /dev/null
2 +++ b/ext/dio/config.m4
3 @@ -0,0 +1,10 @@
4 +dnl
5 +dnl $Id: config.m4 291957 2009-12-10 17:13:14Z cyberspice $
6 +dnl
7 +
8 +PHP_ARG_ENABLE(dio, whether to enable direct I/O support,
9 +[ --enable-dio Enable direct I/O support])
10 +
11 +if test "$PHP_DIO" != "no"; then
12 + PHP_NEW_EXTENSION(dio, dio.c dio_common.c dio_posix.c dio_stream_wrappers.c, $ext_shared)
13 +fi
14 --- /dev/null
15 +++ b/ext/dio/dio.c
16 @@ -0,0 +1,801 @@
17 +/*
18 + +----------------------------------------------------------------------+
19 + | PHP Version 5 |
20 + +----------------------------------------------------------------------+
21 + | Copyright (c) 1997-2009 The PHP Group |
22 + +----------------------------------------------------------------------+
23 + | This source file is subject to version 3.0 of the PHP license, |
24 + | that is bundled with this package in the file LICENSE, and is |
25 + | available through the world-wide-web at the following url: |
26 + | http://www.php.net/license/3_0.txt. |
27 + | If you did not receive a copy of the PHP license and are unable to |
28 + | obtain it through the world-wide-web, please send a note to |
29 + | license@php.net so we can mail you a copy immediately. |
30 + +----------------------------------------------------------------------+
31 + | Author: Sterling Hughes <sterling@php.net> |
32 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
33 + +----------------------------------------------------------------------+
34 + */
35 +
36 +#ifdef HAVE_CONFIG_H
37 +#include "config.h"
38 +#endif
39 +
40 +#include "php.h"
41 +#include "php_ini.h"
42 +#include "ext/standard/info.h"
43 +
44 +#include "php_dio.h"
45 +#include "php_dio_stream_wrappers.h"
46 +
47 +#include <sys/stat.h>
48 +#include <sys/types.h>
49 +
50 +#ifdef HAVE_UNISTD_H
51 +#include <unistd.h>
52 +#endif
53 +
54 +#include <fcntl.h>
55 +#ifndef PHP_WIN32
56 +#include <termios.h>
57 +#endif
58 +
59 +/* e.g. IRIX does not have CRTSCTS */
60 +#ifndef CRTSCTS
61 +# ifdef CNEW_RTSCTS
62 +# define CRTSCTS CNEW_RTSCTS
63 +# else
64 +# define CRTSCTS 0
65 +# endif /* CNEW_RTSCTS */
66 +#endif /* !CRTSCTS */
67 +
68 +/*
69 + +----------------------------------------------------------------------+
70 + | DEPRECATED FUNCTIONALITY |
71 + +----------------------------------------------------------------------+
72 + | The functions below are from the earlier DIO versions. They will |
73 + | continue to be maintained but not extended. It is thoroughly |
74 + | recommended that you should use either the stream wrappers or the |
75 + | DIO classes in new code. - Melanie |
76 + +----------------------------------------------------------------------+
77 + */
78 +
79 +#define le_fd_name "Direct I/O File Descriptor"
80 +static int le_fd;
81 +
82 +static int new_php_fd(php_fd_t **f, int fd)
83 +{
84 + if (!(*f = malloc(sizeof(php_fd_t)))) {
85 + return 0;
86 + }
87 + (*f)->fd = fd;
88 + return 1;
89 +}
90 +
91 +static void _dio_close_fd(zend_rsrc_list_entry *rsrc TSRMLS_DC)
92 +{
93 + php_fd_t *f = (php_fd_t *) rsrc->ptr;
94 + if (f) {
95 + close(f->fd);
96 + free(f);
97 + }
98 +}
99 +
100 +/* {{{ proto resource dio_open(string filename, int flags[, int mode])
101 + Open a new filename with specified permissions of flags and creation permissions of mode */
102 +PHP_FUNCTION(dio_open)
103 +{
104 + php_fd_t *f;
105 + char *file_name;
106 + int file_name_length;
107 + long flags;
108 + long mode = 0;
109 + int fd;
110 +
111 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", &file_name, &file_name_length, &flags, &mode) == FAILURE) {
112 + return;
113 + }
114 +
115 + if (php_check_open_basedir(file_name TSRMLS_CC) || (PG(safe_mode) && !php_checkuid(file_name, "wb+", CHECKUID_CHECK_MODE_PARAM))) {
116 + RETURN_FALSE;
117 + }
118 +
119 + if (ZEND_NUM_ARGS() == 3) {
120 + fd = open(file_name, flags, mode);
121 + } else {
122 + fd = open(file_name, flags);
123 + }
124 +
125 + if (fd == -1) {
126 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot open file %s with flags %ld and permissions %ld: %s", file_name, flags, mode, strerror(errno));
127 + RETURN_FALSE;
128 + }
129 +
130 +
131 + if (!new_php_fd(&f, fd)) {
132 + RETURN_FALSE;
133 + }
134 + ZEND_REGISTER_RESOURCE(return_value, f, le_fd);
135 +}
136 +/* }}} */
137 +
138 +/* {{{ proto string dio_read(resource fd[, int n])
139 + Read n bytes from fd and return them, if n is not specified, read 1k */
140 +PHP_FUNCTION(dio_read)
141 +{
142 + zval *r_fd;
143 + php_fd_t *f;
144 + char *data;
145 + long bytes = 1024;
146 + ssize_t res;
147 +
148 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &r_fd, &bytes) == FAILURE) {
149 + return;
150 + }
151 +
152 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
153 +
154 + if (bytes <= 0) {
155 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0.");
156 + RETURN_FALSE;
157 + }
158 +
159 + data = emalloc(bytes + 1);
160 + res = read(f->fd, data, bytes);
161 + if (res <= 0) {
162 + efree(data);
163 + RETURN_NULL();
164 + }
165 +
166 + data = erealloc(data, res + 1);
167 + data[res] = 0;
168 +
169 + RETURN_STRINGL(data, res, 0);
170 +}
171 +/* }}} */
172 +
173 +/* {{{ proto int dio_write(resource fd, string data[, int len])
174 + Write data to fd with optional truncation at length */
175 +PHP_FUNCTION(dio_write)
176 +{
177 + zval *r_fd;
178 + php_fd_t *f;
179 + char *data;
180 + int data_len;
181 + long trunc_len = 0;
182 + ssize_t res;
183 +
184 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &r_fd, &data, &data_len, &trunc_len) == FAILURE) {
185 + return;
186 + }
187 +
188 + if (trunc_len < 0 || trunc_len > data_len) {
189 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "length must be greater or equal to zero and less then the length of the specified string.");
190 + RETURN_FALSE;
191 + }
192 +
193 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
194 +
195 + res = write(f->fd, data, trunc_len ? trunc_len : data_len);
196 + if (res == -1) {
197 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write data to file descriptor %d, %s", f->fd, strerror(errno));
198 + }
199 +
200 + RETURN_LONG(res);
201 +}
202 +/* }}} */
203 +
204 +#ifndef PHP_WIN32
205 +
206 +/* {{{ proto bool dio_truncate(resource fd, int offset)
207 + Truncate file descriptor fd to offset bytes */
208 +PHP_FUNCTION(dio_truncate)
209 +{
210 + zval *r_fd;
211 + php_fd_t *f;
212 + long offset;
213 +
214 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &r_fd, &offset) == FAILURE) {
215 + return;
216 + }
217 +
218 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
219 +
220 + if (ftruncate(f->fd, offset) == -1) {
221 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't truncate %d to %ld bytes: %s", f->fd, offset, strerror(errno));
222 + RETURN_FALSE;
223 + }
224 +
225 + RETURN_TRUE;
226 +}
227 +/* }}} */
228 +#endif
229 +
230 +#define ADD_FIELD(f, v) add_assoc_long_ex(return_value, (f), sizeof(f), v);
231 +
232 +/* {{{ proto array dio_stat(resource fd)
233 + Get stat information about the file descriptor fd */
234 +PHP_FUNCTION(dio_stat)
235 +{
236 + zval *r_fd;
237 + php_fd_t *f;
238 + struct stat s;
239 +
240 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
241 + return;
242 + }
243 +
244 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
245 +
246 + if (fstat(f->fd, &s) == -1) {
247 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot stat %d: %s", f->fd, strerror(errno));
248 + RETURN_FALSE;
249 + }
250 +
251 + array_init(return_value);
252 + ADD_FIELD("device", s.st_dev);
253 + ADD_FIELD("inode", s.st_ino);
254 + ADD_FIELD("mode", s.st_mode);
255 + ADD_FIELD("nlink", s.st_nlink);
256 + ADD_FIELD("uid", s.st_uid);
257 + ADD_FIELD("gid", s.st_gid);
258 + ADD_FIELD("device_type", s.st_rdev);
259 + ADD_FIELD("size", s.st_size);
260 +#ifndef PHP_WIN32
261 + ADD_FIELD("block_size", s.st_blksize);
262 + ADD_FIELD("blocks", s.st_blocks);
263 +#endif
264 + ADD_FIELD("atime", s.st_atime);
265 + ADD_FIELD("mtime", s.st_mtime);
266 + ADD_FIELD("ctime", s.st_ctime);
267 +}
268 +/* }}} */
269 +
270 +/* {{{ proto int dio_seek(resource fd, int pos, int whence)
271 + Seek to pos on fd from whence */
272 +PHP_FUNCTION(dio_seek)
273 +{
274 + zval *r_fd;
275 + php_fd_t *f;
276 + long offset;
277 + long whence = SEEK_SET;
278 +
279 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &r_fd, &offset, &whence) == FAILURE) {
280 + return;
281 + }
282 +
283 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
284 +
285 + RETURN_LONG(lseek(f->fd, offset, whence));
286 +}
287 +/* }}} */
288 +
289 +#ifndef PHP_WIN32
290 +
291 +/* {{{ proto mixed dio_fcntl(resource fd, int cmd[, mixed arg])
292 + Perform a c library fcntl on fd */
293 +PHP_FUNCTION(dio_fcntl)
294 +{
295 + zval *r_fd;
296 + zval *arg = NULL;
297 + php_fd_t *f;
298 + long cmd;
299 +
300 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &r_fd, &cmd, &arg) == FAILURE) {
301 + return;
302 + }
303 +
304 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
305 +
306 + switch (cmd) {
307 + case F_SETLK:
308 + case F_SETLKW: {
309 + zval **element;
310 + struct flock lk = {0};
311 + HashTable *fh;
312 +
313 + if (!arg) {
314 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be array or int, none given");
315 + RETURN_FALSE;
316 + }
317 + if (Z_TYPE_P(arg) == IS_ARRAY) {
318 + fh = HASH_OF(arg);
319 + if (zend_hash_find(fh, "start", sizeof("start"), (void **) &element) == FAILURE) {
320 + lk.l_start = 0;
321 + } else {
322 + lk.l_start = Z_LVAL_PP(element);
323 + }
324 +
325 + if (zend_hash_find(fh, "length", sizeof("length"), (void **) &element) == FAILURE) {
326 + lk.l_len = 0;
327 + } else {
328 + lk.l_len = Z_LVAL_PP(element);
329 + }
330 +
331 + if (zend_hash_find(fh, "whence", sizeof("whence"), (void **) &element) == FAILURE) {
332 + lk.l_whence = 0;
333 + } else {
334 + lk.l_whence = Z_LVAL_PP(element);
335 + }
336 +
337 + if (zend_hash_find(fh, "type", sizeof("type"), (void **) &element) == FAILURE) {
338 + lk.l_type = 0;
339 + } else {
340 + lk.l_type = Z_LVAL_PP(element);
341 + }
342 + } else if (Z_TYPE_P(arg) == IS_LONG) {
343 + lk.l_start = 0;
344 + lk.l_len = 0;
345 + lk.l_whence = SEEK_SET;
346 + lk.l_type = Z_LVAL_P(arg);
347 + } else {
348 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be array or int, %s given", zend_zval_type_name(arg));
349 + RETURN_FALSE;
350 + }
351 +
352 + RETURN_LONG(fcntl(f->fd, cmd, &lk));
353 + break;
354 + }
355 + case F_GETLK: {
356 + struct flock lk = {0};
357 +
358 + fcntl(f->fd, cmd, &lk);
359 +
360 + array_init(return_value);
361 + add_assoc_long(return_value, "type", lk.l_type);
362 + add_assoc_long(return_value, "whence", lk.l_whence);
363 + add_assoc_long(return_value, "start", lk.l_start);
364 + add_assoc_long(return_value, "length", lk.l_len);
365 + add_assoc_long(return_value, "pid", lk.l_pid);
366 +
367 + break;
368 + }
369 + case F_DUPFD: {
370 + php_fd_t *new_f;
371 +
372 + if (!arg || Z_TYPE_P(arg) != IS_LONG) {
373 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be int");
374 + RETURN_FALSE;
375 + }
376 +
377 + if (!new_php_fd(&new_f, fcntl(f->fd, cmd, Z_LVAL_P(arg)))) {
378 + RETURN_FALSE;
379 + }
380 + ZEND_REGISTER_RESOURCE(return_value, new_f, le_fd);
381 + break;
382 + }
383 + default:
384 + if (!arg || Z_TYPE_P(arg) != IS_LONG) {
385 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects argument 3 to be int");
386 + RETURN_FALSE;
387 + }
388 +
389 + RETURN_LONG(fcntl(f->fd, cmd, Z_LVAL_P(arg)));
390 + }
391 +}
392 +/* }}} */
393 +#endif
394 +
395 +#ifndef PHP_WIN32
396 +
397 +/* {{{ proto mixed dio_tcsetattr(resource fd, array args )
398 + Perform a c library tcsetattr on fd */
399 +PHP_FUNCTION(dio_tcsetattr)
400 +{
401 + zval *r_fd;
402 + zval *arg = NULL;
403 + php_fd_t *f;
404 + struct termios newtio;
405 + int Baud_Rate, Data_Bits=8, Stop_Bits=1, Parity=0, Flow_Control=1, Is_Canonical=1;
406 + long BAUD,DATABITS,STOPBITS,PARITYON,PARITY;
407 + HashTable *fh;
408 + zval **element;
409 +
410 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &r_fd, &arg) == FAILURE) {
411 + return;
412 + }
413 +
414 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
415 +
416 + if (Z_TYPE_P(arg) != IS_ARRAY) {
417 + php_error_docref(NULL TSRMLS_CC, E_WARNING,"tcsetattr, third argument should be an associative array");
418 + return;
419 + }
420 +
421 + fh = HASH_OF(arg);
422 +
423 + if (zend_hash_find(fh, "baud", sizeof("baud"), (void **) &element) == FAILURE) {
424 + Baud_Rate = 9600;
425 + } else {
426 + Baud_Rate = Z_LVAL_PP(element);
427 + }
428 +
429 + if (zend_hash_find(fh, "bits", sizeof("bits"), (void **) &element) == FAILURE) {
430 + Data_Bits = 8;
431 + } else {
432 + Data_Bits = Z_LVAL_PP(element);
433 + }
434 +
435 + if (zend_hash_find(fh, "stop", sizeof("stop"), (void **) &element) == FAILURE) {
436 + Stop_Bits = 1;
437 + } else {
438 + Stop_Bits = Z_LVAL_PP(element);
439 + }
440 +
441 + if (zend_hash_find(fh, "parity", sizeof("parity"), (void **) &element) == FAILURE) {
442 + Parity = 0;
443 + } else {
444 + Parity = Z_LVAL_PP(element);
445 + }
446 +
447 + if (zend_hash_find(fh, "flow_control", sizeof("flow_control"), (void **) &element) == FAILURE) {
448 + Flow_Control = 1;
449 + } else {
450 + Flow_Control = Z_LVAL_PP(element);
451 + }
452 +
453 + if (zend_hash_find(fh, "is_canonical", sizeof("is_canonical"), (void **) &element) == FAILURE) {
454 + Is_Canonical = 1;
455 + } else {
456 + Is_Canonical = Z_LVAL_PP(element);
457 + }
458 +
459 + /* assign to correct values... */
460 + switch (Baud_Rate) {
461 + case 38400:
462 + BAUD = B38400;
463 + break;
464 + case 19200:
465 + BAUD = B19200;
466 + break;
467 + case 9600:
468 + BAUD = B9600;
469 + break;
470 + case 4800:
471 + BAUD = B4800;
472 + break;
473 + case 2400:
474 + BAUD = B2400;
475 + break;
476 + case 1800:
477 + BAUD = B1800;
478 + break;
479 + case 1200:
480 + BAUD = B1200;
481 + break;
482 + case 600:
483 + BAUD = B600;
484 + break;
485 + case 300:
486 + BAUD = B300;
487 + break;
488 + case 200:
489 + BAUD = B200;
490 + break;
491 + case 150:
492 + BAUD = B150;
493 + break;
494 + case 134:
495 + BAUD = B134;
496 + break;
497 + case 110:
498 + BAUD = B110;
499 + break;
500 + case 75:
501 + BAUD = B75;
502 + break;
503 + case 50:
504 + BAUD = B50;
505 + break;
506 + default:
507 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid baud rate %d", Baud_Rate);
508 + RETURN_FALSE;
509 + }
510 + switch (Data_Bits) {
511 + case 8:
512 + DATABITS = CS8;
513 + break;
514 + case 7:
515 + DATABITS = CS7;
516 + break;
517 + case 6:
518 + DATABITS = CS6;
519 + break;
520 + case 5:
521 + DATABITS = CS5;
522 + break;
523 + default:
524 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data bits %d", Data_Bits);
525 + RETURN_FALSE;
526 + }
527 + switch (Stop_Bits) {
528 + case 1:
529 + STOPBITS = 0;
530 + break;
531 + case 2:
532 + STOPBITS = CSTOPB;
533 + break;
534 + default:
535 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop bits %d", Stop_Bits);
536 + RETURN_FALSE;
537 + }
538 +
539 + switch (Parity) {
540 + case 0:
541 + PARITYON = 0;
542 + PARITY = 0;
543 + break;
544 + case 1:
545 + PARITYON = PARENB;
546 + PARITY = PARODD;
547 + break;
548 + case 2:
549 + PARITYON = PARENB;
550 + PARITY = 0;
551 + break;
552 + default:
553 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity %d", Parity);
554 + RETURN_FALSE;
555 + }
556 +
557 + memset(&newtio, 0, sizeof(newtio));
558 + tcgetattr(f->fd, &newtio);
559 +
560 + if (Is_Canonical) {
561 + newtio.c_iflag = IGNPAR | ICRNL;
562 + newtio.c_oflag = 0;
563 + newtio.c_lflag = ICANON;
564 + } else {
565 + cfmakeraw(&newtio);
566 + }
567 +
568 + newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
569 +
570 +#ifdef CRTSCTS
571 + if (Flow_Control) {
572 + newtio.c_cflag |= CRTSCTS;
573 + }
574 +#endif
575 +
576 + if (Is_Canonical)
577 +
578 + newtio.c_cc[VMIN] = 1;
579 + newtio.c_cc[VTIME] = 0;
580 + tcflush(f->fd, TCIFLUSH);
581 + tcsetattr(f->fd,TCSANOW,&newtio);
582 +
583 + RETURN_TRUE;
584 +}
585 +/* }}} */
586 +#endif
587 +
588 +/* {{{ proto void dio_close(resource fd)
589 + Close the file descriptor given by fd */
590 +PHP_FUNCTION(dio_close)
591 +{
592 + zval *r_fd;
593 + php_fd_t *f;
594 +
595 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &r_fd) == FAILURE) {
596 + return;
597 + }
598 +
599 + ZEND_FETCH_RESOURCE(f, php_fd_t *, &r_fd, -1, le_fd_name, le_fd);
600 +
601 + zend_list_delete(Z_LVAL_P(r_fd));
602 +}
603 +/* }}} */
604 +
605 +#define RDIOC(c) REGISTER_LONG_CONSTANT(#c, c, CONST_CS | CONST_PERSISTENT)
606 +
607 +/* {{{ dio_init_legacy_defines
608 + * Initialises the legacy PHP defines
609 + */
610 +static void dio_init_legacy_defines(int module_number TSRMLS_DC) {
611 + RDIOC(O_RDONLY);
612 + RDIOC(O_WRONLY);
613 + RDIOC(O_RDWR);
614 + RDIOC(O_CREAT);
615 + RDIOC(O_EXCL);
616 + RDIOC(O_TRUNC);
617 + RDIOC(O_APPEND);
618 +#ifdef O_NONBLOCK
619 + RDIOC(O_NONBLOCK);
620 +#endif
621 +#ifdef O_NDELAY
622 + RDIOC(O_NDELAY);
623 +#endif
624 +#ifdef O_SYNC
625 + RDIOC(O_SYNC);
626 +#endif
627 +#ifdef O_ASYNC
628 + RDIOC(O_ASYNC);
629 +#endif
630 +#ifdef O_NOCTTY
631 + RDIOC(O_NOCTTY);
632 +#endif
633 +#ifndef PHP_WIN32
634 + RDIOC(S_IRWXU);
635 + RDIOC(S_IRUSR);
636 + RDIOC(S_IWUSR);
637 + RDIOC(S_IXUSR);
638 + RDIOC(S_IRWXG);
639 + RDIOC(S_IRGRP);
640 + RDIOC(S_IWGRP);
641 + RDIOC(S_IXGRP);
642 + RDIOC(S_IRWXO);
643 + RDIOC(S_IROTH);
644 + RDIOC(S_IWOTH);
645 + RDIOC(S_IXOTH);
646 + RDIOC(F_DUPFD);
647 + RDIOC(F_GETFD);
648 + RDIOC(F_GETFL);
649 + RDIOC(F_SETFL);
650 + RDIOC(F_GETLK);
651 + RDIOC(F_SETLK);
652 + RDIOC(F_SETLKW);
653 + RDIOC(F_SETOWN);
654 + RDIOC(F_GETOWN);
655 + RDIOC(F_UNLCK);
656 + RDIOC(F_RDLCK);
657 + RDIOC(F_WRLCK);
658 +#endif
659 +}
660 +
661 +ZEND_BEGIN_ARG_INFO_EX(dio_open_args, 0, 0, 2)
662 + ZEND_ARG_INFO(0, filename)
663 + ZEND_ARG_INFO(0, flags)
664 + ZEND_ARG_INFO(0, mode)
665 +ZEND_END_ARG_INFO()
666 +
667 +ZEND_BEGIN_ARG_INFO_EX(dio_read_args, 0, 0, 1)
668 + ZEND_ARG_INFO(0, fd)
669 + ZEND_ARG_INFO(0, n)
670 +ZEND_END_ARG_INFO()
671 +
672 +ZEND_BEGIN_ARG_INFO_EX(dio_write_args, 0, 0, 2)
673 + ZEND_ARG_INFO(0, fd)
674 + ZEND_ARG_INFO(0, data)
675 + ZEND_ARG_INFO(0, len)
676 +ZEND_END_ARG_INFO()
677 +
678 +ZEND_BEGIN_ARG_INFO_EX(dio_stat_args, 0, 0, 1)
679 + ZEND_ARG_INFO(0, fd)
680 +ZEND_END_ARG_INFO()
681 +
682 +ZEND_BEGIN_ARG_INFO_EX(dio_truncate_args, 0, 0, 2)
683 + ZEND_ARG_INFO(0, fd)
684 + ZEND_ARG_INFO(0, offset)
685 +ZEND_END_ARG_INFO()
686 +
687 +ZEND_BEGIN_ARG_INFO_EX(dio_seek_args, 0, 0, 3)
688 + ZEND_ARG_INFO(0, fd)
689 + ZEND_ARG_INFO(0, pos)
690 + ZEND_ARG_INFO(0, whence)
691 +ZEND_END_ARG_INFO()
692 +
693 +ZEND_BEGIN_ARG_INFO_EX(dio_fcntl_args, 0, 0, 2)
694 + ZEND_ARG_INFO(0, fd)
695 + ZEND_ARG_INFO(0, cmd)
696 + ZEND_ARG_INFO(0, arg)
697 +ZEND_END_ARG_INFO()
698 +
699 +ZEND_BEGIN_ARG_INFO_EX(dio_tcsetattr_args, 0, 0, 2)
700 + ZEND_ARG_INFO(0, fd)
701 + ZEND_ARG_INFO(0, args)
702 +ZEND_END_ARG_INFO()
703 +
704 +ZEND_BEGIN_ARG_INFO_EX(dio_close_args, 0, 0, 1)
705 + ZEND_ARG_INFO(0, fd)
706 +ZEND_END_ARG_INFO()
707 +
708 +/*
709 + +----------------------------------------------------------------------+
710 + | END OF DEPRECATED FUNCTIONALITY |
711 + +----------------------------------------------------------------------+
712 + */
713 +
714 +ZEND_BEGIN_ARG_INFO_EX(dio_raw_args, 0, 0, 2)
715 + ZEND_ARG_INFO(0, filename)
716 + ZEND_ARG_INFO(0, mode)
717 + ZEND_ARG_INFO(0, options)
718 +ZEND_END_ARG_INFO()
719 +
720 +ZEND_BEGIN_ARG_INFO_EX(dio_serial_args, 0, 0, 2)
721 + ZEND_ARG_INFO(0, filename)
722 + ZEND_ARG_INFO(0, mode)
723 + ZEND_ARG_INFO(0, options)
724 +ZEND_END_ARG_INFO()
725 +
726 +static zend_object_handlers dio_raw_object_handlers;
727 +
728 +static function_entry dio_functions[] = {
729 + /* Class functions. */
730 +
731 + /* Legacy functions (Deprecated - See dio_legacy.c) */
732 + PHP_FE(dio_open, dio_open_args)
733 +#ifndef PHP_WIN32
734 + PHP_FE(dio_truncate, dio_truncate_args)
735 +#endif
736 + PHP_FE(dio_stat, dio_stat_args)
737 + PHP_FE(dio_seek, dio_seek_args)
738 +#ifndef PHP_WIN32
739 + PHP_FE(dio_fcntl, dio_fcntl_args)
740 +#endif
741 + PHP_FE(dio_read, dio_read_args)
742 + PHP_FE(dio_write, dio_write_args)
743 + PHP_FE(dio_close, dio_close_args)
744 +#ifndef PHP_WIN32
745 + PHP_FE(dio_tcsetattr, dio_tcsetattr_args)
746 +#endif
747 +
748 + /* Stream functions */
749 + PHP_FE(dio_raw, dio_raw_args)
750 + PHP_FE(dio_serial, dio_serial_args)
751 +
752 + /* End of functions */
753 + {NULL, NULL, NULL}
754 +};
755 +
756 +zend_module_entry dio_module_entry = {
757 + STANDARD_MODULE_HEADER,
758 + "dio",
759 + dio_functions,
760 + PHP_MINIT(dio),
761 + NULL,
762 + NULL,
763 + NULL,
764 + PHP_MINFO(dio),
765 + PHP_DIO_VERSION,
766 + STANDARD_MODULE_PROPERTIES
767 +};
768 +
769 +#ifdef COMPILE_DL_DIO
770 +ZEND_GET_MODULE(dio)
771 +#endif
772 +
773 +#define DIO_UNDEF_CONST -1
774 +
775 +/* {{{ PHP_MINIT_FUNCTION
776 + */
777 +PHP_MINIT_FUNCTION(dio)
778 +{
779 + /* Legacy resource destructor. */
780 + le_fd = zend_register_list_destructors_ex(_dio_close_fd, NULL, le_fd_name, module_number);
781 +
782 + dio_init_legacy_defines(module_number TSRMLS_CC);
783 +
784 + /* Register the stream wrappers */
785 + return (php_register_url_stream_wrapper(DIO_RAW_STREAM_NAME, &php_dio_raw_stream_wrapper TSRMLS_CC) == SUCCESS &&
786 + php_register_url_stream_wrapper(DIO_SERIAL_STREAM_NAME, &php_dio_serial_stream_wrapper TSRMLS_CC) == SUCCESS) ? SUCCESS : FAILURE;
787 +}
788 +/* }}} */
789 +
790 +/* {{{ PHP_MSHUTDOWN_FUNCTION
791 + */
792 +PHP_MSHUTDOWN_FUNCTION(dio)
793 +{
794 + return (php_unregister_url_stream_wrapper(DIO_RAW_STREAM_NAME TSRMLS_CC) == SUCCESS &&
795 + php_unregister_url_stream_wrapper(DIO_SERIAL_STREAM_NAME TSRMLS_CC) == SUCCESS) ? SUCCESS : FAILURE;
796 +}
797 +/* }}} */
798 +
799 +/* {{{ PHP_MINFO_FUNCTION
800 + */
801 +PHP_MINFO_FUNCTION(dio)
802 +{
803 + php_info_print_table_start();
804 + php_info_print_table_header(2, "dio support", "enabled");
805 + php_info_print_table_row(2, "version", PHP_DIO_VERSION);
806 + php_info_print_table_end();
807 +}
808 +/* }}} */
809 +
810 +/*
811 + * Local variables:
812 + * c-basic-offset: 4
813 + * tab-width: 4
814 + * End:
815 + * vim600: fdm=marker
816 + * vim: sw=4 ts=4 noet
817 + */
818 --- /dev/null
819 +++ b/ext/dio/dio_common.c
820 @@ -0,0 +1,230 @@
821 +/*
822 + +----------------------------------------------------------------------+
823 + | PHP Version 5 |
824 + +----------------------------------------------------------------------+
825 + | Copyright (c) 2009 Melanie Rhianna Lewis |
826 + +----------------------------------------------------------------------+
827 + | This source file is subject to version 3.0 of the PHP license, |
828 + | that is bundled with this package in the file LICENSE, and is |
829 + | available through the world-wide-web at the following url: |
830 + | http://www.php.net/license/3_0.txt. |
831 + | If you did not receive a copy of the PHP license and are unable to |
832 + | obtain it through the world-wide-web, please send a note to |
833 + | license@php.net so we can mail you a copy immediately. |
834 + +----------------------------------------------------------------------+
835 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
836 + +----------------------------------------------------------------------+
837 + */
838 +
839 +#ifdef HAVE_CONFIG_H
840 +#include "config.h"
841 +#endif
842 +
843 +#include "php.h"
844 +
845 +#include "php_dio.h"
846 +#include "php_dio_common.h"
847 +
848 +/* {{{ dio_init_stream_data
849 + * Initialises the command parts of the stream data.
850 + */
851 +void dio_init_stream_data(php_dio_stream_data *data) {
852 + data->stream_type = DIO_STREAM_TYPE_NONE;
853 + data->end_of_file = 0;
854 +#ifdef DIO_HAS_FILEPERMS
855 + data->has_perms = 0;
856 + data->perms = 0;
857 +#endif
858 +#ifdef DIO_NONBLOCK
859 + data->is_blocking = 1;
860 + data->has_timeout = 0;
861 + data->timeout_sec = 0;
862 + data->timeout_usec = 0;
863 + data->timed_out = 0;
864 +#endif
865 + /* Serial options */
866 + data->data_rate = 9600;
867 + data->data_bits = 8;
868 + data->stop_bits = 1;
869 + data->parity = 0;
870 + data->flow_control = 1;
871 + data->canonical = 1;
872 +}
873 +/* }}} */
874 +
875 +/* {{{ dio_convert_to_long
876 + * Returns as a long, the value of the zval regardless of its type.
877 + */
878 +long dio_convert_to_long(zval *val) {
879 + zval *copyval;
880 + long longval;
881 +
882 + ALLOC_INIT_ZVAL(copyval);
883 + *copyval = *val;
884 + convert_to_long(copyval);
885 + longval = Z_LVAL_P(copyval);
886 + zval_ptr_dtor(&copyval);
887 +
888 + return longval;
889 +}
890 +/* }}} */
891 +
892 +/* {{{ dio_assoc_array_get_basic_options
893 + * Retrieves the basic open option values from an associative array
894 + */
895 +void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
896 +#if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
897 + zval **tmpzval;
898 + HashTable *opthash;
899 +
900 + opthash = HASH_OF(options);
901 +#endif
902 +
903 +#ifdef DIO_HAS_FILEPERMS
904 + /* This is the file mode flags used by open(). */
905 + if (zend_hash_find(opthash, "perms", sizeof("perms"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
906 + data->perms = (int)dio_convert_to_long(*tmpzval);
907 + data->has_perms = 1;
908 + }
909 +#endif
910 +
911 +#ifdef DIO_NONBLOCK
912 + /* This sets the underlying stream to be blocking/non
913 + block (i.e. O_NONBLOCK) */
914 + if (zend_hash_find(opthash, "is_blocking", sizeof("is_blocking"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
915 + data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
916 + }
917 +
918 + /* This is the timeout value for reads in seconds. Only one of
919 + timeout_secs or timeout_usecs need be defined to define a timeout. */
920 + if (zend_hash_find(opthash, "timeout_secs", sizeof("timeout_secs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
921 + data->timeout_sec = dio_convert_to_long(*tmpzval);
922 + }
923 +
924 + /* This is the timeout value for reads in microseconds. Only one of
925 + timeout_secs or timeout_usecs need be defined to define a timeout. */
926 + if (zend_hash_find(opthash, "timeout_usecs", sizeof("timeout_usecs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
927 + data->timeout_usec = dio_convert_to_long(*tmpzval);
928 + }
929 +
930 + data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
931 +#endif
932 +}
933 +/* }}} */
934 +
935 +/* {{{ dio_assoc_array_get_serial_options
936 + * Retrieves the serial open option values from an associative array
937 + */
938 +void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
939 + zval **tmpzval;
940 + HashTable *opthash;
941 +
942 + opthash = HASH_OF(options);
943 +
944 + if (zend_hash_find(opthash, "data_rate", sizeof("data_rate"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
945 + data->data_rate = dio_convert_to_long(*tmpzval);
946 + }
947 +
948 + if (zend_hash_find(opthash, "data_bits", sizeof("data_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
949 + data->data_bits = (int)dio_convert_to_long(*tmpzval);
950 + }
951 +
952 + if (zend_hash_find(opthash, "stop_bits", sizeof("stop_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
953 + data->stop_bits = (int)dio_convert_to_long(*tmpzval);
954 + }
955 +
956 + if (zend_hash_find(opthash, "parity", sizeof("parity"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
957 + data->parity = (int)dio_convert_to_long(*tmpzval);
958 + }
959 +
960 + if (zend_hash_find(opthash, "flow_control", sizeof("flow_control"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
961 + data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
962 + }
963 +
964 + if (zend_hash_find(opthash, "is_canonical", sizeof("is_canonical"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
965 + data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
966 + }
967 +}
968 +/* }}} */
969 +
970 +/* {{{ dio_stream_context_get_raw_options
971 + * Extracts the option values for dio.raw mode from a context
972 + */
973 +void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
974 +#if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
975 + zval **tmpzval;
976 +#endif
977 +
978 +#ifdef DIO_HAS_FILEPERMS
979 + /* This is the file mode flags used by open(). */
980 + if (php_stream_context_get_option(context, "dio", "perms", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
981 + data->perms = (int)dio_convert_to_long(*tmpzval);
982 + data->has_perms = 1;
983 + }
984 +#endif
985 +
986 +#ifdef DIO_NONBLOCK
987 + /* This sets the underlying stream to be blocking/non
988 + block (i.e. O_NONBLOCK) */
989 + if (php_stream_context_get_option(context, "dio", "is_blocking", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
990 + data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
991 + }
992 +
993 + /* This is the timeout value for reads in seconds. Only one of
994 + timeout_secs or timeout_usecs need be defined to define a timeout. */
995 + if (php_stream_context_get_option(context, "dio", "timeout_secs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
996 + data->timeout_sec = dio_convert_to_long(*tmpzval);
997 + }
998 +
999 + /* This is the timeout value for reads in microseconds. Only one of
1000 + timeout_secs or timeout_usecs need be defined to define a timeout. */
1001 + if (php_stream_context_get_option(context, "dio", "timeout_usecs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1002 + data->timeout_usec = dio_convert_to_long(*tmpzval);
1003 + }
1004 +
1005 + data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
1006 +#endif
1007 +}
1008 +/* }}} */
1009 +
1010 +/* {{{ dio_stream_context_get_serial_options
1011 + * Extracts the option values for dio.serial mode from a context
1012 + */
1013 +void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
1014 + zval **tmpzval;
1015 +
1016 + if (php_stream_context_get_option(context, "dio", "data_rate", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1017 + data->data_rate = dio_convert_to_long(*tmpzval);
1018 + }
1019 +
1020 + if (php_stream_context_get_option(context, "dio", "data_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1021 + data->data_bits = (int)dio_convert_to_long(*tmpzval);
1022 + }
1023 +
1024 + if (php_stream_context_get_option(context, "dio", "stop_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1025 + data->stop_bits = (int)dio_convert_to_long(*tmpzval);
1026 + }
1027 +
1028 + if (php_stream_context_get_option(context, "dio", "parity", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1029 + data->parity = (int)dio_convert_to_long(*tmpzval);
1030 + }
1031 +
1032 + if (php_stream_context_get_option(context, "dio", "flow_control", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1033 + data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1034 + }
1035 +
1036 + if (php_stream_context_get_option(context, "dio", "is_canonical", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
1037 + data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
1038 + }
1039 +}
1040 +/* }}} */
1041 +
1042 +/*
1043 + * Local variables:
1044 + * c-basic-offset: 4
1045 + * tab-width: 4
1046 + * End:
1047 + * vim600: fdm=marker
1048 + * vim: sw=4 ts=4 noet
1049 + */
1050 +
1051 --- /dev/null
1052 +++ b/ext/dio/dio_posix.c
1053 @@ -0,0 +1,659 @@
1054 +/*
1055 + +----------------------------------------------------------------------+
1056 + | PHP Version 5 |
1057 + +----------------------------------------------------------------------+
1058 + | Copyright (c) 2009 Melanie Rhianna Lewis |
1059 + +----------------------------------------------------------------------+
1060 + | This source file is subject to version 3.0 of the PHP license, |
1061 + | that is bundled with this package in the file LICENSE, and is |
1062 + | available through the world-wide-web at the following url: |
1063 + | http://www.php.net/license/3_0.txt. |
1064 + | If you did not receive a copy of the PHP license and are unable to |
1065 + | obtain it through the world-wide-web, please send a note to |
1066 + | license@php.net so we can mail you a copy immediately. |
1067 + +----------------------------------------------------------------------+
1068 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
1069 + +----------------------------------------------------------------------+
1070 + */
1071 +
1072 +#ifdef HAVE_CONFIG_H
1073 +#include "config.h"
1074 +#endif
1075 +
1076 +#include "php.h"
1077 +
1078 +#include "php_dio_common.h"
1079 +
1080 +/* {{{ dio_stream_mode_to_flags
1081 + * Convert an fopen() mode string to open() flags
1082 + */
1083 +static int dio_stream_mode_to_flags(const char *mode) {
1084 + int flags = 0, ch = 0, bin = 1;
1085 +
1086 + switch(mode[ch++]) {
1087 + case 'r':
1088 + flags = 0;
1089 + break;
1090 + case 'w':
1091 + flags = O_TRUNC | O_CREAT;
1092 + break;
1093 + case 'a':
1094 + flags = O_APPEND | O_CREAT;
1095 + break;
1096 + case 'x':
1097 + flags = O_EXCL | O_CREAT;
1098 + break;
1099 + }
1100 +
1101 + if (mode[ch] != '+') {
1102 + bin = (mode[ch++] == 'b');
1103 + }
1104 +
1105 + if (mode[ch] == '+') {
1106 + flags |= O_RDWR;
1107 + } else if (flags) {
1108 + flags |= O_WRONLY;
1109 + } else {
1110 + flags |= O_RDONLY;
1111 + }
1112 +
1113 +#if defined(_O_TEXT) && defined(O_BINARY)
1114 + if (bin) {
1115 + flags |= O_BINARY;
1116 + } else {
1117 + flags |= _O_TEXT;
1118 + }
1119 +#endif
1120 +
1121 + return flags;
1122 +}
1123 +/* }}} */
1124 +
1125 +/* {{{ dio_data_rate_to_define
1126 + * Converts a numeric data rate to a termios define
1127 + */
1128 +static int dio_data_rate_to_define(long rate, speed_t *def) {
1129 + speed_t val;
1130 +
1131 + switch (rate) {
1132 + case 0:
1133 + val = 0;
1134 + break;
1135 + case 50:
1136 + val = B50;
1137 + break;
1138 + case 75:
1139 + val = B75;
1140 + break;
1141 + case 110:
1142 + val = B110;
1143 + break;
1144 + case 134:
1145 + val = B134;
1146 + break;
1147 + case 150:
1148 + val = B150;
1149 + break;
1150 + case 200:
1151 + val = B200;
1152 + break;
1153 + case 300:
1154 + val = B300;
1155 + break;
1156 + case 600:
1157 + val = B600;
1158 + break;
1159 + case 1200:
1160 + val = B1200;
1161 + break;
1162 + case 1800:
1163 + val = B1800;
1164 + break;
1165 + case 2400:
1166 + val = B2400;
1167 + break;
1168 + case 4800:
1169 + val = B4800;
1170 + break;
1171 + case 9600:
1172 + val = B9600;
1173 + break;
1174 + case 19200:
1175 + val = B19200;
1176 + break;
1177 + case 38400:
1178 + val = B38400;
1179 + break;
1180 +#ifdef B57600
1181 + case 57600:
1182 + val = B57600;
1183 + break;
1184 +#endif
1185 +#ifdef B115200
1186 + case 115200:
1187 + val = B115200;
1188 + break;
1189 +#endif
1190 +#ifdef B230400
1191 + case 230400:
1192 + val = B230400;
1193 + break;
1194 +#endif
1195 +#ifdef B460800
1196 + case 460800:
1197 + val = B460800;
1198 + break;
1199 +#endif
1200 + default:
1201 + return 0;
1202 + }
1203 +
1204 + *def = val;
1205 + return 1;
1206 +}
1207 +
1208 +/* {{{ dio_data_bits_to_define
1209 + * Converts a number of data bits to a termios define
1210 + */
1211 +static int dio_data_bits_to_define(int data_bits, int *def) {
1212 + int val;
1213 +
1214 + switch (data_bits) {
1215 + case 8:
1216 + val = CS8;
1217 + break;
1218 + case 7:
1219 + val = CS7;
1220 + break;
1221 + case 6:
1222 + val = CS6;
1223 + break;
1224 + case 5:
1225 + val = CS5;
1226 + break;
1227 + default:
1228 + return 0;
1229 + }
1230 +
1231 + *def = val;
1232 + return 1;
1233 +}
1234 +/* }}} */
1235 +
1236 +/* {{{ dio_stop_bits_to_define
1237 + * Converts a number of stop bits to a termios define
1238 + */
1239 +static int dio_stop_bits_to_define(int stop_bits, int *def) {
1240 + int val;
1241 +
1242 + switch (stop_bits) {
1243 + case 1:
1244 + val = 0;
1245 + break;
1246 + case 2:
1247 + val = CSTOPB;
1248 + break;
1249 + default:
1250 + return 0;
1251 + }
1252 +
1253 + *def = val;
1254 + return 1;
1255 +}
1256 +/* }}} */
1257 +
1258 +/* {{{ dio_parity_to_define
1259 + * Converts a parity type to a termios define
1260 + */
1261 +static int dio_parity_to_define(int parity, int *def) {
1262 + int val;
1263 +
1264 + switch (parity) {
1265 + case 0:
1266 + val = 0;
1267 + break;
1268 + case 1:
1269 + val = PARENB | PARODD;
1270 + break;
1271 + case 2:
1272 + val = PARENB;
1273 + break;
1274 + default:
1275 + return 0;
1276 + }
1277 +
1278 + *def = val;
1279 + return 1;
1280 +}
1281 +/* }}} */
1282 +
1283 +/* {{{ dio_create_stream_data
1284 + * Creates an initialised stream data structure. Free with efree().
1285 + */
1286 +php_dio_stream_data * dio_create_stream_data(void) {
1287 + php_dio_posix_stream_data * data = emalloc(sizeof(php_dio_posix_stream_data));
1288 + dio_init_stream_data(&(data->common));
1289 + data->fd = -1;
1290 + data->flags = 0;
1291 +
1292 + return (php_dio_stream_data *)data;
1293 +}
1294 +/* }}} */
1295 +
1296 +/* {{{ dio_common_write
1297 + * Writes count chars from the buffer to the stream described by the stream data.
1298 + */
1299 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count) {
1300 + size_t ret;
1301 +
1302 + /* Blocking writes can be interrupted by signals etc. If
1303 + * interrupted try again. Not sure about non-blocking
1304 + * writes but it doesn't hurt to check. */
1305 + do {
1306 + ret = write(((php_dio_posix_stream_data*)data)->fd, buf, count);
1307 + if (ret > 0) {
1308 + return ret;
1309 + }
1310 + } while (errno == EINTR);
1311 + return 0;
1312 +}
1313 +/* }}} */
1314 +
1315 +#ifdef DIO_NONBLOCK
1316 +/* {{{ dio_timeval_subtract
1317 + * Calculates the difference between two timevals returning the result in the
1318 + * structure pointed to by diffptr. Returns -1 as error if late time is
1319 + * earlier than early time.
1320 + */
1321 +static int dio_timeval_subtract(struct timeval *late, struct timeval *early, struct timeval *diff) {
1322 + struct timeval *tmp;
1323 +
1324 + /* Handle negatives */
1325 + if (late->tv_sec < early->tv_sec) {
1326 + return 0;
1327 + }
1328 +
1329 + if ((late->tv_sec == early->tv_sec) && (late->tv_usec < early->tv_usec)) {
1330 + return 0;
1331 + }
1332 +
1333 + /* Handle any carry. If later usec is smaller than earlier usec simple
1334 + * subtraction will result in negative value. Since usec has a maximum
1335 + * of one second by adding another second before the subtraction the
1336 + * result will always be positive. */
1337 + if (late->tv_usec < early->tv_usec) {
1338 + late->tv_usec += 1000000;
1339 + late->tv_sec--;
1340 + }
1341 +
1342 + /* Once adjusted can just subtract values. */
1343 + diff->tv_sec = late->tv_sec - early->tv_sec;
1344 + diff->tv_usec = late->tv_usec - early->tv_usec;
1345 +
1346 + return 1;
1347 +}
1348 +#endif
1349 +
1350 +/* {{{ dio_common_read
1351 + * Reads count chars to the buffer to the stream described by the stream data.
1352 + */
1353 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count) {
1354 + int fd = ((php_dio_posix_stream_data*)data)->fd;
1355 + size_t ret, total = 0;
1356 + char *ptr = (char*)buf;
1357 +
1358 + struct timeval timeout, timeouttmp, before, after, diff;
1359 + fd_set rfds;
1360 +
1361 + if (!data->has_timeout) {
1362 + /* Blocking reads can be interrupted by signals etc. If
1363 + * interrupted try again. Not sure about non-blocking
1364 + * reads but it doesn't hurt to check. */
1365 + do {
1366 + ret = read(fd, (char*)ptr, count);
1367 + if (ret > 0) {
1368 + return ret;
1369 + } else if (!ret) {
1370 + data->end_of_file = 1;
1371 + }
1372 + } while ((errno == EINTR) && !data->end_of_file);
1373 + return 0;
1374 + }
1375 +#ifdef DIO_NONBLOCK
1376 + else {
1377 + /* Clear timed out flag */
1378 + data->timed_out = 0;
1379 +
1380 + /* The initial timeout value */
1381 + timeout.tv_sec = data->timeout_sec;
1382 + timeout.tv_usec = data->timeout_usec;
1383 +
1384 + do {
1385 + /* The semantics of select() are that you cannot guarantee
1386 + * that the timeval structure passed in has not been changed by
1387 + * the select call. So you keep a copy. */
1388 + timeouttmp = timeout;
1389 +
1390 + /* The time before we wait for data. */
1391 + (void) gettimeofday(&before, NULL);
1392 +
1393 + /* Wait for an event on our file descriptor. */
1394 + FD_ZERO(&rfds);
1395 + FD_SET(fd, &rfds);
1396 +
1397 + ret = select(fd + 1, &rfds, NULL, NULL, &timeouttmp);
1398 + /* An error. */
1399 + if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN)) {
1400 + return 0;
1401 + }
1402 +
1403 + /* We have data to read. */
1404 + if ((ret > 0) && FD_ISSET(fd, &rfds)) {
1405 + ret = read(fd, ptr, count);
1406 + /* Another error */
1407 + if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN)) {
1408 + return 0;
1409 + }
1410 +
1411 + if (ret > 0) {
1412 + /* Got data, add it to the buffer. */
1413 + ptr += ret;
1414 + total += ret;
1415 + count -= ret;
1416 + } else if (!ret) {
1417 + /* This should never happen since how can we have
1418 + * data to read at an end of file, but still
1419 + * just in case! */
1420 + data->end_of_file = 1;
1421 + break;
1422 + }
1423 + }
1424 +
1425 + /* If not timed out and not end of file and not all data read
1426 + * calculate how long it took us and loop if we still have time
1427 + * out time left. */
1428 + if (count) {
1429 + (void) gettimeofday(&after, NULL);
1430 +
1431 + /* Diff the timevals */
1432 + (void) dio_timeval_subtract(&after, &before, &diff);
1433 +
1434 + /* Now adjust the timeout. */
1435 + if (!dio_timeval_subtract(&timeout, &diff, &timeout)) {
1436 + /* If it errors we've run out of time. */
1437 + data->timed_out = 1;
1438 + break;
1439 + } else if (!timeout.tv_sec && !(timeout.tv_usec / 1000)) {
1440 + /* Check for rounding issues (millisecond accuracy) */
1441 + data->timed_out = 1;
1442 + break;
1443 + }
1444 + }
1445 + } while (count); /* Until time out or end of file or all data read. */
1446 +
1447 + return total;
1448 + }
1449 +#endif
1450 +}
1451 +/* }}} */
1452 +
1453 +/* {{{ php_dio_stream_data
1454 + * Closes the php_stream.
1455 + */
1456 +int dio_common_close(php_dio_stream_data *data) {
1457 + if (close(((php_dio_posix_stream_data*)data)->fd) < 0) {
1458 + return 0;
1459 + }
1460 +
1461 + return 1;
1462 +}
1463 +/* }}} */
1464 +
1465 +/* {{{ dio_common_set_option
1466 + * Sets/gets stream options
1467 + */
1468 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam) {
1469 + int fd = ((php_dio_posix_stream_data*)data)->fd;
1470 + int old_is_blocking;
1471 + int flags;
1472 +
1473 + switch (option) {
1474 +#ifdef DIO_NONBLOCK
1475 + case PHP_STREAM_OPTION_READ_TIMEOUT:
1476 + if (ptrparam) {
1477 + struct timeval *tv = (struct timeval*)ptrparam;
1478 +
1479 + flags = fcntl(fd, F_GETFL, 0);
1480 +
1481 + /* A timeout of zero seconds and zero microseconds disables
1482 + any existing timeout. */
1483 + if (tv->tv_sec || tv->tv_usec) {
1484 + data->timeout_sec = tv->tv_sec;
1485 + data->timeout_usec = tv->tv_usec;
1486 + data->has_timeout = -1;
1487 + (void) fcntl(fd, F_SETFL, flags & ~DIO_NONBLOCK);
1488 + } else {
1489 + data->timeout_sec = 0;
1490 + data->timeout_usec = 0;
1491 + data->has_timeout = 0;
1492 + data->timed_out = 0;
1493 + (void) fcntl(fd, F_SETFL, flags | DIO_NONBLOCK);
1494 + }
1495 +
1496 + return PHP_STREAM_OPTION_RETURN_OK;
1497 + } else {
1498 + return PHP_STREAM_OPTION_RETURN_ERR;
1499 + }
1500 +
1501 + case PHP_STREAM_OPTION_BLOCKING:
1502 + flags = fcntl(fd, F_GETFL, 0);
1503 + if (value) {
1504 + flags &= ~DIO_NONBLOCK;
1505 + } else {
1506 + flags |= DIO_NONBLOCK;
1507 + }
1508 + (void) fcntl(fd, F_SETFL, flags);
1509 +
1510 + old_is_blocking = data->is_blocking;
1511 + data->is_blocking = value;
1512 + return old_is_blocking ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
1513 +#endif /* O_NONBLOCK */
1514 +
1515 + default:
1516 + break;
1517 + }
1518 +
1519 + return 1;
1520 +}
1521 +/* }}} */
1522 +
1523 +/* {{{ dio_raw_open_stream
1524 + * Opens the underlying stream.
1525 + */
1526 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
1527 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1528 + pdata->flags = dio_stream_mode_to_flags(mode);
1529 +
1530 +#ifdef DIO_NONBLOCK
1531 + if (!data->is_blocking || data->has_timeout) {
1532 + pdata->flags |= DIO_NONBLOCK;
1533 + }
1534 +#endif
1535 +
1536 + /* Open the file and handle any errors. */
1537 +#ifdef DIO_HAS_FILEPERMS
1538 + if (data->has_perms) {
1539 + pdata->fd = open(filename, pdata->flags, (mode_t)data->perms);
1540 + } else {
1541 + pdata->fd = open(filename, pdata->flags);
1542 + }
1543 +#else
1544 + pdata->fd = open(filename, pdata->flags);
1545 +#endif
1546 +
1547 + if (pdata->fd < 0) {
1548 + switch (errno) {
1549 + case EEXIST:
1550 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "File exists!");
1551 + return 0;
1552 + default:
1553 + return 0;
1554 + }
1555 + }
1556 +
1557 + return 1;
1558 +}
1559 +/* }}} */
1560 +
1561 +/* {{{ dio_serial_init
1562 + * Initialises the serial settings storing the original settings before hand.
1563 + */
1564 +static int dio_serial_init(php_dio_stream_data *data TSRMLS_DC) {
1565 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1566 + int ret = 0, data_bits_def, stop_bits_def, parity_def;
1567 + struct termios tio;
1568 + speed_t rate_def;
1569 +
1570 + if (!dio_data_rate_to_define(data->data_rate, &rate_def)) {
1571 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_rate value (%ld)", data->data_rate);
1572 + return 0;
1573 + }
1574 +
1575 + if (!dio_data_bits_to_define(data->data_bits, &data_bits_def)) {
1576 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_bits value (%d)", data->data_bits);
1577 + return 0;
1578 + }
1579 +
1580 + if (!dio_stop_bits_to_define(data->stop_bits, &stop_bits_def)) {
1581 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop_bits value (%d)", data->stop_bits);
1582 + return 0;
1583 + }
1584 +
1585 + if (!dio_parity_to_define(data->parity, &parity_def)) {
1586 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity value (%d)", data->parity);
1587 + return 0;
1588 + }
1589 +
1590 + ret = tcgetattr(pdata->fd, &(pdata->oldtio));
1591 + if (ret < 0) {
1592 + if ((errno == ENOTTY) || (errno == ENODEV)) {
1593 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a serial port or terminal!");
1594 + }
1595 + return 0;
1596 + }
1597 +
1598 + ret = tcgetattr(pdata->fd, &tio);
1599 + if (ret < 0) {
1600 + return 0;
1601 + }
1602 +
1603 + if (data->canonical) {
1604 + tio.c_iflag = IGNPAR | ICRNL;
1605 + tio.c_oflag = 0;
1606 + tio.c_lflag = ICANON;
1607 + } else {
1608 + cfmakeraw(&tio);
1609 + }
1610 +
1611 + cfsetispeed(&tio, rate_def);
1612 + cfsetospeed(&tio, rate_def);
1613 +
1614 + tio.c_cflag &= ~CSIZE;
1615 + tio.c_cflag |= data_bits_def;
1616 + tio.c_cflag &= ~CSTOPB;
1617 + tio.c_cflag |= stop_bits_def;
1618 + tio.c_cflag &= ~(PARENB|PARODD);
1619 + tio.c_cflag |= parity_def;
1620 +
1621 +#ifdef CRTSCTS
1622 + tio.c_cflag &= ~(CLOCAL | CRTSCTS);
1623 +#else
1624 + tio.c_cflag &= ~CLOCAL;
1625 +#endif
1626 + if (!data->flow_control) {
1627 + tio.c_cflag |= CLOCAL;
1628 +#ifdef CRTSCTS
1629 + } else {
1630 + tio.c_cflag |= CRTSCTS;
1631 +#endif
1632 + }
1633 +
1634 + ret = tcsetattr(pdata->fd, TCSANOW, &tio);
1635 + if (ret < 0) {
1636 + return 0;
1637 + }
1638 +
1639 + return 1;
1640 +}
1641 +/* }}} */
1642 +
1643 +/* {{{ dio_serial_uninit
1644 + * Restores the serial settings back to their original state.
1645 + */
1646 +int dio_serial_uninit(php_dio_stream_data *data) {
1647 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1648 + int ret;
1649 +
1650 + do {
1651 + ret = tcsetattr(pdata->fd, TCSANOW, &(pdata->oldtio));
1652 + } while ((ret < 0) && (errno == EINTR));
1653 +
1654 + return 1;
1655 +}
1656 +/* }}} */
1657 +
1658 +/* {{{ dio_serial_flush
1659 + * Purges the serial buffers of data.
1660 + */
1661 +int dio_serial_purge(php_dio_stream_data *data) {
1662 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1663 + int ret;
1664 +
1665 + if ((pdata->flags & O_RDWR) == O_RDWR) {
1666 + ret = tcflush(pdata->fd, TCIOFLUSH);
1667 + } else if ((pdata->flags & O_WRONLY) == O_WRONLY) {
1668 + ret = tcflush(pdata->fd, TCOFLUSH);
1669 + } else if ((pdata->flags & O_RDONLY) == O_RDONLY) {
1670 + ret = tcflush(pdata->fd, TCIFLUSH);
1671 + }
1672 +
1673 + if (ret < 0) {
1674 + return 0;
1675 + }
1676 +
1677 + return 1;
1678 +}
1679 +/* }}} */
1680 +
1681 +/* {{{ dio_serial_open_stream
1682 + * Opens the underlying stream.
1683 + */
1684 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
1685 + php_dio_posix_stream_data *pdata = (php_dio_posix_stream_data*)data;
1686 +
1687 +#ifdef O_NOCTTY
1688 + /* We don't want a controlling TTY */
1689 + pdata->flags |= O_NOCTTY;
1690 +#endif
1691 +
1692 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1693 + return 0;
1694 + }
1695 +
1696 + if (!dio_serial_init(data TSRMLS_CC)) {
1697 + close(pdata->fd);
1698 + return 0;
1699 + }
1700 +
1701 + return 1;
1702 +}
1703 +/* }}} */
1704 +
1705 +/*
1706 + * Local variables:
1707 + * c-basic-offset: 4
1708 + * tab-width: 4
1709 + * End:
1710 + * vim600: fdm=marker
1711 + * vim: sw=4 ts=4 noet
1712 + */
1713 --- /dev/null
1714 +++ b/ext/dio/dio_stream_wrappers.c
1715 @@ -0,0 +1,410 @@
1716 +/*
1717 + +----------------------------------------------------------------------+
1718 + | PHP Version 5 |
1719 + +----------------------------------------------------------------------+
1720 + | Copyright (c) 2009 Melanie Rhianna Lewis |
1721 + +----------------------------------------------------------------------+
1722 + | This source file is subject to version 3.0 of the PHP license, |
1723 + | that is bundled with this package in the file LICENSE, and is |
1724 + | available through the world-wide-web at the following url: |
1725 + | http://www.php.net/license/3_0.txt. |
1726 + | If you did not receive a copy of the PHP license and are unable to |
1727 + | obtain it through the world-wide-web, please send a note to |
1728 + | license@php.net so we can mail you a copy immediately. |
1729 + +----------------------------------------------------------------------+
1730 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
1731 + +----------------------------------------------------------------------+
1732 + */
1733 +
1734 +#ifdef HAVE_CONFIG_H
1735 +#include "config.h"
1736 +#endif
1737 +
1738 +#include "php.h"
1739 +#include "ext/standard/url.h"
1740 +
1741 +#include "php_dio.h"
1742 +#include "php_dio_common.h"
1743 +#include "php_dio_stream_wrappers.h"
1744 +
1745 +/*
1746 + +----------------------------------------------------------------------+
1747 + | Raw stream handling |
1748 + +----------------------------------------------------------------------+
1749 +*/
1750 +
1751 +/* {{{ dio_stream_write
1752 + * Write to the stream
1753 + */
1754 +static size_t dio_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
1755 +{
1756 + return dio_common_write((php_dio_stream_data*)stream->abstract, buf, count);
1757 +}
1758 +/* }}} */
1759 +
1760 +/* {{{ dio_stream_read
1761 + * Read from the stream
1762 + */
1763 +static size_t dio_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
1764 +{
1765 + php_dio_stream_data* data = (php_dio_stream_data*)stream->abstract;
1766 + size_t bytes = dio_common_read(data, buf, count);
1767 + stream->eof = data->end_of_file;
1768 +
1769 + return bytes;
1770 +}
1771 +/* }}} */
1772 +
1773 +/* {{{ dio_stream_flush
1774 + * Flush the stream. For raw streams this does nothing.
1775 + */
1776 +static int dio_stream_flush(php_stream *stream TSRMLS_DC)
1777 +{
1778 + return 1;
1779 +}
1780 +/* }}} */
1781 +
1782 +/* {{{ dio_stream_close
1783 + * Close the stream
1784 + */
1785 +static int dio_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
1786 +{
1787 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1788 +
1789 + if (!dio_common_close(abstract)) {
1790 + return 0;
1791 + }
1792 +
1793 + efree(abstract);
1794 + return 1;
1795 +}
1796 +/* }}} */
1797 +
1798 +/* {{{ dio_stream_set_option
1799 + * Set the stream options.
1800 + */
1801 +static int dio_stream_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
1802 +{
1803 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1804 +
1805 + switch (option) {
1806 + case PHP_STREAM_OPTION_META_DATA_API:
1807 +#ifdef DIO_NONBLOCK
1808 + add_assoc_bool((zval *)ptrparam, "timed_out", abstract->timed_out);
1809 + add_assoc_bool((zval *)ptrparam, "blocked", abstract->is_blocking);
1810 +#endif
1811 + add_assoc_bool((zval *)ptrparam, "eof", stream->eof);
1812 + return PHP_STREAM_OPTION_RETURN_OK;
1813 +
1814 +#if PHP_MAJOR_VERSION >= 5
1815 + case PHP_STREAM_OPTION_CHECK_LIVENESS:
1816 + stream->eof = abstract->end_of_file;
1817 + return PHP_STREAM_OPTION_RETURN_OK;
1818 +#endif /* PHP_MAJOR_VERSION >= 5 */
1819 +
1820 + default:
1821 + break;
1822 + }
1823 +
1824 + return dio_common_set_option(abstract, option, value, ptrparam);
1825 +}
1826 +/* }}} */
1827 +
1828 +php_stream_ops dio_raw_stream_ops = {
1829 + dio_stream_write,
1830 + dio_stream_read,
1831 + dio_stream_close,
1832 + dio_stream_flush,
1833 + "dio",
1834 + NULL, /* seek */
1835 + NULL, /* cast */
1836 + NULL, /* stat */
1837 + dio_stream_set_option,
1838 +};
1839 +
1840 +/* {{{ dio_raw_fopen_wrapper
1841 + * fopen for the dio.raw stream.
1842 + */
1843 +static php_stream *dio_raw_fopen_wrapper(php_stream_wrapper *wrapper,
1844 + char *path, char *mode,
1845 + int options, char **opened_path,
1846 + php_stream_context *context STREAMS_DC TSRMLS_DC) {
1847 + php_dio_stream_data *data;
1848 + php_stream *stream;
1849 + char *filename;
1850 +
1851 + /* Check it was actually for us (not a corrupted function pointer
1852 + somewhere!). */
1853 + if (strncmp(path, DIO_RAW_STREAM_PROTOCOL, sizeof(DIO_RAW_STREAM_PROTOCOL) - 1)) {
1854 + return NULL;
1855 + }
1856 +
1857 + /* Get the actually file system name/path. */
1858 + filename = path + sizeof(DIO_RAW_STREAM_PROTOCOL) - 1;
1859 +
1860 + /* Check we can actually access it. */
1861 + if (php_check_open_basedir(filename TSRMLS_CC) ||
1862 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
1863 + return NULL;
1864 + }
1865 +
1866 + data = dio_create_stream_data();
1867 + data->stream_type = DIO_STREAM_TYPE_RAW;
1868 +
1869 + /* Parse the context. */
1870 + if (context) {
1871 + dio_stream_context_get_basic_options(context, data TSRMLS_CC);
1872 + }
1873 +
1874 + /* Try and open a raw stream. */
1875 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1876 + return NULL;
1877 + }
1878 +
1879 + /* Create a PHP stream based on raw stream */
1880 + stream = php_stream_alloc(&dio_raw_stream_ops, data, 0, mode);
1881 + if (!stream) {
1882 + (void) dio_common_close(data);
1883 + efree(data);
1884 + }
1885 +
1886 + return stream;
1887 +}
1888 +/* }}} */
1889 +
1890 +static php_stream_wrapper_ops dio_raw_stream_wops = {
1891 + dio_raw_fopen_wrapper,
1892 + NULL, /* stream_close */
1893 + NULL, /* stat */
1894 + NULL, /* stat_url */
1895 + NULL, /* opendir */
1896 + DIO_RAW_STREAM_NAME
1897 +};
1898 +
1899 +php_stream_wrapper php_dio_raw_stream_wrapper = {
1900 + &dio_raw_stream_wops,
1901 + NULL,
1902 + 0
1903 +};
1904 +
1905 +/* {{{ proto dio_raw(string filename, string mode[, array options])
1906 + * Opens a raw direct IO stream.
1907 + */
1908 +PHP_FUNCTION(dio_raw) {
1909 + zval *options = NULL;
1910 + php_dio_stream_data *data;
1911 + php_stream *stream;
1912 +
1913 + char *filename;
1914 + int filename_len;
1915 + char *mode;
1916 + int mode_len;
1917 +
1918 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
1919 + RETURN_FALSE;
1920 + }
1921 +
1922 + /* Check the third argument is an array. */
1923 + if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
1924 + RETURN_FALSE;
1925 + }
1926 +
1927 + /* Check we can actually access the file. */
1928 + if (php_check_open_basedir(filename TSRMLS_CC) ||
1929 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
1930 + RETURN_FALSE;
1931 + }
1932 +
1933 + data = dio_create_stream_data();
1934 + data->stream_type = DIO_STREAM_TYPE_RAW;
1935 +
1936 + if (options) {
1937 + dio_assoc_array_get_basic_options(options, data TSRMLS_CC);
1938 + }
1939 +
1940 + /* Try and open a raw stream. */
1941 + if (dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
1942 + stream = php_stream_alloc(&dio_raw_stream_ops, data, 0, mode);
1943 + if (!stream) {
1944 + (void) dio_common_close(data);
1945 + efree(data);
1946 + RETURN_FALSE;
1947 + }
1948 + }
1949 +
1950 + php_stream_to_zval(stream, return_value);
1951 +}
1952 +/* }}} */
1953 +
1954 +/*
1955 + +----------------------------------------------------------------------+
1956 + | Serial stream handling |
1957 + +----------------------------------------------------------------------+
1958 +*/
1959 +
1960 +/* {{{ dio_stream_flush
1961 + * Flush the stream. If the stream is read only, it flushes the read
1962 + * stream, if it is write only it flushes the write, otherwise it flushes
1963 + * both.
1964 + */
1965 +static int dio_serial_stream_flush(php_stream *stream TSRMLS_DC)
1966 +{
1967 + return dio_serial_purge((php_dio_stream_data*)stream->abstract);
1968 +}
1969 +/* }}} */
1970 +
1971 +/* {{{ dio_stream_close
1972 + * Close the stream. Restores the serial settings to their value before
1973 + * the stream was open.
1974 + */
1975 +static int dio_serial_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
1976 +{
1977 + php_dio_stream_data *abstract = (php_dio_stream_data*)stream->abstract;
1978 +
1979 + if (!dio_serial_uninit(abstract)) {
1980 + return 0;
1981 + }
1982 +
1983 + if (!dio_common_close(abstract)) {
1984 + return 0;
1985 + }
1986 +
1987 + efree(abstract);
1988 + return 1;
1989 +}
1990 +/* }}} */
1991 +
1992 +php_stream_ops dio_serial_stream_ops = {
1993 + dio_stream_write,
1994 + dio_stream_read,
1995 + dio_serial_stream_close,
1996 + dio_serial_stream_flush,
1997 + "dio",
1998 + NULL, /* seek */
1999 + NULL, /* cast */
2000 + NULL, /* stat */
2001 + dio_stream_set_option,
2002 +};
2003 +
2004 +/* {{{ dio_raw_fopen_wrapper
2005 + * fopen for the dio.raw stream.
2006 + */
2007 +static php_stream *dio_serial_fopen_wrapper(php_stream_wrapper *wrapper,
2008 + char *path, char *mode,
2009 + int options, char **opened_path,
2010 + php_stream_context *context STREAMS_DC TSRMLS_DC) {
2011 + php_dio_stream_data *data;
2012 + php_stream *stream;
2013 + char *filename;
2014 +
2015 + /* Check it was actually for us (not a corrupted function pointer
2016 + somewhere!). */
2017 + if (strncmp(path, DIO_SERIAL_STREAM_PROTOCOL, sizeof(DIO_SERIAL_STREAM_PROTOCOL) - 1)) {
2018 + return NULL;
2019 + }
2020 +
2021 + /* Get the actually file system name/path. */
2022 + filename = path + sizeof(DIO_SERIAL_STREAM_PROTOCOL) - 1;
2023 +
2024 + /* Check we can actually access it. */
2025 + if (php_check_open_basedir(filename TSRMLS_CC) ||
2026 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
2027 + return NULL;
2028 + }
2029 +
2030 + data = dio_create_stream_data();
2031 + data->stream_type = DIO_STREAM_TYPE_SERIAL;
2032 +
2033 + /* Parse the context. */
2034 + if (context) {
2035 + dio_stream_context_get_basic_options(context, data TSRMLS_CC);
2036 + dio_stream_context_get_serial_options(context, data TSRMLS_CC);
2037 + }
2038 +
2039 + /* Try and open a serial stream. */
2040 + if (!dio_serial_open_stream(filename, mode, data TSRMLS_CC)) {
2041 + return NULL;
2042 + }
2043 +
2044 + stream = php_stream_alloc(&dio_serial_stream_ops, data, 0, mode);
2045 + if (!stream) {
2046 + efree(data);
2047 + }
2048 +
2049 + return stream;
2050 +}
2051 +/* }}} */
2052 +
2053 +static php_stream_wrapper_ops dio_serial_stream_wops = {
2054 + dio_serial_fopen_wrapper,
2055 + NULL, /* stream_close */
2056 + NULL, /* stat */
2057 + NULL, /* stat_url */
2058 + NULL, /* opendir */
2059 + DIO_SERIAL_STREAM_NAME
2060 +};
2061 +
2062 +php_stream_wrapper php_dio_serial_stream_wrapper = {
2063 + &dio_serial_stream_wops,
2064 + NULL,
2065 + 0
2066 +};
2067 +
2068 +/* {{{ proto dio_serial(string filename, string mode[, array options])
2069 + * Opens a serial direct IO stream.
2070 + */
2071 +PHP_FUNCTION(dio_serial) {
2072 + zval *options = NULL;
2073 + php_dio_stream_data *data;
2074 + php_stream *stream;
2075 +
2076 + char *filename;
2077 + int filename_len;
2078 + char *mode;
2079 + int mode_len;
2080 +
2081 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|z", &filename, &filename_len, &mode, &mode_len, &options) == FAILURE) {
2082 + RETURN_FALSE;
2083 + }
2084 +
2085 + /* Check the third argument is an array. */
2086 + if (options && (Z_TYPE_P(options) != IS_ARRAY)) {
2087 + php_error_docref(NULL TSRMLS_CC, E_WARNING,"dio_serial, the third argument should be an array of options");
2088 + RETURN_FALSE;
2089 + }
2090 +
2091 + /* Check we can actually access the file. */
2092 + if (php_check_open_basedir(filename TSRMLS_CC) ||
2093 + (PG(safe_mode) && !php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
2094 + RETURN_FALSE;
2095 + }
2096 +
2097 + data = dio_create_stream_data();
2098 + data->stream_type = DIO_STREAM_TYPE_SERIAL;
2099 +
2100 + if (options) {
2101 + dio_assoc_array_get_basic_options(options, data TSRMLS_CC);
2102 + dio_assoc_array_get_serial_options(options, data TSRMLS_CC);
2103 + }
2104 +
2105 + /* Try and open a serial stream. */
2106 + if (dio_serial_open_stream(filename, mode, data TSRMLS_CC)) {
2107 + stream = php_stream_alloc(&dio_serial_stream_ops, data, 0, mode);
2108 + if (!stream) {
2109 + efree(data);
2110 + RETURN_FALSE;
2111 + }
2112 + }
2113 +
2114 + php_stream_to_zval(stream, return_value);
2115 +}
2116 +/* }}} */
2117 +
2118 +/*
2119 + * Local variables:
2120 + * c-basic-offset: 4
2121 + * tab-width: 4
2122 + * End:
2123 + * vim600: fdm=marker
2124 + * vim: sw=4 ts=4 noet
2125 + */
2126 --- /dev/null
2127 +++ b/ext/dio/dio_win32.c
2128 @@ -0,0 +1,669 @@
2129 +/*
2130 + +----------------------------------------------------------------------+
2131 + | PHP Version 5 |
2132 + +----------------------------------------------------------------------+
2133 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2134 + +----------------------------------------------------------------------+
2135 + | This source file is subject to version 3.0 of the PHP license, |
2136 + | that is bundled with this package in the file LICENSE, and is |
2137 + | available through the world-wide-web at the following url: |
2138 + | http://www.php.net/license/3_0.txt. |
2139 + | If you did not receive a copy of the PHP license and are unable to |
2140 + | obtain it through the world-wide-web, please send a note to |
2141 + | license@php.net so we can mail you a copy immediately. |
2142 + +----------------------------------------------------------------------+
2143 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2144 + +----------------------------------------------------------------------+
2145 + */
2146 +
2147 +#ifdef HAVE_CONFIG_H
2148 +#include "config.h"
2149 +#endif
2150 +
2151 +#include "php.h"
2152 +
2153 +#include "php_dio_common.h"
2154 +
2155 +/* {{{ dio_data_rate_to_define
2156 + * Converts a numeric data rate to a termios define
2157 + */
2158 +static int dio_data_rate_to_define(long rate, DWORD *def) {
2159 + switch (rate) {
2160 + case 75:
2161 + case 110:
2162 + case 134:
2163 + case 150:
2164 + case 300:
2165 + case 600:
2166 + case 1200:
2167 + case 1800:
2168 + case 2400:
2169 + case 4800:
2170 + case 7200:
2171 + case 9600:
2172 + case 14400:
2173 + case 19200:
2174 + case 38400:
2175 + case 57600:
2176 + case 115200:
2177 + case 56000:
2178 + case 128000:
2179 + break;
2180 + default:
2181 + return 0;
2182 + }
2183 +
2184 + *def = (DWORD)rate;
2185 + return 1;
2186 +}
2187 +/* }}} */
2188 +
2189 +
2190 +/* {{{ dio_data_bits_to_define
2191 + * Converts a number of data bits to a termios define
2192 + */
2193 +static int dio_data_bits_to_define(int data_bits, DWORD *def) {
2194 + switch (data_bits) {
2195 + case 8:
2196 + case 7:
2197 + case 6:
2198 + case 5:
2199 + case 4:
2200 + break;
2201 + default:
2202 + return 0;
2203 + }
2204 +
2205 + *def = (DWORD)data_bits;
2206 + return 1;
2207 +}
2208 +/* }}} */
2209 +
2210 +/* {{{ dio_stop_bits_to_define
2211 + * Converts a number of stop bits to a termios define
2212 + */
2213 +static int dio_stop_bits_to_define(int stop_bits, DWORD *def) {
2214 + DWORD val;
2215 +
2216 + switch (stop_bits) {
2217 + case 1:
2218 + val = 0;
2219 + break;
2220 + case 2:
2221 + val = 2;
2222 + break;
2223 + case 3:
2224 + val = 1;
2225 + break;
2226 + default:
2227 + return 0;
2228 + }
2229 +
2230 + *def = val;
2231 + return 1;
2232 +}
2233 +/* }}} */
2234 +
2235 +/* {{{ dio_parity_to_define
2236 + * Converts a parity type to a termios define
2237 + */
2238 +static int dio_parity_to_define(int parity, DWORD *def) {
2239 + switch (parity) {
2240 + case 0:
2241 + case 1:
2242 + case 2:
2243 + break;
2244 + default:
2245 + return 0;
2246 + }
2247 +
2248 + *def = (DWORD)parity;
2249 + return 1;
2250 +}
2251 +/* }}} */
2252 +
2253 +/* {{{ dio_create_stream_data
2254 + * Creates an initialised stream data structure. Free with efree().
2255 + */
2256 +php_dio_stream_data * dio_create_stream_data(void) {
2257 + php_dio_win32_stream_data * data = emalloc(sizeof(php_dio_win32_stream_data));
2258 + memset(data, 0, sizeof(php_dio_win32_stream_data));
2259 + dio_init_stream_data(&(data->common));
2260 + data->handle = INVALID_HANDLE_VALUE;
2261 + data->desired_access = 0;
2262 + data->creation_disposition = 0;
2263 + data->olddcb.DCBlength = sizeof(DCB);
2264 +
2265 + return (php_dio_stream_data *)data;
2266 +}
2267 +/* }}} */
2268 +
2269 +/* {{{ dio_common_write
2270 + * Writes count chars from the buffer to the stream described by the stream data.
2271 + */
2272 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count) {
2273 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2274 + DWORD total = 0;
2275 +
2276 + if (WriteFile(wdata->handle, buf, (DWORD)count, &total, NULL)) {
2277 + return (size_t)total;
2278 + }
2279 +
2280 + return 0;
2281 +}
2282 +/* }}} */
2283 +
2284 +/* {{{ dio_buffer_read
2285 + * Reads any available chars from the canonical buffer.
2286 + */
2287 +static size_t dio_buffer_read(php_dio_win32_stream_data *wdata, const char *buf, size_t count) {
2288 + php_dio_win32_canon_data *canon_data = wdata->canon_data;
2289 + size_t total = 0;
2290 +
2291 + /* Read always follows write. I.e. if read ptr > write ptr buffer has
2292 + wrapped and so we need to copy two blocks of data. */
2293 + if (canon_data->read_pos > canon_data->write_pos) {
2294 +
2295 + /* Check we actually need to copy both blocks */
2296 + if ((canon_data->size - canon_data->read_pos) > count) {
2297 +
2298 + /* No we don't. Just copy as much as we were asked for. */
2299 + memcpy((char*)buf,
2300 + &(canon_data->buf[canon_data->read_pos]),
2301 + count);
2302 + /* Update the read pointer. */
2303 + canon_data->read_pos += count;
2304 +
2305 + /* Return the amount read. */
2306 + return count;
2307 + } else {
2308 +
2309 + /* We need to copy both blocks so copy data up to the end of
2310 + the buffer. */
2311 + total = canon_data->size - canon_data->read_pos;
2312 + memcpy((char*)buf,
2313 + &(canon_data->buf[canon_data->read_pos]),
2314 + total);
2315 + canon_data->read_pos = 0;
2316 + count -= total;
2317 +
2318 + /* Now copy the data from the start of the buffer either up
2319 + count or the number of bytes in the buffer. */
2320 +
2321 + if (canon_data->write_pos > count) {
2322 + memcpy((char*)buf, canon_data->buf, count);
2323 + canon_data->read_pos = count;
2324 + total += count;
2325 +
2326 + return total;
2327 + } else {
2328 + memcpy((char*)buf, canon_data->buf, canon_data->write_pos);
2329 + canon_data->read_pos = canon_data->write_pos;
2330 + total += canon_data->write_pos;
2331 +
2332 + return total;
2333 + }
2334 + }
2335 +
2336 + /* Else if write follows read. This is a simpler case. We just copy
2337 + either all the data buffered or count, which ever is smaller. */
2338 + } else if (canon_data->write_pos > canon_data->read_pos) {
2339 + if ((canon_data->write_pos - canon_data->read_pos) > count) {
2340 + memcpy((char*)buf,
2341 + &(canon_data->buf[canon_data->read_pos]),
2342 + count);
2343 + canon_data->read_pos += count;
2344 +
2345 + return count;
2346 + } else {
2347 + total = canon_data->write_pos - canon_data->read_pos;
2348 + memcpy((char*)buf,
2349 + &(canon_data->buf[canon_data->read_pos]),
2350 + total);
2351 + canon_data->read_pos += total;
2352 +
2353 + return total;
2354 + }
2355 + }
2356 +
2357 + /* Else we need to read more data from the data port. */
2358 + return 0;
2359 +}
2360 +
2361 +/* {{{ dio_com_read
2362 + * Read chars from the data port.
2363 + */
2364 +static size_t dio_com_read(php_dio_stream_data *data, const char *buf, size_t count) {
2365 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2366 + DWORD err, total = 0;
2367 +
2368 + if (ReadFile(wdata->handle, (void*)buf, (DWORD)count, &total, NULL)) {
2369 +
2370 + if (total) {
2371 + return (size_t)total;
2372 + }
2373 +
2374 + data->end_of_file = 1;
2375 + }
2376 +
2377 + if (!data->end_of_file) {
2378 + err = GetLastError();
2379 +
2380 + if (ERROR_HANDLE_EOF == err) {
2381 + data->end_of_file = 1;
2382 + }
2383 + }
2384 +
2385 + return 0;
2386 +}
2387 +
2388 +/* {{{ dio_canonical_read
2389 + * Reads chars from the input stream until the internal buffer is full or a new
2390 + * line is reached.
2391 + */
2392 +static size_t dio_canonical_read(php_dio_win32_stream_data *wdata, const char *buf, size_t count) {
2393 + php_dio_win32_canon_data *canon_data = wdata->canon_data;
2394 + size_t total = 0;
2395 + char ch;
2396 +
2397 + /* See if there's any buffered data and copy it. */
2398 + total = dio_buffer_read(wdata, buf, count);
2399 + if (total) {
2400 + return total;
2401 + }
2402 +
2403 + /* Need to read more data from the data port. Buffer should be empty(er)
2404 + by now. */
2405 + do {
2406 + /* Is the buffer full? */
2407 + if (((canon_data->write_pos + 1) % canon_data->size) ==
2408 + canon_data->read_pos) {
2409 + break;
2410 + }
2411 +
2412 + /* Read a byte from the input checking for EOF. */
2413 + if (!dio_com_read((php_dio_stream_data*)wdata, &ch, 1)) {
2414 + break;
2415 + }
2416 +
2417 + /* Translate CR to newlines (same as ICRNL in POSIX) */
2418 + ch = (ch != '\r') ? ch : '\n';
2419 +
2420 + /* We read a character! So buffer it. */
2421 + canon_data->buf[canon_data->write_pos++] = ch;
2422 + if (canon_data->write_pos >= canon_data->size) {
2423 + canon_data->write_pos = 0;
2424 + }
2425 +
2426 + /* End of line/input (^D)? */
2427 + } while ((ch != '\n') && (ch != 0x04));
2428 +
2429 + return dio_buffer_read(wdata, buf, count);
2430 +}
2431 +/* }}} */
2432 +
2433 +/* {{{ dio_common_read
2434 + * Reads count chars to the buffer to the stream described by the stream data.
2435 + */
2436 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count) {
2437 +
2438 + /* You ask for no bytes you'll get none :-) */
2439 + if (!count) {
2440 + return 0;
2441 + }
2442 +
2443 + if (data->canonical) {
2444 + return dio_canonical_read((php_dio_win32_stream_data*)data, buf, count);
2445 + } else {
2446 + return dio_com_read(data, buf, count);
2447 + }
2448 +}
2449 +/* }}} */
2450 +
2451 +/* {{{ php_dio_stream_data
2452 + * Closes the php_stream.
2453 + */
2454 +int dio_common_close(php_dio_stream_data *data) {
2455 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2456 +
2457 + if (data->canonical) {
2458 + efree(wdata->canon_data);
2459 + }
2460 +
2461 + if (!CloseHandle(wdata->handle)) {
2462 + return 0;
2463 + }
2464 +
2465 + return 1;
2466 +}
2467 +/* }}} */
2468 +
2469 +/* {{{ dio_common_set_option
2470 + * Sets/gets stream options
2471 + */
2472 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam) {
2473 + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
2474 + int old_is_blocking = 0;
2475 +
2476 + /* Can't do timeouts or non blocking with raw windows streams :-( */
2477 + if (DIO_STREAM_TYPE_SERIAL == data->stream_type) {
2478 + switch (option) {
2479 + case PHP_STREAM_OPTION_BLOCKING:
2480 + old_is_blocking = data->is_blocking;
2481 + data->is_blocking = value ? 1 : 0;
2482 +
2483 + /* Only change values if we need to change them. */
2484 + if (data->is_blocking != old_is_blocking) {
2485 + /* If we're not blocking but don't have a timeout
2486 + set to return immediately */
2487 + if (!data->is_blocking && !data->has_timeout) {
2488 + cto.ReadIntervalTimeout = MAXDWORD;
2489 + }
2490 +
2491 + /* If we have a timeout ignore the blocking and set
2492 + the total time in which to read the data */
2493 + if (data->has_timeout) {
2494 + cto.ReadIntervalTimeout = MAXDWORD;
2495 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2496 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2497 + (data->timeout_sec * 1000);
2498 + }
2499 +
2500 + if (!SetCommTimeouts(((php_dio_win32_stream_data*)data)->handle, &cto)) {
2501 + return PHP_STREAM_OPTION_RETURN_ERR;
2502 + }
2503 + }
2504 + return old_is_blocking ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
2505 +
2506 + case PHP_STREAM_OPTION_READ_TIMEOUT:
2507 + if (ptrparam) {
2508 + /* struct timeval is supported with PHP_WIN32 defined. */
2509 + struct timeval *tv = (struct timeval*)ptrparam;
2510 +
2511 + /* A timeout of zero seconds and zero microseconds disables
2512 + any existing timeout. */
2513 + if (tv->tv_sec || tv->tv_usec) {
2514 + data->timeout_sec = tv->tv_sec;
2515 + data->timeout_usec = tv->tv_usec;
2516 + data->has_timeout = -1;
2517 +
2518 + cto.ReadIntervalTimeout = MAXDWORD;
2519 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2520 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2521 + (data->timeout_sec * 1000);
2522 + } else {
2523 + data->timeout_sec = 0;
2524 + data->timeout_usec = 0;
2525 + data->has_timeout = 0;
2526 + data->timed_out = 0;
2527 +
2528 + /* If we're not blocking but don't have a timeout
2529 + set to return immediately */
2530 + if (!data->is_blocking) {
2531 + cto.ReadIntervalTimeout = MAXDWORD;
2532 + }
2533 + }
2534 +
2535 + if (!SetCommTimeouts(((php_dio_win32_stream_data*)data)->handle, &cto)) {
2536 + return PHP_STREAM_OPTION_RETURN_ERR;
2537 + } else {
2538 + return PHP_STREAM_OPTION_RETURN_OK;
2539 + }
2540 + } else {
2541 + return PHP_STREAM_OPTION_RETURN_ERR;
2542 + }
2543 +
2544 + default:
2545 + break;
2546 + }
2547 + }
2548 +
2549 + return 1;
2550 +}
2551 +/* }}} */
2552 +
2553 +/* {{{ dio_raw_open_stream
2554 + * Opens the underlying stream.
2555 + */
2556 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
2557 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2558 + DWORD err;
2559 +
2560 + switch(*mode) {
2561 + case 'r':
2562 + wdata->creation_disposition = OPEN_EXISTING;
2563 + break;
2564 + case 'w':
2565 + wdata->creation_disposition = TRUNCATE_EXISTING;
2566 + break;
2567 + case 'a':
2568 + wdata->creation_disposition = OPEN_ALWAYS;
2569 + break;
2570 + case 'x':
2571 + wdata->creation_disposition = CREATE_NEW;
2572 + break;
2573 + }
2574 + mode ++;
2575 +
2576 + if (*mode && (*mode != '+')) {
2577 + mode++;
2578 + }
2579 +
2580 + if (*mode && (*mode == '+')) {
2581 + wdata->desired_access = GENERIC_READ | GENERIC_WRITE;
2582 + } else if (OPEN_EXISTING == wdata->creation_disposition) {
2583 + wdata->desired_access = GENERIC_READ;
2584 + } else {
2585 + wdata->desired_access = GENERIC_WRITE;
2586 + }
2587 +
2588 + wdata->handle = CreateFile(filename, wdata->desired_access, 0,
2589 + NULL, wdata->creation_disposition, FILE_ATTRIBUTE_NORMAL, NULL);
2590 + if (INVALID_HANDLE_VALUE == wdata->handle) {
2591 + err = GetLastError();
2592 + switch (err) {
2593 + case ERROR_FILE_EXISTS:
2594 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "File exists!");
2595 + return 0;
2596 +
2597 + case ERROR_FILE_NOT_FOUND:
2598 + /* ERROR_FILE_NOT_FOUND with TRUNCATE_EXISTING means that
2599 + * the file doesn't exist so now try to create it. */
2600 + if (TRUNCATE_EXISTING == wdata->creation_disposition) {
2601 + wdata->handle = CreateFile(filename, wdata->desired_access, 0,
2602 + NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2603 + if (INVALID_HANDLE_VALUE == wdata->handle) {
2604 + err = GetLastError();
2605 + return 0;
2606 + }
2607 + } else {
2608 + return 0;
2609 + }
2610 + break;
2611 +
2612 + default:
2613 + return 0;
2614 + }
2615 + }
2616 +
2617 + /* If canonical allocate the canonical buffer. */
2618 + if (data->canonical) {
2619 + wdata->canon_data = emalloc(sizeof(php_dio_win32_canon_data));
2620 + memset(wdata->canon_data, 0, sizeof(php_dio_win32_canon_data));
2621 + wdata->canon_data->size = DIO_WIN32_CANON_BUF_SIZE;
2622 + }
2623 +
2624 + return 1;
2625 +}
2626 +/* }}} */
2627 +
2628 +/* {{{ dio_serial_init
2629 + * Initialises the serial port
2630 + */
2631 +static int dio_serial_init(php_dio_stream_data *data TSRMLS_DC) {
2632 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2633 + DWORD err, rate_def, data_bits_def, stop_bits_def, parity_def;
2634 + DCB dcb;
2635 +
2636 + if (!dio_data_rate_to_define(data->data_rate, &rate_def)) {
2637 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_rate value (%d) (%d)", data->data_rate, __LINE__);
2638 + return 0;
2639 + }
2640 +
2641 + if (!dio_data_bits_to_define(data->data_bits, &data_bits_def)) {
2642 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid data_bits value (%d)", data->data_bits);
2643 + return 0;
2644 + }
2645 +
2646 + if (!dio_stop_bits_to_define(data->stop_bits, &stop_bits_def)) {
2647 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid stop_bits value (%d)", data->stop_bits);
2648 + return 0;
2649 + }
2650 +
2651 + if (!dio_parity_to_define(data->parity, &parity_def)) {
2652 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid parity value (%d)", data->parity);
2653 + return 0;
2654 + }
2655 +
2656 + if (!GetCommState(wdata->handle, &(wdata->olddcb))) {
2657 + err = GetLastError();
2658 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "GetCommState() failed! (%d)", err);
2659 + return 0;
2660 + }
2661 +
2662 + /* Init the DCB structure */
2663 + memset(&dcb, 0, sizeof(DCB));
2664 + dcb.DCBlength = sizeof(DCB);
2665 +
2666 + /* Set the communication parameters */
2667 + dcb.fBinary = 1;
2668 + dcb.BaudRate = rate_def;
2669 + dcb.ByteSize = (BYTE)data_bits_def;
2670 + dcb.StopBits = (BYTE)stop_bits_def;
2671 + dcb.Parity = (BYTE)parity_def;
2672 +
2673 + /* Set the control line parameters */
2674 + dcb.fDtrControl = DTR_CONTROL_DISABLE;
2675 + dcb.fDsrSensitivity = FALSE;
2676 + dcb.fOutxDsrFlow = FALSE;
2677 + dcb.fTXContinueOnXoff = FALSE;
2678 + dcb.fOutX = FALSE;
2679 + dcb.fInX = FALSE;
2680 + dcb.fErrorChar = FALSE;
2681 + dcb.fNull = FALSE;
2682 + dcb.fAbortOnError = FALSE;
2683 +
2684 + /* Hardware flow control */
2685 + if (data->flow_control) {
2686 + dcb.fOutxCtsFlow = TRUE;
2687 + dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
2688 + } else {
2689 + dcb.fOutxCtsFlow = FALSE;
2690 + dcb.fRtsControl = RTS_CONTROL_DISABLE;
2691 + }
2692 +
2693 + if (!SetCommState(wdata->handle, &dcb)) {
2694 + return 0;
2695 + }
2696 +
2697 + return 1;
2698 +}
2699 +/* }}} */
2700 +
2701 +
2702 +/* {{{ dio_serial_uninit
2703 + * Restores the serial settings back to their original state.
2704 + */
2705 +int dio_serial_uninit(php_dio_stream_data *data) {
2706 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2707 +
2708 + if (!SetCommState(wdata->handle, &(wdata->olddcb))) {
2709 + return 0;
2710 + }
2711 +
2712 + return 1;
2713 +}
2714 +/* }}} */
2715 +
2716 +/* {{{ dio_serial_flush
2717 + * Purges the serial buffers of data.
2718 + */
2719 +int dio_serial_purge(php_dio_stream_data *data) {
2720 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2721 + BOOL ret;
2722 +
2723 + /* Purge the canonical buffer if required */
2724 + if (data->canonical && ((wdata->desired_access & GENERIC_READ) == GENERIC_READ)) {
2725 + wdata->canon_data->read_pos = 0;
2726 + wdata->canon_data->write_pos = 0;
2727 + }
2728 +
2729 + /* Purge the com port */
2730 + if ((wdata->desired_access & (GENERIC_READ|GENERIC_WRITE)) == (GENERIC_READ|GENERIC_WRITE)) {
2731 + ret = PurgeComm(wdata->handle, PURGE_RXCLEAR|PURGE_TXCLEAR);
2732 + } else if ((wdata->desired_access & GENERIC_WRITE) == GENERIC_WRITE) {
2733 + ret = PurgeComm(wdata->handle, PURGE_TXCLEAR);
2734 + } else if ((wdata->desired_access & GENERIC_READ) == GENERIC_READ) {
2735 + ret = PurgeComm(wdata->handle, PURGE_RXCLEAR);
2736 + }
2737 +
2738 + return ret;
2739 +}
2740 +/* }}} */
2741 +
2742 +/* {{{ dio_serial_open_stream
2743 + * Opens the underlying stream.
2744 + */
2745 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC) {
2746 + php_dio_win32_stream_data *wdata = (php_dio_win32_stream_data*)data;
2747 + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
2748 + DWORD err;
2749 +
2750 + if (!dio_raw_open_stream(filename, mode, data TSRMLS_CC)) {
2751 + return 0;
2752 + }
2753 +
2754 + if (!GetCommTimeouts(wdata->handle, &(wdata->oldcto))) {
2755 + err = GetLastError();
2756 + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SetCommTimeouts() failed! (%d) Not a comm port?", err);
2757 + CloseHandle(wdata->handle);
2758 + return 0;
2759 + }
2760 +
2761 + /* If we're not blocking but don't have a timeout
2762 + set to return immediately */
2763 + if (!data->is_blocking && !data->has_timeout) {
2764 + cto.ReadIntervalTimeout = MAXDWORD;
2765 + }
2766 +
2767 + /* If we have a timeout ignore the blocking and set
2768 + the total time in which to read the data */
2769 + if (data->has_timeout) {
2770 + cto.ReadIntervalTimeout = MAXDWORD;
2771 + cto.ReadTotalTimeoutMultiplier = MAXDWORD;
2772 + cto.ReadTotalTimeoutConstant = (data->timeout_usec / 1000) +
2773 + (data->timeout_sec * 1000);
2774 + }
2775 +
2776 + if (!SetCommTimeouts(wdata->handle, &cto)) {
2777 + CloseHandle(wdata->handle);
2778 + return 0;
2779 + }
2780 +
2781 + if (!dio_serial_init(data TSRMLS_CC)) {
2782 + CloseHandle(wdata->handle);
2783 + return 0;
2784 + }
2785 +
2786 + return 1;
2787 +}
2788 +/* }}} */
2789 +
2790 +/*
2791 + * Local variables:
2792 + * c-basic-offset: 4
2793 + * tab-width: 4
2794 + * End:
2795 + * vim600: fdm=marker
2796 + * vim: sw=4 ts=4 noet
2797 + */
2798 --- /dev/null
2799 +++ b/ext/dio/php_dio.h
2800 @@ -0,0 +1,60 @@
2801 +/*
2802 + +----------------------------------------------------------------------+
2803 + | PHP Version 5 |
2804 + +----------------------------------------------------------------------+
2805 + | Copyright (c) 1997-2004 The PHP Group |
2806 + +----------------------------------------------------------------------+
2807 + | This source file is subject to version 3.0 of the PHP license, |
2808 + | that is bundled with this package in the file LICENSE, and is |
2809 + | available through the world-wide-web at the following url: |
2810 + | http://www.php.net/license/3_0.txt. |
2811 + | If you did not receive a copy of the PHP license and are unable to |
2812 + | obtain it through the world-wide-web, please send a note to |
2813 + | license@php.net so we can mail you a copy immediately. |
2814 + +----------------------------------------------------------------------+
2815 + */
2816 +
2817 +#ifndef PHP_DIO_H
2818 +#define PHP_DIO_H
2819 +
2820 +#include "php.h"
2821 +#include "php_dio_common.h"
2822 +#include "php_dio_stream_wrappers.h"
2823 +
2824 +extern zend_module_entry dio_module_entry;
2825 +#define phpext_dio_ptr &dio_module_entry
2826 +
2827 +#define PHP_DIO_VERSION "0.0.4RC4"
2828 +
2829 +/* Standard module functions. */
2830 +PHP_MINIT_FUNCTION(dio);
2831 +PHP_MSHUTDOWN_FUNCTION(dio);
2832 +PHP_RINIT_FUNCTION(dio);
2833 +PHP_RSHUTDOWN_FUNCTION(dio);
2834 +PHP_MINFO_FUNCTION(dio);
2835 +
2836 +/* Legacy functions. */
2837 +PHP_FUNCTION(dio_open);
2838 +PHP_FUNCTION(dio_truncate);
2839 +PHP_FUNCTION(dio_stat);
2840 +PHP_FUNCTION(dio_seek);
2841 +PHP_FUNCTION(dio_read);
2842 +PHP_FUNCTION(dio_write);
2843 +PHP_FUNCTION(dio_fcntl);
2844 +PHP_FUNCTION(dio_close);
2845 +PHP_FUNCTION(dio_tcsetattr);
2846 +
2847 +typedef struct {
2848 + int fd;
2849 +} php_fd_t;
2850 +
2851 +#endif
2852 +
2853 +
2854 +/*
2855 + * Local variables:
2856 + * tab-width: 4
2857 + * c-basic-offset: 4
2858 + * indent-tabs-mode: t
2859 + * End:
2860 + */
2861 --- /dev/null
2862 +++ b/ext/dio/php_dio_common.h
2863 @@ -0,0 +1,77 @@
2864 +/*
2865 + +----------------------------------------------------------------------+
2866 + | PHP Version 5 |
2867 + +----------------------------------------------------------------------+
2868 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2869 + +----------------------------------------------------------------------+
2870 + | This source file is subject to version 3.0 of the PHP license, |
2871 + | that is bundled with this package in the file LICENSE, and is |
2872 + | available through the world-wide-web at the following url: |
2873 + | http://www.php.net/license/3_0.txt. |
2874 + | If you did not receive a copy of the PHP license and are unable to |
2875 + | obtain it through the world-wide-web, please send a note to |
2876 + | license@php.net so we can mail you a copy immediately. |
2877 + +----------------------------------------------------------------------+
2878 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2879 + +----------------------------------------------------------------------+
2880 + */
2881 +
2882 +#ifndef PHP_DIO_COMMON_H_
2883 +#define PHP_DIO_COMMON_H_
2884 +
2885 +#ifdef PHP_WIN32
2886 +#define PHP_DIO_API __declspec(dllexport)
2887 +#else
2888 +#define PHP_DIO_API
2889 +#endif
2890 +
2891 +#ifdef PHP_WIN32
2892 +#include "php_dio_win32.h"
2893 +#else
2894 +#include "php_dio_posix.h"
2895 +#endif
2896 +
2897 +#define DIO_STREAM_TYPE_NONE 0
2898 +#define DIO_STREAM_TYPE_RAW 1
2899 +#define DIO_STREAM_TYPE_SERIAL 2
2900 +
2901 +long dio_convert_to_long(zval *val);
2902 +
2903 +php_dio_stream_data * dio_create_stream_data(void);
2904 +
2905 +void dio_init_stream_data(php_dio_stream_data *data);
2906 +
2907 +void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC);
2908 +
2909 +void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC);
2910 +
2911 +void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC);
2912 +
2913 +void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC);
2914 +
2915 +size_t dio_common_write(php_dio_stream_data *data, const char *buf, size_t count);
2916 +
2917 +size_t dio_common_read(php_dio_stream_data *data, const char *buf, size_t count);
2918 +
2919 +int dio_common_close(php_dio_stream_data *data);
2920 +
2921 +int dio_common_set_option(php_dio_stream_data *data, int option, int value, void *ptrparam);
2922 +
2923 +int dio_raw_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC);
2924 +
2925 +int dio_serial_uninit(php_dio_stream_data *data);
2926 +
2927 +int dio_serial_purge(php_dio_stream_data *data);
2928 +
2929 +int dio_serial_open_stream(char *filename, char *mode, php_dio_stream_data *data TSRMLS_DC);
2930 +
2931 +#endif /* PHP_DIO_COMMON_H_ */
2932 +
2933 +/*
2934 + * Local variables:
2935 + * c-basic-offset: 4
2936 + * tab-width: 4
2937 + * End:
2938 + * vim600: fdm=marker
2939 + * vim: sw=4 ts=4 noet
2940 + */
2941 --- /dev/null
2942 +++ b/ext/dio/php_dio_common_data.h
2943 @@ -0,0 +1,59 @@
2944 +/*
2945 + +----------------------------------------------------------------------+
2946 + | PHP Version 5 |
2947 + +----------------------------------------------------------------------+
2948 + | Copyright (c) 2009 Melanie Rhianna Lewis |
2949 + +----------------------------------------------------------------------+
2950 + | This source file is subject to version 3.0 of the PHP license, |
2951 + | that is bundled with this package in the file LICENSE, and is |
2952 + | available through the world-wide-web at the following url: |
2953 + | http://www.php.net/license/3_0.txt. |
2954 + | If you did not receive a copy of the PHP license and are unable to |
2955 + | obtain it through the world-wide-web, please send a note to |
2956 + | license@php.net so we can mail you a copy immediately. |
2957 + +----------------------------------------------------------------------+
2958 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
2959 + +----------------------------------------------------------------------+
2960 + */
2961 +
2962 +#ifndef PHP_DIO_COMMON_DATA_H_
2963 +#define PHP_DIO_COMMON_DATA_H_
2964 +
2965 +/* This is the data structure 'base class'. It is common data fields used
2966 + * by all versions of DIO.
2967 + */
2968 +typedef struct _php_dio_stream_data {
2969 + /* Stream type */
2970 + int stream_type;
2971 + /* Stream options */
2972 + int end_of_file;
2973 +#ifdef DIO_HAS_FILEPERMS
2974 + int has_perms;
2975 + int perms;
2976 +#endif
2977 +#ifdef DIO_NONBLOCK
2978 + int is_blocking;
2979 + int has_timeout;
2980 + long timeout_sec;
2981 + long timeout_usec;
2982 + int timed_out;
2983 +#endif
2984 + /* Serial options */
2985 + long data_rate;
2986 + int data_bits;
2987 + int stop_bits;
2988 + int parity;
2989 + int flow_control;
2990 + int canonical;
2991 +} php_dio_stream_data ;
2992 +
2993 +#endif /* PHP_DIO_COMMON_DATA_H_ */
2994 +
2995 +/*
2996 + * Local variables:
2997 + * c-basic-offset: 4
2998 + * tab-width: 4
2999 + * End:
3000 + * vim600: fdm=marker
3001 + * vim: sw=4 ts=4 noet
3002 + */
3003 --- /dev/null
3004 +++ b/ext/dio/php_dio_posix.h
3005 @@ -0,0 +1,70 @@
3006 +/*
3007 + +----------------------------------------------------------------------+
3008 + | PHP Version 5 |
3009 + +----------------------------------------------------------------------+
3010 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3011 + +----------------------------------------------------------------------+
3012 + | This source file is subject to version 3.0 of the PHP license, |
3013 + | that is bundled with this package in the file LICENSE, and is |
3014 + | available through the world-wide-web at the following url: |
3015 + | http://www.php.net/license/3_0.txt. |
3016 + | If you did not receive a copy of the PHP license and are unable to |
3017 + | obtain it through the world-wide-web, please send a note to |
3018 + | license@php.net so we can mail you a copy immediately. |
3019 + +----------------------------------------------------------------------+
3020 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3021 + +----------------------------------------------------------------------+
3022 + */
3023 +
3024 +#ifndef PHP_DIO_POSIX_H_
3025 +#define PHP_DIO_POSIX_H_
3026 +
3027 +#include <sys/stat.h>
3028 +#include <sys/types.h>
3029 +#include <sys/select.h>
3030 +#include <sys/time.h>
3031 +
3032 +#ifdef HAVE_UNISTD_H
3033 +#include <unistd.h>
3034 +#endif
3035 +
3036 +#include <fcntl.h>
3037 +#include <termios.h>
3038 +
3039 +
3040 +/**
3041 + * Detect if we can support non blocking IO.
3042 + */
3043 +#ifdef O_NONBLOCK
3044 +#define DIO_NONBLOCK O_NONBLOCK
3045 +#else
3046 +#ifdef O_NDELAY
3047 +#define DIO_NONBLOCK O_NDELAY
3048 +#endif
3049 +#endif
3050 +
3051 +/**
3052 + * POSIXy platforms have file permissions
3053 + */
3054 +#define DIO_HAS_FILEPERMS
3055 +
3056 +#include "php_dio_common_data.h"
3057 +
3058 +typedef struct _php_dio_posix_stream_data {
3059 + php_dio_stream_data common;
3060 + int fd;
3061 + int flags;
3062 + /* Serial options */
3063 + struct termios oldtio;
3064 +} php_dio_posix_stream_data ;
3065 +
3066 +#endif /* PHP_DIO_POSIX_H_ */
3067 +
3068 +/*
3069 + * Local variables:
3070 + * c-basic-offset: 4
3071 + * tab-width: 4
3072 + * End:
3073 + * vim600: fdm=marker
3074 + * vim: sw=4 ts=4 noet
3075 + */
3076 --- /dev/null
3077 +++ b/ext/dio/php_dio_stream_wrappers.h
3078 @@ -0,0 +1,44 @@
3079 +/*
3080 + +----------------------------------------------------------------------+
3081 + | PHP Version 5 |
3082 + +----------------------------------------------------------------------+
3083 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3084 + +----------------------------------------------------------------------+
3085 + | This source file is subject to version 3.0 of the PHP license, |
3086 + | that is bundled with this package in the file LICENSE, and is |
3087 + | available through the world-wide-web at the following url: |
3088 + | http://www.php.net/license/3_0.txt. |
3089 + | If you did not receive a copy of the PHP license and are unable to |
3090 + | obtain it through the world-wide-web, please send a note to |
3091 + | license@php.net so we can mail you a copy immediately. |
3092 + +----------------------------------------------------------------------+
3093 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3094 + +----------------------------------------------------------------------+
3095 + */
3096 +
3097 +#ifndef PHP_DIO_STREAM_WRAPPERS_H_
3098 +#define PHP_DIO_STREAM_WRAPPERS_H_
3099 +
3100 +#define DIO_RAW_STREAM_NAME "dio.raw"
3101 +#define DIO_RAW_STREAM_PROTOCOL "dio.raw://"
3102 +#define DIO_SERIAL_STREAM_NAME "dio.serial"
3103 +#define DIO_SERIAL_STREAM_PROTOCOL "dio.serial://"
3104 +
3105 +extern php_stream_wrapper php_dio_raw_stream_wrapper;
3106 +
3107 +PHP_FUNCTION(dio_raw);
3108 +
3109 +extern php_stream_wrapper php_dio_serial_stream_wrapper;
3110 +
3111 +PHP_FUNCTION(dio_serial);
3112 +
3113 +#endif /* PHP_DIO_STREAM_WRAPPERS_H_ */
3114 +
3115 +/*
3116 + * Local variables:
3117 + * c-basic-offset: 4
3118 + * tab-width: 4
3119 + * End:
3120 + * vim600: fdm=marker
3121 + * vim: sw=4 ts=4 noet
3122 + */
3123 --- /dev/null
3124 +++ b/ext/dio/php_dio_win32.h
3125 @@ -0,0 +1,62 @@
3126 +/*
3127 + +----------------------------------------------------------------------+
3128 + | PHP Version 5 |
3129 + +----------------------------------------------------------------------+
3130 + | Copyright (c) 2009 Melanie Rhianna Lewis |
3131 + +----------------------------------------------------------------------+
3132 + | This source file is subject to version 3.0 of the PHP license, |
3133 + | that is bundled with this package in the file LICENSE, and is |
3134 + | available through the world-wide-web at the following url: |
3135 + | http://www.php.net/license/3_0.txt. |
3136 + | If you did not receive a copy of the PHP license and are unable to |
3137 + | obtain it through the world-wide-web, please send a note to |
3138 + | license@php.net so we can mail you a copy immediately. |
3139 + +----------------------------------------------------------------------+
3140 + | Author: Melanie Rhianna Lewis <cyberspice@php.net> |
3141 + +----------------------------------------------------------------------+
3142 + */
3143 +
3144 +#ifndef PHP_DIO_WIN32_H_
3145 +#define PHP_DIO_WIN32_H_
3146 +
3147 +#include <windows.h>
3148 +
3149 +/* Windows platform can do non blocking. */
3150 +#define DIO_NONBLOCK
3151 +
3152 +#include "php_dio_common_data.h"
3153 +
3154 +#define DIO_WIN32_CANON_BUF_SIZE 8192
3155 +
3156 +/* This is the buffer information when reading in canonical mode. Data is
3157 + read right up to either buffer being full or a newline being read. Excess
3158 + data will be retained in the buffer until the next read. */
3159 +typedef struct _php_dio_win32_canon_data {
3160 + size_t size;
3161 + size_t read_pos;
3162 + size_t write_pos;
3163 + char buf[DIO_WIN32_CANON_BUF_SIZE];
3164 +
3165 +} php_dio_win32_canon_data;
3166 +
3167 +typedef struct _php_dio_win32_stream_data {
3168 + php_dio_stream_data common;
3169 + HANDLE handle;
3170 + DWORD desired_access;
3171 + DWORD creation_disposition;
3172 + DCB olddcb;
3173 + COMMTIMEOUTS oldcto;
3174 + php_dio_win32_canon_data *canon_data;
3175 +
3176 +} php_dio_win32_stream_data ;
3177 +
3178 +#endif /* PHP_DIO_WIN32_H_ */
3179 +
3180 +/*
3181 + * Local variables:
3182 + * c-basic-offset: 4
3183 + * tab-width: 4
3184 + * End:
3185 + * vim600: fdm=marker
3186 + * vim: sw=4 ts=4 noet
3187 + */
3188 --- /dev/null
3189 +++ b/ext/dio/tests/001.phpt
3190 @@ -0,0 +1,23 @@
3191 +--TEST--
3192 +Test dio legacy open
3193 +--SKIPIF--
3194 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3195 +--FILE--
3196 +<?php
3197 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3198 +
3199 + if (!$iswin) {
3200 + $filename = "/dev/null";
3201 + } else {
3202 + $filename = "NUL";
3203 + }
3204 +
3205 + $f = dio_open($filename,O_RDONLY);
3206 + if ($f) {
3207 + echo "Legacy open passed";
3208 + } else {
3209 + echo "Legacy open failed";
3210 + }
3211 +?>
3212 +--EXPECT--
3213 +Legacy open passed
3214 --- /dev/null
3215 +++ b/ext/dio/tests/dio_raw_stream_001.phpt
3216 @@ -0,0 +1,24 @@
3217 +--TEST--
3218 +Test dio raw stream open
3219 +--SKIPIF--
3220 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3221 +--FILE--
3222 +<?php
3223 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3224 +
3225 + if (!$iswin) {
3226 + $filename = "dio.raw:///dev/null";
3227 + } else {
3228 + $filename = "dio.raw://NUL";
3229 + }
3230 +
3231 + $f = fopen($filename, "r+");
3232 + if ($f) {
3233 + echo "Raw open passed";
3234 + fclose($f);
3235 + } else {
3236 + echo "Raw open failed";
3237 + }
3238 +?>
3239 +--EXPECT--
3240 +Raw open passed
3241 --- /dev/null
3242 +++ b/ext/dio/tests/dio_raw_stream_002.phpt
3243 @@ -0,0 +1,27 @@
3244 +--TEST--
3245 +Test dio raw stream close
3246 +--SKIPIF--
3247 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3248 +--FILE--
3249 +<?php
3250 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3251 +
3252 + if (!$iswin) {
3253 + $filename = "dio.raw:///dev/null";
3254 + } else {
3255 + $filename = "dio.raw://NUL";
3256 + }
3257 +
3258 + $f = fopen($filename, "r+");
3259 + if ($f) {
3260 + if (fclose($f)) {
3261 + echo "Raw close passed";
3262 + } else {
3263 + echo "Raw close failed";
3264 + }
3265 + } else {
3266 + echo "Raw open failed";
3267 + }
3268 +?>
3269 +--EXPECT--
3270 +Raw close passed
3271 --- /dev/null
3272 +++ b/ext/dio/tests/dio_raw_stream_003.phpt
3273 @@ -0,0 +1,29 @@
3274 +--TEST--
3275 +Test dio raw stream write
3276 +--SKIPIF--
3277 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3278 +--FILE--
3279 +<?php
3280 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3281 +
3282 + if (!$iswin) {
3283 + $filename = "dio.raw:///dev/null";
3284 + } else {
3285 + $filename = "dio.raw://NUL";
3286 + }
3287 +
3288 + $f = fopen($filename, "r+");
3289 + if ($f) {
3290 + $data = str_repeat("+", 2048);
3291 + if (fwrite($f, $data)) {
3292 + echo "Raw write passed";
3293 + } else {
3294 + echo "Raw write failed";
3295 + }
3296 + fclose($f);
3297 + } else {
3298 + echo "Raw open failed";
3299 + }
3300 +?>
3301 +--EXPECT--
3302 +Raw write passed
3303 --- /dev/null
3304 +++ b/ext/dio/tests/dio_raw_stream_004.phpt
3305 @@ -0,0 +1,46 @@
3306 +--TEST--
3307 +Test dio raw read
3308 +--SKIPIF--
3309 +<?php
3310 + if (!extension_loaded('dio')) print 'skip';
3311 +?>
3312 +--FILE--
3313 +<?php
3314 + // Create a temp file with some content to read
3315 +
3316 + // Create the temp file name
3317 + if (!function_exists('sys_get_temp_dir')) {
3318 + if (!($tmpdir = getenv('TEMP'))) {
3319 + $tmpdir = '';
3320 + }
3321 + } else {
3322 + $tmpdir = sys_get_temp_dir();
3323 + }
3324 + $filename = tempnam($tmpdir, 'dio_raw_stream_004.tmp');
3325 +
3326 + // Create the temp file
3327 + $tf = fopen($filename, "w");
3328 + if ($tf) {
3329 + fwrite($tf, str_repeat('*', 2048));
3330 + fclose($tf);
3331 + } else {
3332 + echo "Can\'t create temp file";
3333 + }
3334 +
3335 + $f = fopen('dio.raw://' . $filename, "r");
3336 + if ($f) {
3337 + $data = fread($f, 2048);
3338 + if ($data && (strlen($data) == 2048)) {
3339 + echo "Raw read passed";
3340 + } else {
3341 + echo "Raw read failed";
3342 + }
3343 + fclose($f);
3344 + } else {
3345 + echo "Raw open failed";
3346 + }
3347 +
3348 + unlink($filename);
3349 +?>
3350 +--EXPECT--
3351 +Raw read passed
3352 --- /dev/null
3353 +++ b/ext/dio/tests/dio_raw_stream_005.phpt
3354 @@ -0,0 +1,45 @@
3355 +--TEST--
3356 +Test dio eof read
3357 +--SKIPIF--
3358 +<?php
3359 + if (!extension_loaded('dio')) print 'skip';
3360 +?>
3361 +--FILE--
3362 +<?php
3363 + // Create a temp file with some content to read
3364 +
3365 + // Create the temp file name
3366 + if (!function_exists('sys_get_temp_dir')) {
3367 + if (!($tmpdir = getenv('TEMP'))) {
3368 + $tmpdir = '';
3369 + }
3370 + } else {
3371 + $tmpdir = sys_get_temp_dir();
3372 + }
3373 + $filename = tempnam($tmpdir, 'dio_raw_stream_005.tmp');
3374 +
3375 + // Create the temp file
3376 + $tf = fopen($filename, "w");
3377 + if ($tf) {
3378 + fclose($tf);
3379 + } else {
3380 + echo "Can\'t create temp file";
3381 + }
3382 +
3383 + $f = fopen('dio.raw://' . $filename, "r");
3384 + if ($f) {
3385 + $data = fread($f, 2048);
3386 + if (feof($f)) {
3387 + echo "Raw feof passed";
3388 + } else {
3389 + echo "Raw feof failed";
3390 + }
3391 + fclose($f);
3392 + } else {
3393 + echo "Raw open failed";
3394 + }
3395 +
3396 + unlink($filename);
3397 +?>
3398 +--EXPECT--
3399 +Raw feof passed
3400 --- /dev/null
3401 +++ b/ext/dio/tests/dio_raw_stream_006.phpt
3402 @@ -0,0 +1,46 @@
3403 +--TEST--
3404 +Test dio raw read
3405 +--SKIPIF--
3406 +<?php
3407 + if (!extension_loaded('dio')) print 'skip';
3408 +?>
3409 +--FILE--
3410 +<?php
3411 + // Create a temp file with some content to read
3412 +
3413 + // Create the temp file name
3414 + if (!function_exists('sys_get_temp_dir')) {
3415 + if (!($tmpdir = getenv('TEMP'))) {
3416 + $tmpdir = '';
3417 + }
3418 + } else {
3419 + $tmpdir = sys_get_temp_dir();
3420 + }
3421 + $filename = tempnam($tmpdir, 'diotest');
3422 +
3423 + // Create the temp file
3424 + $tf = fopen($filename, "w");
3425 + if ($tf) {
3426 + fwrite($tf, str_repeat('*', 2048));
3427 + fclose($tf);
3428 + } else {
3429 + echo "Can\'t create temp file";
3430 + }
3431 +
3432 + $f = fopen('dio.raw://' . $filename, "r");
3433 + if ($f) {
3434 + $data = fread($f, 1024);
3435 + if (stream_set_blocking($f, false)) {
3436 + echo "Raw set blocking passed";
3437 + } else {
3438 + echo "Raw set blocking failed";
3439 + }
3440 + fclose($f);
3441 + } else {
3442 + echo "Raw open failed";
3443 + }
3444 +
3445 + unlink($filename);
3446 +?>
3447 +--EXPECT--
3448 +Raw set blocking passed
3449 --- /dev/null
3450 +++ b/ext/dio/tests/dio_raw_stream_007.phpt
3451 @@ -0,0 +1,22 @@
3452 +--TEST--
3453 +Test dio_raw() call
3454 +--SKIPIF--
3455 +<?php if (!extension_loaded("dio")) print "skip"; ?>
3456 +--FILE--
3457 +<?php
3458 + $iswin = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
3459 +
3460 + if (!$iswin) {
3461 + $filename = "/dev/null";
3462 + } else {
3463 + $filename = "NUL";
3464 + }
3465 +
3466 + $f = dio_raw($filename, "r+");
3467 + if ($f) {
3468 + echo "dio_raw passed";
3469 + fclose($f);
3470 + }
3471 +?>
3472 +--EXPECT--
3473 +dio_raw passed