Comparison

util-src/pposix.c @ 7931:b619b85e01aa

util.pposix, configure: Move _GNU_SOURCE macro into source files
author Kim Alvefur <zash@zash.se>
date Wed, 01 Mar 2017 22:27:11 +0100
parent 7926:b009c27818c6
child 7965:57f98394b830
comparison
equal deleted inserted replaced
7930:5dec27760ecd 7931:b619b85e01aa
13 * POSIX support functions for Lua 13 * POSIX support functions for Lua
14 */ 14 */
15 15
16 #define MODULE_VERSION "0.3.6" 16 #define MODULE_VERSION "0.3.6"
17 17
18
19 #if defined(__linux__)
20 #define _GNU_SOURCE
21 #else
22 #define _DEFAULT_SOURCE
23 #endif
24 #define _POSIX_C_SOURCE 200809L
25
18 #include <stdlib.h> 26 #include <stdlib.h>
19 #include <math.h> 27 #include <math.h>
20 #include <unistd.h> 28 #include <unistd.h>
21 #include <libgen.h> 29 #include <libgen.h>
22 #include <sys/resource.h> 30 #include <sys/resource.h>
38 #if (LUA_VERSION_NUM == 501) 46 #if (LUA_VERSION_NUM == 501)
39 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) 47 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
40 #endif 48 #endif
41 49
42 #include <fcntl.h> 50 #include <fcntl.h>
43 #if defined(__linux__) && defined(_GNU_SOURCE) 51 #if defined(__linux__)
44 #include <linux/falloc.h> 52 #include <linux/falloc.h>
45 #endif 53 #endif
46 54
47 #if (defined(_SVID_SOURCE) && !defined(WITHOUT_MALLINFO)) 55 #if !defined(WITHOUT_MALLINFO)
48 #include <malloc.h> 56 #include <malloc.h>
49 #define WITH_MALLINFO 57 #define WITH_MALLINFO
50 #endif 58 #endif
51 59
52 #if defined(__FreeBSD__) && defined(RFPROC) 60 #if defined(__FreeBSD__) && defined(RFPROC)
661 lua_setfield(L, -2, "release"); 669 lua_setfield(L, -2, "release");
662 lua_pushstring(L, uname_info.version); 670 lua_pushstring(L, uname_info.version);
663 lua_setfield(L, -2, "version"); 671 lua_setfield(L, -2, "version");
664 lua_pushstring(L, uname_info.machine); 672 lua_pushstring(L, uname_info.machine);
665 lua_setfield(L, -2, "machine"); 673 lua_setfield(L, -2, "machine");
666 #ifdef _GNU_SOURCE 674 #ifdef __USE_GNU
667 lua_pushstring(L, uname_info.domainname); 675 lua_pushstring(L, uname_info.domainname);
668 lua_setfield(L, -2, "domainname"); 676 lua_setfield(L, -2, "domainname");
669 #endif 677 #endif
670 return 1; 678 return 1;
671 } 679 }
724 732
725 /* File handle extraction blatantly stolen from 733 /* File handle extraction blatantly stolen from
726 * https://github.com/rrthomas/luaposix/blob/master/lposix.c#L631 734 * https://github.com/rrthomas/luaposix/blob/master/lposix.c#L631
727 * */ 735 * */
728 736
729 #if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE)
730 int lc_fallocate(lua_State *L) { 737 int lc_fallocate(lua_State *L) {
731 int ret; 738 int ret;
732 off_t offset, len; 739 off_t offset, len;
733 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE); 740 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
734 741
737 } 744 }
738 745
739 offset = luaL_checkinteger(L, 2); 746 offset = luaL_checkinteger(L, 2);
740 len = luaL_checkinteger(L, 3); 747 len = luaL_checkinteger(L, 3);
741 748
742 #if defined(__linux__) && defined(_GNU_SOURCE) 749 #if defined(__linux__)
743 errno = 0; 750 errno = 0;
744 ret = fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len); 751 ret = fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len);
745 752
746 if(ret == 0) { 753 if(ret == 0) {
747 lua_pushboolean(L, 1); 754 lua_pushboolean(L, 1);
757 lua_pushnil(L); 764 lua_pushnil(L);
758 lua_pushstring(L, strerror(errno)); 765 lua_pushstring(L, strerror(errno));
759 return 2; 766 return 2;
760 } 767 }
761 768
762 #else
763 #warning Only using posix_fallocate() fallback.
764 #warning Linux fallocate() is strongly recommended if available: recompile with -D_GNU_SOURCE
765 #warning Note that posix_fallocate() will still be used on filesystems that dont support fallocate()
766 #endif 769 #endif
767 770
768 ret = posix_fallocate(fileno(f), offset, len); 771 ret = posix_fallocate(fileno(f), offset, len);
769 772
770 if(ret == 0) { 773 if(ret == 0) {
782 } 785 }
783 786
784 return 2; 787 return 2;
785 } 788 }
786 } 789 }
787 #endif
788 790
789 /* Register functions */ 791 /* Register functions */
790 792
791 int luaopen_util_pposix(lua_State *L) { 793 int luaopen_util_pposix(lua_State *L) {
792 #if (LUA_VERSION_NUM > 501) 794 #if (LUA_VERSION_NUM > 501)
823 825
824 #ifdef WITH_MALLINFO 826 #ifdef WITH_MALLINFO
825 { "meminfo", lc_meminfo }, 827 { "meminfo", lc_meminfo },
826 #endif 828 #endif
827 829
828 #if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE)
829 { "fallocate", lc_fallocate }, 830 { "fallocate", lc_fallocate },
830 #endif
831 831
832 { NULL, NULL } 832 { NULL, NULL }
833 }; 833 };
834 834
835 lua_newtable(L); 835 lua_newtable(L);