add exception-on-error optional arg to `stat' in scheme
authorAndy Wingo <wingo@pobox.com>
Tue, 2 Jun 2009 20:20:21 +0000 (22:20 +0200)
committerAndy Wingo <wingo@pobox.com>
Wed, 3 Jun 2009 20:20:55 +0000 (22:20 +0200)
* libguile/filesys.h:
* libguile/filesys.c (scm_stat): Add optional arg, exception-on-error,
  which if #f (not the default) will just return #f instead of raising an
  exception if the stat fails.

libguile/filesys.c
libguile/filesys.h

index ec33328..4799dd4 100644 (file)
@@ -580,17 +580,23 @@ static int fstat_Win32 (int fdes, struct stat *buf)
 }
 #endif /* __MINGW32__ */
 
-SCM_DEFINE (scm_stat, "stat", 1, 0, 0, 
-            (SCM object),
+SCM_DEFINE (scm_stat, "stat", 1, 1, 0, 
+            (SCM object, SCM exception_on_error),
            "Return an object containing various information about the file\n"
            "determined by @var{obj}.  @var{obj} can be a string containing\n"
            "a file name or a port or integer file descriptor which is open\n"
            "on a file (in which case @code{fstat} is used as the underlying\n"
            "system call).\n"
            "\n"
-           "The object returned by @code{stat} can be passed as a single\n"
-           "parameter to the following procedures, all of which return\n"
-           "integers:\n"
+            "If the optional @var{exception_on_error} argument is true, which\n"
+            "is the default, an exception will be raised if the underlying\n"
+            "system call returns an error, for example if the file is not\n"
+            "found or is not readable. Otherwise, an error will cause\n"
+            "@code{stat} to return @code{#f}."
+           "\n"
+           "The object returned by a successful call to @code{stat} can be\n"
+            "passed as a single parameter to the following procedures, all of\n"
+            "which return integers:\n"
            "\n"
            "@table @code\n"
            "@item stat:dev\n"
@@ -678,12 +684,16 @@ SCM_DEFINE (scm_stat, "stat", 1, 0, 0,
 
   if (rv == -1)
     {
-      int en = errno;
-
-      SCM_SYSERROR_MSG ("~A: ~S",
-                       scm_list_2 (scm_strerror (scm_from_int (en)),
-                                   object),
-                       en);
+      if (SCM_UNBNDP (exception_on_error) || scm_is_true (exception_on_error))
+        {
+          int en = errno;
+          SCM_SYSERROR_MSG ("~A: ~S",
+                            scm_list_2 (scm_strerror (scm_from_int (en)),
+                                        object),
+                            en);
+        }
+      else
+        return SCM_BOOL_F;
     }
   return scm_stat2scm (&stat_temp);
 }
index a38a5b5..cf0a6ac 100644 (file)
@@ -42,7 +42,7 @@ SCM_API SCM scm_open_fdes (SCM path, SCM flags, SCM mode);
 SCM_API SCM scm_open (SCM path, SCM flags, SCM mode);
 SCM_API SCM scm_close (SCM fd_or_port);
 SCM_API SCM scm_close_fdes (SCM fd);
-SCM_API SCM scm_stat (SCM object);
+SCM_API SCM scm_stat (SCM object, SCM exception_on_error);
 SCM_API SCM scm_link (SCM oldpath, SCM newpath);
 SCM_API SCM scm_rename (SCM oldname, SCM newname);
 SCM_API SCM scm_delete_file (SCM str);