bug 14791: "sleep 1" crashed Scilab 67/21167/4
Antoine ELIAS [Fri, 13 Dec 2019 09:13:43 +0000 (10:13 +0100)]
Change-Id: I4f42416f143f8659e2b29bbd5b6d0407419bbf3c

scilab/CHANGES.md
scilab/modules/time/Makefile.am
scilab/modules/time/Makefile.in
scilab/modules/time/includes/gw_time.h
scilab/modules/time/includes/time_gw.hxx
scilab/modules/time/sci_gateway/c/sci_sleep.c [deleted file]
scilab/modules/time/sci_gateway/cpp/sci_sleep.cpp [new file with mode: 0644]
scilab/modules/time/sci_gateway/cpp/time_gw.vcxproj
scilab/modules/time/sci_gateway/cpp/time_gw.vcxproj.filters

index cf427d9..dcc532f 100644 (file)
@@ -177,6 +177,7 @@ Bug Fixes
 * [#14605](http://bugzilla.scilab.org/show_bug.cgi?id=14605): fixed - `bench_run` was too strict about the specification of tests names.
 * [#14606](http://bugzilla.scilab.org/show_bug.cgi?id=14606): Memory used by variables returned by `[names,mem]=who()` was always zero.
 * [#14741](http://bugzilla.scilab.org/show_bug.cgi?id=14741): The syntax `[m,e]=log2(x)` was not documented. As public function `frexp()` was in duplicate with `[m,e]=log2(x)`.
+* [#14791](http://bugzilla.scilab.org/show_bug.cgi?id=14791): `sleep 1` crashed Scilab.
 * [#14812](http://bugzilla.scilab.org/show_bug.cgi?id=14812): Minor typos in messages.
 * [#14863](http://bugzilla.scilab.org/show_bug.cgi?id=14863): In Xcos, the default ending time was unhandily high (100000), reduced it to 30.
 * [#14982](http://bugzilla.scilab.org/show_bug.cgi?id=14982): `msprintf` segmentation fault was caught due to wrong size
index 80f73ba..393b579 100644 (file)
@@ -14,12 +14,12 @@ GATEWAY_C_SOURCES = \
     sci_gateway/c/sci_timer.c \
     sci_gateway/c/sci_calendar.c \
     sci_gateway/c/sci_xpause.c \
-    sci_gateway/c/sci_sleep.c \
     sci_gateway/c/sci_getdate.c
 
 GATEWAY_CPP_SOURCES = \
     sci_gateway/cpp/time_gw.cpp \
-    sci_gateway/cpp/sci_tictoc.cpp
+    sci_gateway/cpp/sci_tictoc.cpp \
+    sci_gateway/cpp/sci_sleep.cpp
 
 libscitime_la_CPPFLAGS= \
     -I$(srcdir)/includes/ \
index faf0858..70e7648 100644 (file)
@@ -193,10 +193,10 @@ am__objects_2 = sci_gateway/c/libscitime_la-sci_realtime.lo \
        sci_gateway/c/libscitime_la-sci_timer.lo \
        sci_gateway/c/libscitime_la-sci_calendar.lo \
        sci_gateway/c/libscitime_la-sci_xpause.lo \
-       sci_gateway/c/libscitime_la-sci_sleep.lo \
        sci_gateway/c/libscitime_la-sci_getdate.lo
 am__objects_3 = sci_gateway/cpp/libscitime_la-time_gw.lo \
-       sci_gateway/cpp/libscitime_la-sci_tictoc.lo
+       sci_gateway/cpp/libscitime_la-sci_tictoc.lo \
+       sci_gateway/cpp/libscitime_la-sci_sleep.lo
 am_libscitime_la_OBJECTS = $(am__objects_2) $(am__objects_3)
 libscitime_la_OBJECTS = $(am_libscitime_la_OBJECTS)
 @MAINTAINER_MODE_FALSE@am_libscitime_la_rpath =
@@ -220,9 +220,9 @@ am__depfiles_remade =  \
        sci_gateway/c/$(DEPDIR)/libscitime_la-sci_calendar.Plo \
        sci_gateway/c/$(DEPDIR)/libscitime_la-sci_getdate.Plo \
        sci_gateway/c/$(DEPDIR)/libscitime_la-sci_realtime.Plo \
-       sci_gateway/c/$(DEPDIR)/libscitime_la-sci_sleep.Plo \
        sci_gateway/c/$(DEPDIR)/libscitime_la-sci_timer.Plo \
        sci_gateway/c/$(DEPDIR)/libscitime_la-sci_xpause.Plo \
+       sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_sleep.Plo \
        sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_tictoc.Plo \
        sci_gateway/cpp/$(DEPDIR)/libscitime_la-time_gw.Plo \
        src/c/$(DEPDIR)/libscitime_algo_la-getdate.Plo \
@@ -601,12 +601,12 @@ GATEWAY_C_SOURCES = \
     sci_gateway/c/sci_timer.c \
     sci_gateway/c/sci_calendar.c \
     sci_gateway/c/sci_xpause.c \
-    sci_gateway/c/sci_sleep.c \
     sci_gateway/c/sci_getdate.c
 
 GATEWAY_CPP_SOURCES = \
     sci_gateway/cpp/time_gw.cpp \
-    sci_gateway/cpp/sci_tictoc.cpp
+    sci_gateway/cpp/sci_tictoc.cpp \
+    sci_gateway/cpp/sci_sleep.cpp
 
 libscitime_la_CPPFLAGS = \
     -I$(srcdir)/includes/ \
@@ -841,9 +841,6 @@ sci_gateway/c/libscitime_la-sci_calendar.lo:  \
 sci_gateway/c/libscitime_la-sci_xpause.lo:  \
        sci_gateway/c/$(am__dirstamp) \
        sci_gateway/c/$(DEPDIR)/$(am__dirstamp)
-sci_gateway/c/libscitime_la-sci_sleep.lo:  \
-       sci_gateway/c/$(am__dirstamp) \
-       sci_gateway/c/$(DEPDIR)/$(am__dirstamp)
 sci_gateway/c/libscitime_la-sci_getdate.lo:  \
        sci_gateway/c/$(am__dirstamp) \
        sci_gateway/c/$(DEPDIR)/$(am__dirstamp)
@@ -859,6 +856,9 @@ sci_gateway/cpp/libscitime_la-time_gw.lo:  \
 sci_gateway/cpp/libscitime_la-sci_tictoc.lo:  \
        sci_gateway/cpp/$(am__dirstamp) \
        sci_gateway/cpp/$(DEPDIR)/$(am__dirstamp)
+sci_gateway/cpp/libscitime_la-sci_sleep.lo:  \
+       sci_gateway/cpp/$(am__dirstamp) \
+       sci_gateway/cpp/$(DEPDIR)/$(am__dirstamp)
 
 libscitime.la: $(libscitime_la_OBJECTS) $(libscitime_la_DEPENDENCIES) $(EXTRA_libscitime_la_DEPENDENCIES) 
        $(AM_V_CXXLD)$(CXXLINK) $(am_libscitime_la_rpath) $(libscitime_la_OBJECTS) $(libscitime_la_LIBADD) $(LIBS)
@@ -878,9 +878,9 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscitime_la-sci_calendar.Plo@am__quote@ # am--include-marker
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscitime_la-sci_getdate.Plo@am__quote@ # am--include-marker
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscitime_la-sci_realtime.Plo@am__quote@ # am--include-marker
-@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscitime_la-sci_sleep.Plo@am__quote@ # am--include-marker
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscitime_la-sci_timer.Plo@am__quote@ # am--include-marker
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscitime_la-sci_xpause.Plo@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_sleep.Plo@am__quote@ # am--include-marker
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_tictoc.Plo@am__quote@ # am--include-marker
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscitime_la-time_gw.Plo@am__quote@ # am--include-marker
 @AMDEP_TRUE@@am__include@ @am__quote@src/c/$(DEPDIR)/libscitime_algo_la-getdate.Plo@am__quote@ # am--include-marker
@@ -966,13 +966,6 @@ sci_gateway/c/libscitime_la-sci_xpause.lo: sci_gateway/c/sci_xpause.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscitime_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o sci_gateway/c/libscitime_la-sci_xpause.lo `test -f 'sci_gateway/c/sci_xpause.c' || echo '$(srcdir)/'`sci_gateway/c/sci_xpause.c
 
-sci_gateway/c/libscitime_la-sci_sleep.lo: sci_gateway/c/sci_sleep.c
-@am__fastdepCC_TRUE@   $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscitime_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT sci_gateway/c/libscitime_la-sci_sleep.lo -MD -MP -MF sci_gateway/c/$(DEPDIR)/libscitime_la-sci_sleep.Tpo -c -o sci_gateway/c/libscitime_la-sci_sleep.lo `test -f 'sci_gateway/c/sci_sleep.c' || echo '$(srcdir)/'`sci_gateway/c/sci_sleep.c
-@am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) sci_gateway/c/$(DEPDIR)/libscitime_la-sci_sleep.Tpo sci_gateway/c/$(DEPDIR)/libscitime_la-sci_sleep.Plo
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      $(AM_V_CC)source='sci_gateway/c/sci_sleep.c' object='sci_gateway/c/libscitime_la-sci_sleep.lo' libtool=yes @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCC_FALSE@  $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscitime_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o sci_gateway/c/libscitime_la-sci_sleep.lo `test -f 'sci_gateway/c/sci_sleep.c' || echo '$(srcdir)/'`sci_gateway/c/sci_sleep.c
-
 sci_gateway/c/libscitime_la-sci_getdate.lo: sci_gateway/c/sci_getdate.c
 @am__fastdepCC_TRUE@   $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscitime_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT sci_gateway/c/libscitime_la-sci_getdate.lo -MD -MP -MF sci_gateway/c/$(DEPDIR)/libscitime_la-sci_getdate.Tpo -c -o sci_gateway/c/libscitime_la-sci_getdate.lo `test -f 'sci_gateway/c/sci_getdate.c' || echo '$(srcdir)/'`sci_gateway/c/sci_getdate.c
 @am__fastdepCC_TRUE@   $(AM_V_at)$(am__mv) sci_gateway/c/$(DEPDIR)/libscitime_la-sci_getdate.Tpo sci_gateway/c/$(DEPDIR)/libscitime_la-sci_getdate.Plo
@@ -1018,6 +1011,13 @@ sci_gateway/cpp/libscitime_la-sci_tictoc.lo: sci_gateway/cpp/sci_tictoc.cpp
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscitime_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o sci_gateway/cpp/libscitime_la-sci_tictoc.lo `test -f 'sci_gateway/cpp/sci_tictoc.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_tictoc.cpp
 
+sci_gateway/cpp/libscitime_la-sci_sleep.lo: sci_gateway/cpp/sci_sleep.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscitime_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT sci_gateway/cpp/libscitime_la-sci_sleep.lo -MD -MP -MF sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_sleep.Tpo -c -o sci_gateway/cpp/libscitime_la-sci_sleep.lo `test -f 'sci_gateway/cpp/sci_sleep.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_sleep.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_sleep.Tpo sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_sleep.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     $(AM_V_CXX)source='sci_gateway/cpp/sci_sleep.cpp' object='sci_gateway/cpp/libscitime_la-sci_sleep.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscitime_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o sci_gateway/cpp/libscitime_la-sci_sleep.lo `test -f 'sci_gateway/cpp/sci_sleep.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_sleep.cpp
+
 mostlyclean-libtool:
        -rm -f *.lo
 
@@ -1228,9 +1228,9 @@ distclean: distclean-am
                -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_calendar.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_getdate.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_realtime.Plo
-       -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_sleep.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_timer.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_xpause.Plo
+       -rm -f sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_sleep.Plo
        -rm -f sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_tictoc.Plo
        -rm -f sci_gateway/cpp/$(DEPDIR)/libscitime_la-time_gw.Plo
        -rm -f src/c/$(DEPDIR)/libscitime_algo_la-getdate.Plo
@@ -1286,9 +1286,9 @@ maintainer-clean: maintainer-clean-am
                -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_calendar.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_getdate.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_realtime.Plo
-       -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_sleep.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_timer.Plo
        -rm -f sci_gateway/c/$(DEPDIR)/libscitime_la-sci_xpause.Plo
+       -rm -f sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_sleep.Plo
        -rm -f sci_gateway/cpp/$(DEPDIR)/libscitime_la-sci_tictoc.Plo
        -rm -f sci_gateway/cpp/$(DEPDIR)/libscitime_la-time_gw.Plo
        -rm -f src/c/$(DEPDIR)/libscitime_algo_la-getdate.Plo
index a244b0e..6bd560f 100644 (file)
@@ -23,7 +23,6 @@
 TIME_IMPEXP int gw_time(void);
 /*--------------------------------------------------------------------------*/
 int sci_xpause(char *fname, void* pvApiCtx);
-int sci_sleep(char *fname, void* pvApiCtx);
 int sci_realtimeinit(char *fname, void* pvApiCtx);
 int sci_realtime(char *fname, void* pvApiCtx);
 int sci_getdate(char *fname, void* pvApiCtx);
index 8f3e310..5159d91 100644 (file)
@@ -36,6 +36,7 @@ public :
 
 CPP_GATEWAY_PROTOTYPE(sci_tic);
 CPP_GATEWAY_PROTOTYPE(sci_toc);
+CPP_GATEWAY_PROTOTYPE(sci_sleep);
 
 
 #endif /* !__TIME_GW_HXX__ */
diff --git a/scilab/modules/time/sci_gateway/c/sci_sleep.c b/scilab/modules/time/sci_gateway/c/sci_sleep.c
deleted file mode 100644 (file)
index aa94dbf..0000000
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) INRIA - Allan CORNET
- *
- * Copyright (C) 2012 - 2016 - Scilab Enterprises
- *
- * This file is hereby licensed under the terms of the GNU GPL v2.0,
- * pursuant to article 5.3.4 of the CeCILL v.2.1.
- * This file was originally licensed under the terms of the CeCILL v2.1,
- * and continues to be available under such terms.
- * For more information, see the COPYING file which you should have received
- * along with this program.
- *
- */
-
-
-/*--------------------------------------------------------------------------*/
-#ifdef _MSC_VER
-#include <Windows.h>
-#endif
-#include "machine.h"
-#ifdef HAVE_USLEEP
-#include <unistd.h>
-#endif
-#include "gw_time.h"
-#include "api_scilab.h"
-#include "Scierror.h"
-#include "localization.h"
-/*--------------------------------------------------------------------------*/
-int sci_sleep(char *fname, void* pvApiCtx)
-{
-    SciErr sciErr;
-    int m1 = 0, n1 = 0 , iRows = 0, iCols = 0, option = 0;
-    double sec = 0;
-    char * opt = NULL;
-    int * p1_in_address = NULL;
-    int * p2_in_address = NULL;
-    double * pDblReal = NULL;
-
-
-    CheckLhs(0, 1);
-    CheckRhs(1, 2);
-
-    if (Rhs >= 1)
-    {
-        sciErr = getVarAddressFromPosition(pvApiCtx, 1, &p1_in_address);
-        sciErr = getMatrixOfDouble(pvApiCtx, p1_in_address, &m1, &n1, &pDblReal);
-
-        if (isScalar(pvApiCtx, p1_in_address) == 0)
-        {
-            Scierror(999, _("%s: Wrong type for input argument #%d: A real scalar expected.\n"), fname, 1);
-            return 0;
-        }
-        sec = (double)  * pDblReal;
-
-        if (sec <= 0)
-        {
-            Scierror(999, _("%s: Wrong values for input argument #%d: Non-negative integers expected.\n"), fname, 1);
-            return 0;
-        }
-
-        if (Rhs == 2)
-        {
-            sciErr = getVarAddressFromPosition(pvApiCtx, 2, &p2_in_address);
-            if (getAllocatedSingleString(pvApiCtx, p2_in_address, &opt))
-            {
-                return 0;
-            }
-
-            if (strcmp("s", opt) != 0)
-            {
-                Scierror(999, _("%s: Wrong value for input argument #%d: 's' expected.\n"), fname, 2);
-                freeAllocatedSingleString(opt);
-                return 0;
-            }
-            if (strcmp("s", opt) == 0)
-            {
-                option = 1;
-            }
-            freeAllocatedSingleString(opt);
-        }
-
-
-#ifdef _MSC_VER
-        {
-            double ms = 0;
-            if (option == 1)
-            {
-                ms = sec * 1000; /* convert seconds into milliseconds */
-            }
-            else
-            {
-                ms = sec;
-            }
-
-            if (ms > 0)
-            {
-                Sleep((DWORD)ms);    /* Number of milliseconds to sleep. */
-            }
-        }
-#else
-        {
-            unsigned useconds;
-            useconds = (unsigned) sec;
-            if (option == 1)
-            {
-                useconds = useconds * 1000;
-            }
-            if (useconds != 0)
-#ifdef HAVE_USLEEP
-            {
-                usleep(useconds * 1000);
-            }
-#else
-#ifdef HAVE_SLEEP
-            {
-                sleep(useconds * 1000);
-            }
-#endif
-#endif
-        }
-#endif
-    }
-    LhsVar(1) = 0;
-    PutLhsVar();
-
-    return 0;
-}
-/*--------------------------------------------------------------------------*/
-
diff --git a/scilab/modules/time/sci_gateway/cpp/sci_sleep.cpp b/scilab/modules/time/sci_gateway/cpp/sci_sleep.cpp
new file mode 100644 (file)
index 0000000..1c0321b
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ *  Copyright (C) 2019 - ESI Group - Antoine ELIAS
+ *
+ * Copyright (C) 2012 - 2016 - Scilab Enterprises
+ *
+ * This file is hereby licensed under the terms of the GNU GPL v2.0,
+ * pursuant to article 5.3.4 of the CeCILL v.2.1.
+ * This file was originally licensed under the terms of the CeCILL v2.1,
+ * and continues to be available under such terms.
+ * For more information, see the COPYING file which you should have received
+ * along with this program.
+ *
+ */
+
+#include <unistd.h>
+#include "time_gw.hxx"
+#include "function.hxx"
+#include "double.hxx"
+#include "string.hxx"
+
+extern "C"
+{
+#include "Scierror.h"
+#include "localization.h"
+}
+
+static char fname[] = "sleep";
+
+types::Function::ReturnValue sci_sleep(types::typed_list &in, int _iRetCount, types::typed_list &out)
+{
+    int iRhs = static_cast<int>(in.size());
+    bool asSec = false;
+    double t = 0;
+
+    if (iRhs < 1 || iRhs > 2)
+    {
+        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), fname, 1, 2);
+        return types::Function::Error;
+    }
+
+    if (in[0]->isDouble() == false || in[0]->getAs<types::Double>()->isScalar() == false)
+    {
+        Scierror(999, _("%s: Wrong type for input argument #%d: A real scalar expected.\n"), fname, 1);
+        return types::Function::Error;
+    }
+
+    t = in[0]->getAs<types::Double>()->get()[0];
+    if (t < 0)
+    {
+        Scierror(999, _( "%s: Argument #%d: the scalar must be positive.\n"), fname, 1);
+        return types::Function::Error;
+    }
+
+    if (iRhs == 2)
+    {
+        if (in[1]->isString() == false ||
+            in[1]->getAs<types::String>()->isScalar() == false ||
+            wcscmp(in[1]->getAs<types::String>()->get()[0], L"s") != 0)
+        {
+            Scierror(999, _("%s: Wrong value for input argument #%d: 's' expected.\n"), fname, 2);
+            return types::Function::Error;
+        }
+
+        asSec = true;
+    }
+
+#ifdef _MSC_VER
+    {
+        if (asSec)
+        {
+            t *= 1000; /* convert seconds into milliseconds */
+        }
+
+        if (t)
+        {
+            Sleep((DWORD)t);    /* Number of milliseconds to sleep. */
+        }
+    }
+#else
+    {
+        if (asSec)
+        {
+            t *= 1000;
+        }
+
+        if (t)
+#ifdef HAVE_USLEEP
+        {
+            usleep(t * 1000);
+        }
+#else
+#ifdef HAVE_SLEEP
+        {
+            sleep(t * 1000);
+        }
+#endif
+#endif
+    }
+#endif
+
+    return types::Function::OK;
+}
+
index a57d9e9..5eb0b20 100644 (file)
     <ClCompile Include="..\c\sci_calendar.c" />
     <ClCompile Include="..\c\sci_getdate.c" />
     <ClCompile Include="..\c\sci_realtime.c" />
-    <ClCompile Include="..\c\sci_sleep.c" />
     <ClCompile Include="..\c\sci_timer.c" />
     <ClCompile Include="..\c\sci_xpause.c" />
+    <ClCompile Include="sci_sleep.cpp" />
     <ClCompile Include="sci_tictoc.cpp" />
     <ClCompile Include="time_gw.cpp" />
   </ItemGroup>
index 71d8bf7..97dbef1 100644 (file)
     <ClCompile Include="..\c\sci_realtime.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\c\sci_sleep.c">
+    <ClCompile Include="..\c\sci_timer.c">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\c\sci_timer.c">
+    <ClCompile Include="sci_sleep.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
   </ItemGroup>