comparison util.c @ 1949:5a76a7373823

Get WIP net play code compiling on Windows and cleanup some unistd.h includes
author Michael Pavone <pavone@retrodev.com>
date Thu, 30 Apr 2020 23:15:50 -0700
parents a4cae960fd08
children 417e0fc958cc
comparison
equal deleted inserted replaced
1948:d01527615c7c 1949:5a76a7373823
5 #include <stdint.h> 5 #include <stdint.h>
6 #include <stdarg.h> 6 #include <stdarg.h>
7 7
8 #include <sys/types.h> 8 #include <sys/types.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <errno.h> 10 #include <errno.h>
12 11
13 #ifdef __ANDROID__ 12 #ifdef __ANDROID__
14 #include <android/log.h> 13 #include <android/log.h>
15 #define info_puts(msg) __android_log_write(ANDROID_LOG_INFO, "BlastEm", msg) 14 #define info_puts(msg) __android_log_write(ANDROID_LOG_INFO, "BlastEm", msg)
530 { 529 {
531 output_enabled = 0; 530 output_enabled = 0;
532 } 531 }
533 532
534 #ifdef _WIN32 533 #ifdef _WIN32
534 #define WINVER 0x501
535 #include <winsock2.h>
535 #include <windows.h> 536 #include <windows.h>
536 #include <shlobj.h> 537 #include <shlobj.h>
537 538
538 char * get_home_dir() 539 char * get_home_dir()
539 { 540 {
677 } 678 }
678 free(parent); 679 free(parent);
679 return CreateDirectory(path, NULL); 680 return CreateDirectory(path, NULL);
680 } 681 }
681 682
683 static WSADATA wsa_data;
684 static void socket_cleanup(void)
685 {
686 WSACleanup();
687 }
688
689 void socket_init(void)
690 {
691 static uint8_t started;
692 if (!started) {
693 started = 1;
694 WSAStartup(MAKEWORD(2,2), &wsa_data);
695 atexit(socket_cleanup);
696 }
697 }
698
699 int socket_blocking(int sock, int should_block)
700 {
701 u_long param = !should_block;
702 if (ioctlsocket(sock, FIONBIO, &param)) {
703 return WSAGetLastError();
704 }
705 return 0;
706 }
707
708 void socket_close(int sock)
709 {
710 closesocket(sock);
711 }
712
713 int socket_last_error(void)
714 {
715 return WSAGetLastError();
716 }
717
718 int socket_error_is_wouldblock(void)
719 {
720 return WSAGetLastError() == WSAEWOULDBLOCK;
721 }
722
682 #else 723 #else
724 #include <fcntl.h>
725 #include <unistd.h>
726
727 void socket_init(void)
728 {
729 }
730
731 int socket_blocking(int sock, int should_block)
732 {
733 if (fcntl(listen_sock, F_SETFL, should_block ? 0 : O_NONBLOCK)) {
734 return errno;
735 }
736 return 0;
737 }
738
739 void socket_close(int sock)
740 {
741 close(sock);
742 }
743
744 int socket_last_error(void)
745 {
746 return errno;
747 }
748
749 int socket_error_is_wouldblock(void)
750 {
751 return errno == EAGAIN || errno == EWOULDBLOCK;
752 }
683 753
684 char * get_home_dir() 754 char * get_home_dir()
685 { 755 {
686 return getenv("HOME"); 756 return getenv("HOME");
687 } 757 }