posix.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/dynwind.h"
  24. #include "libguile/fports.h"
  25. #include "libguile/scmsigs.h"
  26. #include "libguile/feature.h"
  27. #include "libguile/strings.h"
  28. #include "libguile/srfi-13.h"
  29. #include "libguile/srfi-14.h"
  30. #include "libguile/vectors.h"
  31. #include "libguile/lang.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/posix.h"
  34. #include "libguile/i18n.h"
  35. #include "libguile/threads.h"
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #ifdef TIME_WITH_SYS_TIME
  40. # include <sys/time.h>
  41. # include <time.h>
  42. #else
  43. # if HAVE_SYS_TIME_H
  44. # include <sys/time.h>
  45. # else
  46. # include <time.h>
  47. # endif
  48. #endif
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h>
  51. #else
  52. #ifndef ttyname
  53. extern char *ttyname();
  54. #endif
  55. #endif
  56. #ifdef LIBC_H_WITH_UNISTD_H
  57. #include <libc.h>
  58. #endif
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61. #include <fcntl.h>
  62. #ifdef HAVE_PWD_H
  63. #include <pwd.h>
  64. #endif
  65. #ifdef HAVE_IO_H
  66. #include <io.h>
  67. #endif
  68. #ifdef HAVE_WINSOCK2_H
  69. #include <winsock2.h>
  70. #endif
  71. #ifdef __MINGW32__
  72. /* Some defines for Windows here. */
  73. # include <process.h>
  74. # define pipe(fd) _pipe (fd, 256, O_BINARY)
  75. #endif /* __MINGW32__ */
  76. #if HAVE_SYS_WAIT_H
  77. # include <sys/wait.h>
  78. #endif
  79. #ifndef WEXITSTATUS
  80. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  81. #endif
  82. #ifndef WIFEXITED
  83. # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  84. #endif
  85. #include <signal.h>
  86. extern char ** environ;
  87. #ifdef HAVE_GRP_H
  88. #include <grp.h>
  89. #endif
  90. #ifdef HAVE_SYS_UTSNAME_H
  91. #include <sys/utsname.h>
  92. #endif
  93. #ifdef HAVE_SETLOCALE
  94. #include <locale.h>
  95. #endif
  96. #if HAVE_CRYPT_H
  97. # include <crypt.h>
  98. #endif
  99. #ifdef HAVE_NETDB_H
  100. #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
  101. #endif
  102. #ifdef HAVE_SYS_PARAM_H
  103. #include <sys/param.h> /* for MAXHOSTNAMELEN */
  104. #endif
  105. #if HAVE_SYS_RESOURCE_H
  106. # include <sys/resource.h>
  107. #endif
  108. #if HAVE_SYS_FILE_H
  109. # include <sys/file.h>
  110. #endif
  111. #if HAVE_CRT_EXTERNS_H
  112. #include <crt_externs.h> /* for Darwin _NSGetEnviron */
  113. #endif
  114. /* Some Unix systems don't define these. CPP hair is dangerous, but
  115. this seems safe enough... */
  116. #ifndef R_OK
  117. #define R_OK 4
  118. #endif
  119. #ifndef W_OK
  120. #define W_OK 2
  121. #endif
  122. #ifndef X_OK
  123. #define X_OK 1
  124. #endif
  125. #ifndef F_OK
  126. #define F_OK 0
  127. #endif
  128. /* No prototype for this on Solaris 10. The man page says it's in
  129. <unistd.h> ... but it lies. */
  130. #if ! HAVE_DECL_SETHOSTNAME
  131. int sethostname (char *name, size_t namelen);
  132. #endif
  133. /* On NextStep, <utime.h> doesn't define struct utime, unless we
  134. #define _POSIX_SOURCE before #including it. I think this is less
  135. of a kludge than defining struct utimbuf ourselves. */
  136. #ifdef UTIMBUF_NEEDS_POSIX
  137. #define _POSIX_SOURCE
  138. #endif
  139. #ifdef HAVE_SYS_UTIME_H
  140. #include <sys/utime.h>
  141. #endif
  142. #ifdef HAVE_UTIME_H
  143. #include <utime.h>
  144. #endif
  145. /* Please don't add any more #includes or #defines here. The hack
  146. above means that _POSIX_SOURCE may be #defined, which will
  147. encourage header files to do strange things.
  148. FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
  149. FIXME: Probably should do all the includes first, then all the fallback
  150. declarations and defines, in case things are not in the header we
  151. imagine. */
  152. /* On Apple Darwin in a shared library there's no "environ" to access
  153. directly, instead the address of that variable must be obtained with
  154. _NSGetEnviron(). */
  155. #if HAVE__NSGETENVIRON && defined (PIC)
  156. #define environ (*_NSGetEnviron())
  157. #endif
  158. /* Two often used patterns
  159. */
  160. #define WITH_STRING(str,cstr,code) \
  161. do { \
  162. char *cstr = scm_to_locale_string (str); \
  163. code; \
  164. free (cstr); \
  165. } while (0)
  166. #define STRING_SYSCALL(str,cstr,code) \
  167. do { \
  168. int eno; \
  169. char *cstr = scm_to_locale_string (str); \
  170. SCM_SYSCALL (code); \
  171. eno = errno; free (cstr); errno = eno; \
  172. } while (0)
  173. SCM_SYMBOL (sym_read_pipe, "read pipe");
  174. SCM_SYMBOL (sym_write_pipe, "write pipe");
  175. SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
  176. (),
  177. "Return a newly created pipe: a pair of ports which are linked\n"
  178. "together on the local machine. The @emph{car} is the input\n"
  179. "port and the @emph{cdr} is the output port. Data written (and\n"
  180. "flushed) to the output port can be read from the input port.\n"
  181. "Pipes are commonly used for communication with a newly forked\n"
  182. "child process. The need to flush the output port can be\n"
  183. "avoided by making it unbuffered using @code{setvbuf}.\n"
  184. "\n"
  185. "Writes occur atomically provided the size of the data in bytes\n"
  186. "is not greater than the value of @code{PIPE_BUF}. Note that\n"
  187. "the output port is likely to block if too much data (typically\n"
  188. "equal to @code{PIPE_BUF}) has been written but not yet read\n"
  189. "from the input port.")
  190. #define FUNC_NAME s_scm_pipe
  191. {
  192. int fd[2], rv;
  193. SCM p_rd, p_wt;
  194. rv = pipe (fd);
  195. if (rv)
  196. SCM_SYSERROR;
  197. p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe);
  198. p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe);
  199. return scm_cons (p_rd, p_wt);
  200. }
  201. #undef FUNC_NAME
  202. #ifdef HAVE_GETGROUPS
  203. SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
  204. (),
  205. "Return a vector of integers representing the current\n"
  206. "supplementary group IDs.")
  207. #define FUNC_NAME s_scm_getgroups
  208. {
  209. SCM result;
  210. int ngroups;
  211. size_t size;
  212. GETGROUPS_T *groups;
  213. ngroups = getgroups (0, NULL);
  214. if (ngroups <= 0)
  215. SCM_SYSERROR;
  216. size = ngroups * sizeof (GETGROUPS_T);
  217. groups = scm_malloc (size);
  218. ngroups = getgroups (ngroups, groups);
  219. result = scm_c_make_vector (ngroups, SCM_BOOL_F);
  220. while (--ngroups >= 0)
  221. SCM_SIMPLE_VECTOR_SET (result, ngroups, scm_from_ulong (groups[ngroups]));
  222. free (groups);
  223. return result;
  224. }
  225. #undef FUNC_NAME
  226. #endif
  227. #ifdef HAVE_SETGROUPS
  228. SCM_DEFINE (scm_setgroups, "setgroups", 1, 0, 0,
  229. (SCM group_vec),
  230. "Set the current set of supplementary group IDs to the integers\n"
  231. "in the given vector @var{vec}. The return value is\n"
  232. "unspecified.\n"
  233. "\n"
  234. "Generally only the superuser can set the process group IDs.")
  235. #define FUNC_NAME s_scm_setgroups
  236. {
  237. size_t ngroups;
  238. size_t size;
  239. size_t i;
  240. int result;
  241. int save_errno;
  242. GETGROUPS_T *groups;
  243. SCM_VALIDATE_VECTOR (SCM_ARG1, group_vec);
  244. ngroups = SCM_SIMPLE_VECTOR_LENGTH (group_vec);
  245. /* validate before allocating, so we don't have to worry about leaks */
  246. for (i = 0; i < ngroups; i++)
  247. {
  248. unsigned long ulong_gid;
  249. GETGROUPS_T gid;
  250. SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec, i),
  251. ulong_gid);
  252. gid = ulong_gid;
  253. if (gid != ulong_gid)
  254. SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
  255. }
  256. size = ngroups * sizeof (GETGROUPS_T);
  257. if (size / sizeof (GETGROUPS_T) != ngroups)
  258. SCM_OUT_OF_RANGE (SCM_ARG1, scm_from_int (ngroups));
  259. groups = scm_malloc (size);
  260. for(i = 0; i < ngroups; i++)
  261. groups [i] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
  262. result = setgroups (ngroups, groups);
  263. save_errno = errno; /* don't let free() touch errno */
  264. free (groups);
  265. errno = save_errno;
  266. if (result < 0)
  267. SCM_SYSERROR;
  268. return SCM_UNSPECIFIED;
  269. }
  270. #undef FUNC_NAME
  271. #endif
  272. #ifdef HAVE_GETPWENT
  273. SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
  274. (SCM user),
  275. "Look up an entry in the user database. @var{obj} can be an integer,\n"
  276. "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
  277. "or getpwent respectively.")
  278. #define FUNC_NAME s_scm_getpwuid
  279. {
  280. struct passwd *entry;
  281. SCM result = scm_c_make_vector (7, SCM_UNSPECIFIED);
  282. if (SCM_UNBNDP (user) || scm_is_false (user))
  283. {
  284. SCM_SYSCALL (entry = getpwent ());
  285. if (! entry)
  286. {
  287. return SCM_BOOL_F;
  288. }
  289. }
  290. else if (scm_is_integer (user))
  291. {
  292. entry = getpwuid (scm_to_int (user));
  293. }
  294. else
  295. {
  296. WITH_STRING (user, c_user,
  297. entry = getpwnam (c_user));
  298. }
  299. if (!entry)
  300. SCM_MISC_ERROR ("entry not found", SCM_EOL);
  301. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->pw_name));
  302. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->pw_passwd));
  303. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->pw_uid));
  304. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->pw_gid));
  305. SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (entry->pw_gecos));
  306. if (!entry->pw_dir)
  307. SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (""));
  308. else
  309. SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (entry->pw_dir));
  310. if (!entry->pw_shell)
  311. SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (""));
  312. else
  313. SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (entry->pw_shell));
  314. return result;
  315. }
  316. #undef FUNC_NAME
  317. #endif /* HAVE_GETPWENT */
  318. #ifdef HAVE_SETPWENT
  319. SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
  320. (SCM arg),
  321. "If called with a true argument, initialize or reset the password data\n"
  322. "stream. Otherwise, close the stream. The @code{setpwent} and\n"
  323. "@code{endpwent} procedures are implemented on top of this.")
  324. #define FUNC_NAME s_scm_setpwent
  325. {
  326. if (SCM_UNBNDP (arg) || scm_is_false (arg))
  327. endpwent ();
  328. else
  329. setpwent ();
  330. return SCM_UNSPECIFIED;
  331. }
  332. #undef FUNC_NAME
  333. #endif
  334. #ifdef HAVE_GETGRENT
  335. /* Combines getgrgid and getgrnam. */
  336. SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
  337. (SCM name),
  338. "Look up an entry in the group database. @var{obj} can be an integer,\n"
  339. "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
  340. "or getgrent respectively.")
  341. #define FUNC_NAME s_scm_getgrgid
  342. {
  343. struct group *entry;
  344. SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
  345. if (SCM_UNBNDP (name) || scm_is_false (name))
  346. {
  347. SCM_SYSCALL (entry = getgrent ());
  348. if (! entry)
  349. {
  350. return SCM_BOOL_F;
  351. }
  352. }
  353. else if (scm_is_integer (name))
  354. SCM_SYSCALL (entry = getgrgid (scm_to_int (name)));
  355. else
  356. STRING_SYSCALL (name, c_name,
  357. entry = getgrnam (c_name));
  358. if (!entry)
  359. SCM_SYSERROR;
  360. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->gr_name));
  361. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->gr_passwd));
  362. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->gr_gid));
  363. SCM_SIMPLE_VECTOR_SET(result, 3, scm_makfromstrs (-1, entry->gr_mem));
  364. return result;
  365. }
  366. #undef FUNC_NAME
  367. SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
  368. (SCM arg),
  369. "If called with a true argument, initialize or reset the group data\n"
  370. "stream. Otherwise, close the stream. The @code{setgrent} and\n"
  371. "@code{endgrent} procedures are implemented on top of this.")
  372. #define FUNC_NAME s_scm_setgrent
  373. {
  374. if (SCM_UNBNDP (arg) || scm_is_false (arg))
  375. endgrent ();
  376. else
  377. setgrent ();
  378. return SCM_UNSPECIFIED;
  379. }
  380. #undef FUNC_NAME
  381. #endif /* HAVE_GETGRENT */
  382. SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
  383. (SCM pid, SCM sig),
  384. "Sends a signal to the specified process or group of processes.\n\n"
  385. "@var{pid} specifies the processes to which the signal is sent:\n\n"
  386. "@table @r\n"
  387. "@item @var{pid} greater than 0\n"
  388. "The process whose identifier is @var{pid}.\n"
  389. "@item @var{pid} equal to 0\n"
  390. "All processes in the current process group.\n"
  391. "@item @var{pid} less than -1\n"
  392. "The process group whose identifier is -@var{pid}\n"
  393. "@item @var{pid} equal to -1\n"
  394. "If the process is privileged, all processes except for some special\n"
  395. "system processes. Otherwise, all processes with the current effective\n"
  396. "user ID.\n"
  397. "@end table\n\n"
  398. "@var{sig} should be specified using a variable corresponding to\n"
  399. "the Unix symbolic name, e.g.,\n\n"
  400. "@defvar SIGHUP\n"
  401. "Hang-up signal.\n"
  402. "@end defvar\n\n"
  403. "@defvar SIGINT\n"
  404. "Interrupt signal.\n"
  405. "@end defvar")
  406. #define FUNC_NAME s_scm_kill
  407. {
  408. /* Signal values are interned in scm_init_posix(). */
  409. #ifdef HAVE_KILL
  410. if (kill (scm_to_int (pid), scm_to_int (sig)) != 0)
  411. SCM_SYSERROR;
  412. #else
  413. /* Mingw has raise(), but not kill(). (Other raw DOS environments might
  414. be similar.) Use raise() when the requested pid is our own process,
  415. otherwise bomb. */
  416. if (scm_to_int (pid) == getpid ())
  417. {
  418. if (raise (scm_to_int (sig)) != 0)
  419. {
  420. err:
  421. SCM_SYSERROR;
  422. }
  423. else
  424. {
  425. errno = ENOSYS;
  426. goto err;
  427. }
  428. }
  429. #endif
  430. return SCM_UNSPECIFIED;
  431. }
  432. #undef FUNC_NAME
  433. #ifdef HAVE_WAITPID
  434. SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
  435. (SCM pid, SCM options),
  436. "This procedure collects status information from a child process which\n"
  437. "has terminated or (optionally) stopped. Normally it will\n"
  438. "suspend the calling process until this can be done. If more than one\n"
  439. "child process is eligible then one will be chosen by the operating system.\n\n"
  440. "The value of @var{pid} determines the behaviour:\n\n"
  441. "@table @r\n"
  442. "@item @var{pid} greater than 0\n"
  443. "Request status information from the specified child process.\n"
  444. "@item @var{pid} equal to -1 or WAIT_ANY\n"
  445. "Request status information for any child process.\n"
  446. "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
  447. "Request status information for any child process in the current process\n"
  448. "group.\n"
  449. "@item @var{pid} less than -1\n"
  450. "Request status information for any child process whose process group ID\n"
  451. "is -@var{PID}.\n"
  452. "@end table\n\n"
  453. "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
  454. "values of zero or more of the following variables:\n\n"
  455. "@defvar WNOHANG\n"
  456. "Return immediately even if there are no child processes to be collected.\n"
  457. "@end defvar\n\n"
  458. "@defvar WUNTRACED\n"
  459. "Report status information for stopped processes as well as terminated\n"
  460. "processes.\n"
  461. "@end defvar\n\n"
  462. "The return value is a pair containing:\n\n"
  463. "@enumerate\n"
  464. "@item\n"
  465. "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
  466. "specified and no process was collected.\n"
  467. "@item\n"
  468. "The integer status value.\n"
  469. "@end enumerate")
  470. #define FUNC_NAME s_scm_waitpid
  471. {
  472. int i;
  473. int status;
  474. int ioptions;
  475. if (SCM_UNBNDP (options))
  476. ioptions = 0;
  477. else
  478. {
  479. /* Flags are interned in scm_init_posix. */
  480. ioptions = scm_to_int (options);
  481. }
  482. SCM_SYSCALL (i = waitpid (scm_to_int (pid), &status, ioptions));
  483. if (i == -1)
  484. SCM_SYSERROR;
  485. return scm_cons (scm_from_int (i), scm_from_int (status));
  486. }
  487. #undef FUNC_NAME
  488. #endif /* HAVE_WAITPID */
  489. #ifndef __MINGW32__
  490. SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
  491. (SCM status),
  492. "Return the exit status value, as would be set if a process\n"
  493. "ended normally through a call to @code{exit} or @code{_exit},\n"
  494. "if any, otherwise @code{#f}.")
  495. #define FUNC_NAME s_scm_status_exit_val
  496. {
  497. int lstatus;
  498. /* On Ultrix, the WIF... macros assume their argument is an lvalue;
  499. go figure. */
  500. lstatus = scm_to_int (status);
  501. if (WIFEXITED (lstatus))
  502. return (scm_from_int (WEXITSTATUS (lstatus)));
  503. else
  504. return SCM_BOOL_F;
  505. }
  506. #undef FUNC_NAME
  507. SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
  508. (SCM status),
  509. "Return the signal number which terminated the process, if any,\n"
  510. "otherwise @code{#f}.")
  511. #define FUNC_NAME s_scm_status_term_sig
  512. {
  513. int lstatus;
  514. lstatus = scm_to_int (status);
  515. if (WIFSIGNALED (lstatus))
  516. return scm_from_int (WTERMSIG (lstatus));
  517. else
  518. return SCM_BOOL_F;
  519. }
  520. #undef FUNC_NAME
  521. SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
  522. (SCM status),
  523. "Return the signal number which stopped the process, if any,\n"
  524. "otherwise @code{#f}.")
  525. #define FUNC_NAME s_scm_status_stop_sig
  526. {
  527. int lstatus;
  528. lstatus = scm_to_int (status);
  529. if (WIFSTOPPED (lstatus))
  530. return scm_from_int (WSTOPSIG (lstatus));
  531. else
  532. return SCM_BOOL_F;
  533. }
  534. #undef FUNC_NAME
  535. #endif /* __MINGW32__ */
  536. #ifdef HAVE_GETPPID
  537. SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
  538. (),
  539. "Return an integer representing the process ID of the parent\n"
  540. "process.")
  541. #define FUNC_NAME s_scm_getppid
  542. {
  543. return scm_from_int (getppid ());
  544. }
  545. #undef FUNC_NAME
  546. #endif /* HAVE_GETPPID */
  547. #ifndef __MINGW32__
  548. SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
  549. (),
  550. "Return an integer representing the current real user ID.")
  551. #define FUNC_NAME s_scm_getuid
  552. {
  553. return scm_from_int (getuid ());
  554. }
  555. #undef FUNC_NAME
  556. SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
  557. (),
  558. "Return an integer representing the current real group ID.")
  559. #define FUNC_NAME s_scm_getgid
  560. {
  561. return scm_from_int (getgid ());
  562. }
  563. #undef FUNC_NAME
  564. SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
  565. (),
  566. "Return an integer representing the current effective user ID.\n"
  567. "If the system does not support effective IDs, then the real ID\n"
  568. "is returned. @code{(provided? 'EIDs)} reports whether the\n"
  569. "system supports effective IDs.")
  570. #define FUNC_NAME s_scm_geteuid
  571. {
  572. #ifdef HAVE_GETEUID
  573. return scm_from_int (geteuid ());
  574. #else
  575. return scm_from_int (getuid ());
  576. #endif
  577. }
  578. #undef FUNC_NAME
  579. SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
  580. (),
  581. "Return an integer representing the current effective group ID.\n"
  582. "If the system does not support effective IDs, then the real ID\n"
  583. "is returned. @code{(provided? 'EIDs)} reports whether the\n"
  584. "system supports effective IDs.")
  585. #define FUNC_NAME s_scm_getegid
  586. {
  587. #ifdef HAVE_GETEUID
  588. return scm_from_int (getegid ());
  589. #else
  590. return scm_from_int (getgid ());
  591. #endif
  592. }
  593. #undef FUNC_NAME
  594. SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
  595. (SCM id),
  596. "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
  597. "the process has appropriate privileges.\n"
  598. "The return value is unspecified.")
  599. #define FUNC_NAME s_scm_setuid
  600. {
  601. if (setuid (scm_to_int (id)) != 0)
  602. SCM_SYSERROR;
  603. return SCM_UNSPECIFIED;
  604. }
  605. #undef FUNC_NAME
  606. SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
  607. (SCM id),
  608. "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
  609. "the process has appropriate privileges.\n"
  610. "The return value is unspecified.")
  611. #define FUNC_NAME s_scm_setgid
  612. {
  613. if (setgid (scm_to_int (id)) != 0)
  614. SCM_SYSERROR;
  615. return SCM_UNSPECIFIED;
  616. }
  617. #undef FUNC_NAME
  618. SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
  619. (SCM id),
  620. "Sets the effective user ID to the integer @var{id}, provided the process\n"
  621. "has appropriate privileges. If effective IDs are not supported, the\n"
  622. "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
  623. "system supports effective IDs.\n"
  624. "The return value is unspecified.")
  625. #define FUNC_NAME s_scm_seteuid
  626. {
  627. int rv;
  628. #ifdef HAVE_SETEUID
  629. rv = seteuid (scm_to_int (id));
  630. #else
  631. rv = setuid (scm_to_int (id));
  632. #endif
  633. if (rv != 0)
  634. SCM_SYSERROR;
  635. return SCM_UNSPECIFIED;
  636. }
  637. #undef FUNC_NAME
  638. #endif /* __MINGW32__ */
  639. #ifdef HAVE_SETEGID
  640. SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
  641. (SCM id),
  642. "Sets the effective group ID to the integer @var{id}, provided the process\n"
  643. "has appropriate privileges. If effective IDs are not supported, the\n"
  644. "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
  645. "system supports effective IDs.\n"
  646. "The return value is unspecified.")
  647. #define FUNC_NAME s_scm_setegid
  648. {
  649. int rv;
  650. #ifdef HAVE_SETEUID
  651. rv = setegid (scm_to_int (id));
  652. #else
  653. rv = setgid (scm_to_int (id));
  654. #endif
  655. if (rv != 0)
  656. SCM_SYSERROR;
  657. return SCM_UNSPECIFIED;
  658. }
  659. #undef FUNC_NAME
  660. #endif
  661. #ifdef HAVE_GETPGRP
  662. SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
  663. (),
  664. "Return an integer representing the current process group ID.\n"
  665. "This is the POSIX definition, not BSD.")
  666. #define FUNC_NAME s_scm_getpgrp
  667. {
  668. int (*fn)();
  669. fn = (int (*) ()) getpgrp;
  670. return scm_from_int (fn (0));
  671. }
  672. #undef FUNC_NAME
  673. #endif /* HAVE_GETPGRP */
  674. #ifdef HAVE_SETPGID
  675. SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
  676. (SCM pid, SCM pgid),
  677. "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
  678. "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
  679. "current process.\n"
  680. "Fails on systems that do not support job control.\n"
  681. "The return value is unspecified.")
  682. #define FUNC_NAME s_scm_setpgid
  683. {
  684. /* FIXME(?): may be known as setpgrp. */
  685. if (setpgid (scm_to_int (pid), scm_to_int (pgid)) != 0)
  686. SCM_SYSERROR;
  687. return SCM_UNSPECIFIED;
  688. }
  689. #undef FUNC_NAME
  690. #endif /* HAVE_SETPGID */
  691. #ifdef HAVE_SETSID
  692. SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
  693. (),
  694. "Creates a new session. The current process becomes the session leader\n"
  695. "and is put in a new process group. The process will be detached\n"
  696. "from its controlling terminal if it has one.\n"
  697. "The return value is an integer representing the new process group ID.")
  698. #define FUNC_NAME s_scm_setsid
  699. {
  700. pid_t sid = setsid ();
  701. if (sid == -1)
  702. SCM_SYSERROR;
  703. return SCM_UNSPECIFIED;
  704. }
  705. #undef FUNC_NAME
  706. #endif /* HAVE_SETSID */
  707. /* ttyname returns its result in a single static buffer, hence
  708. scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
  709. continuously calling ttyname will otherwise get an overwrite quite
  710. easily.
  711. ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
  712. there's probably little to be gained in either speed or parallelism. */
  713. #ifdef HAVE_TTYNAME
  714. SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
  715. (SCM port),
  716. "Return a string with the name of the serial terminal device\n"
  717. "underlying @var{port}.")
  718. #define FUNC_NAME s_scm_ttyname
  719. {
  720. char *result;
  721. int fd, err;
  722. SCM ret = SCM_BOOL_F;
  723. port = SCM_COERCE_OUTPORT (port);
  724. SCM_VALIDATE_OPPORT (1, port);
  725. if (!SCM_FPORTP (port))
  726. return SCM_BOOL_F;
  727. fd = SCM_FPORT_FDES (port);
  728. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
  729. SCM_SYSCALL (result = ttyname (fd));
  730. err = errno;
  731. if (result != NULL)
  732. result = strdup (result);
  733. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  734. if (!result)
  735. {
  736. errno = err;
  737. SCM_SYSERROR;
  738. }
  739. else
  740. ret = scm_take_locale_string (result);
  741. return ret;
  742. }
  743. #undef FUNC_NAME
  744. #endif /* HAVE_TTYNAME */
  745. /* For thread safety "buf" is used instead of NULL for the ctermid static
  746. buffer. Actually it's unlikely the controlling terminal will change
  747. during program execution, and indeed on glibc (2.3.2) it's always just
  748. "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
  749. safety everywhere. */
  750. #ifdef HAVE_CTERMID
  751. SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
  752. (),
  753. "Return a string containing the file name of the controlling\n"
  754. "terminal for the current process.")
  755. #define FUNC_NAME s_scm_ctermid
  756. {
  757. char buf[L_ctermid];
  758. char *result = ctermid (buf);
  759. if (*result == '\0')
  760. SCM_SYSERROR;
  761. return scm_from_locale_string (result);
  762. }
  763. #undef FUNC_NAME
  764. #endif /* HAVE_CTERMID */
  765. #ifdef HAVE_TCGETPGRP
  766. SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
  767. (SCM port),
  768. "Return the process group ID of the foreground process group\n"
  769. "associated with the terminal open on the file descriptor\n"
  770. "underlying @var{port}.\n"
  771. "\n"
  772. "If there is no foreground process group, the return value is a\n"
  773. "number greater than 1 that does not match the process group ID\n"
  774. "of any existing process group. This can happen if all of the\n"
  775. "processes in the job that was formerly the foreground job have\n"
  776. "terminated, and no other job has yet been moved into the\n"
  777. "foreground.")
  778. #define FUNC_NAME s_scm_tcgetpgrp
  779. {
  780. int fd;
  781. pid_t pgid;
  782. port = SCM_COERCE_OUTPORT (port);
  783. SCM_VALIDATE_OPFPORT (1, port);
  784. fd = SCM_FPORT_FDES (port);
  785. if ((pgid = tcgetpgrp (fd)) == -1)
  786. SCM_SYSERROR;
  787. return scm_from_int (pgid);
  788. }
  789. #undef FUNC_NAME
  790. #endif /* HAVE_TCGETPGRP */
  791. #ifdef HAVE_TCSETPGRP
  792. SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
  793. (SCM port, SCM pgid),
  794. "Set the foreground process group ID for the terminal used by the file\n"
  795. "descriptor underlying @var{port} to the integer @var{pgid}.\n"
  796. "The calling process\n"
  797. "must be a member of the same session as @var{pgid} and must have the same\n"
  798. "controlling terminal. The return value is unspecified.")
  799. #define FUNC_NAME s_scm_tcsetpgrp
  800. {
  801. int fd;
  802. port = SCM_COERCE_OUTPORT (port);
  803. SCM_VALIDATE_OPFPORT (1, port);
  804. fd = SCM_FPORT_FDES (port);
  805. if (tcsetpgrp (fd, scm_to_int (pgid)) == -1)
  806. SCM_SYSERROR;
  807. return SCM_UNSPECIFIED;
  808. }
  809. #undef FUNC_NAME
  810. #endif /* HAVE_TCSETPGRP */
  811. static void
  812. free_string_pointers (void *data)
  813. {
  814. scm_i_free_string_pointers ((char **)data);
  815. }
  816. SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
  817. (SCM filename, SCM args),
  818. "Executes the file named by @var{path} as a new process image.\n"
  819. "The remaining arguments are supplied to the process; from a C program\n"
  820. "they are accessible as the @code{argv} argument to @code{main}.\n"
  821. "Conventionally the first @var{arg} is the same as @var{path}.\n"
  822. "All arguments must be strings.\n\n"
  823. "If @var{arg} is missing, @var{path} is executed with a null\n"
  824. "argument list, which may have system-dependent side-effects.\n\n"
  825. "This procedure is currently implemented using the @code{execv} system\n"
  826. "call, but we call it @code{execl} because of its Scheme calling interface.")
  827. #define FUNC_NAME s_scm_execl
  828. {
  829. char *exec_file;
  830. char **exec_argv;
  831. scm_dynwind_begin (0);
  832. exec_file = scm_to_locale_string (filename);
  833. scm_dynwind_free (exec_file);
  834. exec_argv = scm_i_allocate_string_pointers (args);
  835. scm_dynwind_unwind_handler (free_string_pointers, exec_argv,
  836. SCM_F_WIND_EXPLICITLY);
  837. execv (exec_file,
  838. #ifdef __MINGW32__
  839. /* extra "const" in mingw formals, provokes warning from gcc */
  840. (const char * const *)
  841. #endif
  842. exec_argv);
  843. SCM_SYSERROR;
  844. /* not reached. */
  845. scm_dynwind_end ();
  846. return SCM_BOOL_F;
  847. }
  848. #undef FUNC_NAME
  849. SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
  850. (SCM filename, SCM args),
  851. "Similar to @code{execl}, however if\n"
  852. "@var{filename} does not contain a slash\n"
  853. "then the file to execute will be located by searching the\n"
  854. "directories listed in the @code{PATH} environment variable.\n\n"
  855. "This procedure is currently implemented using the @code{execvp} system\n"
  856. "call, but we call it @code{execlp} because of its Scheme calling interface.")
  857. #define FUNC_NAME s_scm_execlp
  858. {
  859. char *exec_file;
  860. char **exec_argv;
  861. scm_dynwind_begin (0);
  862. exec_file = scm_to_locale_string (filename);
  863. scm_dynwind_free (exec_file);
  864. exec_argv = scm_i_allocate_string_pointers (args);
  865. scm_dynwind_unwind_handler (free_string_pointers, exec_argv,
  866. SCM_F_WIND_EXPLICITLY);
  867. execvp (exec_file,
  868. #ifdef __MINGW32__
  869. /* extra "const" in mingw formals, provokes warning from gcc */
  870. (const char * const *)
  871. #endif
  872. exec_argv);
  873. SCM_SYSERROR;
  874. /* not reached. */
  875. scm_dynwind_end ();
  876. return SCM_BOOL_F;
  877. }
  878. #undef FUNC_NAME
  879. /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
  880. list strings the way environ_list_to_c gives. */
  881. SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
  882. (SCM filename, SCM env, SCM args),
  883. "Similar to @code{execl}, but the environment of the new process is\n"
  884. "specified by @var{env}, which must be a list of strings as returned by the\n"
  885. "@code{environ} procedure.\n\n"
  886. "This procedure is currently implemented using the @code{execve} system\n"
  887. "call, but we call it @code{execle} because of its Scheme calling interface.")
  888. #define FUNC_NAME s_scm_execle
  889. {
  890. char **exec_argv;
  891. char **exec_env;
  892. char *exec_file;
  893. scm_dynwind_begin (0);
  894. exec_file = scm_to_locale_string (filename);
  895. scm_dynwind_free (exec_file);
  896. exec_argv = scm_i_allocate_string_pointers (args);
  897. scm_dynwind_unwind_handler (free_string_pointers, exec_argv,
  898. SCM_F_WIND_EXPLICITLY);
  899. exec_env = scm_i_allocate_string_pointers (env);
  900. scm_dynwind_unwind_handler (free_string_pointers, exec_env,
  901. SCM_F_WIND_EXPLICITLY);
  902. execve (exec_file,
  903. #ifdef __MINGW32__
  904. /* extra "const" in mingw formals, provokes warning from gcc */
  905. (const char * const *)
  906. #endif
  907. exec_argv,
  908. #ifdef __MINGW32__
  909. /* extra "const" in mingw formals, provokes warning from gcc */
  910. (const char * const *)
  911. #endif
  912. exec_env);
  913. SCM_SYSERROR;
  914. /* not reached. */
  915. scm_dynwind_end ();
  916. return SCM_BOOL_F;
  917. }
  918. #undef FUNC_NAME
  919. #ifdef HAVE_FORK
  920. SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
  921. (),
  922. "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
  923. "In the child the return value is 0. In the parent the return value is\n"
  924. "the integer process ID of the child.\n\n"
  925. "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
  926. "with the scsh fork.")
  927. #define FUNC_NAME s_scm_fork
  928. {
  929. int pid;
  930. pid = fork ();
  931. if (pid == -1)
  932. SCM_SYSERROR;
  933. return scm_from_int (pid);
  934. }
  935. #undef FUNC_NAME
  936. #endif /* HAVE_FORK */
  937. #ifdef __MINGW32__
  938. # include "win32-uname.h"
  939. #endif
  940. #if defined (HAVE_UNAME) || defined (__MINGW32__)
  941. SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
  942. (),
  943. "Return an object with some information about the computer\n"
  944. "system the program is running on.")
  945. #define FUNC_NAME s_scm_uname
  946. {
  947. struct utsname buf;
  948. SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
  949. if (uname (&buf) < 0)
  950. SCM_SYSERROR;
  951. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (buf.sysname));
  952. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (buf.nodename));
  953. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_locale_string (buf.release));
  954. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (buf.version));
  955. SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (buf.machine));
  956. /*
  957. a linux special?
  958. SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
  959. */
  960. return result;
  961. }
  962. #undef FUNC_NAME
  963. #endif /* HAVE_UNAME */
  964. SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
  965. (SCM env),
  966. "If @var{env} is omitted, return the current environment (in the\n"
  967. "Unix sense) as a list of strings. Otherwise set the current\n"
  968. "environment, which is also the default environment for child\n"
  969. "processes, to the supplied list of strings. Each member of\n"
  970. "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
  971. "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
  972. "then the return value is unspecified.")
  973. #define FUNC_NAME s_scm_environ
  974. {
  975. if (SCM_UNBNDP (env))
  976. return scm_makfromstrs (-1, environ);
  977. else
  978. {
  979. char **new_environ;
  980. new_environ = scm_i_allocate_string_pointers (env);
  981. /* Free the old environment, except when called for the first
  982. * time.
  983. */
  984. {
  985. static int first = 1;
  986. if (!first)
  987. scm_i_free_string_pointers (environ);
  988. first = 0;
  989. }
  990. environ = new_environ;
  991. return SCM_UNSPECIFIED;
  992. }
  993. }
  994. #undef FUNC_NAME
  995. #ifdef L_tmpnam
  996. SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
  997. (),
  998. "Return a name in the file system that does not match any\n"
  999. "existing file. However there is no guarantee that another\n"
  1000. "process will not create the file after @code{tmpnam} is called.\n"
  1001. "Care should be taken if opening the file, e.g., use the\n"
  1002. "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
  1003. #define FUNC_NAME s_scm_tmpnam
  1004. {
  1005. char name[L_tmpnam];
  1006. char *rv;
  1007. SCM_SYSCALL (rv = tmpnam (name));
  1008. if (rv == NULL)
  1009. /* not SCM_SYSERROR since errno probably not set. */
  1010. SCM_MISC_ERROR ("tmpnam failed", SCM_EOL);
  1011. return scm_from_locale_string (name);
  1012. }
  1013. #undef FUNC_NAME
  1014. #endif
  1015. #ifndef HAVE_MKSTEMP
  1016. extern int mkstemp (char *);
  1017. #endif
  1018. SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
  1019. (SCM tmpl),
  1020. "Create a new unique file in the file system and return a new\n"
  1021. "buffered port open for reading and writing to the file.\n"
  1022. "\n"
  1023. "@var{tmpl} is a string specifying where the file should be\n"
  1024. "created: it must end with @samp{XXXXXX} and those @samp{X}s\n"
  1025. "will be changed in the string to return the name of the file.\n"
  1026. "(@code{port-filename} on the port also gives the name.)\n"
  1027. "\n"
  1028. "POSIX doesn't specify the permissions mode of the file, on GNU\n"
  1029. "and most systems it's @code{#o600}. An application can use\n"
  1030. "@code{chmod} to relax that if desired. For example\n"
  1031. "@code{#o666} less @code{umask}, which is usual for ordinary\n"
  1032. "file creation,\n"
  1033. "\n"
  1034. "@example\n"
  1035. "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
  1036. " (chmod port (logand #o666 (lognot (umask))))\n"
  1037. " ...)\n"
  1038. "@end example")
  1039. #define FUNC_NAME s_scm_mkstemp
  1040. {
  1041. char *c_tmpl;
  1042. int rv;
  1043. scm_dynwind_begin (0);
  1044. c_tmpl = scm_to_locale_string (tmpl);
  1045. scm_dynwind_free (c_tmpl);
  1046. SCM_SYSCALL (rv = mkstemp (c_tmpl));
  1047. if (rv == -1)
  1048. SCM_SYSERROR;
  1049. scm_substring_move_x (scm_from_locale_string (c_tmpl),
  1050. SCM_INUM0, scm_string_length (tmpl),
  1051. tmpl, SCM_INUM0);
  1052. scm_dynwind_end ();
  1053. return scm_fdes_to_port (rv, "w+", tmpl);
  1054. }
  1055. #undef FUNC_NAME
  1056. SCM_DEFINE (scm_utime, "utime", 1, 2, 0,
  1057. (SCM pathname, SCM actime, SCM modtime),
  1058. "@code{utime} sets the access and modification times for the\n"
  1059. "file named by @var{path}. If @var{actime} or @var{modtime} is\n"
  1060. "not supplied, then the current time is used. @var{actime} and\n"
  1061. "@var{modtime} must be integer time values as returned by the\n"
  1062. "@code{current-time} procedure.\n"
  1063. "@lisp\n"
  1064. "(utime \"foo\" (- (current-time) 3600))\n"
  1065. "@end lisp\n"
  1066. "will set the access time to one hour in the past and the\n"
  1067. "modification time to the current time.")
  1068. #define FUNC_NAME s_scm_utime
  1069. {
  1070. int rv;
  1071. struct utimbuf utm_tmp;
  1072. if (SCM_UNBNDP (actime))
  1073. SCM_SYSCALL (time (&utm_tmp.actime));
  1074. else
  1075. utm_tmp.actime = SCM_NUM2ULONG (2, actime);
  1076. if (SCM_UNBNDP (modtime))
  1077. SCM_SYSCALL (time (&utm_tmp.modtime));
  1078. else
  1079. utm_tmp.modtime = SCM_NUM2ULONG (3, modtime);
  1080. STRING_SYSCALL (pathname, c_pathname,
  1081. rv = utime (c_pathname, &utm_tmp));
  1082. if (rv != 0)
  1083. SCM_SYSERROR;
  1084. return SCM_UNSPECIFIED;
  1085. }
  1086. #undef FUNC_NAME
  1087. SCM_DEFINE (scm_access, "access?", 2, 0, 0,
  1088. (SCM path, SCM how),
  1089. "Test accessibility of a file under the real UID and GID of the\n"
  1090. "calling process. The return is @code{#t} if @var{path} exists\n"
  1091. "and the permissions requested by @var{how} are all allowed, or\n"
  1092. "@code{#f} if not.\n"
  1093. "\n"
  1094. "@var{how} is an integer which is one of the following values,\n"
  1095. "or a bitwise-OR (@code{logior}) of multiple values.\n"
  1096. "\n"
  1097. "@defvar R_OK\n"
  1098. "Test for read permission.\n"
  1099. "@end defvar\n"
  1100. "@defvar W_OK\n"
  1101. "Test for write permission.\n"
  1102. "@end defvar\n"
  1103. "@defvar X_OK\n"
  1104. "Test for execute permission.\n"
  1105. "@end defvar\n"
  1106. "@defvar F_OK\n"
  1107. "Test for existence of the file. This is implied by each of the\n"
  1108. "other tests, so there's no need to combine it with them.\n"
  1109. "@end defvar\n"
  1110. "\n"
  1111. "It's important to note that @code{access?} does not simply\n"
  1112. "indicate what will happen on attempting to read or write a\n"
  1113. "file. In normal circumstances it does, but in a set-UID or\n"
  1114. "set-GID program it doesn't because @code{access?} tests the\n"
  1115. "real ID, whereas an open or execute attempt uses the effective\n"
  1116. "ID.\n"
  1117. "\n"
  1118. "A program which will never run set-UID/GID can ignore the\n"
  1119. "difference between real and effective IDs, but for maximum\n"
  1120. "generality, especially in library functions, it's best not to\n"
  1121. "use @code{access?} to predict the result of an open or execute,\n"
  1122. "instead simply attempt that and catch any exception.\n"
  1123. "\n"
  1124. "The main use for @code{access?} is to let a set-UID/GID program\n"
  1125. "determine what the invoking user would have been allowed to do,\n"
  1126. "without the greater (or perhaps lesser) privileges afforded by\n"
  1127. "the effective ID. For more on this, see ``Testing File\n"
  1128. "Access'' in The GNU C Library Reference Manual.")
  1129. #define FUNC_NAME s_scm_access
  1130. {
  1131. int rv;
  1132. WITH_STRING (path, c_path,
  1133. rv = access (c_path, scm_to_int (how)));
  1134. return scm_from_bool (!rv);
  1135. }
  1136. #undef FUNC_NAME
  1137. SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
  1138. (),
  1139. "Return an integer representing the current process ID.")
  1140. #define FUNC_NAME s_scm_getpid
  1141. {
  1142. return scm_from_ulong (getpid ());
  1143. }
  1144. #undef FUNC_NAME
  1145. SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
  1146. (SCM str),
  1147. "Modifies the environment of the current process, which is\n"
  1148. "also the default environment inherited by child processes.\n\n"
  1149. "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
  1150. "directly into the environment, replacing any existing environment string\n"
  1151. "with\n"
  1152. "name matching @code{NAME}. If @var{string} does not contain an equal\n"
  1153. "sign, then any existing string with name matching @var{string} will\n"
  1154. "be removed.\n\n"
  1155. "The return value is unspecified.")
  1156. #define FUNC_NAME s_scm_putenv
  1157. {
  1158. int rv;
  1159. char *c_str = scm_to_locale_string (str);
  1160. if (strchr (c_str, '=') == NULL)
  1161. {
  1162. /* We want no "=" in the argument to mean remove the variable from the
  1163. environment, but not all putenv()s understand this, for example
  1164. FreeBSD 4.8 doesn't. Getting it happening everywhere is a bit
  1165. painful. What unsetenv() exists, we use that, of course.
  1166. Traditionally putenv("NAME") removes a variable, for example that's
  1167. what we have to do on Solaris 9 (it doesn't have an unsetenv).
  1168. But on DOS and on that DOS overlay manager thing called W-whatever,
  1169. putenv("NAME=") must be used (it too doesn't have an unsetenv).
  1170. Supposedly on AIX a putenv("NAME") could cause a segfault, but also
  1171. supposedly AIX 5.3 and up has unsetenv() available so should be ok
  1172. with the latter there.
  1173. For the moment we hard code the DOS putenv("NAME=") style under
  1174. __MINGW32__ and do the traditional everywhere else. Such
  1175. system-name tests are bad, of course. It'd be possible to use a
  1176. configure test when doing a a native build. For example GNU R has
  1177. such a test (see R_PUTENV_AS_UNSETENV in
  1178. https://svn.r-project.org/R/trunk/m4/R.m4). But when cross
  1179. compiling there'd want to be a guess, one probably based on the
  1180. system name (ie. mingw or not), thus landing back in basically the
  1181. present hard-coded situation. Another possibility for a cross
  1182. build would be to try "NAME" then "NAME=" at runtime, if that's not
  1183. too much like overkill. */
  1184. #if HAVE_UNSETENV
  1185. /* when unsetenv() exists then we use it */
  1186. unsetenv (c_str);
  1187. free (c_str);
  1188. #elif defined (__MINGW32__)
  1189. /* otherwise putenv("NAME=") on DOS */
  1190. int e;
  1191. size_t len = strlen (c_str);
  1192. char *ptr = scm_malloc (len + 2);
  1193. strcpy (ptr, c_str);
  1194. strcpy (ptr+len, "=");
  1195. rv = putenv (ptr);
  1196. e = errno; free (ptr); free (c_str); errno = e;
  1197. if (rv < 0)
  1198. SCM_SYSERROR;
  1199. #else
  1200. /* otherwise traditional putenv("NAME") */
  1201. rv = putenv (c_str);
  1202. if (rv < 0)
  1203. SCM_SYSERROR;
  1204. #endif
  1205. }
  1206. else
  1207. {
  1208. #ifdef __MINGW32__
  1209. /* If str is "FOO=", ie. attempting to set an empty string, then
  1210. we need to see if it's been successful. On MINGW, "FOO="
  1211. means remove FOO from the environment. As a workaround, we
  1212. set "FOO= ", ie. a space, and then modify the string returned
  1213. by getenv. It's not enough just to modify the string we set,
  1214. because MINGW putenv copies it. */
  1215. {
  1216. size_t len = strlen (c_str);
  1217. if (c_str[len-1] == '=')
  1218. {
  1219. char *ptr = scm_malloc (len+2);
  1220. strcpy (ptr, c_str);
  1221. strcpy (ptr+len, " ");
  1222. rv = putenv (ptr);
  1223. if (rv < 0)
  1224. {
  1225. int eno = errno;
  1226. free (c_str);
  1227. errno = eno;
  1228. SCM_SYSERROR;
  1229. }
  1230. /* truncate to just the name */
  1231. c_str[len-1] = '\0';
  1232. ptr = getenv (c_str);
  1233. if (ptr)
  1234. ptr[0] = '\0';
  1235. return SCM_UNSPECIFIED;
  1236. }
  1237. }
  1238. #endif /* __MINGW32__ */
  1239. /* Leave c_str in the environment. */
  1240. rv = putenv (c_str);
  1241. if (rv < 0)
  1242. SCM_SYSERROR;
  1243. }
  1244. return SCM_UNSPECIFIED;
  1245. }
  1246. #undef FUNC_NAME
  1247. #ifdef HAVE_SETLOCALE
  1248. SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
  1249. (SCM category, SCM locale),
  1250. "If @var{locale} is omitted, return the current value of the\n"
  1251. "specified locale category as a system-dependent string.\n"
  1252. "@var{category} should be specified using the values\n"
  1253. "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
  1254. "\n"
  1255. "Otherwise the specified locale category is set to the string\n"
  1256. "@var{locale} and the new value is returned as a\n"
  1257. "system-dependent string. If @var{locale} is an empty string,\n"
  1258. "the locale will be set using environment variables.")
  1259. #define FUNC_NAME s_scm_setlocale
  1260. {
  1261. char *clocale;
  1262. char *rv;
  1263. scm_dynwind_begin (0);
  1264. if (SCM_UNBNDP (locale))
  1265. {
  1266. clocale = NULL;
  1267. }
  1268. else
  1269. {
  1270. clocale = scm_to_locale_string (locale);
  1271. scm_dynwind_free (clocale);
  1272. }
  1273. rv = setlocale (scm_i_to_lc_category (category, 1), clocale);
  1274. if (rv == NULL)
  1275. {
  1276. /* POSIX and C99 don't say anything about setlocale setting errno, so
  1277. force a sensible value here. glibc leaves ENOENT, which would be
  1278. fine, but it's not a documented feature. */
  1279. errno = EINVAL;
  1280. SCM_SYSERROR;
  1281. }
  1282. /* Recompute the standard SRFI-14 character sets in a locale-dependent
  1283. (actually charset-dependent) way. */
  1284. scm_srfi_14_compute_char_sets ();
  1285. scm_dynwind_end ();
  1286. return scm_from_locale_string (rv);
  1287. }
  1288. #undef FUNC_NAME
  1289. #endif /* HAVE_SETLOCALE */
  1290. #ifdef HAVE_MKNOD
  1291. SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
  1292. (SCM path, SCM type, SCM perms, SCM dev),
  1293. "Creates a new special file, such as a file corresponding to a device.\n"
  1294. "@var{path} specifies the name of the file. @var{type} should\n"
  1295. "be one of the following symbols:\n"
  1296. "regular, directory, symlink, block-special, char-special,\n"
  1297. "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
  1298. "@var{dev} (an integer) specifies which device the special file refers\n"
  1299. "to. Its exact interpretation depends on the kind of special file\n"
  1300. "being created.\n\n"
  1301. "E.g.,\n"
  1302. "@lisp\n"
  1303. "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
  1304. "@end lisp\n\n"
  1305. "The return value is unspecified.")
  1306. #define FUNC_NAME s_scm_mknod
  1307. {
  1308. int val;
  1309. const char *p;
  1310. int ctype = 0;
  1311. SCM_VALIDATE_STRING (1, path);
  1312. SCM_VALIDATE_SYMBOL (2, type);
  1313. p = scm_i_symbol_chars (type);
  1314. if (strcmp (p, "regular") == 0)
  1315. ctype = S_IFREG;
  1316. else if (strcmp (p, "directory") == 0)
  1317. ctype = S_IFDIR;
  1318. #ifdef S_IFLNK
  1319. /* systems without symlinks probably don't have S_IFLNK defined */
  1320. else if (strcmp (p, "symlink") == 0)
  1321. ctype = S_IFLNK;
  1322. #endif
  1323. else if (strcmp (p, "block-special") == 0)
  1324. ctype = S_IFBLK;
  1325. else if (strcmp (p, "char-special") == 0)
  1326. ctype = S_IFCHR;
  1327. else if (strcmp (p, "fifo") == 0)
  1328. ctype = S_IFIFO;
  1329. #ifdef S_IFSOCK
  1330. else if (strcmp (p, "socket") == 0)
  1331. ctype = S_IFSOCK;
  1332. #endif
  1333. else
  1334. SCM_OUT_OF_RANGE (2, type);
  1335. STRING_SYSCALL (path, c_path,
  1336. val = mknod (c_path,
  1337. ctype | scm_to_int (perms),
  1338. scm_to_int (dev)));
  1339. if (val != 0)
  1340. SCM_SYSERROR;
  1341. return SCM_UNSPECIFIED;
  1342. }
  1343. #undef FUNC_NAME
  1344. #endif /* HAVE_MKNOD */
  1345. #ifdef HAVE_NICE
  1346. SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
  1347. (SCM incr),
  1348. "Increment the priority of the current process by @var{incr}. A higher\n"
  1349. "priority value means that the process runs less often.\n"
  1350. "The return value is unspecified.")
  1351. #define FUNC_NAME s_scm_nice
  1352. {
  1353. int nice_value;
  1354. /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
  1355. from "prio-NZERO", so an error must be detected from errno changed */
  1356. errno = 0;
  1357. nice_value = nice (scm_to_int (incr));
  1358. if (errno != 0)
  1359. SCM_SYSERROR;
  1360. return SCM_UNSPECIFIED;
  1361. }
  1362. #undef FUNC_NAME
  1363. #endif /* HAVE_NICE */
  1364. #ifdef HAVE_SYNC
  1365. SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
  1366. (),
  1367. "Flush the operating system disk buffers.\n"
  1368. "The return value is unspecified.")
  1369. #define FUNC_NAME s_scm_sync
  1370. {
  1371. sync();
  1372. return SCM_UNSPECIFIED;
  1373. }
  1374. #undef FUNC_NAME
  1375. #endif /* HAVE_SYNC */
  1376. /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
  1377. to avoid another thread overwriting it. A test program running crypt
  1378. continuously in two threads can be quickly seen tripping this problem.
  1379. crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
  1380. glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
  1381. slower (about 5x) than plain crypt if you pass an uninitialized data
  1382. block each time. Presumably there's some one-time setups. The best way
  1383. to use crypt_r for parallel execution in multiple threads would probably
  1384. be to maintain a little pool of initialized crypt_data structures, take
  1385. one and use it, then return it to the pool. That pool could be garbage
  1386. collected so it didn't add permanently to memory use if only a few crypt
  1387. calls are made. But we expect crypt will be used rarely, and even more
  1388. rarely will there be any desire for lots of parallel execution on
  1389. multiple cpus. So for now we don't bother with anything fancy, just
  1390. ensure it works. */
  1391. #if HAVE_CRYPT
  1392. SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
  1393. (SCM key, SCM salt),
  1394. "Encrypt @var{key} using @var{salt} as the salt value to the\n"
  1395. "crypt(3) library call.")
  1396. #define FUNC_NAME s_scm_crypt
  1397. {
  1398. SCM ret;
  1399. char *c_key, *c_salt, *c_ret;
  1400. scm_dynwind_begin (0);
  1401. scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
  1402. c_key = scm_to_locale_string (key);
  1403. scm_dynwind_free (c_key);
  1404. c_salt = scm_to_locale_string (salt);
  1405. scm_dynwind_free (c_salt);
  1406. /* The Linux crypt(3) man page says crypt will return NULL and set errno
  1407. on error. (Eg. ENOSYS if legal restrictions mean it cannot be
  1408. implemented). */
  1409. c_ret = crypt (c_key, c_salt);
  1410. if (c_ret == NULL)
  1411. SCM_SYSERROR;
  1412. ret = scm_from_locale_string (c_ret);
  1413. scm_dynwind_end ();
  1414. return ret;
  1415. }
  1416. #undef FUNC_NAME
  1417. #endif /* HAVE_CRYPT */
  1418. #if HAVE_CHROOT
  1419. SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
  1420. (SCM path),
  1421. "Change the root directory to that specified in @var{path}.\n"
  1422. "This directory will be used for path names beginning with\n"
  1423. "@file{/}. The root directory is inherited by all children\n"
  1424. "of the current process. Only the superuser may change the\n"
  1425. "root directory.")
  1426. #define FUNC_NAME s_scm_chroot
  1427. {
  1428. int rv;
  1429. WITH_STRING (path, c_path,
  1430. rv = chroot (c_path));
  1431. if (rv == -1)
  1432. SCM_SYSERROR;
  1433. return SCM_UNSPECIFIED;
  1434. }
  1435. #undef FUNC_NAME
  1436. #endif /* HAVE_CHROOT */
  1437. #ifdef __MINGW32__
  1438. /* Wrapper function to supplying `getlogin()' under Windows. */
  1439. static char * getlogin (void)
  1440. {
  1441. static char user[256];
  1442. static unsigned long len = 256;
  1443. if (!GetUserName (user, &len))
  1444. return NULL;
  1445. return user;
  1446. }
  1447. #endif /* __MINGW32__ */
  1448. #if defined (HAVE_GETLOGIN) || defined (__MINGW32__)
  1449. SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
  1450. (void),
  1451. "Return a string containing the name of the user logged in on\n"
  1452. "the controlling terminal of the process, or @code{#f} if this\n"
  1453. "information cannot be obtained.")
  1454. #define FUNC_NAME s_scm_getlogin
  1455. {
  1456. char * p;
  1457. p = getlogin ();
  1458. if (!p || !*p)
  1459. return SCM_BOOL_F;
  1460. return scm_from_locale_string (p);
  1461. }
  1462. #undef FUNC_NAME
  1463. #endif /* HAVE_GETLOGIN */
  1464. #if HAVE_CUSERID
  1465. SCM_DEFINE (scm_cuserid, "cuserid", 0, 0, 0,
  1466. (void),
  1467. "Return a string containing a user name associated with the\n"
  1468. "effective user id of the process. Return @code{#f} if this\n"
  1469. "information cannot be obtained.")
  1470. #define FUNC_NAME s_scm_cuserid
  1471. {
  1472. char buf[L_cuserid];
  1473. char * p;
  1474. p = cuserid (buf);
  1475. if (!p || !*p)
  1476. return SCM_BOOL_F;
  1477. return scm_from_locale_string (p);
  1478. }
  1479. #undef FUNC_NAME
  1480. #endif /* HAVE_CUSERID */
  1481. #if HAVE_GETPRIORITY
  1482. SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
  1483. (SCM which, SCM who),
  1484. "Return the scheduling priority of the process, process group\n"
  1485. "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
  1486. "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
  1487. "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
  1488. "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
  1489. "process group identifier for @code{PRIO_PGRP}, and a user\n"
  1490. "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
  1491. "denotes the current process, process group, or user. Return\n"
  1492. "the highest priority (lowest numerical value) of any of the\n"
  1493. "specified processes.")
  1494. #define FUNC_NAME s_scm_getpriority
  1495. {
  1496. int cwhich, cwho, ret;
  1497. cwhich = scm_to_int (which);
  1498. cwho = scm_to_int (who);
  1499. /* We have to clear errno and examine it later, because -1 is a
  1500. legal return value for getpriority(). */
  1501. errno = 0;
  1502. ret = getpriority (cwhich, cwho);
  1503. if (errno != 0)
  1504. SCM_SYSERROR;
  1505. return scm_from_int (ret);
  1506. }
  1507. #undef FUNC_NAME
  1508. #endif /* HAVE_GETPRIORITY */
  1509. #if HAVE_SETPRIORITY
  1510. SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
  1511. (SCM which, SCM who, SCM prio),
  1512. "Set the scheduling priority of the process, process group\n"
  1513. "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
  1514. "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
  1515. "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
  1516. "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
  1517. "process group identifier for @code{PRIO_PGRP}, and a user\n"
  1518. "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
  1519. "denotes the current process, process group, or user.\n"
  1520. "@var{prio} is a value in the range -20 and 20, the default\n"
  1521. "priority is 0; lower priorities cause more favorable\n"
  1522. "scheduling. Sets the priority of all of the specified\n"
  1523. "processes. Only the super-user may lower priorities.\n"
  1524. "The return value is not specified.")
  1525. #define FUNC_NAME s_scm_setpriority
  1526. {
  1527. int cwhich, cwho, cprio;
  1528. cwhich = scm_to_int (which);
  1529. cwho = scm_to_int (who);
  1530. cprio = scm_to_int (prio);
  1531. if (setpriority (cwhich, cwho, cprio) == -1)
  1532. SCM_SYSERROR;
  1533. return SCM_UNSPECIFIED;
  1534. }
  1535. #undef FUNC_NAME
  1536. #endif /* HAVE_SETPRIORITY */
  1537. #if HAVE_GETPASS
  1538. SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
  1539. (SCM prompt),
  1540. "Display @var{prompt} to the standard error output and read\n"
  1541. "a password from @file{/dev/tty}. If this file is not\n"
  1542. "accessible, it reads from standard input. The password may be\n"
  1543. "up to 127 characters in length. Additional characters and the\n"
  1544. "terminating newline character are discarded. While reading\n"
  1545. "the password, echoing and the generation of signals by special\n"
  1546. "characters is disabled.")
  1547. #define FUNC_NAME s_scm_getpass
  1548. {
  1549. char * p;
  1550. SCM passwd;
  1551. SCM_VALIDATE_STRING (1, prompt);
  1552. WITH_STRING (prompt, c_prompt,
  1553. p = getpass(c_prompt));
  1554. passwd = scm_from_locale_string (p);
  1555. /* Clear out the password in the static buffer. */
  1556. memset (p, 0, strlen (p));
  1557. return passwd;
  1558. }
  1559. #undef FUNC_NAME
  1560. #endif /* HAVE_GETPASS */
  1561. /* Wrapper function for flock() support under M$-Windows. */
  1562. #ifdef __MINGW32__
  1563. # include <io.h>
  1564. # include <sys/locking.h>
  1565. # include <errno.h>
  1566. # ifndef _LK_UNLCK
  1567. /* Current MinGW package fails to define this. *sigh* */
  1568. # define _LK_UNLCK 0
  1569. # endif
  1570. # define LOCK_EX 1
  1571. # define LOCK_UN 2
  1572. # define LOCK_SH 4
  1573. # define LOCK_NB 8
  1574. static int flock (int fd, int operation)
  1575. {
  1576. long pos, len;
  1577. int ret, err;
  1578. /* Disable invalid arguments. */
  1579. if (((operation & (LOCK_EX | LOCK_SH)) == (LOCK_EX | LOCK_SH)) ||
  1580. ((operation & (LOCK_EX | LOCK_UN)) == (LOCK_EX | LOCK_UN)) ||
  1581. ((operation & (LOCK_SH | LOCK_UN)) == (LOCK_SH | LOCK_UN)))
  1582. {
  1583. errno = EINVAL;
  1584. return -1;
  1585. }
  1586. /* Determine mode of operation and discard unsupported ones. */
  1587. if (operation == (LOCK_NB | LOCK_EX))
  1588. operation = _LK_NBLCK;
  1589. else if (operation & LOCK_UN)
  1590. operation = _LK_UNLCK;
  1591. else if (operation == LOCK_EX)
  1592. operation = _LK_LOCK;
  1593. else
  1594. {
  1595. errno = EINVAL;
  1596. return -1;
  1597. }
  1598. /* Save current file pointer and seek to beginning. */
  1599. if ((pos = lseek (fd, 0, SEEK_CUR)) == -1 || (len = filelength (fd)) == -1)
  1600. return -1;
  1601. lseek (fd, 0L, SEEK_SET);
  1602. /* Deadlock if necessary. */
  1603. do
  1604. {
  1605. ret = _locking (fd, operation, len);
  1606. }
  1607. while (ret == -1 && errno == EDEADLOCK);
  1608. /* Produce meaningful error message. */
  1609. if (errno == EACCES && operation == _LK_NBLCK)
  1610. err = EDEADLOCK;
  1611. else
  1612. err = errno;
  1613. /* Return to saved file position pointer. */
  1614. lseek (fd, pos, SEEK_SET);
  1615. errno = err;
  1616. return ret;
  1617. }
  1618. #endif /* __MINGW32__ */
  1619. #if HAVE_FLOCK || defined (__MINGW32__)
  1620. SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
  1621. (SCM file, SCM operation),
  1622. "Apply or remove an advisory lock on an open file.\n"
  1623. "@var{operation} specifies the action to be done:\n"
  1624. "\n"
  1625. "@defvar LOCK_SH\n"
  1626. "Shared lock. More than one process may hold a shared lock\n"
  1627. "for a given file at a given time.\n"
  1628. "@end defvar\n"
  1629. "@defvar LOCK_EX\n"
  1630. "Exclusive lock. Only one process may hold an exclusive lock\n"
  1631. "for a given file at a given time.\n"
  1632. "@end defvar\n"
  1633. "@defvar LOCK_UN\n"
  1634. "Unlock the file.\n"
  1635. "@end defvar\n"
  1636. "@defvar LOCK_NB\n"
  1637. "Don't block when locking. This is combined with one of the\n"
  1638. "other operations using @code{logior}. If @code{flock} would\n"
  1639. "block an @code{EWOULDBLOCK} error is thrown.\n"
  1640. "@end defvar\n"
  1641. "\n"
  1642. "The return value is not specified. @var{file} may be an open\n"
  1643. "file descriptor or an open file descriptor port.\n"
  1644. "\n"
  1645. "Note that @code{flock} does not lock files across NFS.")
  1646. #define FUNC_NAME s_scm_flock
  1647. {
  1648. int fdes;
  1649. if (scm_is_integer (file))
  1650. fdes = scm_to_int (file);
  1651. else
  1652. {
  1653. SCM_VALIDATE_OPFPORT (2, file);
  1654. fdes = SCM_FPORT_FDES (file);
  1655. }
  1656. if (flock (fdes, scm_to_int (operation)) == -1)
  1657. SCM_SYSERROR;
  1658. return SCM_UNSPECIFIED;
  1659. }
  1660. #undef FUNC_NAME
  1661. #endif /* HAVE_FLOCK */
  1662. #if HAVE_SETHOSTNAME
  1663. SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
  1664. (SCM name),
  1665. "Set the host name of the current processor to @var{name}. May\n"
  1666. "only be used by the superuser. The return value is not\n"
  1667. "specified.")
  1668. #define FUNC_NAME s_scm_sethostname
  1669. {
  1670. int rv;
  1671. WITH_STRING (name, c_name,
  1672. rv = sethostname (c_name, strlen(c_name)));
  1673. if (rv == -1)
  1674. SCM_SYSERROR;
  1675. return SCM_UNSPECIFIED;
  1676. }
  1677. #undef FUNC_NAME
  1678. #endif /* HAVE_SETHOSTNAME */
  1679. #if HAVE_GETHOSTNAME
  1680. SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
  1681. (void),
  1682. "Return the host name of the current processor.")
  1683. #define FUNC_NAME s_scm_gethostname
  1684. {
  1685. #ifdef MAXHOSTNAMELEN
  1686. /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
  1687. * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
  1688. const int len = MAXHOSTNAMELEN + 1;
  1689. char *const p = scm_malloc (len);
  1690. const int res = gethostname (p, len);
  1691. scm_dynwind_begin (0);
  1692. scm_dynwind_unwind_handler (free, p, 0);
  1693. #else
  1694. /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
  1695. * large enough. SUSv2 specifies 255 maximum too, apparently. */
  1696. int len = 256;
  1697. int res;
  1698. char *p;
  1699. # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
  1700. /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
  1701. * which may reflect a particular kernel configuration.
  1702. * Must watch out for this existing but giving -1, as happens for instance
  1703. * in gnu/linux glibc 2.3.2. */
  1704. {
  1705. const long int n = sysconf (_SC_HOST_NAME_MAX);
  1706. if (n != -1L)
  1707. len = n;
  1708. }
  1709. # endif
  1710. p = scm_malloc (len);
  1711. scm_dynwind_begin (0);
  1712. scm_dynwind_unwind_handler (free, p, 0);
  1713. res = gethostname (p, len);
  1714. while (res == -1 && errno == ENAMETOOLONG)
  1715. {
  1716. len *= 2;
  1717. /* scm_realloc may throw an exception. */
  1718. p = scm_realloc (p, len);
  1719. res = gethostname (p, len);
  1720. }
  1721. #endif
  1722. if (res == -1)
  1723. {
  1724. const int save_errno = errno;
  1725. /* No guile exceptions can occur before we have freed p's memory. */
  1726. scm_dynwind_end ();
  1727. free (p);
  1728. errno = save_errno;
  1729. SCM_SYSERROR;
  1730. }
  1731. else
  1732. {
  1733. /* scm_from_locale_string may throw an exception. */
  1734. const SCM name = scm_from_locale_string (p);
  1735. /* No guile exceptions can occur before we have freed p's memory. */
  1736. scm_dynwind_end ();
  1737. free (p);
  1738. return name;
  1739. }
  1740. }
  1741. #undef FUNC_NAME
  1742. #endif /* HAVE_GETHOSTNAME */
  1743. void
  1744. scm_init_posix ()
  1745. {
  1746. scm_add_feature ("posix");
  1747. #ifdef HAVE_GETEUID
  1748. scm_add_feature ("EIDs");
  1749. #endif
  1750. #ifdef WAIT_ANY
  1751. scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY));
  1752. #endif
  1753. #ifdef WAIT_MYPGRP
  1754. scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP));
  1755. #endif
  1756. #ifdef WNOHANG
  1757. scm_c_define ("WNOHANG", scm_from_int (WNOHANG));
  1758. #endif
  1759. #ifdef WUNTRACED
  1760. scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED));
  1761. #endif
  1762. /* access() symbols. */
  1763. scm_c_define ("R_OK", scm_from_int (R_OK));
  1764. scm_c_define ("W_OK", scm_from_int (W_OK));
  1765. scm_c_define ("X_OK", scm_from_int (X_OK));
  1766. scm_c_define ("F_OK", scm_from_int (F_OK));
  1767. #ifdef LC_COLLATE
  1768. scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE));
  1769. #endif
  1770. #ifdef LC_CTYPE
  1771. scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE));
  1772. #endif
  1773. #ifdef LC_MONETARY
  1774. scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY));
  1775. #endif
  1776. #ifdef LC_NUMERIC
  1777. scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC));
  1778. #endif
  1779. #ifdef LC_TIME
  1780. scm_c_define ("LC_TIME", scm_from_int (LC_TIME));
  1781. #endif
  1782. #ifdef LC_MESSAGES
  1783. scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES));
  1784. #endif
  1785. #ifdef LC_ALL
  1786. scm_c_define ("LC_ALL", scm_from_int (LC_ALL));
  1787. #endif
  1788. #ifdef LC_PAPER
  1789. scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER));
  1790. #endif
  1791. #ifdef LC_NAME
  1792. scm_c_define ("LC_NAME", scm_from_int (LC_NAME));
  1793. #endif
  1794. #ifdef LC_ADDRESS
  1795. scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS));
  1796. #endif
  1797. #ifdef LC_TELEPHONE
  1798. scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE));
  1799. #endif
  1800. #ifdef LC_MEASUREMENT
  1801. scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT));
  1802. #endif
  1803. #ifdef LC_IDENTIFICATION
  1804. scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION));
  1805. #endif
  1806. #ifdef PIPE_BUF
  1807. scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF));
  1808. #endif
  1809. #ifdef PRIO_PROCESS
  1810. scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS));
  1811. #endif
  1812. #ifdef PRIO_PGRP
  1813. scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP));
  1814. #endif
  1815. #ifdef PRIO_USER
  1816. scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER));
  1817. #endif
  1818. #ifdef LOCK_SH
  1819. scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH));
  1820. #endif
  1821. #ifdef LOCK_EX
  1822. scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX));
  1823. #endif
  1824. #ifdef LOCK_UN
  1825. scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN));
  1826. #endif
  1827. #ifdef LOCK_NB
  1828. scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB));
  1829. #endif
  1830. #include "libguile/cpp_sig_symbols.c"
  1831. #include "libguile/posix.x"
  1832. }
  1833. /*
  1834. Local Variables:
  1835. c-file-style: "gnu"
  1836. End:
  1837. */