system.c 32.9 KB
Newer Older
eg's avatar
eg committed
1 2
/*
 *
3
 * s y s t e m . c                              -- System relative primitives
eg's avatar
eg committed
4
 *
5
 * Copyright © 1994-2018 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
6
 *
eg's avatar
eg committed
7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Permission to use, copy, modify, distribute,and license this
 * software and its documentation for any purpose is hereby granted,
 * provided that existing copyright notices are retained in all
 * copies and that this notice is included verbatim in any
 * distributions.  No written agreement, license, or royalty fee is
 * required for any of the authorized uses.
 * This software is provided ``AS IS'' without express or implied
 * warranty.
 *
 *           Author: Erick Gallesio [eg@kaolin.unice.fr]
 *    Creation date: 29-Mar-1994 10:57
Erick's avatar
Erick committed
19
 * Last file update: 12-Oct-2018 17:04 (eg)
eg's avatar
eg committed
20 21 22 23 24 25 26 27
 */

#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
28
#include <time.h>
29
#include <locale.h>
eg's avatar
eg committed
30 31 32 33 34 35 36 37 38
#include "stklos.h"
#include "struct.h"

#ifndef MAXBUFF
#  define MAXBUFF 1024
#endif

#ifdef WIN32
#  define TIME_DIV_CONST 10000
39
#else
eg's avatar
eg committed
40 41 42 43 44
#  define TIME_DIV_CONST 1000
#endif


static SCM exit_procs = STk_nil; /* atexit functions */
45
static SCM date_type, time_type;
eg's avatar
eg committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

/******************************************************************************
 *
 * Utilities
 *
 ******************************************************************************/
static void error_bad_path(SCM path)
{
  STk_error("~S is a bad pathname", path);
}

static void error_bad_string(SCM path)
{
  STk_error("~S is a bad string", path);
}

static void error_bad_int_or_out_of_bounds(SCM val)
{
  STk_error("bad integer ~S (or out of range)", val);
}

static void error_win32_primitive(void)
{
  STk_error("Win32 primitive not available on this system");
}

72 73 74 75
static void error_posix(SCM obj1, SCM obj2)
{
  if (obj1 && obj2)
    STk_error("%s: ~A ~A", strerror(errno), obj1, obj2);
76
  else
77 78 79 80 81
    if (obj1)
      STk_error("%s: ~A", strerror(errno), obj1);
    else
      STk_error("%s", strerror(errno));
}
eg's avatar
eg committed
82 83 84 85

static SCM my_access(SCM path, int mode)
{
  if (!STRINGP(path)) error_bad_path(path);
86 87 88 89 90 91 92
  return MAKE_BOOLEAN(access(STk_expand_file_name(STRING_CHARS(path)), mode) == 0);
}

static int my_stat(SCM path, struct stat *s)
{
  if (!STRINGP(path)) error_bad_path(path);
  return stat(STk_expand_file_name(STRING_CHARS(path)), s);
eg's avatar
eg committed
93 94
}

Erick Gallesio's avatar
Erick Gallesio committed
95 96 97 98 99 100
static int my_lstat(SCM path, struct stat *s)
{
  if (!STRINGP(path)) error_bad_path(path);
  return lstat(STk_expand_file_name(STRING_CHARS(path)), s);
}

eg's avatar
eg committed
101

102

eg's avatar
eg committed
103 104 105
int STk_dirp(const char *path)
{
  struct stat buf;
106 107

  if (stat(path, &buf) >= 0)
eg's avatar
eg committed
108 109 110 111 112 113 114 115 116 117 118
    return S_ISDIR(buf.st_mode);
  return FALSE;
}



#ifdef FIXME
//EG: void STk_whence(char *exec, char *path)
//EG: {
//EG:   char *p, *q, dir[MAX_PATH_LENGTH];
//EG:   struct stat buf;
119
//EG:
eg's avatar
eg committed
120 121 122 123
//EG:   if (ISABSOLUTE(exec)) {
//EG:     strncpy(path, exec, MAX_PATH_LENGTH);
//EG:     return;
//EG:   }
124
//EG:
eg's avatar
eg committed
125 126 127 128 129 130 131 132
//EG:   /* the executable path may be specified by relative path from the cwd. */
//EG:   /* Patch suggested by Shiro Kawai <shiro@squareusa.com> */
//EG:   if (strchr(exec, DIRSEP) != NULL) {
//EG:     getcwd(dir, MAX_PATH_LENGTH);
//EG:     sprintf(dir + strlen(dir), "%c%s", DIRSEP, exec);
//EG:     absolute(dir, path);
//EG:     return;
//EG:   }
133 134
//EG:
//EG: #ifdef FREEBSD
eg's avatar
eg committed
135 136 137 138
//EG:   /* I don't understand why this is needed */
//EG:   if (access(path, X_OK) == 0) {
//EG:     stat(path, &buf);
//EG:     if (!S_ISDIR(buf.st_mode)) return;
139
//EG:   }
eg's avatar
eg committed
140
//EG: #endif
141
//EG:
eg's avatar
eg committed
142 143 144 145
//EG:   p = getenv("PATH");
//EG:   if (p == NULL) {
//EG:     p = "/bin:/usr/bin";
//EG:   }
146
//EG:
eg's avatar
eg committed
147 148 149 150
//EG:   while (*p) {
//EG:     /* Copy the stuck of path in dir */
//EG:     for (q = dir; *p && *p != PATHSEP; p++, q++) *q = *p;
//EG:     *q = '\000';
151 152
//EG:
//EG:     if (!*dir) {
eg's avatar
eg committed
153 154 155 156 157 158
//EG:       /* patch suggested by Erik Ostrom <eostrom@vesuvius.ccs.neu.edu> */
//EG:       getcwd(path, MAX_PATH_LENGTH);
//EG:       sprintf(path + strlen(path), "%c%s", DIRSEP, exec);
//EG:     }
//EG:     else
//EG:       sprintf(path, "%s%c%s", dir, DIRSEP, exec);
159
//EG:
eg's avatar
eg committed
160 161 162 163 164
//EG:     sprintf(path, "%s%c%s", dir, DIRSEP, exec);
//EG:     if (access(path, X_OK) == 0) {
//EG:       stat(path, &buf);
//EG:       if (!S_ISDIR(buf.st_mode)) return;
//EG:     }
165
//EG:
eg's avatar
eg committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
//EG:     /* Try next path */
//EG:     if (*p) p++;
//EG:   }
//EG:   /* Not found. Set path to "" */
//EG:   path[0] = '\0';
//EG: }
#endif

