filesys.c 54 KB

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