filesys.c 53 KB

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