filesys.c 54 KB

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