filesys.c 70 KB

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