filesys.c 54 KB

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