/******************************************************************************
 *
 * Primitives
 *
 ******************************************************************************/

/*
<doc EXT winify-file-name
 * (winify-file-name fn)
 *
 * On Win32 system, when compiled with the Cygwin environment,
 * file names are internally represented in a POSIX-like internal form.
186
 * |Winify-file-bame| permits to obtain back the Win32 name of an interned
eg's avatar
eg committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200
 * file name
 * @lisp
 * (winify-file-name "/tmp")
 *     => "C:\\\\cygwin\\\\tmp"
 * (list (getcwd) (winify-file-name (getcwd)))
 *     => ("//saxo/homes/eg/Projects/STklos"
 *         "\\\\\\\\saxo\\\\homes\\\\eg\\\\Projects\\\\STklos")
 * @end lisp
doc>
*/
DEFINE_PRIMITIVE("winify-file-name", winify_filename, subr1, (SCM f))
{
#ifdef WIN32
  char expanded[2 * MAX_PATH_LENGTH];
201

eg's avatar
eg committed
202
  if (!STRINGP(f)) error_bad_string(f);
203

eg's avatar
eg committed
204 205
  cygwin_conv_to_win32_path(STRING_CHARS(f), expanded);
  return STk_Cstring2string(expanded);
206
#else
eg's avatar
eg committed
207 208 209 210 211 212 213 214 215 216 217 218
  error_win32_primitive();
  return STk_void;
#endif
}


/*
<doc EXT posixify-file-name
 * (posixify-file-name fn)
 *
 * On Win32 system, when compiled with the Cygwin environment,
 * file names are internally represented in a POSIX-like internal form.
219
 * |posixify-file-bame| permits to obtain the interned file name from
eg's avatar
eg committed
220 221 222 223 224 225 226 227 228 229 230 231
 * its external form.
 * file name
 * @lisp
 * (posixify-file-name "C:\\\\cygwin\\\\tmp")
 *        => "/tmp"
 * @end lisp
doc>
*/
DEFINE_PRIMITIVE("posixify-file-name", posixify_filename, subr1, (SCM f))
{
#ifdef WIN32
  char expanded[2 * MAX_PATH_LENGTH];
232

eg's avatar
eg committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246
  if (!STRINGP(f)) error_bad_string(f);

  cygwin_conv_to_posix_path(STRING_CHARS(f), expanded);
  return STk_Cstring2string(expanded);
#else
  error_win32_primitive();
  return STk_void;
#endif
}


/*
<doc EXT expand-file-name
 * (expand-file-name path)
247
 *
eg's avatar
eg committed
248
 * |Expand-file-name| expands the filename given in |path| to
249
 * an absolute path.
eg's avatar
eg committed
250 251 252 253 254
 * @lisp
 *   ;; Current directory is ~eg/stklos (i.e. /users/eg/stklos)
 *   (expand-file-name "..")            => "/users/eg"
 *   (expand-file-name "~eg/../eg/bin") => "/users/eg/bin"
 *   (expand-file-name "~/stklos)"      => "/users/eg/stk"
255
 * @end lisp
eg's avatar
eg committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
doc>
*/

DEFINE_PRIMITIVE("expand-file-name", expand_fn, subr1, (SCM s))
{
  if (!STRINGP(s)) error_bad_string(s);
  return STk_Cstring2string(STk_expand_file_name(STRING_CHARS(s)));
}


/*
<doc EXT canonical-file-name
 * (canonical-file-name path)
 *
 * Expands all symbolic links in |path| and returns its canonicalized
271 272 273
 * absolute path name. The resulting path does not have symbolic links.
 * If |path| doesn't designate a valid path name, |canonical-file-name|
 * returns |#f|.
eg's avatar
eg committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
doc>
*/
DEFINE_PRIMITIVE("canonical-file-name", canonical_path, subr1, (SCM str))
{
  if (!STRINGP(str)) error_bad_string(str);
  return STk_resolve_link(STRING_CHARS(str), 0);
}

/*
<doc EXT getcwd
 * (getcwd)
 *
 * Returns a string containing the current working directory.
doc>
*/
DEFINE_PRIMITIVE("getcwd", getcwd, subr0, (void))
{
  char buf[MAX_PATH_LENGTH], *s;
  SCM z;

  s = getcwd(buf, MAX_PATH_LENGTH);
295
  if (!s)
296
    error_posix(NULL, NULL);
eg's avatar
eg committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  z = STk_Cstring2string(buf);

  return z;
}


