posix.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/fports.h"
  44. #include "libguile/scmsigs.h"
  45. #include "libguile/feature.h"
  46. #include "libguile/strings.h"
  47. #include "libguile/vectors.h"
  48. #include "libguile/validate.h"
  49. #include "libguile/posix.h"
  50. #ifdef HAVE_STRING_H
  51. #include <string.h>
  52. #endif
  53. #ifdef TIME_WITH_SYS_TIME
  54. # include <sys/time.h>
  55. # include <time.h>
  56. #else
  57. # if HAVE_SYS_TIME_H
  58. # include <sys/time.h>
  59. # else
  60. # include <time.h>
  61. # endif
  62. #endif
  63. #ifdef HAVE_UNISTD_H
  64. #include <unistd.h>
  65. #else
  66. #ifndef ttyname
  67. extern char *ttyname();
  68. #endif
  69. #endif
  70. #ifdef LIBC_H_WITH_UNISTD_H
  71. #include <libc.h>
  72. #endif
  73. #include <sys/types.h>
  74. #include <sys/stat.h>
  75. #include <fcntl.h>
  76. #include <pwd.h>
  77. #if HAVE_SYS_WAIT_H
  78. # include <sys/wait.h>
  79. #endif
  80. #ifndef WEXITSTATUS
  81. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  82. #endif
  83. #ifndef WIFEXITED
  84. # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  85. #endif
  86. #include <signal.h>
  87. extern FILE *popen ();
  88. extern char ** environ;
  89. #include <grp.h>
  90. #include <sys/utsname.h>
  91. #if HAVE_DIRENT_H
  92. # include <dirent.h>
  93. # define NAMLEN(dirent) strlen((dirent)->d_name)
  94. #else
  95. # define dirent direct
  96. # define NAMLEN(dirent) (dirent)->d_namlen
  97. # if HAVE_SYS_NDIR_H
  98. # include <sys/ndir.h>
  99. # endif
  100. # if HAVE_SYS_DIR_H
  101. # include <sys/dir.h>
  102. # endif
  103. # if HAVE_NDIR_H
  104. # include <ndir.h>
  105. # endif
  106. #endif
  107. #ifdef HAVE_SETLOCALE
  108. #include <locale.h>
  109. #endif
  110. /* Some Unix systems don't define these. CPP hair is dangerous, but
  111. this seems safe enough... */
  112. #ifndef R_OK
  113. #define R_OK 4
  114. #endif
  115. #ifndef W_OK
  116. #define W_OK 2
  117. #endif
  118. #ifndef X_OK
  119. #define X_OK 1
  120. #endif
  121. #ifndef F_OK
  122. #define F_OK 0
  123. #endif
  124. /* On NextStep, <utime.h> doesn't define struct utime, unless we
  125. #define _POSIX_SOURCE before #including it. I think this is less
  126. of a kludge than defining struct utimbuf ourselves. */
  127. #ifdef UTIMBUF_NEEDS_POSIX
  128. #define _POSIX_SOURCE
  129. #endif
  130. #ifdef HAVE_SYS_UTIME_H
  131. #include <sys/utime.h>
  132. #endif
  133. #ifdef HAVE_UTIME_H
  134. #include <utime.h>
  135. #endif
  136. /* Please don't add any more #includes or #defines here. The hack
  137. above means that _POSIX_SOURCE may be #defined, which will
  138. encourage header files to do strange things. */
  139. SCM_SYMBOL (sym_read_pipe, "read pipe");
  140. SCM_SYMBOL (sym_write_pipe, "write pipe");
  141. SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
  142. (),
  143. "Returns a newly created pipe: a pair of ports which are linked\n"
  144. "together on the local machine. The CAR is the input port and\n"
  145. "the CDR is the output port. Data written (and flushed) to the\n"
  146. "output port can be read from the input port.\n"
  147. "Pipes are commonly used for communication with a newly\n"
  148. "forked child process. The need to flush the output port\n"
  149. "can be avoided by making it unbuffered using @code{setvbuf}.\n\n"
  150. "Writes occur atomically provided the size of the data in\n"
  151. "bytes is not greater than the value of @code{PIPE_BUF}\n"
  152. "Note that the output port is likely to block if too much data\n"
  153. "(typically equal to @code{PIPE_BUF}) has been written but not\n"
  154. "yet read from the input port\n"
  155. )
  156. #define FUNC_NAME s_scm_pipe
  157. {
  158. int fd[2], rv;
  159. SCM p_rd, p_wt;
  160. rv = pipe (fd);
  161. if (rv)
  162. SCM_SYSERROR;
  163. p_rd = scm_fdes_to_port (fd[0], "r", sym_read_pipe);
  164. p_wt = scm_fdes_to_port (fd[1], "w", sym_write_pipe);
  165. return scm_cons (p_rd, p_wt);
  166. }
  167. #undef FUNC_NAME
  168. #ifdef HAVE_GETGROUPS
  169. SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
  170. (),
  171. "Returns a vector of integers representing the current supplimentary group IDs.")
  172. #define FUNC_NAME s_scm_getgroups
  173. {
  174. SCM grps, ans;
  175. int ngroups = getgroups (0, NULL);
  176. if (!ngroups)
  177. SCM_SYSERROR;
  178. SCM_NEWCELL(grps);
  179. SCM_DEFER_INTS;
  180. {
  181. GETGROUPS_T *groups;
  182. int val;
  183. groups = SCM_MUST_MALLOC_TYPE_NUM(GETGROUPS_T,ngroups);
  184. val = getgroups(ngroups, groups);
  185. if (val < 0)
  186. {
  187. int en = errno;
  188. scm_must_free((char *)groups);
  189. errno = en;
  190. SCM_SYSERROR;
  191. }
  192. SCM_SETCHARS(grps, groups); /* set up grps as a GC protect */
  193. SCM_SETLENGTH(grps, 0L + ngroups * sizeof(GETGROUPS_T), scm_tc7_string);
  194. ans = scm_make_vector (SCM_MAKINUM(ngroups), SCM_UNDEFINED);
  195. while (--ngroups >= 0) SCM_VELTS(ans)[ngroups] = SCM_MAKINUM(groups[ngroups]);
  196. SCM_SETCHARS(grps, groups); /* to make sure grps stays around. */
  197. SCM_ALLOW_INTS;
  198. return ans;
  199. }
  200. }
  201. #undef FUNC_NAME
  202. #endif
  203. SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
  204. (SCM user),
  205. "Look up an entry in the user database. @var{obj} can be an integer,\n"
  206. "a string, or omitted, giving the behaviour of getpwuid, getpwnam\n"
  207. "or getpwent respectively.")
  208. #define FUNC_NAME s_scm_getpwuid
  209. {
  210. SCM result;
  211. struct passwd *entry;
  212. SCM *ve;
  213. result = scm_make_vector (SCM_MAKINUM (7), SCM_UNSPECIFIED);
  214. ve = SCM_VELTS (result);
  215. if (SCM_UNBNDP (user) || SCM_FALSEP (user))
  216. {
  217. SCM_SYSCALL (entry = getpwent ());
  218. if (! entry)
  219. {
  220. return SCM_BOOL_F;
  221. }
  222. }
  223. else if (SCM_INUMP (user))
  224. {
  225. entry = getpwuid (SCM_INUM (user));
  226. }
  227. else
  228. {
  229. SCM_VALIDATE_ROSTRING (1,user);
  230. if (SCM_SUBSTRP (user))
  231. user = scm_makfromstr (SCM_ROCHARS (user), SCM_ROLENGTH (user), 0);
  232. entry = getpwnam (SCM_ROCHARS (user));
  233. }
  234. if (!entry)
  235. SCM_MISC_ERROR ("entry not found", SCM_EOL);
  236. ve[0] = scm_makfrom0str (entry->pw_name);
  237. ve[1] = scm_makfrom0str (entry->pw_passwd);
  238. ve[2] = scm_ulong2num ((unsigned long) entry->pw_uid);
  239. ve[3] = scm_ulong2num ((unsigned long) entry->pw_gid);
  240. ve[4] = scm_makfrom0str (entry->pw_gecos);
  241. if (!entry->pw_dir)
  242. ve[5] = scm_makfrom0str ("");
  243. else
  244. ve[5] = scm_makfrom0str (entry->pw_dir);
  245. if (!entry->pw_shell)
  246. ve[6] = scm_makfrom0str ("");
  247. else
  248. ve[6] = scm_makfrom0str (entry->pw_shell);
  249. return result;
  250. }
  251. #undef FUNC_NAME
  252. #ifdef HAVE_SETPWENT
  253. SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
  254. (SCM arg),
  255. "If called with a true argument, initialize or reset the password data\n"
  256. "stream. Otherwise, close the stream. The @code{setpwent} and\n"
  257. "@code{endpwent} procedures are implemented on top of this.")
  258. #define FUNC_NAME s_scm_setpwent
  259. {
  260. if (SCM_UNBNDP (arg) || SCM_FALSEP (arg))
  261. endpwent ();
  262. else
  263. setpwent ();
  264. return SCM_UNSPECIFIED;
  265. }
  266. #undef FUNC_NAME
  267. #endif
  268. /* Combines getgrgid and getgrnam. */
  269. SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
  270. (SCM name),
  271. "Look up an entry in the group database. @var{obj} can be an integer,\n"
  272. "a string, or omitted, giving the behaviour of getgrgid, getgrnam\n"
  273. "or getgrent respectively.")
  274. #define FUNC_NAME s_scm_getgrgid
  275. {
  276. SCM result;
  277. struct group *entry;
  278. SCM *ve;
  279. result = scm_make_vector (SCM_MAKINUM (4), SCM_UNSPECIFIED);
  280. ve = SCM_VELTS (result);
  281. if (SCM_UNBNDP (name) || SCM_FALSEP (name))
  282. {
  283. SCM_SYSCALL (entry = getgrent ());
  284. if (! entry)
  285. {
  286. return SCM_BOOL_F;
  287. }
  288. }
  289. else if (SCM_INUMP (name))
  290. SCM_SYSCALL (entry = getgrgid (SCM_INUM (name)));
  291. else
  292. {
  293. SCM_VALIDATE_ROSTRING (1,name);
  294. SCM_COERCE_SUBSTR (name);
  295. SCM_SYSCALL (entry = getgrnam (SCM_ROCHARS (name)));
  296. }
  297. if (!entry)
  298. SCM_SYSERROR;
  299. ve[0] = scm_makfrom0str (entry->gr_name);
  300. ve[1] = scm_makfrom0str (entry->gr_passwd);
  301. ve[2] = scm_ulong2num ((unsigned long) entry->gr_gid);
  302. ve[3] = scm_makfromstrs (-1, entry->gr_mem);
  303. return result;
  304. }
  305. #undef FUNC_NAME
  306. SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
  307. (SCM arg),
  308. "If called with a true argument, initialize or reset the group data\n"
  309. "stream. Otherwise, close the stream. The @code{setgrent} and\n"
  310. "@code{endgrent} procedures are implemented on top of this.")
  311. #define FUNC_NAME s_scm_setgrent
  312. {
  313. if (SCM_UNBNDP (arg) || SCM_FALSEP (arg))
  314. endgrent ();
  315. else
  316. setgrent ();
  317. return SCM_UNSPECIFIED;
  318. }
  319. #undef FUNC_NAME
  320. SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
  321. (SCM pid, SCM sig),
  322. "Sends a signal to the specified process or group of processes.\n\n"
  323. "@var{pid} specifies the processes to which the signal is sent:\n\n"
  324. "@table @r\n"
  325. "@item @var{pid} greater than 0\n"
  326. "The process whose identifier is @var{pid}.\n"
  327. "@item @var{pid} equal to 0\n"
  328. "All processes in the current process group.\n"
  329. "@item @var{pid} less than -1\n"
  330. "The process group whose identifier is -@var{pid}\n"
  331. "@item @var{pid} equal to -1\n"
  332. "If the process is privileged, all processes except for some special\n"
  333. "system processes. Otherwise, all processes with the current effective\n"
  334. "user ID.\n"
  335. "@end table\n\n"
  336. "@var{sig} should be specified using a variable corresponding to\n"
  337. "the Unix symbolic name, e.g.,\n\n"
  338. "@defvar SIGHUP\n"
  339. "Hang-up signal.\n"
  340. "@end defvar\n\n"
  341. "@defvar SIGINT\n"
  342. "Interrupt signal.\n"
  343. "@end defvar")
  344. #define FUNC_NAME s_scm_kill
  345. {
  346. SCM_VALIDATE_INUM (1,pid);
  347. SCM_VALIDATE_INUM (2,sig);
  348. /* Signal values are interned in scm_init_posix(). */
  349. if (kill ((int) SCM_INUM (pid), (int) SCM_INUM (sig)) != 0)
  350. SCM_SYSERROR;
  351. return SCM_UNSPECIFIED;
  352. }
  353. #undef FUNC_NAME
  354. #ifdef HAVE_WAITPID
  355. SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
  356. (SCM pid, SCM options),
  357. "This procedure collects status information from a child process which\n"
  358. "has terminated or (optionally) stopped. Normally it will\n"
  359. "suspend the calling process until this can be done. If more than one\n"
  360. "child process is eligible then one will be chosen by the operating system.\n\n"
  361. "The value of @var{pid} determines the behaviour:\n\n"
  362. "@table @r\n"
  363. "@item @var{pid} greater than 0\n"
  364. "Request status information from the specified child process.\n"
  365. "@item @var{pid} equal to -1 or WAIT_ANY\n"
  366. "Request status information for any child process.\n"
  367. "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
  368. "Request status information for any child process in the current process\n"
  369. "group.\n"
  370. "@item @var{pid} less than -1\n"
  371. "Request status information for any child process whose process group ID\n"
  372. "is -@var{PID}.\n"
  373. "@end table\n\n"
  374. "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
  375. "values of zero or more of the following variables:\n\n"
  376. "@defvar WNOHANG\n"
  377. "Return immediately even if there are no child processes to be collected.\n"
  378. "@end defvar\n\n"
  379. "@defvar WUNTRACED\n"
  380. "Report status information for stopped processes as well as terminated\n"
  381. "processes.\n"
  382. "@end defvar\n\n"
  383. "The return value is a pair containing:\n\n"
  384. "@enumerate\n"
  385. "@item\n"
  386. "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
  387. "specified and no process was collected.\n"
  388. "@item\n"
  389. "The integer status value.\n"
  390. "@end enumerate")
  391. #define FUNC_NAME s_scm_waitpid
  392. {
  393. int i;
  394. int status;
  395. int ioptions;
  396. SCM_VALIDATE_INUM (1,pid);
  397. if (SCM_UNBNDP (options))
  398. ioptions = 0;
  399. else
  400. {
  401. SCM_VALIDATE_INUM (2,options);
  402. /* Flags are interned in scm_init_posix. */
  403. ioptions = SCM_INUM (options);
  404. }
  405. SCM_SYSCALL (i = waitpid (SCM_INUM (pid), &status, ioptions));
  406. if (i == -1)
  407. SCM_SYSERROR;
  408. return scm_cons (SCM_MAKINUM (0L + i), SCM_MAKINUM (0L + status));
  409. }
  410. #undef FUNC_NAME
  411. #endif /* HAVE_WAITPID */
  412. SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
  413. (SCM status),
  414. "Returns the exit status value, as would be\n"
  415. "set if a process ended normally through a\n"
  416. "call to @code{exit} or @code{_exit}, if any, otherwise @code{#f}.")
  417. #define FUNC_NAME s_scm_status_exit_val
  418. {
  419. int lstatus;
  420. SCM_VALIDATE_INUM (1,status);
  421. /* On Ultrix, the WIF... macros assume their argument is an lvalue;
  422. go figure. SCM_INUM does not yield an lvalue. */
  423. lstatus = SCM_INUM (status);
  424. if (WIFEXITED (lstatus))
  425. return (SCM_MAKINUM (WEXITSTATUS (lstatus)));
  426. else
  427. return SCM_BOOL_F;
  428. }
  429. #undef FUNC_NAME
  430. SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
  431. (SCM status),
  432. "Returns the signal number which terminated the\n"
  433. "process, if any, otherwise @code{#f}.")
  434. #define FUNC_NAME s_scm_status_term_sig
  435. {
  436. int lstatus;
  437. SCM_VALIDATE_INUM (1,status);
  438. lstatus = SCM_INUM (status);
  439. if (WIFSIGNALED (lstatus))
  440. return SCM_MAKINUM (WTERMSIG (lstatus));
  441. else
  442. return SCM_BOOL_F;
  443. }
  444. #undef FUNC_NAME
  445. SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
  446. (SCM status),
  447. "Returns the signal number which stopped the\n"
  448. "process, if any, otherwise @code{#f}.")
  449. #define FUNC_NAME s_scm_status_stop_sig
  450. {
  451. int lstatus;
  452. SCM_VALIDATE_INUM (1,status);
  453. lstatus = SCM_INUM (status);
  454. if (WIFSTOPPED (lstatus))
  455. return SCM_MAKINUM (WSTOPSIG (lstatus));
  456. else
  457. return SCM_BOOL_F;
  458. }
  459. #undef FUNC_NAME
  460. SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
  461. (),
  462. "Returns an integer representing the process ID of the parent process.")
  463. #define FUNC_NAME s_scm_getppid
  464. {
  465. return SCM_MAKINUM (0L + getppid ());
  466. }
  467. #undef FUNC_NAME
  468. SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
  469. (),
  470. "Returns an integer representing the current real user ID.")
  471. #define FUNC_NAME s_scm_getuid
  472. {
  473. return SCM_MAKINUM (0L + getuid ());
  474. }
  475. #undef FUNC_NAME
  476. SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
  477. (),
  478. "Returns an integer representing the current real group ID.")
  479. #define FUNC_NAME s_scm_getgid
  480. {
  481. return SCM_MAKINUM (0L + getgid ());
  482. }
  483. #undef FUNC_NAME
  484. SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
  485. (),
  486. "Returns an integer representing the current effective user ID.\n"
  487. "If the system does not support effective IDs, then the real ID\n"
  488. "is returned. @code{(feature? 'EIDs)} reports whether the system\n"
  489. "supports effective IDs.")
  490. #define FUNC_NAME s_scm_geteuid
  491. {
  492. #ifdef HAVE_GETEUID
  493. return SCM_MAKINUM (0L + geteuid ());
  494. #else
  495. return SCM_MAKINUM (0L + getuid ());
  496. #endif
  497. }
  498. #undef FUNC_NAME
  499. SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
  500. (),
  501. "Returns an integer representing the current effective group ID.\n"
  502. "If the system does not support effective IDs, then the real ID\n"
  503. "is returned. @code{(feature? 'EIDs)} reports whether the system\n"
  504. "supports effective IDs.")
  505. #define FUNC_NAME s_scm_getegid
  506. {
  507. #ifdef HAVE_GETEUID
  508. return SCM_MAKINUM (0L + getegid ());
  509. #else
  510. return SCM_MAKINUM (0L + getgid ());
  511. #endif
  512. }
  513. #undef FUNC_NAME
  514. SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
  515. (SCM id),
  516. "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
  517. "the process has appropriate privileges.\n"
  518. "The return value is unspecified.")
  519. #define FUNC_NAME s_scm_setuid
  520. {
  521. SCM_VALIDATE_INUM (1,id);
  522. if (setuid (SCM_INUM (id)) != 0)
  523. SCM_SYSERROR;
  524. return SCM_UNSPECIFIED;
  525. }
  526. #undef FUNC_NAME
  527. SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
  528. (SCM id),
  529. "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
  530. "the process has appropriate privileges.\n"
  531. "The return value is unspecified.")
  532. #define FUNC_NAME s_scm_setgid
  533. {
  534. SCM_VALIDATE_INUM (1,id);
  535. if (setgid (SCM_INUM (id)) != 0)
  536. SCM_SYSERROR;
  537. return SCM_UNSPECIFIED;
  538. }
  539. #undef FUNC_NAME
  540. SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
  541. (SCM id),
  542. "Sets the effective user ID to the integer @var{id}, provided the process\n"
  543. "has appropriate privileges. If effective IDs are not supported, the\n"
  544. "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n"
  545. "system supports effective IDs.\n"
  546. "The return value is unspecified.")
  547. #define FUNC_NAME s_scm_seteuid
  548. {
  549. int rv;
  550. SCM_VALIDATE_INUM (1,id);
  551. #ifdef HAVE_SETEUID
  552. rv = seteuid (SCM_INUM (id));
  553. #else
  554. rv = setuid (SCM_INUM (id));
  555. #endif
  556. if (rv != 0)
  557. SCM_SYSERROR;
  558. return SCM_UNSPECIFIED;
  559. }
  560. #undef FUNC_NAME
  561. #ifdef HAVE_SETEGID
  562. SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
  563. (SCM id),
  564. "Sets the effective group ID to the integer @var{id}, provided the process\n"
  565. "has appropriate privileges. If effective IDs are not supported, the\n"
  566. "real ID is set instead -- @code{(feature? 'EIDs)} reports whether the\n"
  567. "system supports effective IDs.\n"
  568. "The return value is unspecified.")
  569. #define FUNC_NAME s_scm_setegid
  570. {
  571. int rv;
  572. SCM_VALIDATE_INUM (1,id);
  573. #ifdef HAVE_SETEUID
  574. rv = setegid (SCM_INUM (id));
  575. #else
  576. rv = setgid (SCM_INUM (id));
  577. #endif
  578. if (rv != 0)
  579. SCM_SYSERROR;
  580. return SCM_UNSPECIFIED;
  581. }
  582. #undef FUNC_NAME
  583. #endif
  584. SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
  585. (),
  586. "Returns an integer representing the current process group ID.\n"
  587. "This is the POSIX definition, not BSD.")
  588. #define FUNC_NAME s_scm_getpgrp
  589. {
  590. int (*fn)();
  591. fn = (int (*) ()) getpgrp;
  592. return SCM_MAKINUM (fn (0));
  593. }
  594. #undef FUNC_NAME
  595. #ifdef HAVE_SETPGID
  596. SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
  597. (SCM pid, SCM pgid),
  598. "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
  599. "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
  600. "current process.\n"
  601. "Fails on systems that do not support job control.\n"
  602. "The return value is unspecified.")
  603. #define FUNC_NAME s_scm_setpgid
  604. {
  605. SCM_VALIDATE_INUM (1,pid);
  606. SCM_VALIDATE_INUM (2,pgid);
  607. /* FIXME(?): may be known as setpgrp. */
  608. if (setpgid (SCM_INUM (pid), SCM_INUM (pgid)) != 0)
  609. SCM_SYSERROR;
  610. return SCM_UNSPECIFIED;
  611. }
  612. #undef FUNC_NAME
  613. #endif /* HAVE_SETPGID */
  614. #ifdef HAVE_SETSID
  615. SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
  616. (),
  617. "Creates a new session. The current process becomes the session leader\n"
  618. "and is put in a new process group. The process will be detached\n"
  619. "from its controlling terminal if it has one.\n"
  620. "The return value is an integer representing the new process group ID.")
  621. #define FUNC_NAME s_scm_setsid
  622. {
  623. pid_t sid = setsid ();
  624. if (sid == -1)
  625. SCM_SYSERROR;
  626. return SCM_UNSPECIFIED;
  627. }
  628. #undef FUNC_NAME
  629. #endif /* HAVE_SETSID */
  630. SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
  631. (SCM port),
  632. "Returns a string with the name of the serial terminal device underlying\n"
  633. "@var{port}.")
  634. #define FUNC_NAME s_scm_ttyname
  635. {
  636. char *ans;
  637. int fd;
  638. port = SCM_COERCE_OUTPORT (port);
  639. SCM_VALIDATE_OPPORT (1,port);
  640. if (scm_tc16_fport != SCM_TYP16 (port))
  641. return SCM_BOOL_F;
  642. fd = SCM_FPORT_FDES (port);
  643. SCM_SYSCALL (ans = ttyname (fd));
  644. if (!ans)
  645. SCM_SYSERROR;
  646. /* ans could be overwritten by another call to ttyname */
  647. return (scm_makfrom0str (ans));
  648. }
  649. #undef FUNC_NAME
  650. #ifdef HAVE_CTERMID
  651. SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
  652. (),
  653. "Returns a string containing the file name of the controlling terminal\n"
  654. "for the current process.")
  655. #define FUNC_NAME s_scm_ctermid
  656. {
  657. char *result = ctermid (NULL);
  658. if (*result == '\0')
  659. SCM_SYSERROR;
  660. return scm_makfrom0str (result);
  661. }
  662. #undef FUNC_NAME
  663. #endif /* HAVE_CTERMID */
  664. #ifdef HAVE_TCGETPGRP
  665. SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
  666. (SCM port),
  667. "Returns the process group ID of the foreground\n"
  668. "process group associated with the terminal open on the file descriptor\n"
  669. "underlying @var{port}.\n\n"
  670. "If there is no foreground process group, the return value is a\n"
  671. "number greater than 1 that does not match the process group ID\n"
  672. "of any existing process group. This can happen if all of the\n"
  673. "processes in the job that was formerly the foreground job have\n"
  674. "terminated, and no other job has yet been moved into the\n"
  675. "foreground.")
  676. #define FUNC_NAME s_scm_tcgetpgrp
  677. {
  678. int fd;
  679. pid_t pgid;
  680. port = SCM_COERCE_OUTPORT (port);
  681. SCM_VALIDATE_OPFPORT (1,port);
  682. fd = SCM_FPORT_FDES (port);
  683. if ((pgid = tcgetpgrp (fd)) == -1)
  684. SCM_SYSERROR;
  685. return SCM_MAKINUM (pgid);
  686. }
  687. #undef FUNC_NAME
  688. #endif /* HAVE_TCGETPGRP */
  689. #ifdef HAVE_TCSETPGRP
  690. SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
  691. (SCM port, SCM pgid),
  692. "Set the foreground process group ID for the terminal used by the file\n"
  693. "descriptor underlying @var{port} to the integer @var{pgid}.\n"
  694. "The calling process\n"
  695. "must be a member of the same session as @var{pgid} and must have the same\n"
  696. "controlling terminal. The return value is unspecified.")
  697. #define FUNC_NAME s_scm_tcsetpgrp
  698. {
  699. int fd;
  700. port = SCM_COERCE_OUTPORT (port);
  701. SCM_VALIDATE_OPFPORT (1,port);
  702. SCM_VALIDATE_INUM (2,pgid);
  703. fd = SCM_FPORT_FDES (port);
  704. if (tcsetpgrp (fd, SCM_INUM (pgid)) == -1)
  705. SCM_SYSERROR;
  706. return SCM_UNSPECIFIED;
  707. }
  708. #undef FUNC_NAME
  709. #endif /* HAVE_TCSETPGRP */
  710. /* Copy exec args from an SCM vector into a new C array. */
  711. static char **
  712. scm_convert_exec_args (SCM args, int pos, const char *subr)
  713. {
  714. char **execargv;
  715. int num_args;
  716. int i;
  717. num_args = scm_ilength (args);
  718. SCM_ASSERT (num_args >= 0, args, pos, subr);
  719. execargv = (char **)
  720. scm_must_malloc ((num_args + 1) * sizeof (char *), subr);
  721. for (i = 0; !SCM_NULLP (args); args = SCM_CDR (args), ++i)
  722. {
  723. scm_sizet len;
  724. char *dst;
  725. char *src;
  726. SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (args)),
  727. SCM_CAR (args), SCM_ARGn, subr);
  728. len = 1 + SCM_ROLENGTH (SCM_CAR (args));
  729. dst = (char *) scm_must_malloc ((long) len, subr);
  730. src = SCM_ROCHARS (SCM_CAR (args));
  731. while (len--)
  732. dst[len] = src[len];
  733. execargv[i] = dst;
  734. }
  735. execargv[i] = 0;
  736. return execargv;
  737. }
  738. SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
  739. (SCM filename, SCM args),
  740. "Executes the file named by @var{path} as a new process image.\n"
  741. "The remaining arguments are supplied to the process; from a C program\n"
  742. "they are accessable as the @code{argv} argument to @code{main}.\n"
  743. "Conventionally the first @var{arg} is the same as @var{path}.\n"
  744. "All arguments must be strings. \n\n"
  745. "If @var{arg} is missing, @var{path} is executed with a null\n"
  746. "argument list, which may have system-dependent side-effects.\n\n"
  747. "This procedure is currently implemented using the @code{execv} system\n"
  748. "call, but we call it @code{execl} because of its Scheme calling interface.")
  749. #define FUNC_NAME s_scm_execl
  750. {
  751. char **execargv;
  752. SCM_VALIDATE_ROSTRING (1,filename);
  753. SCM_COERCE_SUBSTR (filename);
  754. execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME);
  755. execv (SCM_ROCHARS (filename), execargv);
  756. SCM_SYSERROR;
  757. /* not reached. */
  758. return SCM_BOOL_F;
  759. }
  760. #undef FUNC_NAME
  761. SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
  762. (SCM filename, SCM args),
  763. "Similar to @code{execl}, however if\n"
  764. "@var{filename} does not contain a slash\n"
  765. "then the file to execute will be located by searching the\n"
  766. "directories listed in the @code{PATH} environment variable.\n\n"
  767. "This procedure is currently implemented using the @code{execvp} system\n"
  768. "call, but we call it @code{execlp} because of its Scheme calling interface.")
  769. #define FUNC_NAME s_scm_execlp
  770. {
  771. char **execargv;
  772. SCM_VALIDATE_ROSTRING (1,filename);
  773. SCM_COERCE_SUBSTR (filename);
  774. execargv = scm_convert_exec_args (args, SCM_ARG2, FUNC_NAME);
  775. execvp (SCM_ROCHARS (filename), execargv);
  776. SCM_SYSERROR;
  777. /* not reached. */
  778. return SCM_BOOL_F;
  779. }
  780. #undef FUNC_NAME
  781. static char **
  782. environ_list_to_c (SCM envlist, int arg, const char *proc)
  783. {
  784. int num_strings;
  785. char **result;
  786. int i = 0;
  787. SCM_ASSERT (SCM_NULLP (envlist) || SCM_CONSP (envlist),
  788. envlist, arg, proc);
  789. num_strings = scm_ilength (envlist);
  790. result = (char **) malloc ((num_strings + 1) * sizeof (char *));
  791. if (result == NULL)
  792. scm_memory_error (proc);
  793. while (SCM_NNULLP (envlist))
  794. {
  795. int len;
  796. char *src;
  797. SCM_ASSERT (SCM_ROSTRINGP (SCM_CAR (envlist)),
  798. envlist, arg, proc);
  799. len = 1 + SCM_ROLENGTH (SCM_CAR (envlist));
  800. result[i] = malloc ((long) len);
  801. if (result[i] == NULL)
  802. scm_memory_error (proc);
  803. src = SCM_ROCHARS (SCM_CAR (envlist));
  804. while (len--)
  805. result[i][len] = src[len];
  806. envlist = SCM_CDR (envlist);
  807. i++;
  808. }
  809. result[i] = 0;
  810. return result;
  811. }
  812. SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
  813. (SCM filename, SCM env, SCM args),
  814. "Similar to @code{execl}, but the environment of the new process is\n"
  815. "specified by @var{env}, which must be a list of strings as returned by the\n"
  816. "@code{environ} procedure.\n\n"
  817. "This procedure is currently implemented using the @code{execve} system\n"
  818. "call, but we call it @code{execle} because of its Scheme calling interface.")
  819. #define FUNC_NAME s_scm_execle
  820. {
  821. char **execargv;
  822. char **exec_env;
  823. SCM_VALIDATE_ROSTRING (1,filename);
  824. SCM_COERCE_SUBSTR (filename);
  825. execargv = scm_convert_exec_args (args, SCM_ARG1, FUNC_NAME);
  826. exec_env = environ_list_to_c (env, SCM_ARG2, FUNC_NAME);
  827. execve (SCM_ROCHARS (filename), execargv, exec_env);
  828. SCM_SYSERROR;
  829. /* not reached. */
  830. return SCM_BOOL_F;
  831. }
  832. #undef FUNC_NAME
  833. SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
  834. (),
  835. "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
  836. "In the child the return value is 0. In the parent the return value is\n"
  837. "the integer process ID of the child.\n\n"
  838. "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
  839. "with the scsh fork.")
  840. #define FUNC_NAME s_scm_fork
  841. {
  842. int pid;
  843. pid = fork ();
  844. if (pid == -1)
  845. SCM_SYSERROR;
  846. return SCM_MAKINUM (0L+pid);
  847. }
  848. #undef FUNC_NAME
  849. #ifdef HAVE_UNAME
  850. SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
  851. (),
  852. "Returns an object with some information about the computer system the\n"
  853. "program is running on.")
  854. #define FUNC_NAME s_scm_uname
  855. {
  856. struct utsname buf;
  857. SCM ans = scm_make_vector (SCM_MAKINUM(5), SCM_UNSPECIFIED);
  858. SCM *ve = SCM_VELTS (ans);
  859. if (uname (&buf) < 0)
  860. SCM_SYSERROR;
  861. ve[0] = scm_makfrom0str (buf.sysname);
  862. ve[1] = scm_makfrom0str (buf.nodename);
  863. ve[2] = scm_makfrom0str (buf.release);
  864. ve[3] = scm_makfrom0str (buf.version);
  865. ve[4] = scm_makfrom0str (buf.machine);
  866. /*
  867. a linux special?
  868. ve[5] = scm_makfrom0str (buf.domainname);
  869. */
  870. return ans;
  871. }
  872. #undef FUNC_NAME
  873. #endif /* HAVE_UNAME */
  874. SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
  875. (SCM env),
  876. "If @var{env} is omitted, returns the current environment as a list of strings.\n"
  877. "Otherwise it sets the current environment, which is also the\n"
  878. "default environment for child processes, to the supplied list of strings.\n"
  879. "Each member of @var{env} should be of the form\n"
  880. "@code{NAME=VALUE} and values of @code{NAME} should not be duplicated.\n"
  881. "If @var{env} is supplied then the return value is unspecified.")
  882. #define FUNC_NAME s_scm_environ
  883. {
  884. if (SCM_UNBNDP (env))
  885. return scm_makfromstrs (-1, environ);
  886. else
  887. {
  888. char **new_environ;
  889. new_environ = environ_list_to_c (env, SCM_ARG1, FUNC_NAME);
  890. /* Free the old environment, except when called for the first
  891. * time.
  892. */
  893. {
  894. char **ep;
  895. static int first = 1;
  896. if (!first)
  897. {
  898. for (ep = environ; *ep != NULL; ep++)
  899. free (*ep);
  900. free ((char *) environ);
  901. }
  902. first = 0;
  903. }
  904. environ = new_environ;
  905. return SCM_UNSPECIFIED;
  906. }
  907. }
  908. #undef FUNC_NAME
  909. #ifdef L_tmpnam
  910. SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
  911. (),
  912. "Create a new file in the file system with a unique name. The return\n"
  913. "value is the name of the new file. This function is implemented with\n"
  914. "the @code{tmpnam} function in the system libraries.")
  915. #define FUNC_NAME s_scm_tmpnam
  916. {
  917. char name[L_tmpnam];
  918. SCM_SYSCALL (tmpnam (name););
  919. return scm_makfrom0str (name);
  920. }
  921. #undef FUNC_NAME
  922. #endif
  923. SCM_DEFINE (scm_utime, "utime", 1, 2, 0,
  924. (SCM pathname, SCM actime, SCM modtime),
  925. "@code{utime} sets the access and modification times for\n"
  926. "the file named by @var{path}. If @var{actime} or @var{modtime}\n"
  927. "is not supplied, then the current time is used.\n"
  928. "@var{actime} and @var{modtime}\n"
  929. "must be integer time values as returned by the @code{current-time}\n"
  930. "procedure.\n\n"
  931. "E.g.,\n\n"
  932. "@smalllisp\n"
  933. "(utime \"foo\" (- (current-time) 3600))\n"
  934. "@end smalllisp\n\n"
  935. "will set the access time to one hour in the past and the modification\n"
  936. "time to the current time.")
  937. #define FUNC_NAME s_scm_utime
  938. {
  939. int rv;
  940. struct utimbuf utm_tmp;
  941. SCM_VALIDATE_ROSTRING (1,pathname);
  942. SCM_COERCE_SUBSTR (pathname);
  943. if (SCM_UNBNDP (actime))
  944. SCM_SYSCALL (time (&utm_tmp.actime));
  945. else
  946. utm_tmp.actime = SCM_NUM2ULONG (2,actime);
  947. if (SCM_UNBNDP (modtime))
  948. SCM_SYSCALL (time (&utm_tmp.modtime));
  949. else
  950. utm_tmp.modtime = SCM_NUM2ULONG (3,modtime);
  951. SCM_SYSCALL (rv = utime (SCM_ROCHARS (pathname), &utm_tmp));
  952. if (rv != 0)
  953. SCM_SYSERROR;
  954. return SCM_UNSPECIFIED;
  955. }
  956. #undef FUNC_NAME
  957. SCM_DEFINE (scm_access, "access?", 2, 0, 0,
  958. (SCM path, SCM how),
  959. "Returns @code{#t} if @var{path} corresponds to an existing\n"
  960. "file and the current process\n"
  961. "has the type of access specified by @var{how}, otherwise \n"
  962. "@code{#f}.\n"
  963. "@var{how} should be specified\n"
  964. "using the values of the variables listed below. Multiple values can\n"
  965. "be combined using a bitwise or, in which case @code{#t} will only\n"
  966. "be returned if all accesses are granted.\n\n"
  967. "Permissions are checked using the real id of the current process,\n"
  968. "not the effective id, although it's the effective id which determines\n"
  969. "whether the access would actually be granted.\n\n"
  970. "@defvar R_OK\n"
  971. "test for read permission.\n"
  972. "@end defvar\n"
  973. "@defvar W_OK\n"
  974. "test for write permission.\n"
  975. "@end defvar\n"
  976. "@defvar X_OK\n"
  977. "test for execute permission.\n"
  978. "@end defvar\n"
  979. "@defvar F_OK\n"
  980. "test for existence of the file.\n"
  981. "@end defvar")
  982. #define FUNC_NAME s_scm_access
  983. {
  984. int rv;
  985. SCM_VALIDATE_ROSTRING (1,path);
  986. if (SCM_SUBSTRP (path))
  987. path = scm_makfromstr (SCM_ROCHARS (path), SCM_ROLENGTH (path), 0);
  988. SCM_VALIDATE_INUM (2,how);
  989. rv = access (SCM_ROCHARS (path), SCM_INUM (how));
  990. return SCM_NEGATE_BOOL(rv);
  991. }
  992. #undef FUNC_NAME
  993. SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
  994. (),
  995. "Returns an integer representing the current process ID.")
  996. #define FUNC_NAME s_scm_getpid
  997. {
  998. return SCM_MAKINUM ((unsigned long) getpid ());
  999. }
  1000. #undef FUNC_NAME
  1001. SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
  1002. (SCM str),
  1003. "Modifies the environment of the current process, which is\n"
  1004. "also the default environment inherited by child processes.\n\n"
  1005. "If @var{string} is of the form @code{NAME=VALUE} then it will be written\n"
  1006. "directly into the environment, replacing any existing environment string\n"
  1007. "with\n"
  1008. "name matching @code{NAME}. If @var{string} does not contain an equal\n"
  1009. "sign, then any existing string with name matching @var{string} will\n"
  1010. "be removed.\n\n"
  1011. "The return value is unspecified.")
  1012. #define FUNC_NAME s_scm_putenv
  1013. {
  1014. int rv;
  1015. char *ptr;
  1016. SCM_VALIDATE_ROSTRING (1,str);
  1017. /* must make a new copy to be left in the environment, safe from gc. */
  1018. ptr = malloc (SCM_LENGTH (str) + 1);
  1019. if (ptr == NULL)
  1020. SCM_MEMORY_ERROR;
  1021. strncpy (ptr, SCM_ROCHARS (str), SCM_LENGTH (str));
  1022. ptr[SCM_LENGTH(str)] = 0;
  1023. rv = putenv (ptr);
  1024. if (rv < 0)
  1025. SCM_SYSERROR;
  1026. return SCM_UNSPECIFIED;
  1027. }
  1028. #undef FUNC_NAME
  1029. #ifdef HAVE_SETLOCALE
  1030. SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
  1031. (SCM category, SCM locale),
  1032. "If @var{locale} is omitted, returns the current value of the specified\n"
  1033. "locale category \n"
  1034. "as a system-dependent string.\n"
  1035. "@var{category} should be specified using the values @code{LC_COLLATE},\n"
  1036. "@code{LC_ALL} etc.\n\n"
  1037. "Otherwise the specified locale category is set to\n"
  1038. "the string @var{locale}\n"
  1039. "and the new value is returned as a system-dependent string. If @var{locale}\n"
  1040. "is an empty string, the locale will be set using envirionment variables.")
  1041. #define FUNC_NAME s_scm_setlocale
  1042. {
  1043. char *clocale;
  1044. char *rv;
  1045. SCM_VALIDATE_INUM (1,category);
  1046. if (SCM_UNBNDP (locale))
  1047. {
  1048. clocale = NULL;
  1049. }
  1050. else
  1051. {
  1052. SCM_VALIDATE_ROSTRING (2,locale);
  1053. SCM_COERCE_SUBSTR (locale);
  1054. clocale = SCM_ROCHARS (locale);
  1055. }
  1056. rv = setlocale (SCM_INUM (category), clocale);
  1057. if (rv == NULL)
  1058. SCM_SYSERROR;
  1059. return scm_makfrom0str (rv);
  1060. }
  1061. #undef FUNC_NAME
  1062. #endif /* HAVE_SETLOCALE */
  1063. #ifdef HAVE_MKNOD
  1064. SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
  1065. (SCM path, SCM type, SCM perms, SCM dev),
  1066. "Creates a new special file, such as a file corresponding to a device.\n"
  1067. "@var{path} specifies the name of the file. @var{type} should\n"
  1068. "be one of the following symbols:\n"
  1069. "regular, directory, symlink, block-special, char-special,\n"
  1070. "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
  1071. "@var{dev} (an integer) specifies which device the special file refers\n"
  1072. "to. Its exact interpretation depends on the kind of special file\n"
  1073. "being created.\n\n"
  1074. "E.g.,\n"
  1075. "@example\n"
  1076. "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))"
  1077. "@end example\n"
  1078. "The return value is unspecified.")
  1079. #define FUNC_NAME s_scm_mknod
  1080. {
  1081. int val;
  1082. char *p;
  1083. int ctype = 0;
  1084. SCM_VALIDATE_ROSTRING (1,path);
  1085. SCM_VALIDATE_SYMBOL (2,type);
  1086. SCM_VALIDATE_INUM (3,perms);
  1087. SCM_VALIDATE_INUM (4,dev);
  1088. SCM_COERCE_SUBSTR (path);
  1089. p = SCM_CHARS (type);
  1090. if (strcmp (p, "regular") == 0)
  1091. ctype = S_IFREG;
  1092. else if (strcmp (p, "directory") == 0)
  1093. ctype = S_IFDIR;
  1094. else if (strcmp (p, "symlink") == 0)
  1095. ctype = S_IFLNK;
  1096. else if (strcmp (p, "block-special") == 0)
  1097. ctype = S_IFBLK;
  1098. else if (strcmp (p, "char-special") == 0)
  1099. ctype = S_IFCHR;
  1100. else if (strcmp (p, "fifo") == 0)
  1101. ctype = S_IFIFO;
  1102. else if (strcmp (p, "socket") == 0)
  1103. ctype = S_IFSOCK;
  1104. else
  1105. SCM_OUT_OF_RANGE (2,type);
  1106. SCM_SYSCALL (val = mknod(SCM_ROCHARS(path), ctype | SCM_INUM (perms),
  1107. SCM_INUM (dev)));
  1108. if (val != 0)
  1109. SCM_SYSERROR;
  1110. return SCM_UNSPECIFIED;
  1111. }
  1112. #undef FUNC_NAME
  1113. #endif /* HAVE_MKNOD */
  1114. #ifdef HAVE_NICE
  1115. SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
  1116. (SCM incr),
  1117. "Increment the priority of the current process by @var{incr}. A higher\n"
  1118. "priority value means that the process runs less often.\n"
  1119. "The return value is unspecified.")
  1120. #define FUNC_NAME s_scm_nice
  1121. {
  1122. SCM_VALIDATE_INUM (1,incr);
  1123. if (nice(SCM_INUM(incr)) != 0)
  1124. SCM_SYSERROR;
  1125. return SCM_UNSPECIFIED;
  1126. }
  1127. #undef FUNC_NAME
  1128. #endif /* HAVE_NICE */
  1129. #ifdef HAVE_SYNC
  1130. SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
  1131. (),
  1132. "Flush the operating system disk buffers.\n"
  1133. "The return value is unspecified.")
  1134. #define FUNC_NAME s_scm_sync
  1135. {
  1136. sync();
  1137. return SCM_UNSPECIFIED;
  1138. }
  1139. #undef FUNC_NAME
  1140. #endif /* HAVE_SYNC */
  1141. void
  1142. scm_init_posix ()
  1143. {
  1144. scm_add_feature ("posix");
  1145. #ifdef HAVE_GETEUID
  1146. scm_add_feature ("EIDs");
  1147. #endif
  1148. #ifdef WAIT_ANY
  1149. scm_sysintern ("WAIT_ANY", SCM_MAKINUM (WAIT_ANY));
  1150. #endif
  1151. #ifdef WAIT_MYPGRP
  1152. scm_sysintern ("WAIT_MYPGRP", SCM_MAKINUM (WAIT_MYPGRP));
  1153. #endif
  1154. #ifdef WNOHANG
  1155. scm_sysintern ("WNOHANG", SCM_MAKINUM (WNOHANG));
  1156. #endif
  1157. #ifdef WUNTRACED
  1158. scm_sysintern ("WUNTRACED", SCM_MAKINUM (WUNTRACED));
  1159. #endif
  1160. /* access() symbols. */
  1161. scm_sysintern ("R_OK", SCM_MAKINUM (R_OK));
  1162. scm_sysintern ("W_OK", SCM_MAKINUM (W_OK));
  1163. scm_sysintern ("X_OK", SCM_MAKINUM (X_OK));
  1164. scm_sysintern ("F_OK", SCM_MAKINUM (F_OK));
  1165. #ifdef LC_COLLATE
  1166. scm_sysintern ("LC_COLLATE", SCM_MAKINUM (LC_COLLATE));
  1167. #endif
  1168. #ifdef LC_CTYPE
  1169. scm_sysintern ("LC_CTYPE", SCM_MAKINUM (LC_CTYPE));
  1170. #endif
  1171. #ifdef LC_MONETARY
  1172. scm_sysintern ("LC_MONETARY", SCM_MAKINUM (LC_MONETARY));
  1173. #endif
  1174. #ifdef LC_NUMERIC
  1175. scm_sysintern ("LC_NUMERIC", SCM_MAKINUM (LC_NUMERIC));
  1176. #endif
  1177. #ifdef LC_TIME
  1178. scm_sysintern ("LC_TIME", SCM_MAKINUM (LC_TIME));
  1179. #endif
  1180. #ifdef LC_MESSAGES
  1181. scm_sysintern ("LC_MESSAGES", SCM_MAKINUM (LC_MESSAGES));
  1182. #endif
  1183. #ifdef LC_ALL
  1184. scm_sysintern ("LC_ALL", SCM_MAKINUM (LC_ALL));
  1185. #endif
  1186. #ifdef PIPE_BUF
  1187. scm_sysintern ("PIPE_BUF", scm_long2num (PIPE_BUF));
  1188. #endif
  1189. #include "libguile/cpp_sig_symbols.c"
  1190. #include "libguile/posix.x"
  1191. }
  1192. /*
  1193. Local Variables:
  1194. c-file-style: "gnu"
  1195. End:
  1196. */