filesys.c 67 KB

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