filesys.c 53 KB

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