spl_directory.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Marcus Boerger <helly@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "ext/standard/file.h"
  26. #include "ext/standard/php_string.h"
  27. #include "zend_compile.h"
  28. #include "zend_exceptions.h"
  29. #include "zend_interfaces.h"
  30. #include "php_spl.h"
  31. #include "spl_functions.h"
  32. #include "spl_engine.h"
  33. #include "spl_iterators.h"
  34. #include "spl_directory.h"
  35. #include "spl_exceptions.h"
  36. #include "php.h"
  37. #include "fopen_wrappers.h"
  38. #include "ext/standard/basic_functions.h"
  39. #include "ext/standard/php_filestat.h"
  40. #define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
  41. /* declare the class handlers */
  42. static zend_object_handlers spl_filesystem_object_handlers;
  43. /* includes handler to validate object state when retrieving methods */
  44. static zend_object_handlers spl_filesystem_object_check_handlers;
  45. /* decalre the class entry */
  46. PHPAPI zend_class_entry *spl_ce_SplFileInfo;
  47. PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
  48. PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
  49. PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
  50. PHPAPI zend_class_entry *spl_ce_GlobIterator;
  51. PHPAPI zend_class_entry *spl_ce_SplFileObject;
  52. PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
  53. static void spl_filesystem_file_free_line(spl_filesystem_object *intern TSRMLS_DC) /* {{{ */
  54. {
  55. if (intern->u.file.current_line) {
  56. efree(intern->u.file.current_line);
  57. intern->u.file.current_line = NULL;
  58. }
  59. if (intern->u.file.current_zval) {
  60. zval_ptr_dtor(&intern->u.file.current_zval);
  61. intern->u.file.current_zval = NULL;
  62. }
  63. } /* }}} */
  64. static void spl_filesystem_object_free_storage(void *object TSRMLS_DC) /* {{{ */
  65. {
  66. spl_filesystem_object *intern = (spl_filesystem_object*)object;
  67. if (intern->oth_handler && intern->oth_handler->dtor) {
  68. intern->oth_handler->dtor(intern TSRMLS_CC);
  69. }
  70. zend_object_std_dtor(&intern->std TSRMLS_CC);
  71. if (intern->_path) {
  72. efree(intern->_path);
  73. }
  74. if (intern->file_name) {
  75. efree(intern->file_name);
  76. }
  77. switch(intern->type) {
  78. case SPL_FS_INFO:
  79. break;
  80. case SPL_FS_DIR:
  81. if (intern->u.dir.dirp) {
  82. php_stream_close(intern->u.dir.dirp);
  83. intern->u.dir.dirp = NULL;
  84. }
  85. if (intern->u.dir.sub_path) {
  86. efree(intern->u.dir.sub_path);
  87. }
  88. break;
  89. case SPL_FS_FILE:
  90. if (intern->u.file.stream) {
  91. if (intern->u.file.zcontext) {
  92. /* zend_list_delref(Z_RESVAL_P(intern->zcontext));*/
  93. }
  94. if (!intern->u.file.stream->is_persistent) {
  95. php_stream_free(intern->u.file.stream, PHP_STREAM_FREE_CLOSE);
  96. } else {
  97. php_stream_free(intern->u.file.stream, PHP_STREAM_FREE_CLOSE_PERSISTENT);
  98. }
  99. if (intern->u.file.open_mode) {
  100. efree(intern->u.file.open_mode);
  101. }
  102. if (intern->orig_path) {
  103. efree(intern->orig_path);
  104. }
  105. }
  106. spl_filesystem_file_free_line(intern TSRMLS_CC);
  107. break;
  108. }
  109. {
  110. zend_object_iterator *iterator;
  111. iterator = (zend_object_iterator*)
  112. spl_filesystem_object_to_iterator(intern);
  113. if (iterator->data != NULL) {
  114. iterator->data = NULL;
  115. iterator->funcs->dtor(iterator TSRMLS_CC);
  116. }
  117. }
  118. efree(object);
  119. } /* }}} */
  120. /* {{{ spl_ce_dir_object_new */
  121. /* creates the object by
  122. - allocating memory
  123. - initializing the object members
  124. - storing the object
  125. - setting it's handlers
  126. called from
  127. - clone
  128. - new
  129. */
  130. static zend_object_value spl_filesystem_object_new_ex(zend_class_entry *class_type, spl_filesystem_object **obj TSRMLS_DC)
  131. {
  132. zend_object_value retval;
  133. spl_filesystem_object *intern;
  134. intern = emalloc(sizeof(spl_filesystem_object));
  135. memset(intern, 0, sizeof(spl_filesystem_object));
  136. /* intern->type = SPL_FS_INFO; done by set 0 */
  137. intern->file_class = spl_ce_SplFileObject;
  138. intern->info_class = spl_ce_SplFileInfo;
  139. if (obj) *obj = intern;
  140. zend_object_std_init(&intern->std, class_type TSRMLS_CC);
  141. object_properties_init(&intern->std, class_type);
  142. retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) spl_filesystem_object_free_storage, NULL TSRMLS_CC);
  143. retval.handlers = &spl_filesystem_object_handlers;
  144. return retval;
  145. }
  146. /* }}} */
  147. /* {{{ spl_filesystem_object_new */
  148. /* See spl_filesystem_object_new_ex */
  149. static zend_object_value spl_filesystem_object_new(zend_class_entry *class_type TSRMLS_DC)
  150. {
  151. return spl_filesystem_object_new_ex(class_type, NULL TSRMLS_CC);
  152. }
  153. /* }}} */
  154. /* {{{ spl_filesystem_object_new_ex */
  155. static zend_object_value spl_filesystem_object_new_check(zend_class_entry *class_type TSRMLS_DC)
  156. {
  157. zend_object_value ret = spl_filesystem_object_new_ex(class_type, NULL TSRMLS_CC);
  158. ret.handlers = &spl_filesystem_object_check_handlers;
  159. return ret;
  160. }
  161. /* }}} */
  162. PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, int *len TSRMLS_DC) /* {{{ */
  163. {
  164. #ifdef HAVE_GLOB
  165. if (intern->type == SPL_FS_DIR) {
  166. if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
  167. return php_glob_stream_get_path(intern->u.dir.dirp, 0, len);
  168. }
  169. }
  170. #endif
  171. if (len) {
  172. *len = intern->_path_len;
  173. }
  174. return intern->_path;
  175. } /* }}} */
  176. static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *intern TSRMLS_DC) /* {{{ */
  177. {
  178. char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
  179. switch (intern->type) {
  180. case SPL_FS_INFO:
  181. case SPL_FS_FILE:
  182. if (!intern->file_name) {
  183. php_error_docref(NULL TSRMLS_CC, E_ERROR, "Object not initialized");
  184. }
  185. break;
  186. case SPL_FS_DIR:
  187. if (intern->file_name) {
  188. efree(intern->file_name);
  189. }
  190. intern->file_name_len = spprintf(&intern->file_name, 0, "%s%c%s",
  191. spl_filesystem_object_get_path(intern, NULL TSRMLS_CC),
  192. slash, intern->u.dir.entry.d_name);
  193. break;
  194. }
  195. } /* }}} */
  196. static int spl_filesystem_dir_read(spl_filesystem_object *intern TSRMLS_DC) /* {{{ */
  197. {
  198. if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
  199. intern->u.dir.entry.d_name[0] = '\0';
  200. return 0;
  201. } else {
  202. return 1;
  203. }
  204. }
  205. /* }}} */
  206. #define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
  207. static inline int spl_filesystem_is_dot(const char * d_name) /* {{{ */
  208. {
  209. return !strcmp(d_name, ".") || !strcmp(d_name, "..");
  210. }
  211. /* }}} */
  212. /* {{{ spl_filesystem_dir_open */
  213. /* open a directory resource */
  214. static void spl_filesystem_dir_open(spl_filesystem_object* intern, char *path TSRMLS_DC)
  215. {
  216. int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
  217. intern->type = SPL_FS_DIR;
  218. intern->_path_len = strlen(path);
  219. intern->u.dir.dirp = php_stream_opendir(path, REPORT_ERRORS, FG(default_context));
  220. if (intern->_path_len > 1 && IS_SLASH_AT(path, intern->_path_len-1)) {
  221. intern->_path = estrndup(path, --intern->_path_len);
  222. } else {
  223. intern->_path = estrndup(path, intern->_path_len);
  224. }
  225. intern->u.dir.index = 0;
  226. if (EG(exception) || intern->u.dir.dirp == NULL) {
  227. intern->u.dir.entry.d_name[0] = '\0';
  228. if (!EG(exception)) {
  229. /* open failed w/out notice (turned to exception due to EH_THROW) */
  230. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0
  231. TSRMLS_CC, "Failed to open directory \"%s\"", path);
  232. }
  233. } else {
  234. do {
  235. spl_filesystem_dir_read(intern TSRMLS_CC);
  236. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  237. }
  238. }
  239. /* }}} */
  240. static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_include_path, int silent TSRMLS_DC) /* {{{ */
  241. {
  242. zval tmp;
  243. intern->type = SPL_FS_FILE;
  244. php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, &tmp TSRMLS_CC);
  245. if (Z_LVAL(tmp)) {
  246. intern->u.file.open_mode = NULL;
  247. intern->file_name = NULL;
  248. zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Cannot use SplFileObject with directories");
  249. return FAILURE;
  250. }
  251. intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
  252. intern->u.file.stream = php_stream_open_wrapper_ex(intern->file_name, intern->u.file.open_mode, (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
  253. if (!intern->file_name_len || !intern->u.file.stream) {
  254. if (!EG(exception)) {
  255. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Cannot open file '%s'", intern->file_name_len ? intern->file_name : "");
  256. }
  257. intern->file_name = NULL; /* until here it is not a copy */
  258. intern->u.file.open_mode = NULL;
  259. return FAILURE;
  260. }
  261. if (intern->u.file.zcontext) {
  262. zend_list_addref(Z_RESVAL_P(intern->u.file.zcontext));
  263. }
  264. if (intern->file_name_len > 1 && IS_SLASH_AT(intern->file_name, intern->file_name_len-1)) {
  265. intern->file_name_len--;
  266. }
  267. intern->orig_path = estrndup(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path));
  268. intern->file_name = estrndup(intern->file_name, intern->file_name_len);
  269. intern->u.file.open_mode = estrndup(intern->u.file.open_mode, intern->u.file.open_mode_len);
  270. /* avoid reference counting in debug mode, thus do it manually */
  271. ZVAL_RESOURCE(&intern->u.file.zresource, php_stream_get_resource_id(intern->u.file.stream));
  272. Z_SET_REFCOUNT(intern->u.file.zresource, 1);
  273. intern->u.file.delimiter = ',';
  274. intern->u.file.enclosure = '"';
  275. intern->u.file.escape = '\\';
  276. zend_hash_find(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline"), (void **) &intern->u.file.func_getCurr);
  277. return SUCCESS;
  278. } /* }}} */
  279. /* {{{ spl_filesystem_object_clone */
  280. /* Local zend_object_value creation (on stack)
  281. Load the 'other' object
  282. Create a new empty object (See spl_filesystem_object_new_ex)
  283. Open the directory
  284. Clone other members (properties)
  285. */
  286. static zend_object_value spl_filesystem_object_clone(zval *zobject TSRMLS_DC)
  287. {
  288. zend_object_value new_obj_val;
  289. zend_object *old_object;
  290. zend_object *new_object;
  291. zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
  292. spl_filesystem_object *intern;
  293. spl_filesystem_object *source;
  294. int index, skip_dots;
  295. old_object = zend_objects_get_address(zobject TSRMLS_CC);
  296. source = (spl_filesystem_object*)old_object;
  297. new_obj_val = spl_filesystem_object_new_ex(old_object->ce, &intern TSRMLS_CC);
  298. new_object = &intern->std;
  299. intern->flags = source->flags;
  300. switch (source->type) {
  301. case SPL_FS_INFO:
  302. intern->_path_len = source->_path_len;
  303. intern->_path = estrndup(source->_path, source->_path_len);
  304. intern->file_name_len = source->file_name_len;
  305. intern->file_name = estrndup(source->file_name, intern->file_name_len);
  306. break;
  307. case SPL_FS_DIR:
  308. spl_filesystem_dir_open(intern, source->_path TSRMLS_CC);
  309. /* read until we hit the position in which we were before */
  310. skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
  311. for(index = 0; index < source->u.dir.index; ++index) {
  312. do {
  313. spl_filesystem_dir_read(intern TSRMLS_CC);
  314. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  315. }
  316. intern->u.dir.index = index;
  317. break;
  318. case SPL_FS_FILE:
  319. php_error_docref(NULL TSRMLS_CC, E_ERROR, "An object of class %s cannot be cloned", old_object->ce->name);
  320. break;
  321. }
  322. intern->file_class = source->file_class;
  323. intern->info_class = source->info_class;
  324. intern->oth = source->oth;
  325. intern->oth_handler = source->oth_handler;
  326. zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
  327. if (intern->oth_handler && intern->oth_handler->clone) {
  328. intern->oth_handler->clone(source, intern TSRMLS_CC);
  329. }
  330. return new_obj_val;
  331. }
  332. /* }}} */
  333. void spl_filesystem_info_set_filename(spl_filesystem_object *intern, char *path, int len, int use_copy TSRMLS_DC) /* {{{ */
  334. {
  335. char *p1, *p2;
  336. if (intern->file_name) {
  337. efree(intern->file_name);
  338. }
  339. intern->file_name = use_copy ? estrndup(path, len) : path;
  340. intern->file_name_len = len;
  341. while(IS_SLASH_AT(intern->file_name, intern->file_name_len-1) && intern->file_name_len > 1) {
  342. intern->file_name[intern->file_name_len-1] = 0;
  343. intern->file_name_len--;
  344. }
  345. p1 = strrchr(intern->file_name, '/');
  346. #if defined(PHP_WIN32) || defined(NETWARE)
  347. p2 = strrchr(intern->file_name, '\\');
  348. #else
  349. p2 = 0;
  350. #endif
  351. if (p1 || p2) {
  352. intern->_path_len = (p1 > p2 ? p1 : p2) - intern->file_name;
  353. } else {
  354. intern->_path_len = 0;
  355. }
  356. if (intern->_path) {
  357. efree(intern->_path);
  358. }
  359. intern->_path = estrndup(path, intern->_path_len);
  360. } /* }}} */
  361. static spl_filesystem_object * spl_filesystem_object_create_info(spl_filesystem_object *source, char *file_path, int file_path_len, int use_copy, zend_class_entry *ce, zval *return_value TSRMLS_DC) /* {{{ */
  362. {
  363. spl_filesystem_object *intern;
  364. zval *arg1;
  365. zend_error_handling error_handling;
  366. if (!file_path || !file_path_len) {
  367. #if defined(PHP_WIN32)
  368. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Cannot create SplFileInfo for empty path");
  369. if (file_path && !use_copy) {
  370. efree(file_path);
  371. }
  372. #else
  373. if (file_path && !use_copy) {
  374. efree(file_path);
  375. }
  376. file_path_len = 1;
  377. file_path = "/";
  378. #endif
  379. return NULL;
  380. }
  381. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
  382. ce = ce ? ce : source->info_class;
  383. zend_update_class_constants(ce TSRMLS_CC);
  384. return_value->value.obj = spl_filesystem_object_new_ex(ce, &intern TSRMLS_CC);
  385. Z_TYPE_P(return_value) = IS_OBJECT;
  386. if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
  387. MAKE_STD_ZVAL(arg1);
  388. ZVAL_STRINGL(arg1, file_path, file_path_len, use_copy);
  389. zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, arg1);
  390. zval_ptr_dtor(&arg1);
  391. } else {
  392. spl_filesystem_info_set_filename(intern, file_path, file_path_len, use_copy TSRMLS_CC);
  393. }
  394. zend_restore_error_handling(&error_handling TSRMLS_CC);
  395. return intern;
  396. } /* }}} */
  397. static spl_filesystem_object * spl_filesystem_object_create_type(int ht, spl_filesystem_object *source, int type, zend_class_entry *ce, zval *return_value TSRMLS_DC) /* {{{ */
  398. {
  399. spl_filesystem_object *intern;
  400. zend_bool use_include_path = 0;
  401. zval *arg1, *arg2;
  402. zend_error_handling error_handling;
  403. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
  404. switch (source->type) {
  405. case SPL_FS_INFO:
  406. case SPL_FS_FILE:
  407. break;
  408. case SPL_FS_DIR:
  409. if (!source->u.dir.entry.d_name[0]) {
  410. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Could not open file");
  411. zend_restore_error_handling(&error_handling TSRMLS_CC);
  412. return NULL;
  413. }
  414. }
  415. switch (type) {
  416. case SPL_FS_INFO:
  417. ce = ce ? ce : source->info_class;
  418. zend_update_class_constants(ce TSRMLS_CC);
  419. return_value->value.obj = spl_filesystem_object_new_ex(ce, &intern TSRMLS_CC);
  420. Z_TYPE_P(return_value) = IS_OBJECT;
  421. spl_filesystem_object_get_file_name(source TSRMLS_CC);
  422. if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
  423. MAKE_STD_ZVAL(arg1);
  424. ZVAL_STRINGL(arg1, source->file_name, source->file_name_len, 1);
  425. zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, arg1);
  426. zval_ptr_dtor(&arg1);
  427. } else {
  428. intern->file_name = estrndup(source->file_name, source->file_name_len);
  429. intern->file_name_len = source->file_name_len;
  430. intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len TSRMLS_CC);
  431. intern->_path = estrndup(intern->_path, intern->_path_len);
  432. }
  433. break;
  434. case SPL_FS_FILE:
  435. ce = ce ? ce : source->file_class;
  436. zend_update_class_constants(ce TSRMLS_CC);
  437. return_value->value.obj = spl_filesystem_object_new_ex(ce, &intern TSRMLS_CC);
  438. Z_TYPE_P(return_value) = IS_OBJECT;
  439. spl_filesystem_object_get_file_name(source TSRMLS_CC);
  440. if (ce->constructor->common.scope != spl_ce_SplFileObject) {
  441. MAKE_STD_ZVAL(arg1);
  442. MAKE_STD_ZVAL(arg2);
  443. ZVAL_STRINGL(arg1, source->file_name, source->file_name_len, 1);
  444. ZVAL_STRINGL(arg2, "r", 1, 1);
  445. zend_call_method_with_2_params(&return_value, ce, &ce->constructor, "__construct", NULL, arg1, arg2);
  446. zval_ptr_dtor(&arg1);
  447. zval_ptr_dtor(&arg2);
  448. } else {
  449. intern->file_name = source->file_name;
  450. intern->file_name_len = source->file_name_len;
  451. intern->_path = spl_filesystem_object_get_path(source, &intern->_path_len TSRMLS_CC);
  452. intern->_path = estrndup(intern->_path, intern->_path_len);
  453. intern->u.file.open_mode = "r";
  454. intern->u.file.open_mode_len = 1;
  455. if (ht && zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sbr",
  456. &intern->u.file.open_mode, &intern->u.file.open_mode_len,
  457. &use_include_path, &intern->u.file.zcontext) == FAILURE) {
  458. zend_restore_error_handling(&error_handling TSRMLS_CC);
  459. intern->u.file.open_mode = NULL;
  460. intern->file_name = NULL;
  461. zval_dtor(return_value);
  462. Z_TYPE_P(return_value) = IS_NULL;
  463. return NULL;
  464. }
  465. if (spl_filesystem_file_open(intern, use_include_path, 0 TSRMLS_CC) == FAILURE) {
  466. zend_restore_error_handling(&error_handling TSRMLS_CC);
  467. zval_dtor(return_value);
  468. Z_TYPE_P(return_value) = IS_NULL;
  469. return NULL;
  470. }
  471. }
  472. break;
  473. case SPL_FS_DIR:
  474. zend_restore_error_handling(&error_handling TSRMLS_CC);
  475. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Operation not supported");
  476. return NULL;
  477. }
  478. zend_restore_error_handling(&error_handling TSRMLS_CC);
  479. return NULL;
  480. } /* }}} */
  481. static int spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
  482. {
  483. return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
  484. }
  485. /* }}} */
  486. static char *spl_filesystem_object_get_pathname(spl_filesystem_object *intern, int *len TSRMLS_DC) { /* {{{ */
  487. switch (intern->type) {
  488. case SPL_FS_INFO:
  489. case SPL_FS_FILE:
  490. *len = intern->file_name_len;
  491. return intern->file_name;
  492. case SPL_FS_DIR:
  493. if (intern->u.dir.entry.d_name[0]) {
  494. spl_filesystem_object_get_file_name(intern TSRMLS_CC);
  495. *len = intern->file_name_len;
  496. return intern->file_name;
  497. }
  498. }
  499. *len = 0;
  500. return NULL;
  501. }
  502. /* }}} */
  503. static HashTable* spl_filesystem_object_get_debug_info(zval *obj, int *is_temp TSRMLS_DC) /* {{{ */
  504. {
  505. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(obj TSRMLS_CC);
  506. HashTable *rv;
  507. zval *tmp, zrv;
  508. char *pnstr, *path;
  509. int pnlen, path_len;
  510. char stmp[2];
  511. *is_temp = 1;
  512. if (!intern->std.properties) {
  513. rebuild_object_properties(&intern->std);
  514. }
  515. ALLOC_HASHTABLE(rv);
  516. ZEND_INIT_SYMTABLE_EX(rv, zend_hash_num_elements(intern->std.properties) + 3, 0);
  517. INIT_PZVAL(&zrv);
  518. Z_ARRVAL(zrv) = rv;
  519. zend_hash_copy(rv, intern->std.properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  520. pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "pathName", sizeof("pathName")-1, &pnlen TSRMLS_CC);
  521. path = spl_filesystem_object_get_pathname(intern, &path_len TSRMLS_CC);
  522. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, path, path_len, 1);
  523. efree(pnstr);
  524. if (intern->file_name) {
  525. pnstr = spl_gen_private_prop_name(spl_ce_SplFileInfo, "fileName", sizeof("fileName")-1, &pnlen TSRMLS_CC);
  526. spl_filesystem_object_get_path(intern, &path_len TSRMLS_CC);
  527. if (path_len && path_len < intern->file_name_len) {
  528. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1), 1);
  529. } else {
  530. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, intern->file_name, intern->file_name_len, 1);
  531. }
  532. efree(pnstr);
  533. }
  534. if (intern->type == SPL_FS_DIR) {
  535. #ifdef HAVE_GLOB
  536. pnstr = spl_gen_private_prop_name(spl_ce_DirectoryIterator, "glob", sizeof("glob")-1, &pnlen TSRMLS_CC);
  537. if (php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
  538. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, intern->_path, intern->_path_len, 1);
  539. } else {
  540. add_assoc_bool_ex(&zrv, pnstr, pnlen+1, 0);
  541. }
  542. efree(pnstr);
  543. #endif
  544. pnstr = spl_gen_private_prop_name(spl_ce_RecursiveDirectoryIterator, "subPathName", sizeof("subPathName")-1, &pnlen TSRMLS_CC);
  545. if (intern->u.dir.sub_path) {
  546. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, intern->u.dir.sub_path, intern->u.dir.sub_path_len, 1);
  547. } else {
  548. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, "", 0, 1);
  549. }
  550. efree(pnstr);
  551. }
  552. if (intern->type == SPL_FS_FILE) {
  553. pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "openMode", sizeof("openMode")-1, &pnlen TSRMLS_CC);
  554. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, intern->u.file.open_mode, intern->u.file.open_mode_len, 1);
  555. efree(pnstr);
  556. stmp[1] = '\0';
  557. stmp[0] = intern->u.file.delimiter;
  558. pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "delimiter", sizeof("delimiter")-1, &pnlen TSRMLS_CC);
  559. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, stmp, 1, 1);
  560. efree(pnstr);
  561. stmp[0] = intern->u.file.enclosure;
  562. pnstr = spl_gen_private_prop_name(spl_ce_SplFileObject, "enclosure", sizeof("enclosure")-1, &pnlen TSRMLS_CC);
  563. add_assoc_stringl_ex(&zrv, pnstr, pnlen+1, stmp, 1, 1);
  564. efree(pnstr);
  565. }
  566. return rv;
  567. }
  568. /* }}} */
  569. zend_function *spl_filesystem_object_get_method_check(zval **object_ptr, char *method, int method_len, const struct _zend_literal *key TSRMLS_DC) /* {{{ */
  570. {
  571. spl_filesystem_object *fsobj = zend_object_store_get_object(*object_ptr TSRMLS_CC);
  572. if (fsobj->u.dir.dirp == NULL && fsobj->orig_path == NULL) {
  573. method = "_bad_state_ex";
  574. method_len = sizeof("_bad_state_ex") - 1;
  575. key = NULL;
  576. }
  577. return zend_get_std_object_handlers()->get_method(object_ptr, method, method_len, key TSRMLS_CC);
  578. }
  579. /* }}} */
  580. #define DIT_CTOR_FLAGS 0x00000001
  581. #define DIT_CTOR_GLOB 0x00000002
  582. void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, long ctor_flags) /* {{{ */
  583. {
  584. spl_filesystem_object *intern;
  585. char *path;
  586. int parsed, len;
  587. long flags;
  588. zend_error_handling error_handling;
  589. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling TSRMLS_CC);
  590. if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
  591. flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
  592. parsed = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &path, &len, &flags);
  593. } else {
  594. flags = SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
  595. parsed = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &len);
  596. }
  597. if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_SKIPDOTS)) {
  598. flags |= SPL_FILE_DIR_SKIPDOTS;
  599. }
  600. if (SPL_HAS_FLAG(ctor_flags, SPL_FILE_DIR_UNIXPATHS)) {
  601. flags |= SPL_FILE_DIR_UNIXPATHS;
  602. }
  603. if (parsed == FAILURE) {
  604. zend_restore_error_handling(&error_handling TSRMLS_CC);
  605. return;
  606. }
  607. if (!len) {
  608. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Directory name must not be empty.");
  609. zend_restore_error_handling(&error_handling TSRMLS_CC);
  610. return;
  611. }
  612. intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  613. if (intern->_path) {
  614. /* object is alreay initialized */
  615. zend_restore_error_handling(&error_handling TSRMLS_CC);
  616. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Directory object is already initialized");
  617. return;
  618. }
  619. intern->flags = flags;
  620. #ifdef HAVE_GLOB
  621. if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && strstr(path, "glob://") != path) {
  622. spprintf(&path, 0, "glob://%s", path);
  623. spl_filesystem_dir_open(intern, path TSRMLS_CC);
  624. efree(path);
  625. } else
  626. #endif
  627. {
  628. spl_filesystem_dir_open(intern, path TSRMLS_CC);
  629. }
  630. intern->u.dir.is_recursive = instanceof_function(intern->std.ce, spl_ce_RecursiveDirectoryIterator TSRMLS_CC) ? 1 : 0;
  631. zend_restore_error_handling(&error_handling TSRMLS_CC);
  632. }
  633. /* }}} */
  634. /* {{{ proto void DirectoryIterator::__construct(string path)
  635. Cronstructs a new dir iterator from a path. */
  636. SPL_METHOD(DirectoryIterator, __construct)
  637. {
  638. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  639. }
  640. /* }}} */
  641. /* {{{ proto void DirectoryIterator::rewind()
  642. Rewind dir back to the start */
  643. SPL_METHOD(DirectoryIterator, rewind)
  644. {
  645. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  646. if (zend_parse_parameters_none() == FAILURE) {
  647. return;
  648. }
  649. intern->u.dir.index = 0;
  650. if (intern->u.dir.dirp) {
  651. php_stream_rewinddir(intern->u.dir.dirp);
  652. }
  653. spl_filesystem_dir_read(intern TSRMLS_CC);
  654. }
  655. /* }}} */
  656. /* {{{ proto string DirectoryIterator::key()
  657. Return current dir entry */
  658. SPL_METHOD(DirectoryIterator, key)
  659. {
  660. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  661. if (zend_parse_parameters_none() == FAILURE) {
  662. return;
  663. }
  664. if (intern->u.dir.dirp) {
  665. RETURN_LONG(intern->u.dir.index);
  666. } else {
  667. RETURN_FALSE;
  668. }
  669. }
  670. /* }}} */
  671. /* {{{ proto DirectoryIterator DirectoryIterator::current()
  672. Return this (needed for Iterator interface) */
  673. SPL_METHOD(DirectoryIterator, current)
  674. {
  675. if (zend_parse_parameters_none() == FAILURE) {
  676. return;
  677. }
  678. RETURN_ZVAL(getThis(), 1, 0);
  679. }
  680. /* }}} */
  681. /* {{{ proto void DirectoryIterator::next()
  682. Move to next entry */
  683. SPL_METHOD(DirectoryIterator, next)
  684. {
  685. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  686. int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
  687. if (zend_parse_parameters_none() == FAILURE) {
  688. return;
  689. }
  690. intern->u.dir.index++;
  691. do {
  692. spl_filesystem_dir_read(intern TSRMLS_CC);
  693. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  694. if (intern->file_name) {
  695. efree(intern->file_name);
  696. intern->file_name = NULL;
  697. }
  698. }
  699. /* }}} */
  700. /* {{{ proto void DirectoryIterator::seek(int position)
  701. Seek to the given position */
  702. SPL_METHOD(DirectoryIterator, seek)
  703. {
  704. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  705. zval *retval = NULL;
  706. long pos;
  707. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &pos) == FAILURE) {
  708. return;
  709. }
  710. if (intern->u.dir.index > pos) {
  711. /* we first rewind */
  712. zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), &intern->u.dir.func_rewind, "rewind", &retval);
  713. if (retval) {
  714. zval_ptr_dtor(&retval);
  715. retval = NULL;
  716. }
  717. }
  718. while (intern->u.dir.index < pos) {
  719. int valid = 0;
  720. zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), &intern->u.dir.func_valid, "valid", &retval);
  721. if (retval) {
  722. valid = zend_is_true(retval);
  723. zval_ptr_dtor(&retval);
  724. retval = NULL;
  725. }
  726. if (!valid) {
  727. zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0 TSRMLS_CC, "Seek position %ld is out of range", pos);
  728. return;
  729. }
  730. zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), &intern->u.dir.func_next, "next", &retval);
  731. if (retval) {
  732. zval_ptr_dtor(&retval);
  733. }
  734. }
  735. } /* }}} */
  736. /* {{{ proto string DirectoryIterator::valid()
  737. Check whether dir contains more entries */
  738. SPL_METHOD(DirectoryIterator, valid)
  739. {
  740. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  741. if (zend_parse_parameters_none() == FAILURE) {
  742. return;
  743. }
  744. RETURN_BOOL(intern->u.dir.entry.d_name[0] != '\0');
  745. }
  746. /* }}} */
  747. /* {{{ proto string SplFileInfo::getPath()
  748. Return the path */
  749. SPL_METHOD(SplFileInfo, getPath)
  750. {
  751. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  752. char *path;
  753. int path_len;
  754. if (zend_parse_parameters_none() == FAILURE) {
  755. return;
  756. }
  757. path = spl_filesystem_object_get_path(intern, &path_len TSRMLS_CC);
  758. RETURN_STRINGL(path, path_len, 1);
  759. }
  760. /* }}} */
  761. /* {{{ proto string SplFileInfo::getFilename()
  762. Return filename only */
  763. SPL_METHOD(SplFileInfo, getFilename)
  764. {
  765. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  766. int path_len;
  767. if (zend_parse_parameters_none() == FAILURE) {
  768. return;
  769. }
  770. spl_filesystem_object_get_path(intern, &path_len TSRMLS_CC);
  771. if (path_len && path_len < intern->file_name_len) {
  772. RETURN_STRINGL(intern->file_name + path_len + 1, intern->file_name_len - (path_len + 1), 1);
  773. } else {
  774. RETURN_STRINGL(intern->file_name, intern->file_name_len, 1);
  775. }
  776. }
  777. /* }}} */
  778. /* {{{ proto string DirectoryIterator::getFilename()
  779. Return filename of current dir entry */
  780. SPL_METHOD(DirectoryIterator, getFilename)
  781. {
  782. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  783. if (zend_parse_parameters_none() == FAILURE) {
  784. return;
  785. }
  786. RETURN_STRING(intern->u.dir.entry.d_name, 1);
  787. }
  788. /* }}} */
  789. /* {{{ proto string SplFileInfo::getExtension()
  790. Returns file extension component of path */
  791. SPL_METHOD(SplFileInfo, getExtension)
  792. {
  793. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  794. char *fname = NULL;
  795. const char *p;
  796. size_t flen;
  797. int path_len, idx;
  798. if (zend_parse_parameters_none() == FAILURE) {
  799. return;
  800. }
  801. spl_filesystem_object_get_path(intern, &path_len TSRMLS_CC);
  802. if (path_len && path_len < intern->file_name_len) {
  803. fname = intern->file_name + path_len + 1;
  804. flen = intern->file_name_len - (path_len + 1);
  805. } else {
  806. fname = intern->file_name;
  807. flen = intern->file_name_len;
  808. }
  809. php_basename(fname, flen, NULL, 0, &fname, &flen TSRMLS_CC);
  810. p = zend_memrchr(fname, '.', flen);
  811. if (p) {
  812. idx = p - fname;
  813. RETVAL_STRINGL(fname + idx + 1, flen - idx - 1, 1);
  814. efree(fname);
  815. return;
  816. } else {
  817. if (fname) {
  818. efree(fname);
  819. }
  820. RETURN_EMPTY_STRING();
  821. }
  822. }
  823. /* }}}*/
  824. /* {{{ proto string DirectoryIterator::getExtension()
  825. Returns the file extension component of path */
  826. SPL_METHOD(DirectoryIterator, getExtension)
  827. {
  828. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  829. char *fname = NULL;
  830. const char *p;
  831. size_t flen;
  832. int idx;
  833. if (zend_parse_parameters_none() == FAILURE) {
  834. return;
  835. }
  836. php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0, &fname, &flen TSRMLS_CC);
  837. p = zend_memrchr(fname, '.', flen);
  838. if (p) {
  839. idx = p - fname;
  840. RETVAL_STRINGL(fname + idx + 1, flen - idx - 1, 1);
  841. efree(fname);
  842. return;
  843. } else {
  844. if (fname) {
  845. efree(fname);
  846. }
  847. RETURN_EMPTY_STRING();
  848. }
  849. }
  850. /* }}} */
  851. /* {{{ proto string SplFileInfo::getBasename([string $suffix]) U
  852. Returns filename component of path */
  853. SPL_METHOD(SplFileInfo, getBasename)
  854. {
  855. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  856. char *fname, *suffix = 0;
  857. size_t flen;
  858. int slen = 0, path_len;
  859. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &suffix, &slen) == FAILURE) {
  860. return;
  861. }
  862. spl_filesystem_object_get_path(intern, &path_len TSRMLS_CC);
  863. if (path_len && path_len < intern->file_name_len) {
  864. fname = intern->file_name + path_len + 1;
  865. flen = intern->file_name_len - (path_len + 1);
  866. } else {
  867. fname = intern->file_name;
  868. flen = intern->file_name_len;
  869. }
  870. php_basename(fname, flen, suffix, slen, &fname, &flen TSRMLS_CC);
  871. RETURN_STRINGL(fname, flen, 0);
  872. }
  873. /* }}}*/
  874. /* {{{ proto string DirectoryIterator::getBasename([string $suffix]) U
  875. Returns filename component of current dir entry */
  876. SPL_METHOD(DirectoryIterator, getBasename)
  877. {
  878. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  879. char *suffix = 0, *fname;
  880. int slen = 0;
  881. size_t flen;
  882. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &suffix, &slen) == FAILURE) {
  883. return;
  884. }
  885. php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), suffix, slen, &fname, &flen TSRMLS_CC);
  886. RETURN_STRINGL(fname, flen, 0);
  887. }
  888. /* }}} */
  889. /* {{{ proto string SplFileInfo::getPathname()
  890. Return path and filename */
  891. SPL_METHOD(SplFileInfo, getPathname)
  892. {
  893. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  894. char *path;
  895. int path_len;
  896. if (zend_parse_parameters_none() == FAILURE) {
  897. return;
  898. }
  899. path = spl_filesystem_object_get_pathname(intern, &path_len TSRMLS_CC);
  900. if (path != NULL) {
  901. RETURN_STRINGL(path, path_len, 1);
  902. } else {
  903. RETURN_FALSE;
  904. }
  905. }
  906. /* }}} */
  907. /* {{{ proto string FilesystemIterator::key()
  908. Return getPathname() or getFilename() depending on flags */
  909. SPL_METHOD(FilesystemIterator, key)
  910. {
  911. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  912. if (zend_parse_parameters_none() == FAILURE) {
  913. return;
  914. }
  915. if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
  916. RETURN_STRING(intern->u.dir.entry.d_name, 1);
  917. } else {
  918. spl_filesystem_object_get_file_name(intern TSRMLS_CC);
  919. RETURN_STRINGL(intern->file_name, intern->file_name_len, 1);
  920. }
  921. }
  922. /* }}} */
  923. /* {{{ proto string FilesystemIterator::current()
  924. Return getFilename(), getFileInfo() or $this depending on flags */
  925. SPL_METHOD(FilesystemIterator, current)
  926. {
  927. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  928. if (zend_parse_parameters_none() == FAILURE) {
  929. return;
  930. }
  931. if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
  932. spl_filesystem_object_get_file_name(intern TSRMLS_CC);
  933. RETURN_STRINGL(intern->file_name, intern->file_name_len, 1);
  934. } else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
  935. spl_filesystem_object_get_file_name(intern TSRMLS_CC);
  936. spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value TSRMLS_CC);
  937. } else {
  938. RETURN_ZVAL(getThis(), 1, 0);
  939. /*RETURN_STRING(intern->u.dir.entry.d_name, 1);*/
  940. }
  941. }
  942. /* }}} */
  943. /* {{{ proto bool DirectoryIterator::isDot()
  944. Returns true if current entry is '.' or '..' */
  945. SPL_METHOD(DirectoryIterator, isDot)
  946. {
  947. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  948. if (zend_parse_parameters_none() == FAILURE) {
  949. return;
  950. }
  951. RETURN_BOOL(spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  952. }
  953. /* }}} */
  954. /* {{{ proto void SplFileInfo::__construct(string file_name)
  955. Cronstructs a new SplFileInfo from a path. */
  956. /* zend_replace_error_handling() is used to throw exceptions in case
  957. the constructor fails. Here we use this to ensure the object
  958. has a valid directory resource.
  959. When the constructor gets called the object is already created
  960. by the engine, so we must only call 'additional' initializations.
  961. */
  962. SPL_METHOD(SplFileInfo, __construct)
  963. {
  964. spl_filesystem_object *intern;
  965. char *path;
  966. int len;
  967. zend_error_handling error_handling;
  968. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
  969. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &len) == FAILURE) {
  970. zend_restore_error_handling(&error_handling TSRMLS_CC);
  971. return;
  972. }
  973. intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  974. spl_filesystem_info_set_filename(intern, path, len, 1 TSRMLS_CC);
  975. zend_restore_error_handling(&error_handling TSRMLS_CC);
  976. /* intern->type = SPL_FS_INFO; already set */
  977. }
  978. /* }}} */
  979. /* {{{ FileInfoFunction */
  980. #define FileInfoFunction(func_name, func_num) \
  981. SPL_METHOD(SplFileInfo, func_name) \
  982. { \
  983. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
  984. zend_error_handling error_handling; \
  985. if (zend_parse_parameters_none() == FAILURE) { \
  986. return; \
  987. } \
  988. \
  989. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);\
  990. spl_filesystem_object_get_file_name(intern TSRMLS_CC); \
  991. php_stat(intern->file_name, intern->file_name_len, func_num, return_value TSRMLS_CC); \
  992. zend_restore_error_handling(&error_handling TSRMLS_CC); \
  993. }
  994. /* }}} */
  995. /* {{{ proto int SplFileInfo::getPerms()
  996. Get file permissions */
  997. FileInfoFunction(getPerms, FS_PERMS)
  998. /* }}} */
  999. /* {{{ proto int SplFileInfo::getInode()
  1000. Get file inode */
  1001. FileInfoFunction(getInode, FS_INODE)
  1002. /* }}} */
  1003. /* {{{ proto int SplFileInfo::getSize()
  1004. Get file size */
  1005. FileInfoFunction(getSize, FS_SIZE)
  1006. /* }}} */
  1007. /* {{{ proto int SplFileInfo::getOwner()
  1008. Get file owner */
  1009. FileInfoFunction(getOwner, FS_OWNER)
  1010. /* }}} */
  1011. /* {{{ proto int SplFileInfo::getGroup()
  1012. Get file group */
  1013. FileInfoFunction(getGroup, FS_GROUP)
  1014. /* }}} */
  1015. /* {{{ proto int SplFileInfo::getATime()
  1016. Get last access time of file */
  1017. FileInfoFunction(getATime, FS_ATIME)
  1018. /* }}} */
  1019. /* {{{ proto int SplFileInfo::getMTime()
  1020. Get last modification time of file */
  1021. FileInfoFunction(getMTime, FS_MTIME)
  1022. /* }}} */
  1023. /* {{{ proto int SplFileInfo::getCTime()
  1024. Get inode modification time of file */
  1025. FileInfoFunction(getCTime, FS_CTIME)
  1026. /* }}} */
  1027. /* {{{ proto string SplFileInfo::getType()
  1028. Get file type */
  1029. FileInfoFunction(getType, FS_TYPE)
  1030. /* }}} */
  1031. /* {{{ proto bool SplFileInfo::isWritable()
  1032. Returns true if file can be written */
  1033. FileInfoFunction(isWritable, FS_IS_W)
  1034. /* }}} */
  1035. /* {{{ proto bool SplFileInfo::isReadable()
  1036. Returns true if file can be read */
  1037. FileInfoFunction(isReadable, FS_IS_R)
  1038. /* }}} */
  1039. /* {{{ proto bool SplFileInfo::isExecutable()
  1040. Returns true if file is executable */
  1041. FileInfoFunction(isExecutable, FS_IS_X)
  1042. /* }}} */
  1043. /* {{{ proto bool SplFileInfo::isFile()
  1044. Returns true if file is a regular file */
  1045. FileInfoFunction(isFile, FS_IS_FILE)
  1046. /* }}} */
  1047. /* {{{ proto bool SplFileInfo::isDir()
  1048. Returns true if file is directory */
  1049. FileInfoFunction(isDir, FS_IS_DIR)
  1050. /* }}} */
  1051. /* {{{ proto bool SplFileInfo::isLink()
  1052. Returns true if file is symbolic link */
  1053. FileInfoFunction(isLink, FS_IS_LINK)
  1054. /* }}} */
  1055. /* {{{ proto string SplFileInfo::getLinkTarget() U
  1056. Return the target of a symbolic link */
  1057. SPL_METHOD(SplFileInfo, getLinkTarget)
  1058. {
  1059. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1060. int ret;
  1061. char buff[MAXPATHLEN];
  1062. zend_error_handling error_handling;
  1063. if (zend_parse_parameters_none() == FAILURE) {
  1064. return;
  1065. }
  1066. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
  1067. #if defined(PHP_WIN32) || HAVE_SYMLINK
  1068. if (intern->file_name == NULL) {
  1069. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty filename");
  1070. RETURN_FALSE;
  1071. } else if (!IS_ABSOLUTE_PATH(intern->file_name, intern->file_name_len)) {
  1072. char expanded_path[MAXPATHLEN];
  1073. if (!expand_filepath_with_mode(intern->file_name, expanded_path, NULL, 0, CWD_EXPAND TSRMLS_CC)) {
  1074. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such file or directory");
  1075. RETURN_FALSE;
  1076. }
  1077. ret = php_sys_readlink(expanded_path, buff, MAXPATHLEN - 1);
  1078. } else {
  1079. ret = php_sys_readlink(intern->file_name, buff, MAXPATHLEN-1);
  1080. }
  1081. #else
  1082. ret = -1; /* always fail if not implemented */
  1083. #endif
  1084. if (ret == -1) {
  1085. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Unable to read link %s, error: %s", intern->file_name, strerror(errno));
  1086. RETVAL_FALSE;
  1087. } else {
  1088. /* Append NULL to the end of the string */
  1089. buff[ret] = '\0';
  1090. RETVAL_STRINGL(buff, ret, 1);
  1091. }
  1092. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1093. }
  1094. /* }}} */
  1095. #if (!defined(__BEOS__) && !defined(NETWARE) && HAVE_REALPATH) || defined(ZTS)
  1096. /* {{{ proto string SplFileInfo::getRealPath()
  1097. Return the resolved path */
  1098. SPL_METHOD(SplFileInfo, getRealPath)
  1099. {
  1100. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1101. char buff[MAXPATHLEN];
  1102. char *filename;
  1103. zend_error_handling error_handling;
  1104. if (zend_parse_parameters_none() == FAILURE) {
  1105. return;
  1106. }
  1107. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
  1108. if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
  1109. spl_filesystem_object_get_file_name(intern TSRMLS_CC);
  1110. }
  1111. if (intern->orig_path) {
  1112. filename = intern->orig_path;
  1113. } else {
  1114. filename = intern->file_name;
  1115. }
  1116. if (filename && VCWD_REALPATH(filename, buff)) {
  1117. #ifdef ZTS
  1118. if (VCWD_ACCESS(buff, F_OK)) {
  1119. RETVAL_FALSE;
  1120. } else
  1121. #endif
  1122. RETVAL_STRING(buff, 1);
  1123. } else {
  1124. RETVAL_FALSE;
  1125. }
  1126. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1127. }
  1128. /* }}} */
  1129. #endif
  1130. /* {{{ proto SplFileObject SplFileInfo::openFile([string mode = 'r' [, bool use_include_path [, resource context]]])
  1131. Open the current file */
  1132. SPL_METHOD(SplFileInfo, openFile)
  1133. {
  1134. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1135. spl_filesystem_object_create_type(ht, intern, SPL_FS_FILE, NULL, return_value TSRMLS_CC);
  1136. }
  1137. /* }}} */
  1138. /* {{{ proto void SplFileInfo::setFileClass([string class_name])
  1139. Class to use in openFile() */
  1140. SPL_METHOD(SplFileInfo, setFileClass)
  1141. {
  1142. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1143. zend_class_entry *ce = spl_ce_SplFileObject;
  1144. zend_error_handling error_handling;
  1145. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling TSRMLS_CC);
  1146. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &ce) == SUCCESS) {
  1147. intern->file_class = ce;
  1148. }
  1149. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1150. }
  1151. /* }}} */
  1152. /* {{{ proto void SplFileInfo::setInfoClass([string class_name])
  1153. Class to use in getFileInfo(), getPathInfo() */
  1154. SPL_METHOD(SplFileInfo, setInfoClass)
  1155. {
  1156. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1157. zend_class_entry *ce = spl_ce_SplFileInfo;
  1158. zend_error_handling error_handling;
  1159. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling TSRMLS_CC);
  1160. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &ce) == SUCCESS) {
  1161. intern->info_class = ce;
  1162. }
  1163. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1164. }
  1165. /* }}} */
  1166. /* {{{ proto SplFileInfo SplFileInfo::getFileInfo([string $class_name])
  1167. Get/copy file info */
  1168. SPL_METHOD(SplFileInfo, getFileInfo)
  1169. {
  1170. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1171. zend_class_entry *ce = intern->info_class;
  1172. zend_error_handling error_handling;
  1173. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling TSRMLS_CC);
  1174. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &ce) == SUCCESS) {
  1175. spl_filesystem_object_create_type(ht, intern, SPL_FS_INFO, ce, return_value TSRMLS_CC);
  1176. }
  1177. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1178. }
  1179. /* }}} */
  1180. /* {{{ proto SplFileInfo SplFileInfo::getPathInfo([string $class_name])
  1181. Get/copy file info */
  1182. SPL_METHOD(SplFileInfo, getPathInfo)
  1183. {
  1184. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1185. zend_class_entry *ce = intern->info_class;
  1186. zend_error_handling error_handling;
  1187. zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling TSRMLS_CC);
  1188. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &ce) == SUCCESS) {
  1189. int path_len;
  1190. char *path = spl_filesystem_object_get_pathname(intern, &path_len TSRMLS_CC);
  1191. if (path) {
  1192. char *dpath = estrndup(path, path_len);
  1193. path_len = php_dirname(dpath, path_len);
  1194. spl_filesystem_object_create_info(intern, dpath, path_len, 1, ce, return_value TSRMLS_CC);
  1195. efree(dpath);
  1196. }
  1197. }
  1198. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1199. }
  1200. /* }}} */
  1201. /* {{{ */
  1202. SPL_METHOD(SplFileInfo, _bad_state_ex)
  1203. {
  1204. zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC,
  1205. "The parent constructor was not called: the object is in an "
  1206. "invalid state ");
  1207. }
  1208. /* }}} */
  1209. /* {{{ proto void FilesystemIterator::__construct(string path [, int flags])
  1210. Cronstructs a new dir iterator from a path. */
  1211. SPL_METHOD(FilesystemIterator, __construct)
  1212. {
  1213. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS | SPL_FILE_DIR_SKIPDOTS);
  1214. }
  1215. /* }}} */
  1216. /* {{{ proto void FilesystemIterator::rewind()
  1217. Rewind dir back to the start */
  1218. SPL_METHOD(FilesystemIterator, rewind)
  1219. {
  1220. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1221. int skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
  1222. if (zend_parse_parameters_none() == FAILURE) {
  1223. return;
  1224. }
  1225. intern->u.dir.index = 0;
  1226. if (intern->u.dir.dirp) {
  1227. php_stream_rewinddir(intern->u.dir.dirp);
  1228. }
  1229. do {
  1230. spl_filesystem_dir_read(intern TSRMLS_CC);
  1231. } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
  1232. }
  1233. /* }}} */
  1234. /* {{{ proto int FilesystemIterator::getFlags()
  1235. Get handling flags */
  1236. SPL_METHOD(FilesystemIterator, getFlags)
  1237. {
  1238. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1239. if (zend_parse_parameters_none() == FAILURE) {
  1240. return;
  1241. }
  1242. RETURN_LONG(intern->flags & (SPL_FILE_DIR_KEY_MODE_MASK | SPL_FILE_DIR_CURRENT_MODE_MASK | SPL_FILE_DIR_OTHERS_MASK));
  1243. } /* }}} */
  1244. /* {{{ proto void FilesystemIterator::setFlags(long $flags)
  1245. Set handling flags */
  1246. SPL_METHOD(FilesystemIterator, setFlags)
  1247. {
  1248. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1249. long flags;
  1250. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &flags) == FAILURE) {
  1251. return;
  1252. }
  1253. intern->flags &= ~(SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK);
  1254. intern->flags |= ((SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK) & flags);
  1255. } /* }}} */
  1256. /* {{{ proto bool RecursiveDirectoryIterator::hasChildren([bool $allow_links = false])
  1257. Returns whether current entry is a directory and not '.' or '..' */
  1258. SPL_METHOD(RecursiveDirectoryIterator, hasChildren)
  1259. {
  1260. zend_bool allow_links = 0;
  1261. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1262. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &allow_links) == FAILURE) {
  1263. return;
  1264. }
  1265. if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
  1266. RETURN_FALSE;
  1267. } else {
  1268. spl_filesystem_object_get_file_name(intern TSRMLS_CC);
  1269. if (!allow_links && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
  1270. php_stat(intern->file_name, intern->file_name_len, FS_IS_LINK, return_value TSRMLS_CC);
  1271. if (zend_is_true(return_value)) {
  1272. RETURN_FALSE;
  1273. }
  1274. }
  1275. php_stat(intern->file_name, intern->file_name_len, FS_IS_DIR, return_value TSRMLS_CC);
  1276. }
  1277. }
  1278. /* }}} */
  1279. /* {{{ proto RecursiveDirectoryIterator DirectoryIterator::getChildren()
  1280. Returns an iterator for the current entry if it is a directory */
  1281. SPL_METHOD(RecursiveDirectoryIterator, getChildren)
  1282. {
  1283. zval *zpath, *zflags;
  1284. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1285. spl_filesystem_object *subdir;
  1286. char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
  1287. if (zend_parse_parameters_none() == FAILURE) {
  1288. return;
  1289. }
  1290. spl_filesystem_object_get_file_name(intern TSRMLS_CC);
  1291. MAKE_STD_ZVAL(zflags);
  1292. MAKE_STD_ZVAL(zpath);
  1293. ZVAL_LONG(zflags, intern->flags);
  1294. ZVAL_STRINGL(zpath, intern->file_name, intern->file_name_len, 1);
  1295. spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), &return_value, 0, zpath, zflags TSRMLS_CC);
  1296. zval_ptr_dtor(&zpath);
  1297. zval_ptr_dtor(&zflags);
  1298. subdir = (spl_filesystem_object*)zend_object_store_get_object(return_value TSRMLS_CC);
  1299. if (subdir) {
  1300. if (intern->u.dir.sub_path && intern->u.dir.sub_path[0]) {
  1301. subdir->u.dir.sub_path_len = spprintf(&subdir->u.dir.sub_path, 0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name);
  1302. } else {
  1303. subdir->u.dir.sub_path_len = strlen(intern->u.dir.entry.d_name);
  1304. subdir->u.dir.sub_path = estrndup(intern->u.dir.entry.d_name, subdir->u.dir.sub_path_len);
  1305. }
  1306. subdir->info_class = intern->info_class;
  1307. subdir->file_class = intern->file_class;
  1308. subdir->oth = intern->oth;
  1309. }
  1310. }
  1311. /* }}} */
  1312. /* {{{ proto void RecursiveDirectoryIterator::getSubPath()
  1313. Get sub path */
  1314. SPL_METHOD(RecursiveDirectoryIterator, getSubPath)
  1315. {
  1316. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1317. if (zend_parse_parameters_none() == FAILURE) {
  1318. return;
  1319. }
  1320. if (intern->u.dir.sub_path) {
  1321. RETURN_STRINGL(intern->u.dir.sub_path, intern->u.dir.sub_path_len, 1);
  1322. } else {
  1323. RETURN_STRINGL("", 0, 1);
  1324. }
  1325. }
  1326. /* }}} */
  1327. /* {{{ proto void RecursiveDirectoryIterator::getSubPathname()
  1328. Get sub path and file name */
  1329. SPL_METHOD(RecursiveDirectoryIterator, getSubPathname)
  1330. {
  1331. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1332. char *sub_name;
  1333. int len;
  1334. char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
  1335. if (zend_parse_parameters_none() == FAILURE) {
  1336. return;
  1337. }
  1338. if (intern->u.dir.sub_path) {
  1339. len = spprintf(&sub_name, 0, "%s%c%s", intern->u.dir.sub_path, slash, intern->u.dir.entry.d_name);
  1340. RETURN_STRINGL(sub_name, len, 0);
  1341. } else {
  1342. RETURN_STRING(intern->u.dir.entry.d_name, 1);
  1343. }
  1344. }
  1345. /* }}} */
  1346. /* {{{ proto int RecursiveDirectoryIterator::__construct(string path [, int flags])
  1347. Cronstructs a new dir iterator from a path. */
  1348. SPL_METHOD(RecursiveDirectoryIterator, __construct)
  1349. {
  1350. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS);
  1351. }
  1352. /* }}} */
  1353. #ifdef HAVE_GLOB
  1354. /* {{{ proto int GlobIterator::__construct(string path [, int flags])
  1355. Cronstructs a new dir iterator from a glob expression (no glob:// needed). */
  1356. SPL_METHOD(GlobIterator, __construct)
  1357. {
  1358. spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS|DIT_CTOR_GLOB);
  1359. }
  1360. /* }}} */
  1361. /* {{{ proto int GlobIterator::cont()
  1362. Return the number of directories and files found by globbing */
  1363. SPL_METHOD(GlobIterator, count)
  1364. {
  1365. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1366. if (zend_parse_parameters_none() == FAILURE) {
  1367. return;
  1368. }
  1369. if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
  1370. RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
  1371. } else {
  1372. /* should not happen */
  1373. php_error_docref(NULL TSRMLS_CC, E_ERROR, "GlobIterator lost glob state");
  1374. }
  1375. }
  1376. /* }}} */
  1377. #endif /* HAVE_GLOB */
  1378. /* {{{ forward declarations to the iterator handlers */
  1379. static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter TSRMLS_DC);
  1380. static int spl_filesystem_dir_it_valid(zend_object_iterator *iter TSRMLS_DC);
  1381. static void spl_filesystem_dir_it_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC);
  1382. static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC);
  1383. static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter TSRMLS_DC);
  1384. static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter TSRMLS_DC);
  1385. /* iterator handler table */
  1386. zend_object_iterator_funcs spl_filesystem_dir_it_funcs = {
  1387. spl_filesystem_dir_it_dtor,
  1388. spl_filesystem_dir_it_valid,
  1389. spl_filesystem_dir_it_current_data,
  1390. spl_filesystem_dir_it_current_key,
  1391. spl_filesystem_dir_it_move_forward,
  1392. spl_filesystem_dir_it_rewind
  1393. };
  1394. /* }}} */
  1395. /* {{{ spl_ce_dir_get_iterator */
  1396. zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
  1397. {
  1398. spl_filesystem_iterator *iterator;
  1399. spl_filesystem_object *dir_object;
  1400. if (by_ref) {
  1401. zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
  1402. }
  1403. dir_object = (spl_filesystem_object*)zend_object_store_get_object(object TSRMLS_CC);
  1404. iterator = spl_filesystem_object_to_iterator(dir_object);
  1405. /* initialize iterator if it wasn't gotten before */
  1406. if (iterator->intern.data == NULL) {
  1407. iterator->intern.data = object;
  1408. iterator->intern.funcs = &spl_filesystem_dir_it_funcs;
  1409. /* ->current must be initialized; rewind doesn't set it and valid
  1410. * doesn't check whether it's set */
  1411. iterator->current = object;
  1412. }
  1413. zval_add_ref(&object);
  1414. return (zend_object_iterator*)iterator;
  1415. }
  1416. /* }}} */
  1417. /* {{{ spl_filesystem_dir_it_dtor */
  1418. static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter TSRMLS_DC)
  1419. {
  1420. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1421. if (iterator->intern.data) {
  1422. zval *object = iterator->intern.data;
  1423. zval_ptr_dtor(&object);
  1424. }
  1425. /* Otherwise we were called from the owning object free storage handler as
  1426. * it sets
  1427. * iterator->intern.data to NULL.
  1428. * We don't even need to destroy iterator->current as we didn't add a
  1429. * reference to it in move_forward or get_iterator */
  1430. }
  1431. /* }}} */
  1432. /* {{{ spl_filesystem_dir_it_valid */
  1433. static int spl_filesystem_dir_it_valid(zend_object_iterator *iter TSRMLS_DC)
  1434. {
  1435. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1436. return object->u.dir.entry.d_name[0] != '\0' ? SUCCESS : FAILURE;
  1437. }
  1438. /* }}} */
  1439. /* {{{ spl_filesystem_dir_it_current_data */
  1440. static void spl_filesystem_dir_it_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC)
  1441. {
  1442. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1443. *data = &iterator->current;
  1444. }
  1445. /* }}} */
  1446. /* {{{ spl_filesystem_dir_it_current_key */
  1447. static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC)
  1448. {
  1449. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1450. ZVAL_LONG(key, object->u.dir.index);
  1451. }
  1452. /* }}} */
  1453. /* {{{ spl_filesystem_dir_it_move_forward */
  1454. static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
  1455. {
  1456. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1457. object->u.dir.index++;
  1458. spl_filesystem_dir_read(object TSRMLS_CC);
  1459. if (object->file_name) {
  1460. efree(object->file_name);
  1461. object->file_name = NULL;
  1462. }
  1463. }
  1464. /* }}} */
  1465. /* {{{ spl_filesystem_dir_it_rewind */
  1466. static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter TSRMLS_DC)
  1467. {
  1468. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1469. object->u.dir.index = 0;
  1470. if (object->u.dir.dirp) {
  1471. php_stream_rewinddir(object->u.dir.dirp);
  1472. }
  1473. spl_filesystem_dir_read(object TSRMLS_CC);
  1474. }
  1475. /* }}} */
  1476. /* {{{ spl_filesystem_tree_it_dtor */
  1477. static void spl_filesystem_tree_it_dtor(zend_object_iterator *iter TSRMLS_DC)
  1478. {
  1479. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1480. if (iterator->intern.data) {
  1481. zval *object = iterator->intern.data;
  1482. zval_ptr_dtor(&object);
  1483. } else {
  1484. if (iterator->current) {
  1485. zval_ptr_dtor(&iterator->current);
  1486. }
  1487. }
  1488. }
  1489. /* }}} */
  1490. /* {{{ spl_filesystem_tree_it_current_data */
  1491. static void spl_filesystem_tree_it_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC)
  1492. {
  1493. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1494. spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
  1495. if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
  1496. if (!iterator->current) {
  1497. ALLOC_INIT_ZVAL(iterator->current);
  1498. spl_filesystem_object_get_file_name(object TSRMLS_CC);
  1499. ZVAL_STRINGL(iterator->current, object->file_name, object->file_name_len, 1);
  1500. }
  1501. *data = &iterator->current;
  1502. } else if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
  1503. if (!iterator->current) {
  1504. ALLOC_INIT_ZVAL(iterator->current);
  1505. spl_filesystem_object_get_file_name(object TSRMLS_CC);
  1506. spl_filesystem_object_create_type(0, object, SPL_FS_INFO, NULL, iterator->current TSRMLS_CC);
  1507. }
  1508. *data = &iterator->current;
  1509. } else {
  1510. *data = (zval**)&iterator->intern.data;
  1511. }
  1512. }
  1513. /* }}} */
  1514. /* {{{ spl_filesystem_tree_it_current_key */
  1515. static void spl_filesystem_tree_it_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC)
  1516. {
  1517. spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
  1518. if (SPL_FILE_DIR_KEY(object, SPL_FILE_DIR_KEY_AS_FILENAME)) {
  1519. ZVAL_STRING(key, object->u.dir.entry.d_name, 1);
  1520. } else {
  1521. spl_filesystem_object_get_file_name(object TSRMLS_CC);
  1522. ZVAL_STRINGL(key, object->file_name, object->file_name_len, 1);
  1523. }
  1524. }
  1525. /* }}} */
  1526. /* {{{ spl_filesystem_tree_it_move_forward */
  1527. static void spl_filesystem_tree_it_move_forward(zend_object_iterator *iter TSRMLS_DC)
  1528. {
  1529. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1530. spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
  1531. object->u.dir.index++;
  1532. do {
  1533. spl_filesystem_dir_read(object TSRMLS_CC);
  1534. } while (spl_filesystem_is_dot(object->u.dir.entry.d_name));
  1535. if (object->file_name) {
  1536. efree(object->file_name);
  1537. object->file_name = NULL;
  1538. }
  1539. if (iterator->current) {
  1540. zval_ptr_dtor(&iterator->current);
  1541. iterator->current = NULL;
  1542. }
  1543. }
  1544. /* }}} */
  1545. /* {{{ spl_filesystem_tree_it_rewind */
  1546. static void spl_filesystem_tree_it_rewind(zend_object_iterator *iter TSRMLS_DC)
  1547. {
  1548. spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
  1549. spl_filesystem_object *object = spl_filesystem_iterator_to_object(iterator);
  1550. object->u.dir.index = 0;
  1551. if (object->u.dir.dirp) {
  1552. php_stream_rewinddir(object->u.dir.dirp);
  1553. }
  1554. do {
  1555. spl_filesystem_dir_read(object TSRMLS_CC);
  1556. } while (spl_filesystem_is_dot(object->u.dir.entry.d_name));
  1557. if (iterator->current) {
  1558. zval_ptr_dtor(&iterator->current);
  1559. iterator->current = NULL;
  1560. }
  1561. }
  1562. /* }}} */
  1563. /* {{{ iterator handler table */
  1564. zend_object_iterator_funcs spl_filesystem_tree_it_funcs = {
  1565. spl_filesystem_tree_it_dtor,
  1566. spl_filesystem_dir_it_valid,
  1567. spl_filesystem_tree_it_current_data,
  1568. spl_filesystem_tree_it_current_key,
  1569. spl_filesystem_tree_it_move_forward,
  1570. spl_filesystem_tree_it_rewind
  1571. };
  1572. /* }}} */
  1573. /* {{{ spl_ce_dir_get_iterator */
  1574. zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
  1575. {
  1576. spl_filesystem_iterator *iterator;
  1577. spl_filesystem_object *dir_object;
  1578. if (by_ref) {
  1579. zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
  1580. }
  1581. dir_object = (spl_filesystem_object*)zend_object_store_get_object(object TSRMLS_CC);
  1582. iterator = spl_filesystem_object_to_iterator(dir_object);
  1583. /* initialize iterator if wasn't gotten before */
  1584. if (iterator->intern.data == NULL) {
  1585. iterator->intern.data = object;
  1586. iterator->intern.funcs = &spl_filesystem_tree_it_funcs;
  1587. }
  1588. zval_add_ref(&object);
  1589. return (zend_object_iterator*)iterator;
  1590. }
  1591. /* }}} */
  1592. /* {{{ spl_filesystem_object_cast */
  1593. static int spl_filesystem_object_cast(zval *readobj, zval *writeobj, int type TSRMLS_DC)
  1594. {
  1595. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(readobj TSRMLS_CC);
  1596. if (type == IS_STRING) {
  1597. if (Z_OBJCE_P(readobj)->__tostring) {
  1598. return std_object_handlers.cast_object(readobj, writeobj, type TSRMLS_CC);
  1599. }
  1600. switch (intern->type) {
  1601. case SPL_FS_INFO:
  1602. case SPL_FS_FILE:
  1603. if (readobj == writeobj) {
  1604. zval retval;
  1605. zval *retval_ptr = &retval;
  1606. ZVAL_STRINGL(retval_ptr, intern->file_name, intern->file_name_len, 1);
  1607. zval_dtor(readobj);
  1608. ZVAL_ZVAL(writeobj, retval_ptr, 0, 0);
  1609. } else {
  1610. ZVAL_STRINGL(writeobj, intern->file_name, intern->file_name_len, 1);
  1611. }
  1612. return SUCCESS;
  1613. case SPL_FS_DIR:
  1614. if (readobj == writeobj) {
  1615. zval retval;
  1616. zval *retval_ptr = &retval;
  1617. ZVAL_STRING(retval_ptr, intern->u.dir.entry.d_name, 1);
  1618. zval_dtor(readobj);
  1619. ZVAL_ZVAL(writeobj, retval_ptr, 0, 0);
  1620. } else {
  1621. ZVAL_STRING(writeobj, intern->u.dir.entry.d_name, 1);
  1622. }
  1623. return SUCCESS;
  1624. }
  1625. } else if (type == IS_BOOL) {
  1626. ZVAL_BOOL(writeobj, 1);
  1627. return SUCCESS;
  1628. }
  1629. if (readobj == writeobj) {
  1630. zval_dtor(readobj);
  1631. }
  1632. ZVAL_NULL(writeobj);
  1633. return FAILURE;
  1634. }
  1635. /* }}} */
  1636. /* {{{ declare method parameters */
  1637. /* supply a name and default to call by parameter */
  1638. ZEND_BEGIN_ARG_INFO(arginfo_info___construct, 0)
  1639. ZEND_ARG_INFO(0, file_name)
  1640. ZEND_END_ARG_INFO()
  1641. ZEND_BEGIN_ARG_INFO_EX(arginfo_info_openFile, 0, 0, 0)
  1642. ZEND_ARG_INFO(0, open_mode)
  1643. ZEND_ARG_INFO(0, use_include_path)
  1644. ZEND_ARG_INFO(0, context)
  1645. ZEND_END_ARG_INFO()
  1646. ZEND_BEGIN_ARG_INFO_EX(arginfo_info_optinalFileClass, 0, 0, 0)
  1647. ZEND_ARG_INFO(0, class_name)
  1648. ZEND_END_ARG_INFO()
  1649. ZEND_BEGIN_ARG_INFO_EX(arginfo_optinalSuffix, 0, 0, 0)
  1650. ZEND_ARG_INFO(0, suffix)
  1651. ZEND_END_ARG_INFO()
  1652. ZEND_BEGIN_ARG_INFO(arginfo_splfileinfo_void, 0)
  1653. ZEND_END_ARG_INFO()
  1654. /* the method table */
  1655. /* each method can have its own parameters and visibility */
  1656. static const zend_function_entry spl_SplFileInfo_functions[] = {
  1657. SPL_ME(SplFileInfo, __construct, arginfo_info___construct, ZEND_ACC_PUBLIC)
  1658. SPL_ME(SplFileInfo, getPath, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1659. SPL_ME(SplFileInfo, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1660. SPL_ME(SplFileInfo, getExtension, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1661. SPL_ME(SplFileInfo, getBasename, arginfo_optinalSuffix, ZEND_ACC_PUBLIC)
  1662. SPL_ME(SplFileInfo, getPathname, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1663. SPL_ME(SplFileInfo, getPerms, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1664. SPL_ME(SplFileInfo, getInode, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1665. SPL_ME(SplFileInfo, getSize, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1666. SPL_ME(SplFileInfo, getOwner, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1667. SPL_ME(SplFileInfo, getGroup, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1668. SPL_ME(SplFileInfo, getATime, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1669. SPL_ME(SplFileInfo, getMTime, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1670. SPL_ME(SplFileInfo, getCTime, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1671. SPL_ME(SplFileInfo, getType, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1672. SPL_ME(SplFileInfo, isWritable, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1673. SPL_ME(SplFileInfo, isReadable, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1674. SPL_ME(SplFileInfo, isExecutable, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1675. SPL_ME(SplFileInfo, isFile, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1676. SPL_ME(SplFileInfo, isDir, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1677. SPL_ME(SplFileInfo, isLink, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1678. SPL_ME(SplFileInfo, getLinkTarget, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1679. #if (!defined(__BEOS__) && !defined(NETWARE) && HAVE_REALPATH) || defined(ZTS)
  1680. SPL_ME(SplFileInfo, getRealPath, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1681. #endif
  1682. SPL_ME(SplFileInfo, getFileInfo, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1683. SPL_ME(SplFileInfo, getPathInfo, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1684. SPL_ME(SplFileInfo, openFile, arginfo_info_openFile, ZEND_ACC_PUBLIC)
  1685. SPL_ME(SplFileInfo, setFileClass, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1686. SPL_ME(SplFileInfo, setInfoClass, arginfo_info_optinalFileClass, ZEND_ACC_PUBLIC)
  1687. SPL_ME(SplFileInfo, _bad_state_ex, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1688. SPL_MA(SplFileInfo, __toString, SplFileInfo, getPathname, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1689. PHP_FE_END
  1690. };
  1691. ZEND_BEGIN_ARG_INFO(arginfo_dir___construct, 0)
  1692. ZEND_ARG_INFO(0, path)
  1693. ZEND_END_ARG_INFO()
  1694. ZEND_BEGIN_ARG_INFO(arginfo_dir_it_seek, 0)
  1695. ZEND_ARG_INFO(0, position)
  1696. ZEND_END_ARG_INFO();
  1697. /* the method table */
  1698. /* each method can have its own parameters and visibility */
  1699. static const zend_function_entry spl_DirectoryIterator_functions[] = {
  1700. SPL_ME(DirectoryIterator, __construct, arginfo_dir___construct, ZEND_ACC_PUBLIC)
  1701. SPL_ME(DirectoryIterator, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1702. SPL_ME(DirectoryIterator, getExtension, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1703. SPL_ME(DirectoryIterator, getBasename, arginfo_optinalSuffix, ZEND_ACC_PUBLIC)
  1704. SPL_ME(DirectoryIterator, isDot, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1705. SPL_ME(DirectoryIterator, rewind, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1706. SPL_ME(DirectoryIterator, valid, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1707. SPL_ME(DirectoryIterator, key, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1708. SPL_ME(DirectoryIterator, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1709. SPL_ME(DirectoryIterator, next, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1710. SPL_ME(DirectoryIterator, seek, arginfo_dir_it_seek, ZEND_ACC_PUBLIC)
  1711. SPL_MA(DirectoryIterator, __toString, DirectoryIterator, getFilename, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1712. PHP_FE_END
  1713. };
  1714. ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir___construct, 0, 0, 1)
  1715. ZEND_ARG_INFO(0, path)
  1716. ZEND_ARG_INFO(0, flags)
  1717. ZEND_END_ARG_INFO()
  1718. ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir_hasChildren, 0, 0, 0)
  1719. ZEND_ARG_INFO(0, allow_links)
  1720. ZEND_END_ARG_INFO()
  1721. ZEND_BEGIN_ARG_INFO_EX(arginfo_r_dir_setFlags, 0, 0, 0)
  1722. ZEND_ARG_INFO(0, flags)
  1723. ZEND_END_ARG_INFO()
  1724. static const zend_function_entry spl_FilesystemIterator_functions[] = {
  1725. SPL_ME(FilesystemIterator, __construct, arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
  1726. SPL_ME(FilesystemIterator, rewind, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1727. SPL_ME(DirectoryIterator, next, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1728. SPL_ME(FilesystemIterator, key, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1729. SPL_ME(FilesystemIterator, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1730. SPL_ME(FilesystemIterator, getFlags, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1731. SPL_ME(FilesystemIterator, setFlags, arginfo_r_dir_setFlags, ZEND_ACC_PUBLIC)
  1732. PHP_FE_END
  1733. };
  1734. static const zend_function_entry spl_RecursiveDirectoryIterator_functions[] = {
  1735. SPL_ME(RecursiveDirectoryIterator, __construct, arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
  1736. SPL_ME(RecursiveDirectoryIterator, hasChildren, arginfo_r_dir_hasChildren, ZEND_ACC_PUBLIC)
  1737. SPL_ME(RecursiveDirectoryIterator, getChildren, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1738. SPL_ME(RecursiveDirectoryIterator, getSubPath, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1739. SPL_ME(RecursiveDirectoryIterator, getSubPathname,arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1740. PHP_FE_END
  1741. };
  1742. #ifdef HAVE_GLOB
  1743. static const zend_function_entry spl_GlobIterator_functions[] = {
  1744. SPL_ME(GlobIterator, __construct, arginfo_r_dir___construct, ZEND_ACC_PUBLIC)
  1745. SPL_ME(GlobIterator, count, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  1746. PHP_FE_END
  1747. };
  1748. #endif
  1749. /* }}} */
  1750. static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent TSRMLS_DC) /* {{{ */
  1751. {
  1752. char *buf;
  1753. size_t line_len = 0;
  1754. long line_add = (intern->u.file.current_line || intern->u.file.current_zval) ? 1 : 0;
  1755. spl_filesystem_file_free_line(intern TSRMLS_CC);
  1756. if (php_stream_eof(intern->u.file.stream)) {
  1757. if (!silent) {
  1758. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Cannot read from file %s", intern->file_name);
  1759. }
  1760. return FAILURE;
  1761. }
  1762. if (intern->u.file.max_line_len > 0) {
  1763. buf = safe_emalloc((intern->u.file.max_line_len + 1), sizeof(char), 0);
  1764. if (php_stream_get_line(intern->u.file.stream, buf, intern->u.file.max_line_len + 1, &line_len) == NULL) {
  1765. efree(buf);
  1766. buf = NULL;
  1767. } else {
  1768. buf[line_len] = '\0';
  1769. }
  1770. } else {
  1771. buf = php_stream_get_line(intern->u.file.stream, NULL, 0, &line_len);
  1772. }
  1773. if (!buf) {
  1774. intern->u.file.current_line = estrdup("");
  1775. intern->u.file.current_line_len = 0;
  1776. } else {
  1777. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) {
  1778. line_len = strcspn(buf, "\r\n");
  1779. buf[line_len] = '\0';
  1780. }
  1781. intern->u.file.current_line = buf;
  1782. intern->u.file.current_line_len = line_len;
  1783. }
  1784. intern->u.file.current_line_num += line_add;
  1785. return SUCCESS;
  1786. } /* }}} */
  1787. static int spl_filesystem_file_call(spl_filesystem_object *intern, zend_function *func_ptr, int pass_num_args, zval *return_value, zval *arg2 TSRMLS_DC) /* {{{ */
  1788. {
  1789. zend_fcall_info fci;
  1790. zend_fcall_info_cache fcic;
  1791. zval z_fname;
  1792. zval * zresource_ptr = &intern->u.file.zresource, *retval = NULL;
  1793. int result;
  1794. int num_args = pass_num_args + (arg2 ? 2 : 1);
  1795. zval ***params = (zval***)safe_emalloc(num_args, sizeof(zval**), 0);
  1796. params[0] = &zresource_ptr;
  1797. if (arg2) {
  1798. params[1] = &arg2;
  1799. }
  1800. zend_get_parameters_array_ex(pass_num_args, params+(arg2 ? 2 : 1));
  1801. ZVAL_STRING(&z_fname, func_ptr->common.function_name, 0);
  1802. fci.size = sizeof(fci);
  1803. fci.function_table = EG(function_table);
  1804. fci.object_ptr = NULL;
  1805. fci.function_name = &z_fname;
  1806. fci.retval_ptr_ptr = &retval;
  1807. fci.param_count = num_args;
  1808. fci.params = params;
  1809. fci.no_separation = 1;
  1810. fci.symbol_table = NULL;
  1811. fcic.initialized = 1;
  1812. fcic.function_handler = func_ptr;
  1813. fcic.calling_scope = NULL;
  1814. fcic.called_scope = NULL;
  1815. fcic.object_ptr = NULL;
  1816. result = zend_call_function(&fci, &fcic TSRMLS_CC);
  1817. if (result == FAILURE || retval == NULL) {
  1818. RETVAL_FALSE;
  1819. } else {
  1820. ZVAL_ZVAL(return_value, retval, 1, 1);
  1821. }
  1822. efree(params);
  1823. return result;
  1824. } /* }}} */
  1825. #define FileFunctionCall(func_name, pass_num_args, arg2) /* {{{ */ \
  1826. { \
  1827. zend_function *func_ptr; \
  1828. int ret; \
  1829. ret = zend_hash_find(EG(function_table), #func_name, sizeof(#func_name), (void **) &func_ptr); \
  1830. if (ret != SUCCESS) { \
  1831. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Internal error, function '%s' not found. Please report", #func_name); \
  1832. return; \
  1833. } \
  1834. spl_filesystem_file_call(intern, func_ptr, pass_num_args, return_value, arg2 TSRMLS_CC); \
  1835. } /* }}} */
  1836. static int spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, char escape, zval *return_value TSRMLS_DC) /* {{{ */
  1837. {
  1838. int ret = SUCCESS;
  1839. do {
  1840. ret = spl_filesystem_file_read(intern, 1 TSRMLS_CC);
  1841. } while (ret == SUCCESS && !intern->u.file.current_line_len && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY));
  1842. if (ret == SUCCESS) {
  1843. size_t buf_len = intern->u.file.current_line_len;
  1844. char *buf = estrndup(intern->u.file.current_line, buf_len);
  1845. if (intern->u.file.current_zval) {
  1846. zval_ptr_dtor(&intern->u.file.current_zval);
  1847. }
  1848. ALLOC_INIT_ZVAL(intern->u.file.current_zval);
  1849. php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf, intern->u.file.current_zval TSRMLS_CC);
  1850. if (return_value) {
  1851. if (Z_TYPE_P(return_value) != IS_NULL) {
  1852. zval_dtor(return_value);
  1853. ZVAL_NULL(return_value);
  1854. }
  1855. ZVAL_ZVAL(return_value, intern->u.file.current_zval, 1, 0);
  1856. }
  1857. }
  1858. return ret;
  1859. }
  1860. /* }}} */
  1861. static int spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, int silent TSRMLS_DC) /* {{{ */
  1862. {
  1863. zval *retval = NULL;
  1864. /* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */
  1865. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || intern->u.file.func_getCurr->common.scope != spl_ce_SplFileObject) {
  1866. if (php_stream_eof(intern->u.file.stream)) {
  1867. if (!silent) {
  1868. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Cannot read from file %s", intern->file_name);
  1869. }
  1870. return FAILURE;
  1871. }
  1872. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
  1873. return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL TSRMLS_CC);
  1874. } else {
  1875. zend_call_method_with_0_params(&this_ptr, Z_OBJCE_P(getThis()), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
  1876. }
  1877. if (retval) {
  1878. if (intern->u.file.current_line || intern->u.file.current_zval) {
  1879. intern->u.file.current_line_num++;
  1880. }
  1881. spl_filesystem_file_free_line(intern TSRMLS_CC);
  1882. if (Z_TYPE_P(retval) == IS_STRING) {
  1883. intern->u.file.current_line = estrndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval));
  1884. intern->u.file.current_line_len = Z_STRLEN_P(retval);
  1885. } else {
  1886. MAKE_STD_ZVAL(intern->u.file.current_zval);
  1887. ZVAL_ZVAL(intern->u.file.current_zval, retval, 1, 0);
  1888. }
  1889. zval_ptr_dtor(&retval);
  1890. return SUCCESS;
  1891. } else {
  1892. return FAILURE;
  1893. }
  1894. } else {
  1895. return spl_filesystem_file_read(intern, silent TSRMLS_CC);
  1896. }
  1897. } /* }}} */
  1898. static int spl_filesystem_file_is_empty_line(spl_filesystem_object *intern TSRMLS_DC) /* {{{ */
  1899. {
  1900. if (intern->u.file.current_line) {
  1901. return intern->u.file.current_line_len == 0;
  1902. } else if (intern->u.file.current_zval) {
  1903. switch(Z_TYPE_P(intern->u.file.current_zval)) {
  1904. case IS_STRING:
  1905. return Z_STRLEN_P(intern->u.file.current_zval) == 0;
  1906. case IS_ARRAY:
  1907. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
  1908. && zend_hash_num_elements(Z_ARRVAL_P(intern->u.file.current_zval)) == 1) {
  1909. zval ** first = Z_ARRVAL_P(intern->u.file.current_zval)->pListHead->pData;
  1910. return Z_TYPE_PP(first) == IS_STRING && Z_STRLEN_PP(first) == 0;
  1911. }
  1912. return zend_hash_num_elements(Z_ARRVAL_P(intern->u.file.current_zval)) == 0;
  1913. case IS_NULL:
  1914. return 1;
  1915. default:
  1916. return 0;
  1917. }
  1918. } else {
  1919. return 1;
  1920. }
  1921. }
  1922. /* }}} */
  1923. static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, int silent TSRMLS_DC) /* {{{ */
  1924. {
  1925. int ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent TSRMLS_CC);
  1926. while (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY) && ret == SUCCESS && spl_filesystem_file_is_empty_line(intern TSRMLS_CC)) {
  1927. spl_filesystem_file_free_line(intern TSRMLS_CC);
  1928. ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent TSRMLS_CC);
  1929. }
  1930. return ret;
  1931. }
  1932. /* }}} */
  1933. static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern TSRMLS_DC) /* {{{ */
  1934. {
  1935. if(!intern->u.file.stream) {
  1936. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  1937. return;
  1938. }
  1939. if (-1 == php_stream_rewind(intern->u.file.stream)) {
  1940. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Cannot rewind file %s", intern->file_name);
  1941. } else {
  1942. spl_filesystem_file_free_line(intern TSRMLS_CC);
  1943. intern->u.file.current_line_num = 0;
  1944. }
  1945. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
  1946. spl_filesystem_file_read_line(this_ptr, intern, 1 TSRMLS_CC);
  1947. }
  1948. } /* }}} */
  1949. /* {{{ proto void SplFileObject::__construct(string filename [, string mode = 'r' [, bool use_include_path [, resource context]]]])
  1950. Construct a new file object */
  1951. SPL_METHOD(SplFileObject, __construct)
  1952. {
  1953. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1954. zend_bool use_include_path = 0;
  1955. char *p1, *p2;
  1956. char *tmp_path;
  1957. int tmp_path_len;
  1958. zend_error_handling error_handling;
  1959. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
  1960. intern->u.file.open_mode = NULL;
  1961. intern->u.file.open_mode_len = 0;
  1962. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|sbr!",
  1963. &intern->file_name, &intern->file_name_len,
  1964. &intern->u.file.open_mode, &intern->u.file.open_mode_len,
  1965. &use_include_path, &intern->u.file.zcontext) == FAILURE) {
  1966. intern->u.file.open_mode = NULL;
  1967. intern->file_name = NULL;
  1968. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1969. return;
  1970. }
  1971. if (intern->u.file.open_mode == NULL) {
  1972. intern->u.file.open_mode = "r";
  1973. intern->u.file.open_mode_len = 1;
  1974. }
  1975. if (spl_filesystem_file_open(intern, use_include_path, 0 TSRMLS_CC) == SUCCESS) {
  1976. tmp_path_len = strlen(intern->u.file.stream->orig_path);
  1977. if (tmp_path_len > 1 && IS_SLASH_AT(intern->u.file.stream->orig_path, tmp_path_len-1)) {
  1978. tmp_path_len--;
  1979. }
  1980. tmp_path = estrndup(intern->u.file.stream->orig_path, tmp_path_len);
  1981. p1 = strrchr(tmp_path, '/');
  1982. #if defined(PHP_WIN32) || defined(NETWARE)
  1983. p2 = strrchr(tmp_path, '\\');
  1984. #else
  1985. p2 = 0;
  1986. #endif
  1987. if (p1 || p2) {
  1988. intern->_path_len = (p1 > p2 ? p1 : p2) - tmp_path;
  1989. } else {
  1990. intern->_path_len = 0;
  1991. }
  1992. efree(tmp_path);
  1993. intern->_path = estrndup(intern->u.file.stream->orig_path, intern->_path_len);
  1994. }
  1995. zend_restore_error_handling(&error_handling TSRMLS_CC);
  1996. } /* }}} */
  1997. /* {{{ proto void SplTempFileObject::__construct([int max_memory])
  1998. Construct a new temp file object */
  1999. SPL_METHOD(SplTempFileObject, __construct)
  2000. {
  2001. long max_memory = PHP_STREAM_MAX_MEM;
  2002. char tmp_fname[48];
  2003. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2004. zend_error_handling error_handling;
  2005. zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
  2006. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &max_memory) == FAILURE) {
  2007. zend_restore_error_handling(&error_handling TSRMLS_CC);
  2008. return;
  2009. }
  2010. if (max_memory < 0) {
  2011. intern->file_name = "php://memory";
  2012. intern->file_name_len = 12;
  2013. } else if (ZEND_NUM_ARGS()) {
  2014. intern->file_name_len = slprintf(tmp_fname, sizeof(tmp_fname), "php://temp/maxmemory:%ld", max_memory);
  2015. intern->file_name = tmp_fname;
  2016. } else {
  2017. intern->file_name = "php://temp";
  2018. intern->file_name_len = 10;
  2019. }
  2020. intern->u.file.open_mode = "wb";
  2021. intern->u.file.open_mode_len = 1;
  2022. intern->u.file.zcontext = NULL;
  2023. if (spl_filesystem_file_open(intern, 0, 0 TSRMLS_CC) == SUCCESS) {
  2024. intern->_path_len = 0;
  2025. intern->_path = estrndup("", 0);
  2026. }
  2027. zend_restore_error_handling(&error_handling TSRMLS_CC);
  2028. } /* }}} */
  2029. /* {{{ proto void SplFileObject::rewind()
  2030. Rewind the file and read the first line */
  2031. SPL_METHOD(SplFileObject, rewind)
  2032. {
  2033. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2034. if (zend_parse_parameters_none() == FAILURE) {
  2035. return;
  2036. }
  2037. spl_filesystem_file_rewind(getThis(), intern TSRMLS_CC);
  2038. } /* }}} */
  2039. /* {{{ proto void SplFileObject::eof()
  2040. Return whether end of file is reached */
  2041. SPL_METHOD(SplFileObject, eof)
  2042. {
  2043. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2044. if (zend_parse_parameters_none() == FAILURE) {
  2045. return;
  2046. }
  2047. if(!intern->u.file.stream) {
  2048. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2049. return;
  2050. }
  2051. RETURN_BOOL(php_stream_eof(intern->u.file.stream));
  2052. } /* }}} */
  2053. /* {{{ proto void SplFileObject::valid()
  2054. Return !eof() */
  2055. SPL_METHOD(SplFileObject, valid)
  2056. {
  2057. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2058. if (zend_parse_parameters_none() == FAILURE) {
  2059. return;
  2060. }
  2061. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
  2062. RETURN_BOOL(intern->u.file.current_line || intern->u.file.current_zval);
  2063. } else {
  2064. if(!intern->u.file.stream) {
  2065. RETURN_FALSE;
  2066. }
  2067. RETVAL_BOOL(!php_stream_eof(intern->u.file.stream));
  2068. }
  2069. } /* }}} */
  2070. /* {{{ proto string SplFileObject::fgets()
  2071. Rturn next line from file */
  2072. SPL_METHOD(SplFileObject, fgets)
  2073. {
  2074. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2075. if (zend_parse_parameters_none() == FAILURE) {
  2076. return;
  2077. }
  2078. if(!intern->u.file.stream) {
  2079. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2080. return;
  2081. }
  2082. if (spl_filesystem_file_read(intern, 0 TSRMLS_CC) == FAILURE) {
  2083. RETURN_FALSE;
  2084. }
  2085. RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len, 1);
  2086. } /* }}} */
  2087. /* {{{ proto string SplFileObject::current()
  2088. Return current line from file */
  2089. SPL_METHOD(SplFileObject, current)
  2090. {
  2091. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2092. if (zend_parse_parameters_none() == FAILURE) {
  2093. return;
  2094. }
  2095. if(!intern->u.file.stream) {
  2096. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2097. return;
  2098. }
  2099. if (!intern->u.file.current_line && !intern->u.file.current_zval) {
  2100. spl_filesystem_file_read_line(getThis(), intern, 1 TSRMLS_CC);
  2101. }
  2102. if (intern->u.file.current_line && (!SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || !intern->u.file.current_zval)) {
  2103. RETURN_STRINGL(intern->u.file.current_line, intern->u.file.current_line_len, 1);
  2104. } else if (intern->u.file.current_zval) {
  2105. RETURN_ZVAL(intern->u.file.current_zval, 1, 0);
  2106. }
  2107. RETURN_FALSE;
  2108. } /* }}} */
  2109. /* {{{ proto int SplFileObject::key()
  2110. Return line number */
  2111. SPL_METHOD(SplFileObject, key)
  2112. {
  2113. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2114. if (zend_parse_parameters_none() == FAILURE) {
  2115. return;
  2116. }
  2117. /* Do not read the next line to support correct counting with fgetc()
  2118. if (!intern->current_line) {
  2119. spl_filesystem_file_read_line(getThis(), intern, 1 TSRMLS_CC);
  2120. } */
  2121. RETURN_LONG(intern->u.file.current_line_num);
  2122. } /* }}} */
  2123. /* {{{ proto void SplFileObject::next()
  2124. Read next line */
  2125. SPL_METHOD(SplFileObject, next)
  2126. {
  2127. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2128. if (zend_parse_parameters_none() == FAILURE) {
  2129. return;
  2130. }
  2131. spl_filesystem_file_free_line(intern TSRMLS_CC);
  2132. if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
  2133. spl_filesystem_file_read_line(getThis(), intern, 1 TSRMLS_CC);
  2134. }
  2135. intern->u.file.current_line_num++;
  2136. } /* }}} */
  2137. /* {{{ proto void SplFileObject::setFlags(int flags)
  2138. Set file handling flags */
  2139. SPL_METHOD(SplFileObject, setFlags)
  2140. {
  2141. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2142. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &intern->flags) == FAILURE) {
  2143. return;
  2144. }
  2145. } /* }}} */
  2146. /* {{{ proto int SplFileObject::getFlags()
  2147. Get file handling flags */
  2148. SPL_METHOD(SplFileObject, getFlags)
  2149. {
  2150. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2151. if (zend_parse_parameters_none() == FAILURE) {
  2152. return;
  2153. }
  2154. RETURN_LONG(intern->flags & SPL_FILE_OBJECT_MASK);
  2155. } /* }}} */
  2156. /* {{{ proto void SplFileObject::setMaxLineLen(int max_len)
  2157. Set maximum line length */
  2158. SPL_METHOD(SplFileObject, setMaxLineLen)
  2159. {
  2160. long max_len;
  2161. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2162. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &max_len) == FAILURE) {
  2163. return;
  2164. }
  2165. if (max_len < 0) {
  2166. zend_throw_exception_ex(spl_ce_DomainException, 0 TSRMLS_CC, "Maximum line length must be greater than or equal zero");
  2167. return;
  2168. }
  2169. intern->u.file.max_line_len = max_len;
  2170. } /* }}} */
  2171. /* {{{ proto int SplFileObject::getMaxLineLen()
  2172. Get maximum line length */
  2173. SPL_METHOD(SplFileObject, getMaxLineLen)
  2174. {
  2175. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2176. if (zend_parse_parameters_none() == FAILURE) {
  2177. return;
  2178. }
  2179. RETURN_LONG((long)intern->u.file.max_line_len);
  2180. } /* }}} */
  2181. /* {{{ proto bool SplFileObject::hasChildren()
  2182. Return false */
  2183. SPL_METHOD(SplFileObject, hasChildren)
  2184. {
  2185. if (zend_parse_parameters_none() == FAILURE) {
  2186. return;
  2187. }
  2188. RETURN_FALSE;
  2189. } /* }}} */
  2190. /* {{{ proto bool SplFileObject::getChildren()
  2191. Read NULL */
  2192. SPL_METHOD(SplFileObject, getChildren)
  2193. {
  2194. if (zend_parse_parameters_none() == FAILURE) {
  2195. return;
  2196. }
  2197. /* return NULL */
  2198. } /* }}} */
  2199. /* {{{ FileFunction */
  2200. #define FileFunction(func_name) \
  2201. SPL_METHOD(SplFileObject, func_name) \
  2202. { \
  2203. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
  2204. FileFunctionCall(func_name, ZEND_NUM_ARGS(), NULL); \
  2205. }
  2206. /* }}} */
  2207. /* {{{ proto array SplFileObject::fgetcsv([string delimiter [, string enclosure [, escape = '\\']]])
  2208. Return current line as csv */
  2209. SPL_METHOD(SplFileObject, fgetcsv)
  2210. {
  2211. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2212. char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure, escape = intern->u.file.escape;
  2213. char *delim = NULL, *enclo = NULL, *esc = NULL;
  2214. int d_len = 0, e_len = 0, esc_len = 0;
  2215. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
  2216. if(!intern->u.file.stream) {
  2217. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2218. return;
  2219. }
  2220. switch(ZEND_NUM_ARGS())
  2221. {
  2222. case 3:
  2223. if (esc_len != 1) {
  2224. php_error_docref(NULL TSRMLS_CC, E_WARNING, "escape must be a character");
  2225. RETURN_FALSE;
  2226. }
  2227. escape = esc[0];
  2228. /* no break */
  2229. case 2:
  2230. if (e_len != 1) {
  2231. php_error_docref(NULL TSRMLS_CC, E_WARNING, "enclosure must be a character");
  2232. RETURN_FALSE;
  2233. }
  2234. enclosure = enclo[0];
  2235. /* no break */
  2236. case 1:
  2237. if (d_len != 1) {
  2238. php_error_docref(NULL TSRMLS_CC, E_WARNING, "delimiter must be a character");
  2239. RETURN_FALSE;
  2240. }
  2241. delimiter = delim[0];
  2242. /* no break */
  2243. case 0:
  2244. break;
  2245. }
  2246. spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value TSRMLS_CC);
  2247. }
  2248. }
  2249. /* }}} */
  2250. /* {{{ proto int SplFileObject::fputcsv(array fields, [string delimiter [, string enclosure [, string escape]]])
  2251. Output a field array as a CSV line */
  2252. SPL_METHOD(SplFileObject, fputcsv)
  2253. {
  2254. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2255. char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure, escape = intern->u.file.escape;
  2256. char *delim = NULL, *enclo = NULL, *esc = NULL;
  2257. int d_len = 0, e_len = 0, esc_len = 0, ret;
  2258. zval *fields = NULL;
  2259. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|sss", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
  2260. switch(ZEND_NUM_ARGS())
  2261. {
  2262. case 4:
  2263. if (esc_len != 1) {
  2264. php_error_docref(NULL TSRMLS_CC, E_WARNING, "escape must be a character");
  2265. RETURN_FALSE;
  2266. }
  2267. escape = esc[0];
  2268. /* no break */
  2269. case 3:
  2270. if (e_len != 1) {
  2271. php_error_docref(NULL TSRMLS_CC, E_WARNING, "enclosure must be a character");
  2272. RETURN_FALSE;
  2273. }
  2274. enclosure = enclo[0];
  2275. /* no break */
  2276. case 2:
  2277. if (d_len != 1) {
  2278. php_error_docref(NULL TSRMLS_CC, E_WARNING, "delimiter must be a character");
  2279. RETURN_FALSE;
  2280. }
  2281. delimiter = delim[0];
  2282. /* no break */
  2283. case 1:
  2284. case 0:
  2285. break;
  2286. }
  2287. ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape TSRMLS_CC);
  2288. RETURN_LONG(ret);
  2289. }
  2290. }
  2291. /* }}} */
  2292. /* {{{ proto void SplFileObject::setCsvControl([string delimiter = ',' [, string enclosure = '"' [, string escape = '\\']]])
  2293. Set the delimiter, enclosure and escape character used in fgetcsv */
  2294. SPL_METHOD(SplFileObject, setCsvControl)
  2295. {
  2296. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2297. char delimiter = ',', enclosure = '"', escape='\\';
  2298. char *delim = NULL, *enclo = NULL, *esc = NULL;
  2299. int d_len = 0, e_len = 0, esc_len = 0;
  2300. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
  2301. switch(ZEND_NUM_ARGS())
  2302. {
  2303. case 3:
  2304. if (esc_len != 1) {
  2305. php_error_docref(NULL TSRMLS_CC, E_WARNING, "escape must be a character");
  2306. RETURN_FALSE;
  2307. }
  2308. escape = esc[0];
  2309. /* no break */
  2310. case 2:
  2311. if (e_len != 1) {
  2312. php_error_docref(NULL TSRMLS_CC, E_WARNING, "enclosure must be a character");
  2313. RETURN_FALSE;
  2314. }
  2315. enclosure = enclo[0];
  2316. /* no break */
  2317. case 1:
  2318. if (d_len != 1) {
  2319. php_error_docref(NULL TSRMLS_CC, E_WARNING, "delimiter must be a character");
  2320. RETURN_FALSE;
  2321. }
  2322. delimiter = delim[0];
  2323. /* no break */
  2324. case 0:
  2325. break;
  2326. }
  2327. intern->u.file.delimiter = delimiter;
  2328. intern->u.file.enclosure = enclosure;
  2329. intern->u.file.escape = escape;
  2330. }
  2331. }
  2332. /* }}} */
  2333. /* {{{ proto array SplFileObject::getCsvControl()
  2334. Get the delimiter, enclosure and escape character used in fgetcsv */
  2335. SPL_METHOD(SplFileObject, getCsvControl)
  2336. {
  2337. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2338. char delimiter[2], enclosure[2], escape[2];
  2339. array_init(return_value);
  2340. delimiter[0] = intern->u.file.delimiter;
  2341. delimiter[1] = '\0';
  2342. enclosure[0] = intern->u.file.enclosure;
  2343. enclosure[1] = '\0';
  2344. escape[0] = intern->u.file.escape;
  2345. escape[1] = '\0';
  2346. add_next_index_string(return_value, delimiter, 1);
  2347. add_next_index_string(return_value, enclosure, 1);
  2348. add_next_index_string(return_value, escape, 1);
  2349. }
  2350. /* }}} */
  2351. /* {{{ proto bool SplFileObject::flock(int operation [, int &wouldblock])
  2352. Portable file locking */
  2353. FileFunction(flock)
  2354. /* }}} */
  2355. /* {{{ proto bool SplFileObject::fflush()
  2356. Flush the file */
  2357. SPL_METHOD(SplFileObject, fflush)
  2358. {
  2359. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2360. if(!intern->u.file.stream) {
  2361. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2362. return;
  2363. }
  2364. RETURN_BOOL(!php_stream_flush(intern->u.file.stream));
  2365. } /* }}} */
  2366. /* {{{ proto int SplFileObject::ftell()
  2367. Return current file position */
  2368. SPL_METHOD(SplFileObject, ftell)
  2369. {
  2370. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2371. long ret;
  2372. if(!intern->u.file.stream) {
  2373. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2374. return;
  2375. }
  2376. ret = php_stream_tell(intern->u.file.stream);
  2377. if (ret == -1) {
  2378. RETURN_FALSE;
  2379. } else {
  2380. RETURN_LONG(ret);
  2381. }
  2382. } /* }}} */
  2383. /* {{{ proto int SplFileObject::fseek(int pos [, int whence = SEEK_SET])
  2384. Return current file position */
  2385. SPL_METHOD(SplFileObject, fseek)
  2386. {
  2387. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2388. long pos, whence = SEEK_SET;
  2389. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &pos, &whence) == FAILURE) {
  2390. return;
  2391. }
  2392. if(!intern->u.file.stream) {
  2393. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2394. return;
  2395. }
  2396. spl_filesystem_file_free_line(intern TSRMLS_CC);
  2397. RETURN_LONG(php_stream_seek(intern->u.file.stream, pos, whence));
  2398. } /* }}} */
  2399. /* {{{ proto int SplFileObject::fgetc()
  2400. Get a character form the file */
  2401. SPL_METHOD(SplFileObject, fgetc)
  2402. {
  2403. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2404. char buf[2];
  2405. int result;
  2406. if(!intern->u.file.stream) {
  2407. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2408. return;
  2409. }
  2410. spl_filesystem_file_free_line(intern TSRMLS_CC);
  2411. result = php_stream_getc(intern->u.file.stream);
  2412. if (result == EOF) {
  2413. RETVAL_FALSE;
  2414. } else {
  2415. if (result == '\n') {
  2416. intern->u.file.current_line_num++;
  2417. }
  2418. buf[0] = result;
  2419. buf[1] = '\0';
  2420. RETURN_STRINGL(buf, 1, 1);
  2421. }
  2422. } /* }}} */
  2423. /* {{{ proto string SplFileObject::fgetss([string allowable_tags])
  2424. Get a line from file pointer and strip HTML tags */
  2425. SPL_METHOD(SplFileObject, fgetss)
  2426. {
  2427. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2428. zval *arg2 = NULL;
  2429. MAKE_STD_ZVAL(arg2);
  2430. if(!intern->u.file.stream) {
  2431. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2432. return;
  2433. }
  2434. if (intern->u.file.max_line_len > 0) {
  2435. ZVAL_LONG(arg2, intern->u.file.max_line_len);
  2436. } else {
  2437. ZVAL_LONG(arg2, 1024);
  2438. }
  2439. spl_filesystem_file_free_line(intern TSRMLS_CC);
  2440. intern->u.file.current_line_num++;
  2441. FileFunctionCall(fgetss, ZEND_NUM_ARGS(), arg2);
  2442. zval_ptr_dtor(&arg2);
  2443. } /* }}} */
  2444. /* {{{ proto int SplFileObject::fpassthru()
  2445. Output all remaining data from a file pointer */
  2446. SPL_METHOD(SplFileObject, fpassthru)
  2447. {
  2448. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2449. if(!intern->u.file.stream) {
  2450. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2451. return;
  2452. }
  2453. RETURN_LONG(php_stream_passthru(intern->u.file.stream));
  2454. } /* }}} */
  2455. /* {{{ proto bool SplFileObject::fscanf(string format [, string ...])
  2456. Implements a mostly ANSI compatible fscanf() */
  2457. SPL_METHOD(SplFileObject, fscanf)
  2458. {
  2459. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2460. if(!intern->u.file.stream) {
  2461. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2462. return;
  2463. }
  2464. spl_filesystem_file_free_line(intern TSRMLS_CC);
  2465. intern->u.file.current_line_num++;
  2466. FileFunctionCall(fscanf, ZEND_NUM_ARGS(), NULL);
  2467. }
  2468. /* }}} */
  2469. /* {{{ proto mixed SplFileObject::fwrite(string str [, int length])
  2470. Binary-safe file write */
  2471. SPL_METHOD(SplFileObject, fwrite)
  2472. {
  2473. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2474. char *str;
  2475. int str_len;
  2476. long length = 0;
  2477. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len, &length) == FAILURE) {
  2478. return;
  2479. }
  2480. if(!intern->u.file.stream) {
  2481. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2482. return;
  2483. }
  2484. if (ZEND_NUM_ARGS() > 1) {
  2485. str_len = MAX(0, MIN(length, str_len));
  2486. }
  2487. if (!str_len) {
  2488. RETURN_LONG(0);
  2489. }
  2490. RETURN_LONG(php_stream_write(intern->u.file.stream, str, str_len));
  2491. } /* }}} */
  2492. SPL_METHOD(SplFileObject, fread)
  2493. {
  2494. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2495. long length = 0;
  2496. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE) {
  2497. return;
  2498. }
  2499. if(!intern->u.file.stream) {
  2500. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2501. return;
  2502. }
  2503. if (length <= 0) {
  2504. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
  2505. RETURN_FALSE;
  2506. }
  2507. if (length > INT_MAX) {
  2508. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be no more than %d", INT_MAX);
  2509. RETURN_FALSE;
  2510. }
  2511. Z_STRVAL_P(return_value) = emalloc(length + 1);
  2512. Z_STRLEN_P(return_value) = php_stream_read(intern->u.file.stream, Z_STRVAL_P(return_value), length);
  2513. /* needed because recv/read/gzread doesnt put a null at the end*/
  2514. Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
  2515. Z_TYPE_P(return_value) = IS_STRING;
  2516. }
  2517. /* {{{ proto bool SplFileObject::fstat()
  2518. Stat() on a filehandle */
  2519. FileFunction(fstat)
  2520. /* }}} */
  2521. /* {{{ proto bool SplFileObject::ftruncate(int size)
  2522. Truncate file to 'size' length */
  2523. SPL_METHOD(SplFileObject, ftruncate)
  2524. {
  2525. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2526. long size;
  2527. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &size) == FAILURE) {
  2528. return;
  2529. }
  2530. if(!intern->u.file.stream) {
  2531. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2532. return;
  2533. }
  2534. if (!php_stream_truncate_supported(intern->u.file.stream)) {
  2535. zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Can't truncate file %s", intern->file_name);
  2536. RETURN_FALSE;
  2537. }
  2538. RETURN_BOOL(0 == php_stream_truncate_set_size(intern->u.file.stream, size));
  2539. } /* }}} */
  2540. /* {{{ proto void SplFileObject::seek(int line_pos)
  2541. Seek to specified line */
  2542. SPL_METHOD(SplFileObject, seek)
  2543. {
  2544. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  2545. long line_pos;
  2546. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &line_pos) == FAILURE) {
  2547. return;
  2548. }
  2549. if(!intern->u.file.stream) {
  2550. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "Object not initialized");
  2551. return;
  2552. }
  2553. if (line_pos < 0) {
  2554. zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Can't seek file %s to negative line %ld", intern->file_name, line_pos);
  2555. RETURN_FALSE;
  2556. }
  2557. spl_filesystem_file_rewind(getThis(), intern TSRMLS_CC);
  2558. while(intern->u.file.current_line_num < line_pos) {
  2559. if (spl_filesystem_file_read_line(getThis(), intern, 1 TSRMLS_CC) == FAILURE) {
  2560. break;
  2561. }
  2562. }
  2563. } /* }}} */
  2564. /* {{{ Function/Class/Method definitions */
  2565. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object___construct, 0, 0, 1)
  2566. ZEND_ARG_INFO(0, file_name)
  2567. ZEND_ARG_INFO(0, open_mode)
  2568. ZEND_ARG_INFO(0, use_include_path)
  2569. ZEND_ARG_INFO(0, context)
  2570. ZEND_END_ARG_INFO()
  2571. ZEND_BEGIN_ARG_INFO(arginfo_file_object_setFlags, 0)
  2572. ZEND_ARG_INFO(0, flags)
  2573. ZEND_END_ARG_INFO()
  2574. ZEND_BEGIN_ARG_INFO(arginfo_file_object_setMaxLineLen, 0)
  2575. ZEND_ARG_INFO(0, max_len)
  2576. ZEND_END_ARG_INFO()
  2577. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fgetcsv, 0, 0, 0)
  2578. ZEND_ARG_INFO(0, delimiter)
  2579. ZEND_ARG_INFO(0, enclosure)
  2580. ZEND_ARG_INFO(0, escape)
  2581. ZEND_END_ARG_INFO()
  2582. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fputcsv, 0, 0, 1)
  2583. ZEND_ARG_INFO(0, fields)
  2584. ZEND_ARG_INFO(0, delimiter)
  2585. ZEND_ARG_INFO(0, enclosure)
  2586. ZEND_ARG_INFO(0, escape)
  2587. ZEND_END_ARG_INFO()
  2588. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_flock, 0, 0, 1)
  2589. ZEND_ARG_INFO(0, operation)
  2590. ZEND_ARG_INFO(1, wouldblock)
  2591. ZEND_END_ARG_INFO()
  2592. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fseek, 0, 0, 1)
  2593. ZEND_ARG_INFO(0, pos)
  2594. ZEND_ARG_INFO(0, whence)
  2595. ZEND_END_ARG_INFO()
  2596. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fgetss, 0, 0, 0)
  2597. ZEND_ARG_INFO(0, allowable_tags)
  2598. ZEND_END_ARG_INFO()
  2599. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fscanf, 0, 0, 1)
  2600. ZEND_ARG_INFO(0, format)
  2601. ZEND_ARG_VARIADIC_INFO(1, vars)
  2602. ZEND_END_ARG_INFO()
  2603. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fwrite, 0, 0, 1)
  2604. ZEND_ARG_INFO(0, str)
  2605. ZEND_ARG_INFO(0, length)
  2606. ZEND_END_ARG_INFO()
  2607. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_fread, 0, 0, 1)
  2608. ZEND_ARG_INFO(0, length)
  2609. ZEND_END_ARG_INFO()
  2610. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_ftruncate, 0, 0, 1)
  2611. ZEND_ARG_INFO(0, size)
  2612. ZEND_END_ARG_INFO()
  2613. ZEND_BEGIN_ARG_INFO_EX(arginfo_file_object_seek, 0, 0, 1)
  2614. ZEND_ARG_INFO(0, line_pos)
  2615. ZEND_END_ARG_INFO()
  2616. static const zend_function_entry spl_SplFileObject_functions[] = {
  2617. SPL_ME(SplFileObject, __construct, arginfo_file_object___construct, ZEND_ACC_PUBLIC)
  2618. SPL_ME(SplFileObject, rewind, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2619. SPL_ME(SplFileObject, eof, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2620. SPL_ME(SplFileObject, valid, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2621. SPL_ME(SplFileObject, fgets, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2622. SPL_ME(SplFileObject, fgetcsv, arginfo_file_object_fgetcsv, ZEND_ACC_PUBLIC)
  2623. SPL_ME(SplFileObject, fputcsv, arginfo_file_object_fputcsv, ZEND_ACC_PUBLIC)
  2624. SPL_ME(SplFileObject, setCsvControl, arginfo_file_object_fgetcsv, ZEND_ACC_PUBLIC)
  2625. SPL_ME(SplFileObject, getCsvControl, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2626. SPL_ME(SplFileObject, flock, arginfo_file_object_flock, ZEND_ACC_PUBLIC)
  2627. SPL_ME(SplFileObject, fflush, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2628. SPL_ME(SplFileObject, ftell, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2629. SPL_ME(SplFileObject, fseek, arginfo_file_object_fseek, ZEND_ACC_PUBLIC)
  2630. SPL_ME(SplFileObject, fgetc, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2631. SPL_ME(SplFileObject, fpassthru, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2632. SPL_ME(SplFileObject, fgetss, arginfo_file_object_fgetss, ZEND_ACC_PUBLIC)
  2633. SPL_ME(SplFileObject, fscanf, arginfo_file_object_fscanf, ZEND_ACC_PUBLIC)
  2634. SPL_ME(SplFileObject, fwrite, arginfo_file_object_fwrite, ZEND_ACC_PUBLIC)
  2635. SPL_ME(SplFileObject, fread, arginfo_file_object_fread, ZEND_ACC_PUBLIC)
  2636. SPL_ME(SplFileObject, fstat, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2637. SPL_ME(SplFileObject, ftruncate, arginfo_file_object_ftruncate, ZEND_ACC_PUBLIC)
  2638. SPL_ME(SplFileObject, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2639. SPL_ME(SplFileObject, key, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2640. SPL_ME(SplFileObject, next, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2641. SPL_ME(SplFileObject, setFlags, arginfo_file_object_setFlags, ZEND_ACC_PUBLIC)
  2642. SPL_ME(SplFileObject, getFlags, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2643. SPL_ME(SplFileObject, setMaxLineLen, arginfo_file_object_setMaxLineLen, ZEND_ACC_PUBLIC)
  2644. SPL_ME(SplFileObject, getMaxLineLen, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2645. SPL_ME(SplFileObject, hasChildren, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2646. SPL_ME(SplFileObject, getChildren, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2647. SPL_ME(SplFileObject, seek, arginfo_file_object_seek, ZEND_ACC_PUBLIC)
  2648. /* mappings */
  2649. SPL_MA(SplFileObject, getCurrentLine, SplFileObject, fgets, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2650. SPL_MA(SplFileObject, __toString, SplFileObject, current, arginfo_splfileinfo_void, ZEND_ACC_PUBLIC)
  2651. PHP_FE_END
  2652. };
  2653. ZEND_BEGIN_ARG_INFO_EX(arginfo_temp_file_object___construct, 0, 0, 0)
  2654. ZEND_ARG_INFO(0, max_memory)
  2655. ZEND_END_ARG_INFO()
  2656. static const zend_function_entry spl_SplTempFileObject_functions[] = {
  2657. SPL_ME(SplTempFileObject, __construct, arginfo_temp_file_object___construct, ZEND_ACC_PUBLIC)
  2658. PHP_FE_END
  2659. };
  2660. /* }}} */
  2661. /* {{{ PHP_MINIT_FUNCTION(spl_directory)
  2662. */
  2663. PHP_MINIT_FUNCTION(spl_directory)
  2664. {
  2665. REGISTER_SPL_STD_CLASS_EX(SplFileInfo, spl_filesystem_object_new, spl_SplFileInfo_functions);
  2666. memcpy(&spl_filesystem_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  2667. spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
  2668. spl_filesystem_object_handlers.cast_object = spl_filesystem_object_cast;
  2669. spl_filesystem_object_handlers.get_debug_info = spl_filesystem_object_get_debug_info;
  2670. spl_ce_SplFileInfo->serialize = zend_class_serialize_deny;
  2671. spl_ce_SplFileInfo->unserialize = zend_class_unserialize_deny;
  2672. REGISTER_SPL_SUB_CLASS_EX(DirectoryIterator, SplFileInfo, spl_filesystem_object_new, spl_DirectoryIterator_functions);
  2673. zend_class_implements(spl_ce_DirectoryIterator TSRMLS_CC, 1, zend_ce_iterator);
  2674. REGISTER_SPL_IMPLEMENTS(DirectoryIterator, SeekableIterator);
  2675. spl_ce_DirectoryIterator->get_iterator = spl_filesystem_dir_get_iterator;
  2676. REGISTER_SPL_SUB_CLASS_EX(FilesystemIterator, DirectoryIterator, spl_filesystem_object_new, spl_FilesystemIterator_functions);
  2677. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_MODE_MASK", SPL_FILE_DIR_CURRENT_MODE_MASK);
  2678. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_PATHNAME", SPL_FILE_DIR_CURRENT_AS_PATHNAME);
  2679. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_FILEINFO", SPL_FILE_DIR_CURRENT_AS_FILEINFO);
  2680. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "CURRENT_AS_SELF", SPL_FILE_DIR_CURRENT_AS_SELF);
  2681. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_MODE_MASK", SPL_FILE_DIR_KEY_MODE_MASK);
  2682. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_PATHNAME", SPL_FILE_DIR_KEY_AS_PATHNAME);
  2683. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "FOLLOW_SYMLINKS", SPL_FILE_DIR_FOLLOW_SYMLINKS);
  2684. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "KEY_AS_FILENAME", SPL_FILE_DIR_KEY_AS_FILENAME);
  2685. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "NEW_CURRENT_AND_KEY", SPL_FILE_DIR_KEY_AS_FILENAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO);
  2686. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "OTHER_MODE_MASK", SPL_FILE_DIR_OTHERS_MASK);
  2687. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "SKIP_DOTS", SPL_FILE_DIR_SKIPDOTS);
  2688. REGISTER_SPL_CLASS_CONST_LONG(FilesystemIterator, "UNIX_PATHS", SPL_FILE_DIR_UNIXPATHS);
  2689. spl_ce_FilesystemIterator->get_iterator = spl_filesystem_tree_get_iterator;
  2690. REGISTER_SPL_SUB_CLASS_EX(RecursiveDirectoryIterator, FilesystemIterator, spl_filesystem_object_new, spl_RecursiveDirectoryIterator_functions);
  2691. REGISTER_SPL_IMPLEMENTS(RecursiveDirectoryIterator, RecursiveIterator);
  2692. memcpy(&spl_filesystem_object_check_handlers, &spl_filesystem_object_handlers, sizeof(zend_object_handlers));
  2693. spl_filesystem_object_check_handlers.get_method = spl_filesystem_object_get_method_check;
  2694. #ifdef HAVE_GLOB
  2695. REGISTER_SPL_SUB_CLASS_EX(GlobIterator, FilesystemIterator, spl_filesystem_object_new_check, spl_GlobIterator_functions);
  2696. REGISTER_SPL_IMPLEMENTS(GlobIterator, Countable);
  2697. #endif
  2698. REGISTER_SPL_SUB_CLASS_EX(SplFileObject, SplFileInfo, spl_filesystem_object_new_check, spl_SplFileObject_functions);
  2699. REGISTER_SPL_IMPLEMENTS(SplFileObject, RecursiveIterator);
  2700. REGISTER_SPL_IMPLEMENTS(SplFileObject, SeekableIterator);
  2701. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "DROP_NEW_LINE", SPL_FILE_OBJECT_DROP_NEW_LINE);
  2702. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_AHEAD", SPL_FILE_OBJECT_READ_AHEAD);
  2703. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "SKIP_EMPTY", SPL_FILE_OBJECT_SKIP_EMPTY);
  2704. REGISTER_SPL_CLASS_CONST_LONG(SplFileObject, "READ_CSV", SPL_FILE_OBJECT_READ_CSV);
  2705. REGISTER_SPL_SUB_CLASS_EX(SplTempFileObject, SplFileObject, spl_filesystem_object_new_check, spl_SplTempFileObject_functions);
  2706. return SUCCESS;
  2707. }
  2708. /* }}} */
  2709. /*
  2710. * Local variables:
  2711. * tab-width: 4
  2712. * c-basic-offset: 4
  2713. * End:
  2714. * vim600: noet sw=4 ts=4 fdm=marker
  2715. * vim<600: noet sw=4 ts=4
  2716. */