src/windows/native/java/io/io_util_md.c

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "io_util.h"
  30 #include "io_util_md.h"
  31 #include <stdio.h>

  32 
  33 #include <wchar.h>
  34 #include <io.h>
  35 #include <fcntl.h>
  36 #include <errno.h>
  37 #include <string.h>
  38 #include <sys/types.h>
  39 #include <sys/stat.h>
  40 #include <limits.h>
  41 #include <wincon.h>
  42 
  43 extern jboolean onNT = JNI_FALSE;
  44 
  45 static DWORD MAX_INPUT_EVENTS = 2000;
  46 
  47 void
  48 initializeWindowsVersion() {
  49     OSVERSIONINFO ver;
  50     ver.dwOSVersionInfoSize = sizeof(ver);
  51     GetVersionEx(&ver);


 571     DWORD lowPos = 0;
 572     long highPos = 0;
 573     DWORD op = FILE_CURRENT;
 574     HANDLE h = (HANDLE)fd;
 575 
 576     if (whence == SEEK_END) {
 577         op = FILE_END;
 578     }
 579     if (whence == SEEK_CUR) {
 580         op = FILE_CURRENT;
 581     }
 582     if (whence == SEEK_SET) {
 583         op = FILE_BEGIN;
 584     }
 585 
 586     distance.QuadPart = offset;
 587     if (SetFilePointerEx(h, distance, &pos, op) == 0) {
 588         return -1;
 589     }
 590     return long_to_jlong(pos.QuadPart);










































































 591 }


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "io_util.h"
  30 #include "io_util_md.h"
  31 #include <stdio.h>
  32 #include <windows.h>
  33 
  34 #include <wchar.h>
  35 #include <io.h>
  36 #include <fcntl.h>
  37 #include <errno.h>
  38 #include <string.h>
  39 #include <sys/types.h>
  40 #include <sys/stat.h>
  41 #include <limits.h>
  42 #include <wincon.h>
  43 
  44 extern jboolean onNT = JNI_FALSE;
  45 
  46 static DWORD MAX_INPUT_EVENTS = 2000;
  47 
  48 void
  49 initializeWindowsVersion() {
  50     OSVERSIONINFO ver;
  51     ver.dwOSVersionInfoSize = sizeof(ver);
  52     GetVersionEx(&ver);


 572     DWORD lowPos = 0;
 573     long highPos = 0;
 574     DWORD op = FILE_CURRENT;
 575     HANDLE h = (HANDLE)fd;
 576 
 577     if (whence == SEEK_END) {
 578         op = FILE_END;
 579     }
 580     if (whence == SEEK_CUR) {
 581         op = FILE_CURRENT;
 582     }
 583     if (whence == SEEK_SET) {
 584         op = FILE_BEGIN;
 585     }
 586 
 587     distance.QuadPart = offset;
 588     if (SetFilePointerEx(h, distance, &pos, op) == 0) {
 589         return -1;
 590     }
 591     return long_to_jlong(pos.QuadPart);
 592 }
 593 
 594 size_t
 595 getLastErrorString(char *utf8_jvmErrorMsg, size_t cbErrorMsg)
 596 {
 597     size_t n = 0;
 598     if (cbErrorMsg > 0) {
 599         BOOLEAN noError = FALSE;
 600         WCHAR *utf16_osErrorMsg = (WCHAR *)malloc(cbErrorMsg*sizeof(WCHAR));
 601         if (utf16_osErrorMsg == NULL) {
 602             // OOM accident
 603             strncpy(utf8_jvmErrorMsg, "Out of memory", cbErrorMsg);
 604             // truncate if too long
 605             utf8_jvmErrorMsg[cbErrorMsg - 1] = '\0';
 606             n = strlen(utf8_jvmErrorMsg);
 607         } else {
 608             DWORD errval = GetLastError();
 609             if (errval != 0) {
 610                 // WIN32 error
 611                 n = (size_t)FormatMessageW(
 612                     FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
 613                     NULL,
 614                     errval,
 615                     0,
 616                     utf16_osErrorMsg,
 617                     (DWORD)cbErrorMsg,
 618                     NULL);
 619                 if (n > 3) {
 620                     // Drop final '.', CR, LF
 621                     if (utf16_osErrorMsg[n - 1] == L'\n') --n;
 622                     if (utf16_osErrorMsg[n - 1] == L'\r') --n;
 623                     if (utf16_osErrorMsg[n - 1] == L'.') --n;
 624                     utf16_osErrorMsg[n] = L'\0';
 625                 }
 626             } else if (errno != 0) {
 627                 // C runtime error that has no corresponding WIN32 error code
 628                 const WCHAR *rtError = _wcserror(errno);
 629                 if (rtError != NULL) {
 630                     wcsncpy(utf16_osErrorMsg, rtError, cbErrorMsg);
 631                     // truncate if too long
 632                     utf16_osErrorMsg[cbErrorMsg - 1] = L'\0';
 633                     n = wcslen(utf16_osErrorMsg);
 634                 }
 635             } else
 636                 noError = TRUE; //OS has no error to report
 637 
 638             if (!noError) {
 639                 if (n > 0) {
 640                     n = WideCharToMultiByte(
 641                         CP_UTF8,
 642                         0,
 643                         utf16_osErrorMsg,
 644                         n,
 645                         utf8_jvmErrorMsg,
 646                         cbErrorMsg,
 647                         NULL,
 648                         NULL);
 649 
 650                     // no way to die
 651                     if (n > 0)
 652                         utf8_jvmErrorMsg[min(cbErrorMsg - 1, n)] = '\0';
 653                 }
 654 
 655                 if (n <= 0) {
 656                     strncpy(utf8_jvmErrorMsg, "Secondary error while OS message extraction", cbErrorMsg);
 657                     // truncate if too long
 658                     utf8_jvmErrorMsg[cbErrorMsg - 1] = '\0';
 659                     n = strlen(utf8_jvmErrorMsg);
 660                 }
 661             }
 662             free(utf16_osErrorMsg);
 663         }
 664     }
 665     return n;
 666 }