filesys.c 69 KB

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