filesys.c 54 KB

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