/*
<doc EXT chdir
 * (chdir dir)
 *
 * Changes the current directory to the directory given in string |dir|.
doc>
*/
DEFINE_PRIMITIVE("chdir", chdir, subr1, (SCM s))
{
  if (!STRINGP(s)) error_bad_path(s);
313

314 315
  if (chdir(STk_expand_file_name(STRING_CHARS(s))) != 0)
    error_posix(s, NULL);
316

eg's avatar
eg committed
317 318 319
  return STk_void;
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/*
<doc EXT make-directory
 * (make-directory dir)
 *
 * Create a directory with name |dir|.
doc>
*/
DEFINE_PRIMITIVE("make-directory", make_directory, subr1, (SCM path))
{
  mode_t mask;

  if (!STRINGP(path)) error_bad_path(path);
  umask(mask = umask(0));
  if (mkdir(STRING_CHARS(path), 0777) != 0)
    error_posix(path, NULL);
  return STk_void;
}

/*
Erick's avatar
Erick committed
339
<doc EXT delete-directory remove-directory
Erick Gallesio's avatar
Erick Gallesio committed
340
 * (remove-directory dir)
Erick's avatar
Erick committed
341
 * (delete-directory dir)
342 343
 *
 * Delete the directory with name |dir|.
Erick's avatar
Erick committed
344 345
 * @l
 * ,(bold "Note:") The name |remove-directory| is kept for compatibility.
346 347
doc>
*/
Erick Gallesio's avatar
Erick Gallesio committed
348
DEFINE_PRIMITIVE("remove-directory", remove_directory, subr1, (SCM path))
349 350 351 352 353 354 355
{
  if (!STRINGP(path)) error_bad_path(path);
  if (rmdir(STRING_CHARS(path)) != 0)
    error_posix(path, NULL);
  return STk_void;
}

eg's avatar
eg committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
/*
<doc EXT getpid
 * (getpid)
 *
 * Returns the system process number of the current program (i.e. the
 * Unix ,(emph "pid")) as an integer.
doc>
*/
DEFINE_PRIMITIVE("getpid", getpid, subr0, (void))
{
  return (MAKE_INT((int) getpid()));
}


/*
<doc EXT system
 * (system string)
 *
 * Sends the given |string| to the system shell |/bin/sh|. The result of
 * |system| is the integer status code the shell returns.
doc>
*/
DEFINE_PRIMITIVE("system", system, subr1, (SCM com))
{
  if (!STRINGP(com)) error_bad_string(com);
  return MAKE_INT(system(STRING_CHARS(com)));
}

/*
385
<doc EXT file-is-directory? file-is-regular? file-is-writable? file-is-readable? file-is-executable?
eg's avatar
eg committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
 * (file-is-directory?  string)
 * (file-is-regular?    string)
 * (file-is-readable?   string)
 * (file-is-writable?   string)
 * (file-is-executable? string)
 *
 * Returns |#t| if the predicate is true for the path name given in
 * |string|; returns |#f| otherwise (or if |string| denotes a file
 * which does not exist).
doc>
 */
DEFINE_PRIMITIVE("file-is-directory?", file_is_directoryp, subr1, (SCM f))
{
  struct stat info;

401
  if (my_stat(f, &info) != 0) return STk_false;
eg's avatar
eg committed
402 403 404 405 406 407 408 409
  return MAKE_BOOLEAN((S_ISDIR(info.st_mode)));
}


DEFINE_PRIMITIVE("file-is-regular?", file_is_regularp, subr1, (SCM f))
{
  struct stat info;

410
  if (my_stat(f, &info) != 0) return STk_false;
eg's avatar
eg committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  return MAKE_BOOLEAN((S_ISREG(info.st_mode)));
}


DEFINE_PRIMITIVE("file-is-readable?", file_is_readablep, subr1, (SCM f))
{
  return my_access(f, R_OK);
}


DEFINE_PRIMITIVE("file-is-writable?", file_is_writablep, subr1, (SCM f))
{
  return my_access(f, W_OK);
}


DEFINE_PRIMITIVE("file-is-executable?", file_is_executablep, subr1, (SCM f))
{
  return my_access(f, X_OK);
}


433 434 435 436 437 438 439 440 441

/*
<doc R7RS file-exists?
 * (file-exists? string)
 *
 * Returns |#t| if the path name given in |string| denotes an existing file;
 * returns |#f| otherwise.
doc>
 */
eg's avatar
eg committed
442 443
DEFINE_PRIMITIVE("file-exists?", file_existsp, subr1, (SCM f))
{
Erick Gallesio's avatar
Erick Gallesio committed
444 445 446
  struct stat info;

  return MAKE_BOOLEAN(my_lstat(f, &info) == 0);
eg's avatar
eg committed
447 448 449 450 451 452 453
}

/*
<doc EXT file-size
 * (file-size string)
 *
 * Returns the size of the file whose path name is given in
454
 * |string|.If |string| denotes a file which does not exist,
eg's avatar
eg committed
455 456 457 458 459 460 461 462
 * |file-size| returns |#f|.
doc>
 */

DEFINE_PRIMITIVE("file-size", file_size, subr1, (SCM f))
{
  struct stat info;

463
  if (my_stat(f, &info) != 0) return STk_false;
eg's avatar
eg committed
464 465 466 467 468 469 470
  return STk_long2integer((long) info.st_size);
}

/*
<doc EXT glob
 * (glob pattern ...)
 *
471
 * |Glob| performs file name ``globbing'' in a fashion similar to the
eg's avatar
eg committed
472 473 474
 * csh shell. |Glob| returns a list of the filenames that match at least
 * one of |pattern| arguments.  The |pattern| arguments may contain
 * the following special characters:
475
 * ,(itemize
eg's avatar
eg committed
476 477
 * (item [|?| Matches any single character.])
 * (item [|*| Matches any sequence of zero or more characters.])
478 479
 * (item [|\[chars\]| Matches any single character in |chars|.
 * If chars contains a sequence of the form |a-b| then any character
eg's avatar
eg committed
480 481 482 483
 * between |a| and |b| (inclusive) will match.])
 * (item [|\\x| Matches the character |x|.])
 * (item [|{a,b,...}| Matches any of the strings |a|, |b|, etc.])
 * )
484 485 486
 *
 * As with csh, a '.' at the beginning of a file's name or just after
 * a '/ must be matched explicitly or with a |@{@}| construct.
eg's avatar
eg committed
487
 * In addition, all '/' characters must be matched explicitly.
488
 * @l
eg's avatar
eg committed
489 490 491 492
 * If the first character in a pattern is '~' then it refers to
 * the home directory of the user whose name follows the '~'.
 * If the '~' is followed immediately by `/' then the value of
 * the environment variable HOME is used.
493
 * @l
eg's avatar
eg committed
494 495 496
 * |Glob| differs from csh globbing in two ways.  First, it does not
 * sort its result list (use the |sort| procedure if you want the list
 * sorted).
497 498
 * Second, |glob| only returns the names of files that actually exist;
 * in csh no check for existence is made unless a pattern contains a
eg's avatar
eg committed
499 500 501 502 503 504 505 506 507 508
 * |?|, |*|, or |\[\]| construct.
doc>
*/
DEFINE_PRIMITIVE("glob", glob, vsubr, (int argc, SCM *argv))
{
  return STk_do_glob(argc, argv);
}


/*
Erick's avatar
Erick committed
509
<doc R7RS delete-file remove-file
Erick's avatar
Erick committed
510
 * (delete-file string)
eg's avatar
eg committed
511 512
 *
 * Removes the file whose path name is given in |string|.
Erick's avatar
Erick committed
513 514 515 516
 * The result of |delete-file| is ,(emph "void").
 * @l
 * This function is also called |remove-file| for compatibility 
 * reasons. ,(index "remove-file")
eg's avatar
eg committed
517 518
doc>
*/
Erick's avatar
Erick committed
519 520 521 522 523 524 525 526 527 528 529
#define do_remove(filename)                             \
  if (!STRINGP(filename)) error_bad_string(filename);   \
  if (remove(STRING_CHARS(filename)) != 0)              \
    error_posix(filename, NULL);                        \
  return STk_void;                                      \

DEFINE_PRIMITIVE("delete-file", delete_file, subr1, (SCM filename))
{
  do_remove(filename);
}

eg's avatar
eg committed
530 531
DEFINE_PRIMITIVE("remove-file", remove_file, subr1, (SCM filename))
{
Erick's avatar
Erick committed
532
  do_remove(filename);
eg's avatar
eg committed
533
}
534

eg's avatar
eg committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548

/*
<doc EXT rename-file
 * (rename-file string1 string2)
 *
 * Renames the file whose path-name is |string1| to a file whose path-name is
 * |string2|. The result of |rename-file| is ,(emph "void").
doc>
*/
DEFINE_PRIMITIVE("rename-file", rename_file, subr2, (SCM filename1, SCM filename2))
{
  if (!STRINGP(filename1)) error_bad_string(filename1);
  if (!STRINGP(filename2)) error_bad_string(filename2);
  if (rename(STRING_CHARS(filename1), STRING_CHARS(filename2)) != 0)
549
    error_posix(filename1, filename2);
eg's avatar
eg committed
550 551 552
  return STk_void;
}

553 554 555 556 557

/*
<doc EXT directory-files
 * (directory-files path)
 *
558 559
 * Returns the list of the files in the directory |path|. Directories ,(q ".")
 * and ,(q "..") don't appear in the result.
560 561 562 563 564 565 566 567 568 569 570 571 572
doc>
*/
DEFINE_PRIMITIVE("directory-files", directory_files, subr1, (SCM dirname))
{
  char *path;
  DIR* dir;
  SCM res = STk_nil;
  struct dirent *d;

  if (!STRINGP(dirname)) error_bad_string(dirname);
  path = STk_expand_file_name(STRING_CHARS(dirname));
  dir  = opendir(path);
  if (!dir) error_posix(dirname, NULL);
573

574
  for (d = readdir(dir); d ; d = readdir(dir)) {
575
    if (d->d_name[0] == '.')
576
      if ((d->d_name[1] == '\0') || (d->d_name[1] == '.' && d->d_name[2] == '\0'))
577
        continue;
578 579 580 581 582
    res = STk_cons(STk_Cstring2string(d->d_name), res);
  }
  closedir(dir);
  return STk_dreverse(res);
}
583 584 585



eg's avatar
eg committed
586 587 588 589 590
/*
<doc EXT copy-file
 * (copy-file string1 string2)
 *
 * Copies the file whose path-name is |string1| to a file whose path-name is
591
 * |string2|. If the file |string2| already exists, its content prior
eg's avatar
eg committed
592 593 594 595 596 597 598 599 600 601 602
 * the call to |copy-file| is lost. The result of |copy-file| is ,(emph "void").
doc>
*/
DEFINE_PRIMITIVE("copy-file", copy_file, subr2, (SCM filename1, SCM filename2))
{
  char buff[1024];
  int f1, f2, n;

  /* Should I use sendfile on Linux here? */
  if (!STRINGP(filename1)) error_bad_string(filename1);
  if (!STRINGP(filename2)) error_bad_string(filename2);
603

eg's avatar
eg committed
604
  f1 = open(STRING_CHARS(filename1), O_RDONLY);
Erick Gallesio's avatar
Erick Gallesio committed
605
  if (f1 == -1) error_posix(filename1, NULL);
eg's avatar
eg committed
606
  f2 = open(STRING_CHARS(filename2), O_WRONLY|O_CREAT|O_TRUNC, 0666);
Erick Gallesio's avatar
Erick Gallesio committed
607
  if (f2 == -1) error_posix(filename2, NULL);
eg's avatar
eg committed
608 609 610

  while ((n = read(f1, buff, MAXBUFF)) > 0) {
    if ((n < 0) || (write(f2, buff, n) < n)) {
611
      close(f1); close(f2);
612
      error_posix(filename1, filename2);
eg's avatar
eg committed
613 614
    }
  }
615

eg's avatar
eg committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  close(f1); close(f2);
  return STk_void;
}


/*
<doc EXT temporary-file-name
 * (temporary-file-name)
 *
 * Generates a unique temporary file name. The value returned by
 * |temporary-file-name| is the newly generated name of |#f|
 * if a unique name cannot be generated.
doc>
*/
DEFINE_PRIMITIVE("temporary-file-name", tmp_file, subr0, (void))
{
#ifdef WIN32
  char buff[MAX_PATH_LENGTH], *s;
634

eg's avatar
eg committed
635 636 637 638
  s = tmpnam(buff);
  return s ? STk_Cstring2string(s) : STk_false;
#else
  static int cpt=0;
Erick's avatar
.  
Erick committed
639
  int pid = (int) getpid();
eg's avatar
eg committed
640
  char buff[MAX_PATH_LENGTH];
641
  MUT_DECL(tmpnam_mutex);
eg's avatar
eg committed
642

643
  MUT_LOCK(tmpnam_mutex);
eg's avatar
eg committed
644
  for ( ; ; ) {
Erick's avatar
.  
Erick committed
645
    sprintf(buff, "/tmp/stklos%05d-%05x", pid, cpt++);
646
    if (cpt > 100000)           /* arbitrary limit to avoid infinite search */
647
      return STk_false;
eg's avatar
eg committed
648 649
    if (access(buff, F_OK) == -1) break;
  }
650
  MUT_UNLOCK(tmpnam_mutex);
eg's avatar
eg committed
651 652 653 654 655 656 657 658 659 660

  return STk_Cstring2string(buff);
#endif
}


/*
<doc EXT register-exit-function!
 * (register-exit-function! proc)
 *
661 662 663 664 665
 * This function registers |proc| as an exit function. This function will
 * be called when the program exits. When called, |proc| will be passed one
 * parmater which is the status given to the |exit| function (or 0 if the
 * programe terminates normally). The result of  |register-exit-function!|
 * is undefined.
eg's avatar
eg committed
666 667 668 669 670
 * @lisp
 * (let* ((tmp (temporary-file-name))
 *        (out (open-output-file tmp)))
 *   (register-exit-function! (lambda (n)
 *                              (when (zero? n)
Erick's avatar
Erick committed
671
 *                                (delete-file tmp))))
eg's avatar
eg committed
672 673 674 675
 *   out)
 * @end lisp
doc>
*/
676
MUT_DECL(at_exit_mutex);         /* The exit mutex */
677

eg's avatar
eg committed
678 679 680
DEFINE_PRIMITIVE("register-exit-function!", at_exit, subr1, (SCM proc))
{
  if (STk_procedurep(proc) == STk_false) STk_error("bad procedure ~S", proc);
681 682

  MUT_LOCK(at_exit_mutex);
eg's avatar
eg committed
683
  exit_procs = STk_cons(proc, exit_procs);
684
  MUT_UNLOCK(at_exit_mutex);
eg's avatar
eg committed
685 686 687 688
  return STk_void;
}


689 690 691
DEFINE_PRIMITIVE("%pre-exit", pre_exit, subr1, (SCM retcode))
{
  /* Execute the at-exit handlers */
692
  MUT_LOCK(at_exit_mutex);
693 694
  for (  ; !NULLP(exit_procs); exit_procs = CDR(exit_procs))
    STk_C_apply(CAR(exit_procs), 1, retcode);
695 696
  MUT_UNLOCK(at_exit_mutex);

697 698 699 700 701 702
  /* Flush all bufers */
  STk_close_all_ports();

  return STk_void;
}

eg's avatar
eg committed
703 704
/*
<doc EXT exit
705
 * (exit)
eg's avatar
eg committed
706 707 708 709
 * (exit ret-code)
 *
 * Exits the program with the specified integer return code. If |ret-code|
 * is omitted, the program terminates with a return code of 0.
710
 * If  program has registered exit functions with |register-exit-function!|,
eg's avatar
eg committed
711
 * they are called (in an order which is the reverse of their call order).
712 713 714
 * @l
 * ,(bold "Note:") The ,(stklos) |exit| primitive accepts also an
 * integer value as parameter (,(rseven) accepts only a boolean).
eg's avatar
eg committed
715 716
doc>
*/
717
DEFINE_PRIMITIVE("exit", exit, subr01, (SCM retcode))
eg's avatar
eg committed
718 719
{
  long ret = 0;
720
  SCM cond;
721

eg's avatar
eg committed
722
  if (retcode) {
723 724 725 726 727 728
    if (BOOLEANP(retcode)) {
      ret = (retcode != STk_true);
    } else {
      ret = STk_integer_value(retcode);
      if (ret == LONG_MIN) STk_error("bad return code ~S", retcode);
    }
eg's avatar
eg committed
729 730
  }

731 732
  /* Raise a &exit-r7rs condition  with the numeric value of the exit code*/
  cond = STk_make_C_cond(STk_exit_condition, 1, MAKE_INT(ret));
Erick's avatar
Erick committed
733
  STk_raise(cond);
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

  return STk_void; /* never reached */
}

/*
<doc EXT emergency-exit
 * (emergency-exit)
 * (emergency-exit ret-code)
 *
 * Terminates the program without running any outstanding
 * dynamic-wind ,(emph "after") procedures and communicates an exit
 * value to the operating system in the same manner as |exit|.
 * @l
 * ,(bold "Note:") The ,(stklos) |emergency-exit| primitive accepts also an
 * integer value as parameter (,(rseven) accepts only a boolean).
doc>
*/
DEFINE_PRIMITIVE("emergency-exit", emergency_exit, subr01, (SCM retcode))
{
  long ret = 0;

  if (retcode) {
    if (BOOLEANP(retcode)) {
      ret = (retcode != STk_true);
    } else {
      ret = STk_integer_value(retcode);
      if (ret == LONG_MIN) STk_error("bad return code ~S", retcode);
    }
  }
  _exit(ret);
764

eg's avatar
eg committed
765 766 767 768 769 770 771 772 773
  return STk_void; /* never reached */
}


/*
<doc EXT machine-type
 * (machine-type)
 *
 * Returns a string identifying the kind of machine which is running the
774
 * program. The result string is of the form
eg's avatar
eg committed
775 776 777 778 779 780 781 782 783 784 785 786 787
 * |[os-name]-[os-version]-[processor-type]|.
doc>
*/
DEFINE_PRIMITIVE("machine-type", machine_type, subr0, (void))
{
  return STk_Cstring2string(BUILD_MACHINE);
}


#ifdef FIXME
//EG: #ifndef HZ
//EG: #define HZ 60.0
//EG: #endif
788
//EG:
eg's avatar
eg committed
789 790
//EG: #ifdef CLOCKS_PER_SEC
//EG: #  define TIC CLOCKS_PER_SEC
791
//EG: #else
eg's avatar
eg committed
792 793
//EG: #  define TIC HZ
//EG: #endif
794
//EG:
eg's avatar
eg committed
795 796 797 798
//EG: PRIMITIVE STk_get_internal_info(void)
//EG: {
//EG:   SCM z = STk_makevect(7, STk_nil);
//EG:   long allocated, used, calls;
799
//EG:
eg's avatar
eg committed
800
//EG:   /* The result is a vector which contains
801 802
//EG:    *      0 The total cpu used in ms
//EG:    *      1 The number of cells currently in use.
eg's avatar
eg committed
803
//EG:    *    2 Total number of allocated cells
804 805
//EG:    *      3 The number of cells used since the last call to get-internal-info
//EG:    *      4 Number of gc calls
eg's avatar
eg committed
806
//EG:    *    5 Total time used in the gc
807
//EG:    *      6 A boolean indicating if Tk is initialized
eg's avatar
eg committed
808
//EG:    */
809
//EG:
eg's avatar
eg committed
810
//EG:   STk_gc_count_cells(&allocated, &used, &calls);
811
//EG:
eg's avatar
eg committed
812 813 814 815 816 817 818 819 820 821 822
//EG:   VECT(z)[0] = STk_makenumber(STk_my_time());
//EG:   VECT(z)[1] = STk_makeinteger(used);
//EG:   VECT(z)[2] = STk_makeinteger(allocated);
//EG:   VECT(z)[3] = STk_makenumber((double) STk_alloc_cells);
//EG:   VECT(z)[4] = STk_makeinteger(calls);
//EG:   VECT(z)[5] = STk_makenumber((double) STk_total_gc_time);
//EG: #ifdef USE_TK
//EG:   VECT(z)[6] = Tk_initialized ? STk_true: STk_false;
//EG: #else
//EG:   VECT(z)[6] = STk_false;
//EG: #endif
823
//EG:
eg's avatar
eg committed
824 825 826 827 828 829 830 831 832
//EG:   STk_alloc_cells = 0;
//EG:   return z;
//EG: }
#endif


/*
<doc EXT date
 * (date)
833
 *
eg's avatar
eg committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
 * Returns the current date in a string
doc>
*/
DEFINE_PRIMITIVE("date", date, subr0, (void))
{
  time_t t   = time(NULL);
  char * res;

  res = ctime(&t);
  res[strlen(res)-1] = '\0'; /* to "delete" the newline added by asctime */
  return STk_Cstring2string(res);
}


/*
849
<doc EXT clock
eg's avatar
eg committed
850 851 852 853 854 855 856 857
 * (clock)
 *
 * Returns an approximation of processor time, in milliseconds, used so far by the
 * program.
doc>
 */
DEFINE_PRIMITIVE("clock", clock, subr0, (void))
{
858
  return STk_double2real((double) clock() /
859
                         CLOCKS_PER_SEC * (double) TIME_DIV_CONST);
eg's avatar
eg committed
860 861
}

862

eg's avatar
eg committed
863
/*
864
<doc EXT current-seconds
865
 * (current-seconds)
eg's avatar
eg committed
866
 *
867
 * Returns the time since the Epoch (that is 00:00:00 UTC, January 1, 1970),
eg's avatar
eg committed
868
 * measured in seconds.
869 870 871 872 873 874
 * @l
 * @bold("Note"): This ,(stklos) function should not be confused with
 * the ,(rseven)  primitive |current-second| which returns an inexact number
 * and whose result is expressed using  the International Atomic Time
 * instead of UTC.
 *
eg's avatar
eg committed
875 876
doc>
 */
877
DEFINE_PRIMITIVE("current-seconds", current_seconds, subr0, (void))
eg's avatar
eg committed
878 879 880 881
{
  return STk_ulong2integer(time(NULL));
}

882 883 884 885 886 887 888 889 890
/*
<doc R7RS current-second
 * (current-second)
 *
 * Returns an inexact number representing the current time on the
 * International Atomic Time (TAI) scale.  The value 0.0 represents
 * midnight on January 1, 1970 TAI (equivalent to ten seconds before
 * midnight Universal Time) and the value 1.0 represents one TAI
 * second later.
Erick's avatar
Erick committed
891
doc>
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
 */

/* Offset: https://fr.wikipedia.org/wiki/Temps_atomique_international */
#define TAI_OFFSET +37.0L

DEFINE_PRIMITIVE("current-second", current_second, subr0, (void))
{
  /* R7RS states: Neither high accuracy nor high precision are
   * required; in particular, returning Coordinated Universal Time plus
   * a suitable constant might be the best an implementation can do.
   */
  struct timespec now;

  clock_gettime(CLOCK_REALTIME, &now);
  return STk_double2real(TAI_OFFSET +
                         (double) now.tv_sec +
                         1.0E-9 * (double) now.tv_nsec);
}


eg's avatar
eg committed
912
/*
Erick's avatar
Erick committed
913
<doc EXT current-time
914
 * (current-time)
915
 *
916
 * Returns a time object corresponding to the current time.
eg's avatar
eg committed
917 918
doc>
*/
919
DEFINE_PRIMITIVE("current-time", current_time, subr0, (void))
eg's avatar
eg committed
920
{
921
  struct timespec now;
922
  SCM argv[3];
eg's avatar
eg committed
923

924
  clock_gettime(CLOCK_REALTIME, &now);
925

926 927
  argv[2] = time_type;
  argv[1] =  STk_long2integer(now.tv_sec);
928
  argv[0] =  STk_long2integer(now.tv_nsec);
929
  return STk_make_struct(3, &argv[2]);
eg's avatar
eg committed
930 931
}

932 933


eg's avatar
eg committed
934 935 936
/*
<doc EXT sleep
 * (sleep n)
937 938
 *
 * Suspend the execution of the program for at |ms| milliseconds. Note that due
eg's avatar
eg committed
939
 * to system clock resolution, the pause may be a little bit longer. If a
940 941
 * signal arrives during the pause, the execution may be resumed.
 *
eg's avatar
eg committed
942 943 944 945 946 947 948
doc>
*/
DEFINE_PRIMITIVE("sleep", sleep, subr1, (SCM ms))
{
  long n = STk_integer_value(ms);
  struct timespec ts;

949
  if (n == LONG_MIN)
eg's avatar
eg committed
950
    error_bad_int_or_out_of_bounds(ms);
951

952 953
  ts.tv_sec  = n / 1000;
  ts.tv_nsec = (n % 1000) * 1000000;
eg's avatar
eg committed
954 955 956 957 958 959 960 961 962

  nanosleep(&ts, NULL);
  return STk_void;
}


/*
<doc EXT seconds->date
 * (seconds->date n)
963
 *
eg's avatar
eg committed
964 965 966
 * Convert the date |n| expressed as a number of seconds since the Epoch
 * to a date.
doc>
967
*/
968
DEFINE_PRIMITIVE("%seconds->date", seconds2date, subr1, (SCM seconds))
eg's avatar
eg committed
969
{
970
  int overflow;
eg's avatar
eg committed
971 972 973 974 975
  SCM argv[11];
  struct tm *t;
  time_t tt;

  tt = (time_t) STk_integer2int32(seconds, &overflow);
976

eg's avatar
eg committed
977
  if (overflow) error_bad_int_or_out_of_bounds(seconds);
978

eg's avatar
eg committed
979 980 981 982 983 984 985 986 987 988 989 990
  t = localtime(&tt);
  argv[10]  = date_type;
  argv[9]  = MAKE_INT(t->tm_sec);
  argv[8]  = MAKE_INT(t->tm_min);
  argv[7]  = MAKE_INT(t->tm_hour);
  argv[6]  = MAKE_INT(t->tm_mday);
  argv[5]  = MAKE_INT(t->tm_mon + 1);
  argv[4]  = MAKE_INT(t->tm_year + 1900);
  argv[3]  = MAKE_INT(t->tm_wday);
  argv[2]  = MAKE_INT(t->tm_yday + 1);
  argv[1]  = MAKE_INT(t->tm_isdst);
#ifdef DARWIN
991
  argv[0]  = MAKE_INT(0);       /* Cannot figure how to find the timezone */
992
#else
eg's avatar
eg committed
993 994 995 996 997 998 999 1000 1001
  argv[0]  = STk_long2integer(timezone);
#endif
  return STk_make_struct(11, &argv[10]);
}


/*
<doc EXT date->seconds
 * (date->seconds d)
1002
 *
eg's avatar
eg committed
1003 1004
 * Convert the date |d| to the number of seconds since the ,(emph "Epoch").
doc>
1005
*/
eg's avatar
eg committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
DEFINE_PRIMITIVE("date->seconds", date2seconds, subr1, (SCM date))
{
  struct tm t;
  time_t n;
  SCM *p;

  if (!STRUCTP(date) || STRUCT_TYPE(date) != date_type)
    STk_error("bad date ~S", date);

  p = (SCM *) &(STRUCT_SLOTS(date));
  t.tm_sec   = STk_integer_value(*p++);
  t.tm_min   = STk_integer_value(*p++);
  t.tm_hour  = STk_integer_value(*p++);
  t.tm_mday  = STk_integer_value(*p++);
  t.tm_mon   = STk_integer_value(*p++) - 1;
  t.tm_year  = STk_integer_value(*p++) - 1900;
1022
  t.tm_isdst = -1;                      /* to ignore DST */
1023

eg's avatar
eg committed
1024 1025
  n = mktime(&t);
  if (n == (time_t)(-1)) STk_error("cannot convert date to seconds (~S)", date);
1026

1027
  return STk_double2real((double) n);
eg's avatar
eg committed
1028 1029 1030 1031 1032 1033 1034 1035
}



DEFINE_PRIMITIVE("%seconds->string", date2string, subr2, (SCM fmt, SCM seconds))
{
  char buffer[1024];
  struct tm *p;
1036
  time_t tt;
eg's avatar
eg committed
1037 1038 1039
  int len, overflow;

  tt = (time_t) STk_integer2int32(seconds, &overflow);
1040

eg's avatar
eg committed
1041 1042
  if (!STRINGP(fmt)) error_bad_string(fmt);
  if (overflow)      error_bad_int_or_out_of_bounds(seconds);
1043

eg's avatar
eg committed
1044 1045 1046 1047 1048
  p   = localtime(&tt);
  len = strftime(buffer, 1023, STRING_CHARS(fmt), p);

  if (len > 0)
    return STk_Cstring2string(buffer);
1049
  else
eg's avatar
eg committed
1050
    STk_error("buffer too short!");
1051

eg's avatar
eg committed
1052 1053 1054 1055 1056 1057 1058
  return STk_void; /* never reached */
}


/*
<doc EXT running-os
 * (running-os)
1059 1060 1061 1062
 *
 * Returns the name of the underlying Operating System which is running
 * the program.
 * The value returned by |runnin-os| is a symbol. For now, this procedure
eg's avatar
eg committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
 * returns either |unix|, |windows|, or |cygwin-windows|.
doc>
*/
DEFINE_PRIMITIVE("running-os", running_os, subr0, (void))
{
#ifdef WIN32
# ifdef __CYGWIN32__
  return STk_intern("cygwin-windows");
# else
  return STk_intern("windows");
# endif
#else
  return STk_intern("unix");
#endif
}



/*
<doc EXT getenv
 * (getenv str)
 * (getenv)
 *
 * Looks for the environment variable named |str| and returns its
 * value as a string, if it exists. Otherwise, |getenv| returns |#f|.
1088 1089
 * If |getenv| is called without parameter, it returns the list of
 * all the environment variables accessible from the program as an
eg's avatar
eg committed
1090 1091
 * A-list.
 * @lisp
1092
 * (getenv "SHELL")
eg's avatar
eg committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
 *      => "/bin/zsh"
 * (getenv)
 *      => (("TERM" . "xterm") ("PATH" . "/bin:/usr/bin") ...)
 * @end lisp
doc>
 */
static SCM build_posix_environment(char **env)
{
  SCM l;

  for (l=STk_nil; *env; env++) {
    char *s, *p;

    s = *env; p = strchr(s, '=');
    if (p)
      l = STk_cons(STk_cons(STk_makestring(p-s, s), STk_Cstring2string(p+1)),l);
  }
  return l;
}


DEFINE_PRIMITIVE("getenv", getenv, subr01, (SCM str))
{
  char *tmp;
1117

1118
  if (str) {            /* One parameter: find the value of the given variable */
eg's avatar
eg committed
1119
    if (!STRINGP(str)) error_bad_string(str);
1120

eg's avatar
eg committed
1121 1122
    tmp = getenv(STRING_CHARS(str));
    return tmp ? STk_Cstring2string(tmp) : STk_false;
1123
  } else {              /* No parameter: give the complete environment */
eg's avatar
eg committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    extern char **environ;
    return build_posix_environment(environ);
  }
}

/*
<doc EXT setenv!
 * (setenv! var value)
 *
 * Sets the environment variable |var| to |value|. |Var| and
 * |value| must be strings. The result of |setenv!| is ,(emph "void").
doc>
 */
DEFINE_PRIMITIVE("setenv!", setenv, subr2, (SCM var, SCM value))
{
  char *s;

1141
  if (!STRINGP(var))                  error_bad_string(var);
eg's avatar
eg committed
1142
  if (strchr(STRING_CHARS(var), '=')) STk_error("variable ~S contains a '='", var);
1143
  if (!STRINGP(value))                STk_error("value ~S is not a string", value);
eg's avatar
eg committed
1144

1145
  s = STk_must_malloc(strlen(STRING_CHARS(var))   +
1146
                      strlen(STRING_CHARS(value)) + 2); /* 2 because of '=' & \0 */
eg's avatar
eg committed
1147 1148 1149 1150 1151 1152
  sprintf(s, "%s=%s", STRING_CHARS(var), STRING_CHARS(value));
  putenv(s);
  return STk_void;
}
/*
<doc EXT unsetenv!
1153
 * (unsetenv! var)
eg's avatar
eg committed
1154
 *
1155
 * Unsets the environment variable |var|. |Var| must be a string.
eg's avatar
eg committed
1156 1157 1158 1159 1160
 * The result of |unsetenv!| is ,(emph "void").
doc>
 */
DEFINE_PRIMITIVE("unsetenv!", unsetenv, subr1, (SCM var))
{
1161
#ifndef SOLARIS
eg's avatar
eg committed
1162 1163
  if (!STRINGP(var)) error_bad_string(var);
  unsetenv(STRING_CHARS(var));
1164
  return STk_void;
1165 1166 1167 1168
#else
  /* not exactly the same since getenv will not return #f after that */
  return STk_setenv(var, STk_Cstring2string(""));
#endif
eg's avatar
eg committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
}

/*
<doc EXT hostname
 * (hostname)
 *
 * Return the  host name of the current processor as a string.
doc>
*/
DEFINE_PRIMITIVE("hostname", hostname, subr0, (void))
{
  char buff[256];
1181

eg's avatar
eg committed
1182 1183 1184 1185
  if (gethostname(buff, 256) < 0)
    buff[255] = '0';
  return STk_Cstring2string(buff);
}
1186

Erick's avatar
Erick committed
1187 1188 1189 1190 1191 1192
/*
<doc EXT pause
 * (pause)
 *
doc>
*/
Erick's avatar
Erick committed
1193 1194 1195 1196 1197
DEFINE_PRIMITIVE("pause", pause, subr0, (void))
{
  pause();
  return STk_void;
}
Erick's avatar
Erick committed
1198 1199


1200 1201


Erick's avatar
Erick committed
1202

eg's avatar
eg committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
/*
 * Undocumented primitives
 *
 */

DEFINE_PRIMITIVE("%library-prefix", library_prefix, subr0, (void))
{
  return STk_Cstring2string(PREFIXDIR);
}

1213
DEFINE_PRIMITIVE("%shared-suffix", shared_suffix, subr0, (void))
1214 1215 1216 1217
{
  return STk_Cstring2string(SHARED_SUFFIX);
}

1218 1219 1220 1221 1222
DEFINE_PRIMITIVE("%shared-library-suffix", shared_library_suffix, subr0, (void))
{
  return STk_Cstring2string(SHARED_LIB_SUFFIX);
}

eg's avatar
eg committed
1223 1224 1225 1226 1227 1228
DEFINE_PRIMITIVE("%chmod", change_mode, subr2, (SCM file, SCM value))
{
  long mode = STk_integer_value(value);

  if (!STRINGP(file))   error_bad_path(file);
  if (mode < 0 || mode > 0777) error_bad_int_or_out_of_bounds(value);
1229

eg's avatar
eg committed
1230 1231 1232 1233
  return MAKE_BOOLEAN(chmod(STRING_CHARS(file), mode) == 0);
}


Erick's avatar
Erick committed
1234 1235 1236 1237
DEFINE_PRIMITIVE("%big-endian?", big_endianp, subr0, (void))
{
  int i = 1;
  char *p = (char *)&i;
1238

Erick's avatar
Erick committed
1239 1240 1241
  return MAKE_BOOLEAN(p[0] != 1);
}

eg's avatar
eg committed
1242

1243 1244 1245 1246 1247 1248 1249 1250
DEFINE_PRIMITIVE("%get-locale", get_locale, subr0, (void))
{
  char *str = setlocale(LC_ALL, NULL);

  return str? STk_Cstring2string(str) : STk_false;
}


eg's avatar
eg committed
1251 1252
int STk_init_system(void)
{
1253 1254
  SCM current_module = STk_STklos_module;

eg's avatar
eg committed
1255 1256
  /* Create the system-date structure-type */
  date_type =  STk_make_struct_type(STk_intern("%date"),
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
                                    STk_false,
                                    LIST10(STk_intern("second"),
                                           STk_intern("minute"),
                                           STk_intern("hour"),
                                           STk_intern("day"),
                                           STk_intern("month"),
                                           STk_intern("year"),
                                           STk_intern("week-day"),
                                           STk_intern("year-day"),
                                           STk_intern("dst"),
                                           STk_intern("tz")));
1268
  STk_define_variable(STk_intern("%date"), date_type, current_module);
eg's avatar
eg committed
1269

1270 1271
  /* Create the time structure-type */
  time_type =  STk_make_struct_type(STk_intern("%time"),
1272 1273 1274
                                    STk_false,
                                    LIST2(STk_intern("second"),
                                          STk_intern("nanosecond")));
1275
  STk_define_variable(STk_intern("%time"), time_type, current_module);
1276

eg's avatar
eg committed
1277 1278 1279
  /* Declare primitives */
  ADD_PRIMITIVE(clock);
  ADD_PRIMITIVE(date);
1280
  ADD_PRIMITIVE(current_seconds);
1281
  ADD_PRIMITIVE(current_second);
eg's avatar
eg committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
  ADD_PRIMITIVE(current_time);
  ADD_PRIMITIVE(sleep);
  ADD_PRIMITIVE(seconds2date);
  ADD_PRIMITIVE(date2seconds);
  ADD_PRIMITIVE(date2string);
  ADD_PRIMITIVE(running_os);
  ADD_PRIMITIVE(getenv);
  ADD_PRIMITIVE(setenv);
  ADD_PRIMITIVE(unsetenv);
  ADD_PRIMITIVE(hostname);
  ADD_PRIMITIVE(library_prefix);
1293
  ADD_PRIMITIVE(shared_suffix);
1294
  ADD_PRIMITIVE(shared_library_suffix);
eg's avatar
eg committed
1295 1296 1297 1298
  ADD_PRIMITIVE(change_mode);

  ADD_PRIMITIVE(getcwd);
  ADD_PRIMITIVE(chdir);
1299
  ADD_PRIMITIVE(make_directory);
Erick Gallesio's avatar
Erick Gallesio committed
1300
  ADD_PRIMITIVE(remove_directory);
1301
  ADD_PRIMITIVE(directory_files);
eg's avatar
eg committed
1302 1303
  ADD_PRIMITIVE(getpid);
  ADD_PRIMITIVE(system);
1304

eg's avatar
eg committed
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
  ADD_PRIMITIVE(file_is_directoryp);
  ADD_PRIMITIVE(file_is_regularp);
  ADD_PRIMITIVE(file_is_readablep);
  ADD_PRIMITIVE(file_is_writablep);
  ADD_PRIMITIVE(file_is_executablep);
  ADD_PRIMITIVE(file_existsp);
  ADD_PRIMITIVE(file_size);
  ADD_PRIMITIVE(glob);
  ADD_PRIMITIVE(expand_fn);
  ADD_PRIMITIVE(canonical_path);

  ADD_PRIMITIVE(remove_file);
  ADD_PRIMITIVE(rename_file);
  ADD_PRIMITIVE(copy_file);
  ADD_PRIMITIVE(tmp_file);
1320 1321
  ADD_PRIMITIVE(pre_exit);
  ADD_PRIMITIVE(exit);
1322
  ADD_PRIMITIVE(emergency_exit);
eg's avatar
eg committed
1323 1324 1325 1326 1327
  ADD_PRIMITIVE(at_exit);
  ADD_PRIMITIVE(machine_type);

  ADD_PRIMITIVE(winify_filename);
  ADD_PRIMITIVE(posixify_filename);
1328

Erick's avatar
Erick committed
1329
  ADD_PRIMITIVE(pause);
Erick's avatar
Erick committed
1330
  ADD_PRIMITIVE(big_endianp);
1331
  ADD_PRIMITIVE(get_locale);
eg's avatar
eg committed
1332 1333
  return TRUE;
}