filesys.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2002, 2004, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /* See stime.c for comments on why _POSIX_C_SOURCE is not always defined. */
  18. #define _LARGEFILE64_SOURCE /* ask for stat64 etc */
  19. #ifdef __hpux
  20. #define _POSIX_C_SOURCE 199506L /* for readdir_r */
  21. #endif
  22. #ifdef HAVE_CONFIG_H
  23. # include <config.h>
  24. #endif
  25. /* This blob per the Autoconf manual (under "Particular Functions"), updated
  26. to match that of Gnulib. */
  27. #ifndef alloca
  28. # if HAVE_ALLOCA_H
  29. # include <alloca.h>
  30. # elif defined __GNUC__
  31. # define alloca __builtin_alloca
  32. # elif defined _AIX
  33. # define alloca __alloca
  34. # elif defined _MSC_VER
  35. # include <malloc.h>
  36. # define alloca _alloca
  37. # else
  38. # include <stddef.h>
  39. # ifdef __cplusplus
  40. extern "C"
  41. # endif
  42. void *alloca (size_t);
  43. # endif
  44. #endif
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include "libguile/_scm.h"
  48. #include "libguile/smob.h"
  49. #include "libguile/feature.h"
  50. #include "libguile/fports.h"
  51. #include "libguile/private-gc.h" /* for SCM_MAX */
  52. #include "libguile/iselect.h"
  53. #include "libguile/strings.h"
  54. #include "libguile/vectors.h"
  55. #include "libguile/lang.h"
  56. #include "libguile/dynwind.h"
  57. #include "libguile/validate.h"
  58. #include "libguile/filesys.h"
  59. #ifdef HAVE_IO_H
  60. #include <io.h>
  61. #endif
  62. #ifdef HAVE_DIRECT_H
  63. #include <direct.h>
  64. #endif
  65. #ifdef TIME_WITH_SYS_TIME
  66. # include <sys/time.h>
  67. # include <time.h>
  68. #else
  69. # if HAVE_SYS_TIME_H
  70. # include <sys/time.h>
  71. # else
  72. # include <time.h>
  73. # endif
  74. #endif
  75. #ifdef HAVE_UNISTD_H
  76. #include <unistd.h>
  77. #endif
  78. #ifdef LIBC_H_WITH_UNISTD_H
  79. #include <libc.h>
  80. #endif
  81. #ifdef HAVE_SYS_SELECT_H
  82. #include <sys/select.h>
  83. #endif
  84. #ifdef HAVE_STRING_H
  85. #include <string.h>
  86. #endif
  87. #include <sys/types.h>
  88. #include <sys/stat.h>
  89. #include <fcntl.h>
  90. #ifdef HAVE_PWD_H
  91. #include <pwd.h>
  92. #endif
  93. #if defined (__MINGW32__) || defined (_MSC_VER) || defined (__BORLANDC__)
  94. # include "win32-dirent.h"
  95. # define NAMLEN(dirent) strlen((dirent)->d_name)
  96. /* The following bits are per AC_HEADER_DIRENT doco in the autoconf manual */
  97. #elif HAVE_DIRENT_H
  98. # include <dirent.h>
  99. # define NAMLEN(dirent) strlen((dirent)->d_name)
  100. #else
  101. # define dirent direct
  102. # define NAMLEN(dirent) (dirent)->d_namlen
  103. # if HAVE_SYS_NDIR_H
  104. # include <sys/ndir.h>
  105. # endif
  106. # if HAVE_SYS_DIR_H
  107. # include <sys/dir.h>
  108. # endif
  109. # if HAVE_NDIR_H
  110. # include <ndir.h>
  111. # endif
  112. #endif
  113. /* Ultrix has S_IFSOCK, but no S_ISSOCK. Ipe! */
  114. #if defined (S_IFSOCK) && ! defined (S_ISSOCK)
  115. #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
  116. #endif
  117. /* The MinGW gcc does not define the S_ISSOCK macro. Any other native Windows
  118. compiler like BorlandC or MSVC has none of these macros defined. */
  119. #ifdef __MINGW32__
  120. # ifdef _S_IFIFO
  121. # undef _S_IFIFO
  122. # endif
  123. # ifdef _S_IFCHR
  124. # undef _S_IFCHR
  125. # endif
  126. # ifdef _S_IFBLK
  127. # undef _S_IFBLK
  128. # endif
  129. # ifdef _S_IFDIR
  130. # undef _S_IFDIR
  131. # endif
  132. # ifdef _S_IFREG
  133. # undef _S_IFREG
  134. # endif
  135. # ifdef _S_IFSOCK
  136. # undef _S_IFSOCK
  137. # endif
  138. # define _S_IFIFO 0x1000 /* FIFO */
  139. # define _S_IFCHR 0x2000 /* Character */
  140. # define _S_IFBLK 0x3000 /* Block */
  141. # define _S_IFDIR 0x4000 /* Directory */
  142. # define _S_IFREG 0x8000 /* Regular */
  143. # define _S_IFSOCK 0xC000 /* Socket */
  144. # ifdef S_ISBLK
  145. # undef S_ISBLK
  146. # endif
  147. # ifdef S_ISFIFO
  148. # undef S_ISFIFO
  149. # endif
  150. # ifdef S_ISCHR
  151. # undef S_ISCHR
  152. # endif
  153. # ifdef S_ISDIR
  154. # undef S_ISDIR
  155. # endif
  156. # ifdef S_ISREG
  157. # undef S_ISREG
  158. # endif
  159. # ifdef S_ISSOCK
  160. # undef S_ISSOCK
  161. # endif
  162. # define S_ISBLK(mode) (((mode) & _S_IFMT) == _S_IFBLK)
  163. # define S_ISFIFO(mode) (((mode) & _S_IFMT) == _S_IFIFO)
  164. # define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
  165. # define S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR)
  166. # define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
  167. # define S_ISSOCK(mode) (((mode) & _S_IFMT) == _S_IFSOCK)
  168. #endif /* __MINGW32__ */
  169. /* Some more definitions for the native Windows port. */
  170. #ifdef __MINGW32__
  171. # define mkdir(path, mode) mkdir (path)
  172. # define fsync(fd) _commit (fd)
  173. # define fchmod(fd, mode) (-1)
  174. #endif /* __MINGW32__ */
  175. /* dirfd() returns the file descriptor underlying a "DIR*" directory stream.
  176. Found on MacOS X for instance. The following definition is for Solaris
  177. 10, it's probably not right elsewhere, but that's ok, it shouldn't be
  178. used elsewhere. Crib note: If we need more then gnulib has a dirfd.m4
  179. figuring out how to get the fd (dirfd function, dirfd macro, dd_fd field,
  180. or d_fd field). */
  181. #ifndef dirfd
  182. #define dirfd(dirstream) ((dirstream)->dd_fd)
  183. #endif
  184. /* Two helper macros for an often used pattern */
  185. #define STRING_SYSCALL(str,cstr,code) \
  186. do { \
  187. int eno; \
  188. char *cstr = scm_to_locale_string (str); \
  189. SCM_SYSCALL (code); \
  190. eno = errno; free (cstr); errno = eno; \
  191. } while (0)
  192. #define STRING2_SYSCALL(str1,cstr1,str2,cstr2,code) \
  193. do { \
  194. int eno; \
  195. char *cstr1, *cstr2; \
  196. scm_dynwind_begin (0); \
  197. cstr1 = scm_to_locale_string (str1); \
  198. scm_dynwind_free (cstr1); \
  199. cstr2 = scm_to_locale_string (str2); \
  200. scm_dynwind_free (cstr2); \
  201. SCM_SYSCALL (code); \
  202. eno = errno; scm_dynwind_end (); errno = eno; \
  203. } while (0)
  204. /* {Permissions}
  205. */
  206. #ifdef HAVE_CHOWN
  207. SCM_DEFINE (scm_chown, "chown", 3, 0, 0,
  208. (SCM object, SCM owner, SCM group),
  209. "Change the ownership and group of the file referred to by @var{object} to\n"
  210. "the integer values @var{owner} and @var{group}. @var{object} can be\n"
  211. "a string containing a file name or, if the platform\n"
  212. "supports fchown, a port or integer file descriptor\n"
  213. "which is open on the file. The return value\n"
  214. "is unspecified.\n\n"
  215. "If @var{object} is a symbolic link, either the\n"
  216. "ownership of the link or the ownership of the referenced file will be\n"
  217. "changed depending on the operating system (lchown is\n"
  218. "unsupported at present). If @var{owner} or @var{group} is specified\n"
  219. "as @code{-1}, then that ID is not changed.")
  220. #define FUNC_NAME s_scm_chown
  221. {
  222. int rv;
  223. object = SCM_COERCE_OUTPORT (object);
  224. #ifdef HAVE_FCHOWN
  225. if (scm_is_integer (object) || (SCM_OPFPORTP (object)))
  226. {
  227. int fdes = (SCM_OPFPORTP (object)?
  228. SCM_FPORT_FDES (object) : scm_to_int (object));
  229. SCM_SYSCALL (rv = fchown (fdes, scm_to_int (owner), scm_to_int (group)));
  230. }
  231. else
  232. #endif
  233. {
  234. STRING_SYSCALL (object, c_object,
  235. rv = chown (c_object,
  236. scm_to_int (owner), scm_to_int (group)));
  237. }
  238. if (rv == -1)
  239. SCM_SYSERROR;
  240. return SCM_UNSPECIFIED;
  241. }
  242. #undef FUNC_NAME
  243. #endif /* HAVE_CHOWN */
  244. SCM_DEFINE (scm_chmod, "chmod", 2, 0, 0,
  245. (SCM object, SCM mode),
  246. "Changes the permissions of the file referred to by @var{obj}.\n"
  247. "@var{obj} can be a string containing a file name or a port or integer file\n"
  248. "descriptor which is open on a file (in which case @code{fchmod} is used\n"
  249. "as the underlying system call).\n"
  250. "@var{mode} specifies\n"
  251. "the new permissions as a decimal number, e.g., @code{(chmod \"foo\" #o755)}.\n"
  252. "The return value is unspecified.")
  253. #define FUNC_NAME s_scm_chmod
  254. {
  255. int rv;
  256. int fdes;
  257. object = SCM_COERCE_OUTPORT (object);
  258. if (scm_is_integer (object) || SCM_OPFPORTP (object))
  259. {
  260. if (scm_is_integer (object))
  261. fdes = scm_to_int (object);
  262. else
  263. fdes = SCM_FPORT_FDES (object);
  264. SCM_SYSCALL (rv = fchmod (fdes, scm_to_int (mode)));
  265. }
  266. else
  267. {
  268. STRING_SYSCALL (object, c_object,
  269. rv = chmod (c_object, scm_to_int (mode)));
  270. }
  271. if (rv == -1)
  272. SCM_SYSERROR;
  273. return SCM_UNSPECIFIED;
  274. }
  275. #undef FUNC_NAME
  276. SCM_DEFINE (scm_umask, "umask", 0, 1, 0,
  277. (SCM mode),
  278. "If @var{mode} is omitted, returns a decimal number representing the current\n"
  279. "file creation mask. Otherwise the file creation mask is set to\n"
  280. "@var{mode} and the previous value is returned.\n\n"
  281. "E.g., @code{(umask #o022)} sets the mask to octal 22, decimal 18.")
  282. #define FUNC_NAME s_scm_umask
  283. {
  284. mode_t mask;
  285. if (SCM_UNBNDP (mode))
  286. {
  287. mask = umask (0);
  288. umask (mask);
  289. }
  290. else
  291. {
  292. mask = umask (scm_to_uint (mode));
  293. }
  294. return scm_from_uint (mask);
  295. }
  296. #undef FUNC_NAME
  297. SCM_DEFINE (scm_open_fdes, "open-fdes", 2, 1, 0,
  298. (SCM path, SCM flags, SCM mode),
  299. "Similar to @code{open} but return a file descriptor instead of\n"
  300. "a port.")
  301. #define FUNC_NAME s_scm_open_fdes
  302. {
  303. int fd;
  304. int iflags;
  305. int imode;
  306. iflags = SCM_NUM2INT (2, flags);
  307. imode = SCM_NUM2INT_DEF (3, mode, 0666);
  308. STRING_SYSCALL (path, c_path, fd = open_or_open64 (c_path, iflags, imode));
  309. if (fd == -1)
  310. SCM_SYSERROR;
  311. return scm_from_int (fd);
  312. }
  313. #undef FUNC_NAME
  314. SCM_DEFINE (scm_open, "open", 2, 1, 0,
  315. (SCM path, SCM flags, SCM mode),
  316. "Open the file named by @var{path} for reading and/or writing.\n"
  317. "@var{flags} is an integer specifying how the file should be opened.\n"
  318. "@var{mode} is an integer specifying the permission bits of the file, if\n"
  319. "it needs to be created, before the umask is applied. The default is 666\n"
  320. "(Unix itself has no default).\n\n"
  321. "@var{flags} can be constructed by combining variables using @code{logior}.\n"
  322. "Basic flags are:\n\n"
  323. "@defvar O_RDONLY\n"
  324. "Open the file read-only.\n"
  325. "@end defvar\n"
  326. "@defvar O_WRONLY\n"
  327. "Open the file write-only.\n"
  328. "@end defvar\n"
  329. "@defvar O_RDWR\n"
  330. "Open the file read/write.\n"
  331. "@end defvar\n"
  332. "@defvar O_APPEND\n"
  333. "Append to the file instead of truncating.\n"
  334. "@end defvar\n"
  335. "@defvar O_CREAT\n"
  336. "Create the file if it does not already exist.\n"
  337. "@end defvar\n\n"
  338. "See the Unix documentation of the @code{open} system call\n"
  339. "for additional flags.")
  340. #define FUNC_NAME s_scm_open
  341. {
  342. SCM newpt;
  343. char *port_mode;
  344. int fd;
  345. int iflags;
  346. fd = scm_to_int (scm_open_fdes (path, flags, mode));
  347. iflags = SCM_NUM2INT (2, flags);
  348. if (iflags & O_RDWR)
  349. {
  350. if (iflags & O_APPEND)
  351. port_mode = "a+";
  352. else if (iflags & O_CREAT)
  353. port_mode = "w+";
  354. else
  355. port_mode = "r+";
  356. }
  357. else {
  358. if (iflags & O_APPEND)
  359. port_mode = "a";
  360. else if (iflags & O_WRONLY)
  361. port_mode = "w";
  362. else
  363. port_mode = "r";
  364. }
  365. newpt = scm_fdes_to_port (fd, port_mode, path);
  366. return newpt;
  367. }
  368. #undef FUNC_NAME
  369. SCM_DEFINE (scm_close, "close", 1, 0, 0,
  370. (SCM fd_or_port),
  371. "Similar to close-port (@pxref{Closing, close-port}),\n"
  372. "but also works on file descriptors. A side\n"
  373. "effect of closing a file descriptor is that any ports using that file\n"
  374. "descriptor are moved to a different file descriptor and have\n"
  375. "their revealed counts set to zero.")
  376. #define FUNC_NAME s_scm_close
  377. {
  378. int rv;
  379. int fd;
  380. fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
  381. if (SCM_PORTP (fd_or_port))
  382. return scm_close_port (fd_or_port);
  383. fd = scm_to_int (fd_or_port);
  384. scm_evict_ports (fd); /* see scsh manual. */
  385. SCM_SYSCALL (rv = close (fd));
  386. /* following scsh, closing an already closed file descriptor is
  387. not an error. */
  388. if (rv < 0 && errno != EBADF)
  389. SCM_SYSERROR;
  390. return scm_from_bool (rv >= 0);
  391. }
  392. #undef FUNC_NAME
  393. SCM_DEFINE (scm_close_fdes, "close-fdes", 1, 0, 0,
  394. (SCM fd),
  395. "A simple wrapper for the @code{close} system call.\n"
  396. "Close file descriptor @var{fd}, which must be an integer.\n"
  397. "Unlike close (@pxref{Ports and File Descriptors, close}),\n"
  398. "the file descriptor will be closed even if a port is using it.\n"
  399. "The return value is unspecified.")
  400. #define FUNC_NAME s_scm_close_fdes
  401. {
  402. int c_fd;
  403. int rv;
  404. c_fd = scm_to_int (fd);
  405. SCM_SYSCALL (rv = close (c_fd));
  406. if (rv < 0)
  407. SCM_SYSERROR;
  408. return SCM_UNSPECIFIED;
  409. }
  410. #undef FUNC_NAME
  411. /* {Files}
  412. */
  413. SCM_SYMBOL (scm_sym_regular, "regular");
  414. SCM_SYMBOL (scm_sym_directory, "directory");
  415. #ifdef S_ISLNK
  416. SCM_SYMBOL (scm_sym_symlink, "symlink");
  417. #endif
  418. SCM_SYMBOL (scm_sym_block_special, "block-special");
  419. SCM_SYMBOL (scm_sym_char_special, "char-special");
  420. SCM_SYMBOL (scm_sym_fifo, "fifo");
  421. SCM_SYMBOL (scm_sym_sock, "socket");
  422. SCM_SYMBOL (scm_sym_unknown, "unknown");
  423. static SCM
  424. scm_stat2scm (struct stat_or_stat64 *stat_temp)
  425. {
  426. SCM ans = scm_c_make_vector (15, SCM_UNSPECIFIED);
  427. SCM_SIMPLE_VECTOR_SET(ans, 0, scm_from_ulong (stat_temp->st_dev));
  428. SCM_SIMPLE_VECTOR_SET(ans, 1, scm_from_ino_t_or_ino64_t (stat_temp->st_ino));
  429. SCM_SIMPLE_VECTOR_SET(ans, 2, scm_from_ulong (stat_temp->st_mode));
  430. SCM_SIMPLE_VECTOR_SET(ans, 3, scm_from_ulong (stat_temp->st_nlink));
  431. SCM_SIMPLE_VECTOR_SET(ans, 4, scm_from_ulong (stat_temp->st_uid));
  432. SCM_SIMPLE_VECTOR_SET(ans, 5, scm_from_ulong (stat_temp->st_gid));
  433. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  434. SCM_SIMPLE_VECTOR_SET(ans, 6, scm_from_ulong (stat_temp->st_rdev));
  435. #else
  436. SCM_SIMPLE_VECTOR_SET(ans, 6, SCM_BOOL_F);
  437. #endif
  438. SCM_SIMPLE_VECTOR_SET(ans, 7, scm_from_off_t_or_off64_t (stat_temp->st_size));
  439. SCM_SIMPLE_VECTOR_SET(ans, 8, scm_from_ulong (stat_temp->st_atime));
  440. SCM_SIMPLE_VECTOR_SET(ans, 9, scm_from_ulong (stat_temp->st_mtime));
  441. SCM_SIMPLE_VECTOR_SET(ans, 10, scm_from_ulong (stat_temp->st_ctime));
  442. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  443. SCM_SIMPLE_VECTOR_SET(ans, 11, scm_from_ulong (stat_temp->st_blksize));
  444. #else
  445. SCM_SIMPLE_VECTOR_SET(ans, 11, scm_from_ulong (4096L));
  446. #endif
  447. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  448. SCM_SIMPLE_VECTOR_SET(ans, 12, scm_from_blkcnt_t_or_blkcnt64_t (stat_temp->st_blocks));
  449. #else
  450. SCM_SIMPLE_VECTOR_SET(ans, 12, SCM_BOOL_F);
  451. #endif
  452. {
  453. int mode = stat_temp->st_mode;
  454. if (S_ISREG (mode))
  455. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_regular);
  456. else if (S_ISDIR (mode))
  457. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_directory);
  458. #ifdef S_ISLNK
  459. /* systems without symlinks probably don't have S_ISLNK */
  460. else if (S_ISLNK (mode))
  461. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_symlink);
  462. #endif
  463. else if (S_ISBLK (mode))
  464. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_block_special);
  465. else if (S_ISCHR (mode))
  466. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_char_special);
  467. else if (S_ISFIFO (mode))
  468. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_fifo);
  469. #ifdef S_ISSOCK
  470. else if (S_ISSOCK (mode))
  471. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_sock);
  472. #endif
  473. else
  474. SCM_SIMPLE_VECTOR_SET(ans, 13, scm_sym_unknown);
  475. SCM_SIMPLE_VECTOR_SET(ans, 14, scm_from_int ((~S_IFMT) & mode));
  476. /* the layout of the bits in ve[14] is intended to be portable.
  477. If there are systems that don't follow the usual convention,
  478. the following could be used:
  479. tmp = 0;
  480. if (S_ISUID & mode) tmp += 1;
  481. tmp <<= 1;
  482. if (S_IRGRP & mode) tmp += 1;
  483. tmp <<= 1;
  484. if (S_ISVTX & mode) tmp += 1;
  485. tmp <<= 1;
  486. if (S_IRUSR & mode) tmp += 1;
  487. tmp <<= 1;
  488. if (S_IWUSR & mode) tmp += 1;
  489. tmp <<= 1;
  490. if (S_IXUSR & mode) tmp += 1;
  491. tmp <<= 1;
  492. if (S_IWGRP & mode) tmp += 1;
  493. tmp <<= 1;
  494. if (S_IXGRP & mode) tmp += 1;
  495. tmp <<= 1;
  496. if (S_IROTH & mode) tmp += 1;
  497. tmp <<= 1;
  498. if (S_IWOTH & mode) tmp += 1;
  499. tmp <<= 1;
  500. if (S_IXOTH & mode) tmp += 1;
  501. SCM_SIMPLE_VECTOR_SET(ans, 14, scm_from_int (tmp));
  502. */
  503. }
  504. return ans;
  505. }
  506. #ifdef __MINGW32__
  507. /*
  508. * Try getting the appropiate stat buffer for a given file descriptor
  509. * under Windows. It differentiates between file, pipe and socket
  510. * descriptors.
  511. */
  512. static int fstat_Win32 (int fdes, struct stat *buf)
  513. {
  514. int error, optlen = sizeof (int);
  515. memset (buf, 0, sizeof (struct stat));
  516. /* Is this a socket ? */
  517. if (getsockopt (fdes, SOL_SOCKET, SO_ERROR, (void *) &error, &optlen) >= 0)
  518. {
  519. buf->st_mode = _S_IFSOCK | _S_IREAD | _S_IWRITE | _S_IEXEC;
  520. buf->st_nlink = 1;
  521. buf->st_atime = buf->st_ctime = buf->st_mtime = time (NULL);
  522. return 0;
  523. }
  524. /* Maybe a regular file or pipe ? */
  525. return fstat (fdes, buf);
  526. }
  527. #endif /* __MINGW32__ */
  528. SCM_DEFINE (scm_stat, "stat", 1, 0, 0,
  529. (SCM object),
  530. "Return an object containing various information about the file\n"
  531. "determined by @var{obj}. @var{obj} can be a string containing\n"
  532. "a file name or a port or integer file descriptor which is open\n"
  533. "on a file (in which case @code{fstat} is used as the underlying\n"
  534. "system call).\n"
  535. "\n"
  536. "The object returned by @code{stat} can be passed as a single\n"
  537. "parameter to the following procedures, all of which return\n"
  538. "integers:\n"
  539. "\n"
  540. "@table @code\n"
  541. "@item stat:dev\n"
  542. "The device containing the file.\n"
  543. "@item stat:ino\n"
  544. "The file serial number, which distinguishes this file from all\n"
  545. "other files on the same device.\n"
  546. "@item stat:mode\n"
  547. "The mode of the file. This includes file type information and\n"
  548. "the file permission bits. See @code{stat:type} and\n"
  549. "@code{stat:perms} below.\n"
  550. "@item stat:nlink\n"
  551. "The number of hard links to the file.\n"
  552. "@item stat:uid\n"
  553. "The user ID of the file's owner.\n"
  554. "@item stat:gid\n"
  555. "The group ID of the file.\n"
  556. "@item stat:rdev\n"
  557. "Device ID; this entry is defined only for character or block\n"
  558. "special files.\n"
  559. "@item stat:size\n"
  560. "The size of a regular file in bytes.\n"
  561. "@item stat:atime\n"
  562. "The last access time for the file.\n"
  563. "@item stat:mtime\n"
  564. "The last modification time for the file.\n"
  565. "@item stat:ctime\n"
  566. "The last modification time for the attributes of the file.\n"
  567. "@item stat:blksize\n"
  568. "The optimal block size for reading or writing the file, in\n"
  569. "bytes.\n"
  570. "@item stat:blocks\n"
  571. "The amount of disk space that the file occupies measured in\n"
  572. "units of 512 byte blocks.\n"
  573. "@end table\n"
  574. "\n"
  575. "In addition, the following procedures return the information\n"
  576. "from stat:mode in a more convenient form:\n"
  577. "\n"
  578. "@table @code\n"
  579. "@item stat:type\n"
  580. "A symbol representing the type of file. Possible values are\n"
  581. "regular, directory, symlink, block-special, char-special, fifo,\n"
  582. "socket and unknown\n"
  583. "@item stat:perms\n"
  584. "An integer representing the access permission bits.\n"
  585. "@end table")
  586. #define FUNC_NAME s_scm_stat
  587. {
  588. int rv;
  589. int fdes;
  590. struct stat_or_stat64 stat_temp;
  591. if (scm_is_integer (object))
  592. {
  593. #ifdef __MINGW32__
  594. SCM_SYSCALL (rv = fstat_Win32 (scm_to_int (object), &stat_temp));
  595. #else
  596. SCM_SYSCALL (rv = fstat_or_fstat64 (scm_to_int (object), &stat_temp));
  597. #endif
  598. }
  599. else if (scm_is_string (object))
  600. {
  601. char *file = scm_to_locale_string (object);
  602. #ifdef __MINGW32__
  603. char *p;
  604. p = file + strlen (file) - 1;
  605. while (p > file && (*p == '/' || *p == '\\'))
  606. *p-- = '\0';
  607. #endif
  608. SCM_SYSCALL (rv = stat_or_stat64 (file, &stat_temp));
  609. free (file);
  610. }
  611. else
  612. {
  613. object = SCM_COERCE_OUTPORT (object);
  614. SCM_VALIDATE_OPFPORT (1, object);
  615. fdes = SCM_FPORT_FDES (object);
  616. #ifdef __MINGW32__
  617. SCM_SYSCALL (rv = fstat_Win32 (fdes, &stat_temp));
  618. #else
  619. SCM_SYSCALL (rv = fstat_or_fstat64 (fdes, &stat_temp));
  620. #endif
  621. }
  622. if (rv == -1)
  623. {
  624. int en = errno;
  625. SCM_SYSERROR_MSG ("~A: ~S",
  626. scm_list_2 (scm_strerror (scm_from_int (en)),
  627. object),
  628. en);
  629. }
  630. return scm_stat2scm (&stat_temp);
  631. }
  632. #undef FUNC_NAME
  633. /* {Modifying Directories}
  634. */
  635. #ifdef HAVE_LINK
  636. SCM_DEFINE (scm_link, "link", 2, 0, 0,
  637. (SCM oldpath, SCM newpath),
  638. "Creates a new name @var{newpath} in the file system for the\n"
  639. "file named by @var{oldpath}. If @var{oldpath} is a symbolic\n"
  640. "link, the link may or may not be followed depending on the\n"
  641. "system.")
  642. #define FUNC_NAME s_scm_link
  643. {
  644. int val;
  645. STRING2_SYSCALL (oldpath, c_oldpath,
  646. newpath, c_newpath,
  647. val = link (c_oldpath, c_newpath));
  648. if (val != 0)
  649. SCM_SYSERROR;
  650. return SCM_UNSPECIFIED;
  651. }
  652. #undef FUNC_NAME
  653. #endif /* HAVE_LINK */
  654. #ifdef HAVE_RENAME
  655. #define my_rename rename
  656. #else
  657. static int
  658. my_rename (const char *oldname, const char *newname)
  659. {
  660. int rv;
  661. SCM_SYSCALL (rv = link (oldname, newname));
  662. if (rv == 0)
  663. {
  664. SCM_SYSCALL (rv = unlink (oldname));
  665. if (rv != 0)
  666. /* unlink failed. remove new name */
  667. SCM_SYSCALL (unlink (newname));
  668. }
  669. return rv;
  670. }
  671. #endif
  672. SCM_DEFINE (scm_rename, "rename-file", 2, 0, 0,
  673. (SCM oldname, SCM newname),
  674. "Renames the file specified by @var{oldname} to @var{newname}.\n"
  675. "The return value is unspecified.")
  676. #define FUNC_NAME s_scm_rename
  677. {
  678. int rv;
  679. STRING2_SYSCALL (oldname, c_oldname,
  680. newname, c_newname,
  681. rv = my_rename (c_oldname, c_newname));
  682. if (rv != 0)
  683. SCM_SYSERROR;
  684. return SCM_UNSPECIFIED;
  685. }
  686. #undef FUNC_NAME
  687. SCM_DEFINE (scm_delete_file, "delete-file", 1, 0, 0,
  688. (SCM str),
  689. "Deletes (or \"unlinks\") the file specified by @var{path}.")
  690. #define FUNC_NAME s_scm_delete_file
  691. {
  692. int ans;
  693. STRING_SYSCALL (str, c_str, ans = unlink (c_str));
  694. if (ans != 0)
  695. SCM_SYSERROR;
  696. return SCM_UNSPECIFIED;
  697. }
  698. #undef FUNC_NAME
  699. #ifdef HAVE_MKDIR
  700. SCM_DEFINE (scm_mkdir, "mkdir", 1, 1, 0,
  701. (SCM path, SCM mode),
  702. "Create a new directory named by @var{path}. If @var{mode} is omitted\n"
  703. "then the permissions of the directory file are set using the current\n"
  704. "umask. Otherwise they are set to the decimal value specified with\n"
  705. "@var{mode}. The return value is unspecified.")
  706. #define FUNC_NAME s_scm_mkdir
  707. {
  708. int rv;
  709. mode_t mask;
  710. if (SCM_UNBNDP (mode))
  711. {
  712. mask = umask (0);
  713. umask (mask);
  714. STRING_SYSCALL (path, c_path, rv = mkdir (c_path, 0777 ^ mask));
  715. }
  716. else
  717. {
  718. STRING_SYSCALL (path, c_path, rv = mkdir (c_path, scm_to_uint (mode)));
  719. }
  720. if (rv != 0)
  721. SCM_SYSERROR;
  722. return SCM_UNSPECIFIED;
  723. }
  724. #undef FUNC_NAME
  725. #endif /* HAVE_MKDIR */
  726. #ifdef HAVE_RMDIR
  727. SCM_DEFINE (scm_rmdir, "rmdir", 1, 0, 0,
  728. (SCM path),
  729. "Remove the existing directory named by @var{path}. The directory must\n"
  730. "be empty for this to succeed. The return value is unspecified.")
  731. #define FUNC_NAME s_scm_rmdir
  732. {
  733. int val;
  734. STRING_SYSCALL (path, c_path, val = rmdir (c_path));
  735. if (val != 0)
  736. SCM_SYSERROR;
  737. return SCM_UNSPECIFIED;
  738. }
  739. #undef FUNC_NAME
  740. #endif
  741. /* {Examining Directories}
  742. */
  743. scm_t_bits scm_tc16_dir;
  744. SCM_DEFINE (scm_directory_stream_p, "directory-stream?", 1, 0, 0,
  745. (SCM obj),
  746. "Return a boolean indicating whether @var{object} is a directory\n"
  747. "stream as returned by @code{opendir}.")
  748. #define FUNC_NAME s_scm_directory_stream_p
  749. {
  750. return scm_from_bool (SCM_DIRP (obj));
  751. }
  752. #undef FUNC_NAME
  753. SCM_DEFINE (scm_opendir, "opendir", 1, 0, 0,
  754. (SCM dirname),
  755. "Open the directory specified by @var{path} and return a directory\n"
  756. "stream.")
  757. #define FUNC_NAME s_scm_opendir
  758. {
  759. DIR *ds;
  760. STRING_SYSCALL (dirname, c_dirname, ds = opendir (c_dirname));
  761. if (ds == NULL)
  762. SCM_SYSERROR;
  763. SCM_RETURN_NEWSMOB (scm_tc16_dir | SCM_DIR_FLAG_OPEN, ds);
  764. }
  765. #undef FUNC_NAME
  766. /* FIXME: The glibc manual has a portability note that readdir_r may not
  767. null-terminate its return string. The circumstances outlined for this
  768. are not clear, nor is it clear what should be done about it. Lets use
  769. NAMLEN and worry about what else should be done if/when someone can
  770. figure it out. */
  771. SCM_DEFINE (scm_readdir, "readdir", 1, 0, 0,
  772. (SCM port),
  773. "Return (as a string) the next directory entry from the directory stream\n"
  774. "@var{stream}. If there is no remaining entry to be read then the\n"
  775. "end of file object is returned.")
  776. #define FUNC_NAME s_scm_readdir
  777. {
  778. struct dirent_or_dirent64 *rdent;
  779. SCM_VALIDATE_DIR (1, port);
  780. if (!SCM_DIR_OPEN_P (port))
  781. SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port));
  782. #if HAVE_READDIR_R
  783. /* As noted in the glibc manual, on various systems (such as Solaris) the
  784. d_name[] field is only 1 char and you're expected to size the dirent
  785. buffer for readdir_r based on NAME_MAX. The SCM_MAX expressions below
  786. effectively give either sizeof(d_name) or NAME_MAX+1, whichever is
  787. bigger.
  788. On solaris 10 there's no NAME_MAX constant, it's necessary to use
  789. pathconf(). We prefer NAME_MAX though, since it should be a constant
  790. and will therefore save a system call. We also prefer it since dirfd()
  791. is not available everywhere.
  792. An alternative to dirfd() would be to open() the directory and then use
  793. fdopendir(), if the latter is available. That'd let us hold the fd
  794. somewhere in the smob, or just the dirent size calculated once. */
  795. {
  796. struct dirent_or_dirent64 de; /* just for sizeof */
  797. DIR *ds = (DIR *) SCM_CELL_WORD_1 (port);
  798. size_t namlen;
  799. #ifdef NAME_MAX
  800. char buf [SCM_MAX (sizeof (de),
  801. sizeof (de) - sizeof (de.d_name) + NAME_MAX + 1)];
  802. #else
  803. char *buf;
  804. long name_max = fpathconf (dirfd (ds), _PC_NAME_MAX);
  805. if (name_max == -1)
  806. SCM_SYSERROR;
  807. buf = alloca (SCM_MAX (sizeof (de),
  808. sizeof (de) - sizeof (de.d_name) + name_max + 1));
  809. #endif
  810. errno = 0;
  811. SCM_SYSCALL (readdir_r_or_readdir64_r (ds, (struct dirent_or_dirent64 *) buf, &rdent));
  812. if (errno != 0)
  813. SCM_SYSERROR;
  814. if (! rdent)
  815. return SCM_EOF_VAL;
  816. namlen = NAMLEN (rdent);
  817. return (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent))
  818. : SCM_EOF_VAL);
  819. }
  820. #else
  821. {
  822. SCM ret;
  823. scm_dynwind_begin (0);
  824. scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
  825. errno = 0;
  826. SCM_SYSCALL (rdent = readdir_or_readdir64 ((DIR *) SCM_CELL_WORD_1 (port)));
  827. if (errno != 0)
  828. SCM_SYSERROR;
  829. ret = (rdent ? scm_from_locale_stringn (rdent->d_name, NAMLEN (rdent))
  830. : SCM_EOF_VAL);
  831. scm_dynwind_end ();
  832. return ret;
  833. }
  834. #endif
  835. }
  836. #undef FUNC_NAME
  837. SCM_DEFINE (scm_rewinddir, "rewinddir", 1, 0, 0,
  838. (SCM port),
  839. "Reset the directory port @var{stream} so that the next call to\n"
  840. "@code{readdir} will return the first directory entry.")
  841. #define FUNC_NAME s_scm_rewinddir
  842. {
  843. SCM_VALIDATE_DIR (1, port);
  844. if (!SCM_DIR_OPEN_P (port))
  845. SCM_MISC_ERROR ("Directory ~S is not open.", scm_list_1 (port));
  846. rewinddir ((DIR *) SCM_CELL_WORD_1 (port));
  847. return SCM_UNSPECIFIED;
  848. }
  849. #undef FUNC_NAME
  850. SCM_DEFINE (scm_closedir, "closedir", 1, 0, 0,
  851. (SCM port),
  852. "Close the directory stream @var{stream}.\n"
  853. "The return value is unspecified.")
  854. #define FUNC_NAME s_scm_closedir
  855. {
  856. SCM_VALIDATE_DIR (1, port);
  857. if (SCM_DIR_OPEN_P (port))
  858. {
  859. int sts;
  860. SCM_SYSCALL (sts = closedir ((DIR *) SCM_CELL_WORD_1 (port)));
  861. if (sts != 0)
  862. SCM_SYSERROR;
  863. SCM_SET_CELL_WORD_0 (port, scm_tc16_dir);
  864. }
  865. return SCM_UNSPECIFIED;
  866. }
  867. #undef FUNC_NAME
  868. static int
  869. scm_dir_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  870. {
  871. scm_puts ("#<", port);
  872. if (!SCM_DIR_OPEN_P (exp))
  873. scm_puts ("closed: ", port);
  874. scm_puts ("directory stream ", port);
  875. scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
  876. scm_putc ('>', port);
  877. return 1;
  878. }
  879. static size_t
  880. scm_dir_free (SCM p)
  881. {
  882. if (SCM_DIR_OPEN_P (p))
  883. closedir ((DIR *) SCM_CELL_WORD_1 (p));
  884. return 0;
  885. }
  886. /* {Navigating Directories}
  887. */
  888. SCM_DEFINE (scm_chdir, "chdir", 1, 0, 0,
  889. (SCM str),
  890. "Change the current working directory to @var{path}.\n"
  891. "The return value is unspecified.")
  892. #define FUNC_NAME s_scm_chdir
  893. {
  894. int ans;
  895. STRING_SYSCALL (str, c_str, ans = chdir (c_str));
  896. if (ans != 0)
  897. SCM_SYSERROR;
  898. return SCM_UNSPECIFIED;
  899. }
  900. #undef FUNC_NAME
  901. #ifdef HAVE_GETCWD
  902. SCM_DEFINE (scm_getcwd, "getcwd", 0, 0, 0,
  903. (),
  904. "Return the name of the current working directory.")
  905. #define FUNC_NAME s_scm_getcwd
  906. {
  907. char *rv;
  908. size_t size = 100;
  909. char *wd;
  910. SCM result;
  911. wd = scm_malloc (size);
  912. while ((rv = getcwd (wd, size)) == 0 && errno == ERANGE)
  913. {
  914. free (wd);
  915. size *= 2;
  916. wd = scm_malloc (size);
  917. }
  918. if (rv == 0)
  919. {
  920. int save_errno = errno;
  921. free (wd);
  922. errno = save_errno;
  923. SCM_SYSERROR;
  924. }
  925. result = scm_from_locale_stringn (wd, strlen (wd));
  926. free (wd);
  927. return result;
  928. }
  929. #undef FUNC_NAME
  930. #endif /* HAVE_GETCWD */
  931. #ifdef HAVE_SELECT
  932. /* check that element is a port or file descriptor. if it's a port
  933. and its buffer is ready for use, add it to the ports_ready list.
  934. otherwise add its file descriptor to *set. the type of list can be
  935. determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
  936. SCM_ARG3 for excepts. */
  937. static int
  938. set_element (SELECT_TYPE *set, SCM *ports_ready, SCM element, int pos)
  939. {
  940. int fd;
  941. if (scm_is_integer (element))
  942. {
  943. fd = scm_to_int (element);
  944. }
  945. else
  946. {
  947. int use_buf = 0;
  948. element = SCM_COERCE_OUTPORT (element);
  949. SCM_ASSERT (SCM_OPFPORTP (element), element, pos, "select");
  950. if (pos == SCM_ARG1)
  951. {
  952. /* check whether port has buffered input. */
  953. scm_t_port *pt = SCM_PTAB_ENTRY (element);
  954. if (pt->read_pos < pt->read_end)
  955. use_buf = 1;
  956. }
  957. else if (pos == SCM_ARG2)
  958. {
  959. /* check whether port's output buffer has room. */
  960. scm_t_port *pt = SCM_PTAB_ENTRY (element);
  961. /* > 1 since writing the last byte in the buffer causes flush. */
  962. if (pt->write_end - pt->write_pos > 1)
  963. use_buf = 1;
  964. }
  965. fd = use_buf ? -1 : SCM_FPORT_FDES (element);
  966. }
  967. if (fd == -1)
  968. *ports_ready = scm_cons (element, *ports_ready);
  969. else
  970. FD_SET (fd, set);
  971. return fd;
  972. }
  973. /* check list_or_vec, a list or vector of ports or file descriptors,
  974. adding each member to either the ports_ready list (if it's a port
  975. with a usable buffer) or to *set. the kind of list_or_vec can be
  976. determined from pos: SCM_ARG1 for reads, SCM_ARG2 for writes,
  977. SCM_ARG3 for excepts. */
  978. static int
  979. fill_select_type (SELECT_TYPE *set, SCM *ports_ready, SCM list_or_vec, int pos)
  980. {
  981. int max_fd = 0;
  982. if (scm_is_simple_vector (list_or_vec))
  983. {
  984. int i = SCM_SIMPLE_VECTOR_LENGTH (list_or_vec);
  985. while (--i >= 0)
  986. {
  987. int fd = set_element (set, ports_ready,
  988. SCM_SIMPLE_VECTOR_REF (list_or_vec, i), pos);
  989. if (fd > max_fd)
  990. max_fd = fd;
  991. }
  992. }
  993. else
  994. {
  995. while (!SCM_NULL_OR_NIL_P (list_or_vec))
  996. {
  997. int fd = set_element (set, ports_ready, SCM_CAR (list_or_vec), pos);
  998. if (fd > max_fd)
  999. max_fd = fd;
  1000. list_or_vec = SCM_CDR (list_or_vec);
  1001. }
  1002. }
  1003. return max_fd;
  1004. }
  1005. /* if element (a file descriptor or port) appears in *set, cons it to
  1006. list. return list. */
  1007. static SCM
  1008. get_element (SELECT_TYPE *set, SCM element, SCM list)
  1009. {
  1010. int fd;
  1011. if (scm_is_integer (element))
  1012. {
  1013. fd = scm_to_int (element);
  1014. }
  1015. else
  1016. {
  1017. fd = SCM_FPORT_FDES (SCM_COERCE_OUTPORT (element));
  1018. }
  1019. if (FD_ISSET (fd, set))
  1020. list = scm_cons (element, list);
  1021. return list;
  1022. }
  1023. /* construct component of scm_select return value.
  1024. set: pointer to set of file descriptors found by select to be ready
  1025. ports_ready: ports ready due to buffering
  1026. list_or_vec: original list/vector handed to scm_select.
  1027. the return value is a list/vector of ready ports/file descriptors.
  1028. works by finding the objects in list which correspond to members of
  1029. *set and appending them to ports_ready. result is converted to a
  1030. vector if list_or_vec is a vector. */
  1031. static SCM
  1032. retrieve_select_type (SELECT_TYPE *set, SCM ports_ready, SCM list_or_vec)
  1033. {
  1034. SCM answer_list = ports_ready;
  1035. if (scm_is_simple_vector (list_or_vec))
  1036. {
  1037. int i = SCM_SIMPLE_VECTOR_LENGTH (list_or_vec);
  1038. while (--i >= 0)
  1039. {
  1040. answer_list = get_element (set,
  1041. SCM_SIMPLE_VECTOR_REF (list_or_vec, i),
  1042. answer_list);
  1043. }
  1044. return scm_vector (answer_list);
  1045. }
  1046. else
  1047. {
  1048. /* list_or_vec must be a list. */
  1049. while (!SCM_NULL_OR_NIL_P (list_or_vec))
  1050. {
  1051. answer_list = get_element (set, SCM_CAR (list_or_vec), answer_list);
  1052. list_or_vec = SCM_CDR (list_or_vec);
  1053. }
  1054. return answer_list;
  1055. }
  1056. }
  1057. /* Static helper functions above refer to s_scm_select directly as s_select */
  1058. SCM_DEFINE (scm_select, "select", 3, 2, 0,
  1059. (SCM reads, SCM writes, SCM excepts, SCM secs, SCM usecs),
  1060. "This procedure has a variety of uses: waiting for the ability\n"
  1061. "to provide input, accept output, or the existence of\n"
  1062. "exceptional conditions on a collection of ports or file\n"
  1063. "descriptors, or waiting for a timeout to occur.\n"
  1064. "It also returns if interrupted by a signal.\n\n"
  1065. "@var{reads}, @var{writes} and @var{excepts} can be lists or\n"
  1066. "vectors, with each member a port or a file descriptor.\n"
  1067. "The value returned is a list of three corresponding\n"
  1068. "lists or vectors containing only the members which meet the\n"
  1069. "specified requirement. The ability of port buffers to\n"
  1070. "provide input or accept output is taken into account.\n"
  1071. "Ordering of the input lists or vectors is not preserved.\n\n"
  1072. "The optional arguments @var{secs} and @var{usecs} specify the\n"
  1073. "timeout. Either @var{secs} can be specified alone, as\n"
  1074. "either an integer or a real number, or both @var{secs} and\n"
  1075. "@var{usecs} can be specified as integers, in which case\n"
  1076. "@var{usecs} is an additional timeout expressed in\n"
  1077. "microseconds. If @var{secs} is omitted or is @code{#f} then\n"
  1078. "select will wait for as long as it takes for one of the other\n"
  1079. "conditions to be satisfied.\n\n"
  1080. "The scsh version of @code{select} differs as follows:\n"
  1081. "Only vectors are accepted for the first three arguments.\n"
  1082. "The @var{usecs} argument is not supported.\n"
  1083. "Multiple values are returned instead of a list.\n"
  1084. "Duplicates in the input vectors appear only once in output.\n"
  1085. "An additional @code{select!} interface is provided.")
  1086. #define FUNC_NAME s_scm_select
  1087. {
  1088. struct timeval timeout;
  1089. struct timeval * time_ptr;
  1090. SELECT_TYPE read_set;
  1091. SELECT_TYPE write_set;
  1092. SELECT_TYPE except_set;
  1093. int read_count;
  1094. int write_count;
  1095. int except_count;
  1096. /* these lists accumulate ports which are ready due to buffering.
  1097. their file descriptors don't need to be added to the select sets. */
  1098. SCM read_ports_ready = SCM_EOL;
  1099. SCM write_ports_ready = SCM_EOL;
  1100. int max_fd;
  1101. if (scm_is_simple_vector (reads))
  1102. {
  1103. read_count = SCM_SIMPLE_VECTOR_LENGTH (reads);
  1104. }
  1105. else
  1106. {
  1107. read_count = scm_ilength (reads);
  1108. SCM_ASSERT (read_count >= 0, reads, SCM_ARG1, FUNC_NAME);
  1109. }
  1110. if (scm_is_simple_vector (writes))
  1111. {
  1112. write_count = SCM_SIMPLE_VECTOR_LENGTH (writes);
  1113. }
  1114. else
  1115. {
  1116. write_count = scm_ilength (writes);
  1117. SCM_ASSERT (write_count >= 0, writes, SCM_ARG2, FUNC_NAME);
  1118. }
  1119. if (scm_is_simple_vector (excepts))
  1120. {
  1121. except_count = SCM_SIMPLE_VECTOR_LENGTH (excepts);
  1122. }
  1123. else
  1124. {
  1125. except_count = scm_ilength (excepts);
  1126. SCM_ASSERT (except_count >= 0, excepts, SCM_ARG3, FUNC_NAME);
  1127. }
  1128. FD_ZERO (&read_set);
  1129. FD_ZERO (&write_set);
  1130. FD_ZERO (&except_set);
  1131. max_fd = fill_select_type (&read_set, &read_ports_ready, reads, SCM_ARG1);
  1132. {
  1133. int write_max = fill_select_type (&write_set, &write_ports_ready,
  1134. writes, SCM_ARG2);
  1135. int except_max = fill_select_type (&except_set, NULL,
  1136. excepts, SCM_ARG3);
  1137. if (write_max > max_fd)
  1138. max_fd = write_max;
  1139. if (except_max > max_fd)
  1140. max_fd = except_max;
  1141. }
  1142. /* if there's a port with a ready buffer, don't block, just
  1143. check for ready file descriptors. */
  1144. if (!scm_is_null (read_ports_ready) || !scm_is_null (write_ports_ready))
  1145. {
  1146. timeout.tv_sec = 0;
  1147. timeout.tv_usec = 0;
  1148. time_ptr = &timeout;
  1149. }
  1150. else if (SCM_UNBNDP (secs) || scm_is_false (secs))
  1151. time_ptr = 0;
  1152. else
  1153. {
  1154. if (scm_is_unsigned_integer (secs, 0, ULONG_MAX))
  1155. {
  1156. timeout.tv_sec = scm_to_ulong (secs);
  1157. if (SCM_UNBNDP (usecs))
  1158. timeout.tv_usec = 0;
  1159. else
  1160. timeout.tv_usec = scm_to_long (usecs);
  1161. }
  1162. else
  1163. {
  1164. double fl = scm_to_double (secs);
  1165. if (!SCM_UNBNDP (usecs))
  1166. SCM_WRONG_TYPE_ARG (4, secs);
  1167. if (fl > LONG_MAX)
  1168. SCM_OUT_OF_RANGE (4, secs);
  1169. timeout.tv_sec = (long) fl;
  1170. timeout.tv_usec = (long) ((fl - timeout.tv_sec) * 1000000);
  1171. }
  1172. time_ptr = &timeout;
  1173. }
  1174. {
  1175. int rv = scm_std_select (max_fd + 1,
  1176. &read_set, &write_set, &except_set,
  1177. time_ptr);
  1178. if (rv < 0)
  1179. SCM_SYSERROR;
  1180. }
  1181. return scm_list_3 (retrieve_select_type (&read_set, read_ports_ready, reads),
  1182. retrieve_select_type (&write_set, write_ports_ready, writes),
  1183. retrieve_select_type (&except_set, SCM_EOL, excepts));
  1184. }
  1185. #undef FUNC_NAME
  1186. #endif /* HAVE_SELECT */
  1187. #ifdef HAVE_FCNTL
  1188. SCM_DEFINE (scm_fcntl, "fcntl", 2, 1, 0,
  1189. (SCM object, SCM cmd, SCM value),
  1190. "Apply @var{command} to the specified file descriptor or the underlying\n"
  1191. "file descriptor of the specified port. @var{value} is an optional\n"
  1192. "integer argument.\n\n"
  1193. "Values for @var{command} are:\n\n"
  1194. "@table @code\n"
  1195. "@item F_DUPFD\n"
  1196. "Duplicate a file descriptor\n"
  1197. "@item F_GETFD\n"
  1198. "Get flags associated with the file descriptor.\n"
  1199. "@item F_SETFD\n"
  1200. "Set flags associated with the file descriptor to @var{value}.\n"
  1201. "@item F_GETFL\n"
  1202. "Get flags associated with the open file.\n"
  1203. "@item F_SETFL\n"
  1204. "Set flags associated with the open file to @var{value}\n"
  1205. "@item F_GETOWN\n"
  1206. "Get the process ID of a socket's owner, for @code{SIGIO} signals.\n"
  1207. "@item F_SETOWN\n"
  1208. "Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.\n"
  1209. "@item FD_CLOEXEC\n"
  1210. "The value used to indicate the \"close on exec\" flag with @code{F_GETFL} or\n"
  1211. "@code{F_SETFL}.\n"
  1212. "@end table")
  1213. #define FUNC_NAME s_scm_fcntl
  1214. {
  1215. int rv;
  1216. int fdes;
  1217. int ivalue;
  1218. object = SCM_COERCE_OUTPORT (object);
  1219. if (SCM_OPFPORTP (object))
  1220. fdes = SCM_FPORT_FDES (object);
  1221. else
  1222. fdes = scm_to_int (object);
  1223. if (SCM_UNBNDP (value))
  1224. ivalue = 0;
  1225. else
  1226. ivalue = scm_to_int (value);
  1227. SCM_SYSCALL (rv = fcntl (fdes, scm_to_int (cmd), ivalue));
  1228. if (rv == -1)
  1229. SCM_SYSERROR;
  1230. return scm_from_int (rv);
  1231. }
  1232. #undef FUNC_NAME
  1233. #endif /* HAVE_FCNTL */
  1234. SCM_DEFINE (scm_fsync, "fsync", 1, 0, 0,
  1235. (SCM object),
  1236. "Copies any unwritten data for the specified output file descriptor to disk.\n"
  1237. "If @var{port/fd} is a port, its buffer is flushed before the underlying\n"
  1238. "file descriptor is fsync'd.\n"
  1239. "The return value is unspecified.")
  1240. #define FUNC_NAME s_scm_fsync
  1241. {
  1242. int fdes;
  1243. object = SCM_COERCE_OUTPORT (object);
  1244. if (SCM_OPFPORTP (object))
  1245. {
  1246. scm_flush (object);
  1247. fdes = SCM_FPORT_FDES (object);
  1248. }
  1249. else
  1250. fdes = scm_to_int (object);
  1251. if (fsync (fdes) == -1)
  1252. SCM_SYSERROR;
  1253. return SCM_UNSPECIFIED;
  1254. }
  1255. #undef FUNC_NAME
  1256. #ifdef HAVE_SYMLINK
  1257. SCM_DEFINE (scm_symlink, "symlink", 2, 0, 0,
  1258. (SCM oldpath, SCM newpath),
  1259. "Create a symbolic link named @var{path-to} with the value (i.e., pointing to)\n"
  1260. "@var{path-from}. The return value is unspecified.")
  1261. #define FUNC_NAME s_scm_symlink
  1262. {
  1263. int val;
  1264. STRING2_SYSCALL (oldpath, c_oldpath,
  1265. newpath, c_newpath,
  1266. val = symlink (c_oldpath, c_newpath));
  1267. if (val != 0)
  1268. SCM_SYSERROR;
  1269. return SCM_UNSPECIFIED;
  1270. }
  1271. #undef FUNC_NAME
  1272. #endif /* HAVE_SYMLINK */
  1273. #ifdef HAVE_READLINK
  1274. SCM_DEFINE (scm_readlink, "readlink", 1, 0, 0,
  1275. (SCM path),
  1276. "Return the value of the symbolic link named by @var{path} (a\n"
  1277. "string), i.e., the file that the link points to.")
  1278. #define FUNC_NAME s_scm_readlink
  1279. {
  1280. int rv;
  1281. int size = 100;
  1282. char *buf;
  1283. SCM result;
  1284. char *c_path;
  1285. scm_dynwind_begin (0);
  1286. c_path = scm_to_locale_string (path);
  1287. scm_dynwind_free (c_path);
  1288. buf = scm_malloc (size);
  1289. while ((rv = readlink (c_path, buf, size)) == size)
  1290. {
  1291. free (buf);
  1292. size *= 2;
  1293. buf = scm_malloc (size);
  1294. }
  1295. if (rv == -1)
  1296. {
  1297. int save_errno = errno;
  1298. free (buf);
  1299. errno = save_errno;
  1300. SCM_SYSERROR;
  1301. }
  1302. result = scm_take_locale_stringn (buf, rv);
  1303. scm_dynwind_end ();
  1304. return result;
  1305. }
  1306. #undef FUNC_NAME
  1307. #endif /* HAVE_READLINK */
  1308. #ifdef HAVE_LSTAT
  1309. SCM_DEFINE (scm_lstat, "lstat", 1, 0, 0,
  1310. (SCM str),
  1311. "Similar to @code{stat}, but does not follow symbolic links, i.e.,\n"
  1312. "it will return information about a symbolic link itself, not the\n"
  1313. "file it points to. @var{path} must be a string.")
  1314. #define FUNC_NAME s_scm_lstat
  1315. {
  1316. int rv;
  1317. struct stat_or_stat64 stat_temp;
  1318. STRING_SYSCALL (str, c_str, rv = lstat_or_lstat64 (c_str, &stat_temp));
  1319. if (rv != 0)
  1320. {
  1321. int en = errno;
  1322. SCM_SYSERROR_MSG ("~A: ~S",
  1323. scm_list_2 (scm_strerror (scm_from_int (en)), str),
  1324. en);
  1325. }
  1326. return scm_stat2scm (&stat_temp);
  1327. }
  1328. #undef FUNC_NAME
  1329. #endif /* HAVE_LSTAT */
  1330. SCM_DEFINE (scm_copy_file, "copy-file", 2, 0, 0,
  1331. (SCM oldfile, SCM newfile),
  1332. "Copy the file specified by @var{path-from} to @var{path-to}.\n"
  1333. "The return value is unspecified.")
  1334. #define FUNC_NAME s_scm_copy_file
  1335. {
  1336. char *c_oldfile, *c_newfile;
  1337. int oldfd, newfd;
  1338. int n, rv;
  1339. char buf[BUFSIZ];
  1340. struct stat_or_stat64 oldstat;
  1341. scm_dynwind_begin (0);
  1342. c_oldfile = scm_to_locale_string (oldfile);
  1343. scm_dynwind_free (c_oldfile);
  1344. c_newfile = scm_to_locale_string (newfile);
  1345. scm_dynwind_free (c_newfile);
  1346. oldfd = open_or_open64 (c_oldfile, O_RDONLY);
  1347. if (oldfd == -1)
  1348. SCM_SYSERROR;
  1349. #ifdef __MINGW32__
  1350. SCM_SYSCALL (rv = fstat_Win32 (oldfd, &oldstat));
  1351. #else
  1352. SCM_SYSCALL (rv = fstat_or_fstat64 (oldfd, &oldstat));
  1353. #endif
  1354. if (rv == -1)
  1355. goto err_close_oldfd;
  1356. /* use POSIX flags instead of 07777?. */
  1357. newfd = open_or_open64 (c_newfile, O_WRONLY | O_CREAT | O_TRUNC,
  1358. oldstat.st_mode & 07777);
  1359. if (newfd == -1)
  1360. {
  1361. err_close_oldfd:
  1362. close (oldfd);
  1363. SCM_SYSERROR;
  1364. }
  1365. while ((n = read (oldfd, buf, sizeof buf)) > 0)
  1366. if (write (newfd, buf, n) != n)
  1367. {
  1368. close (oldfd);
  1369. close (newfd);
  1370. SCM_SYSERROR;
  1371. }
  1372. close (oldfd);
  1373. if (close (newfd) == -1)
  1374. SCM_SYSERROR;
  1375. scm_dynwind_end ();
  1376. return SCM_UNSPECIFIED;
  1377. }
  1378. #undef FUNC_NAME
  1379. /* Filename manipulation */
  1380. SCM scm_dot_string;
  1381. SCM_DEFINE (scm_dirname, "dirname", 1, 0, 0,
  1382. (SCM filename),
  1383. "Return the directory name component of the file name\n"
  1384. "@var{filename}. If @var{filename} does not contain a directory\n"
  1385. "component, @code{.} is returned.")
  1386. #define FUNC_NAME s_scm_dirname
  1387. {
  1388. const char *s;
  1389. long int i;
  1390. unsigned long int len;
  1391. SCM_VALIDATE_STRING (1, filename);
  1392. s = scm_i_string_chars (filename);
  1393. len = scm_i_string_length (filename);
  1394. i = len - 1;
  1395. #ifdef __MINGW32__
  1396. while (i >= 0 && (s[i] == '/' || s[i] == '\\')) --i;
  1397. while (i >= 0 && (s[i] != '/' && s[i] != '\\')) --i;
  1398. while (i >= 0 && (s[i] == '/' || s[i] == '\\')) --i;
  1399. #else
  1400. while (i >= 0 && s[i] == '/') --i;
  1401. while (i >= 0 && s[i] != '/') --i;
  1402. while (i >= 0 && s[i] == '/') --i;
  1403. #endif /* ndef __MINGW32__ */
  1404. if (i < 0)
  1405. {
  1406. #ifdef __MINGW32__
  1407. if (len > 0 && (s[0] == '/' || s[0] == '\\'))
  1408. #else
  1409. if (len > 0 && s[0] == '/')
  1410. #endif /* ndef __MINGW32__ */
  1411. return scm_c_substring (filename, 0, 1);
  1412. else
  1413. return scm_dot_string;
  1414. }
  1415. else
  1416. return scm_c_substring (filename, 0, i + 1);
  1417. }
  1418. #undef FUNC_NAME
  1419. SCM_DEFINE (scm_basename, "basename", 1, 1, 0,
  1420. (SCM filename, SCM suffix),
  1421. "Return the base name of the file name @var{filename}. The\n"
  1422. "base name is the file name without any directory components.\n"
  1423. "If @var{suffix} is provided, and is equal to the end of\n"
  1424. "@var{basename}, it is removed also.")
  1425. #define FUNC_NAME s_scm_basename
  1426. {
  1427. const char *f, *s = 0;
  1428. int i, j, len, end;
  1429. SCM_VALIDATE_STRING (1, filename);
  1430. f = scm_i_string_chars (filename);
  1431. len = scm_i_string_length (filename);
  1432. if (SCM_UNBNDP (suffix))
  1433. j = -1;
  1434. else
  1435. {
  1436. SCM_VALIDATE_STRING (2, suffix);
  1437. s = scm_i_string_chars (suffix);
  1438. j = scm_i_string_length (suffix) - 1;
  1439. }
  1440. i = len - 1;
  1441. #ifdef __MINGW32__
  1442. while (i >= 0 && (f[i] == '/' || f[i] == '\\')) --i;
  1443. #else
  1444. while (i >= 0 && f[i] == '/') --i;
  1445. #endif /* ndef __MINGW32__ */
  1446. end = i;
  1447. while (i >= 0 && j >= 0 && f[i] == s[j]) --i, --j;
  1448. if (j == -1)
  1449. end = i;
  1450. #ifdef __MINGW32__
  1451. while (i >= 0 && f[i] != '/' && f[i] != '\\') --i;
  1452. #else
  1453. while (i >= 0 && f[i] != '/') --i;
  1454. #endif /* ndef __MINGW32__ */
  1455. if (i == end)
  1456. {
  1457. #ifdef __MINGW32__
  1458. if (len > 0 && (f[0] == '/' || f[0] == '\\'))
  1459. #else
  1460. if (len > 0 && f[0] == '/')
  1461. #endif /* ndef __MINGW32__ */
  1462. return scm_c_substring (filename, 0, 1);
  1463. else
  1464. return scm_dot_string;
  1465. }
  1466. else
  1467. return scm_c_substring (filename, i+1, end+1);
  1468. }
  1469. #undef FUNC_NAME
  1470. void
  1471. scm_init_filesys ()
  1472. {
  1473. scm_tc16_dir = scm_make_smob_type ("directory", 0);
  1474. scm_set_smob_free (scm_tc16_dir, scm_dir_free);
  1475. scm_set_smob_print (scm_tc16_dir, scm_dir_print);
  1476. scm_dot_string = scm_permanent_object (scm_from_locale_string ("."));
  1477. #ifdef O_RDONLY
  1478. scm_c_define ("O_RDONLY", scm_from_int (O_RDONLY));
  1479. #endif
  1480. #ifdef O_WRONLY
  1481. scm_c_define ("O_WRONLY", scm_from_int (O_WRONLY));
  1482. #endif
  1483. #ifdef O_RDWR
  1484. scm_c_define ("O_RDWR", scm_from_int (O_RDWR));
  1485. #endif
  1486. #ifdef O_CREAT
  1487. scm_c_define ("O_CREAT", scm_from_int (O_CREAT));
  1488. #endif
  1489. #ifdef O_EXCL
  1490. scm_c_define ("O_EXCL", scm_from_int (O_EXCL));
  1491. #endif
  1492. #ifdef O_NOCTTY
  1493. scm_c_define ("O_NOCTTY", scm_from_int (O_NOCTTY));
  1494. #endif
  1495. #ifdef O_TRUNC
  1496. scm_c_define ("O_TRUNC", scm_from_int (O_TRUNC));
  1497. #endif
  1498. #ifdef O_APPEND
  1499. scm_c_define ("O_APPEND", scm_from_int (O_APPEND));
  1500. #endif
  1501. #ifdef O_NONBLOCK
  1502. scm_c_define ("O_NONBLOCK", scm_from_int (O_NONBLOCK));
  1503. #endif
  1504. #ifdef O_NDELAY
  1505. scm_c_define ("O_NDELAY", scm_from_int (O_NDELAY));
  1506. #endif
  1507. #ifdef O_SYNC
  1508. scm_c_define ("O_SYNC", scm_from_int (O_SYNC));
  1509. #endif
  1510. #ifdef O_LARGEFILE
  1511. scm_c_define ("O_LARGEFILE", scm_from_int (O_LARGEFILE));
  1512. #endif
  1513. #ifdef F_DUPFD
  1514. scm_c_define ("F_DUPFD", scm_from_int (F_DUPFD));
  1515. #endif
  1516. #ifdef F_GETFD
  1517. scm_c_define ("F_GETFD", scm_from_int (F_GETFD));
  1518. #endif
  1519. #ifdef F_SETFD
  1520. scm_c_define ("F_SETFD", scm_from_int (F_SETFD));
  1521. #endif
  1522. #ifdef F_GETFL
  1523. scm_c_define ("F_GETFL", scm_from_int (F_GETFL));
  1524. #endif
  1525. #ifdef F_SETFL
  1526. scm_c_define ("F_SETFL", scm_from_int (F_SETFL));
  1527. #endif
  1528. #ifdef F_GETOWN
  1529. scm_c_define ("F_GETOWN", scm_from_int (F_GETOWN));
  1530. #endif
  1531. #ifdef F_SETOWN
  1532. scm_c_define ("F_SETOWN", scm_from_int (F_SETOWN));
  1533. #endif
  1534. #ifdef FD_CLOEXEC
  1535. scm_c_define ("FD_CLOEXEC", scm_from_int (FD_CLOEXEC));
  1536. #endif
  1537. #include "libguile/filesys.x"
  1538. }
  1539. /*
  1540. Local Variables:
  1541. c-file-style: "gnu"
  1542. End:
  1543. */