libc

What's the difference between hard and soft floating point numbers?

你。 提交于 2019-11-28 15:24:28
When I compile C code with my cross toolchain, the linker prints pages of warnings saying that my executable uses hard floats but my libc uses soft floats. What's the difference? Hard floats use an on-chip floating point unit. Soft floats emulate one in software. The difference is speed. It's strange to see both used on the same target architecture, since the chip either has an FPU or doesn't. You can enable soft floating point in GCC with -msoft-float. You may want to recompile your libc to use hardware floating point if you use it. There are three ways to do floating point arithmetic: Use

Initializing sigset_t in Rust

跟風遠走 提交于 2019-11-28 14:28:16
I'm trying to learn more about the FFI in Rust and linking with C libraries (specifically libc ). While on my "quest" I came accross the following problem. Normal pattern in C void(* sig_set(int sig, void(*handler)(int))) { // uninitialized sigaction structs struct sigaction new_action, old_action; // assign options to new action new_action.sa_flags = SA_RESTART; new_action.sa_handler = handler; sigemptyset(&new_action.sa_mask); if(sigaction(sig, &new_action, &old_action) < 0) { fprintf(stderr, "Error: %s!\n", "signal error"); exit(1); } return old_action.sa_handler; } Attempt in Rust fn sig

How to tell which interface the socket received the message from?

百般思念 提交于 2019-11-28 12:01:27
If a socket is bound to IN6ADDR_ANY or INADDR_ANY and you use a call such as recvfrom() to receive messages on the socket. Is there a way to find out which interface the message came from? In the case of IPv6 link-scope messages, I was hoping that the from argument of recvfrom() would have the scope_id field initialized to the interface Id. Unfortunately it is set to 0 in my test program. Anybody know of a way to find out this information? Apart from binding to each interface, I'm not aware of a way with IPv4, per se. IPv6 has added the IPV6_PKTINFO socket option to address this shortcoming.

Determing the number of bytes ready to be recv()'d

瘦欲@ 提交于 2019-11-28 11:49:20
I can use select() to determine if a call to recv() would block, but once I've determined that their are bytes to be read, is their a way to query how many bytes are currently available before I actually call recv()? nickm If your OS provides it (and most do), you can use ioctl(..,FIONREAD,..): int get_n_readable_bytes(int fd) { int n = -1; if (ioctl(fd, FIONREAD, &n) < 0) { perror("ioctl failed"); return -1; } return n; } Windows provides an analogous ioctlsocket(..,FIONREAD,..), which expects a pointer to unsigned long: unsigned long get_n_readable_bytes(SOCKET sock) { unsigned long n = -1;

IsBadReadPtr analogue on Unix

那年仲夏 提交于 2019-11-28 11:25:47
问题 Is there a function analogous to IsBadReadPtr in Unix? At least some functionalities of IsBadReadPtr? I want to write a procedure which would react if something bad happens to a process (like SIGSEGV ) and recover some information. But I want to check the pointers to make sure that the data is not corrupt and see if they can be accessed safely. Otherwise the crash handling procedure itself will crash, thus becoming useless. Any suggestions? 回答1: The usual way to do this on POSIX systems is to

Why the absolute value of the max negative integer -2147483648 is still -2147483648?

折月煮酒 提交于 2019-11-28 07:26:54
The result of abs(-2147483648) is -2147483648, isn't it? it seems unacceptable. printf("abs(-2147483648): %d\n", abs(-2147483648)); output: abs(-2147483648): -2147483648 Alexey Frunze The standard says about abs() : The abs , labs , and llabs functions compute the absolute value of an integer j . If the result cannot be represented, the behavior is undefined. And the result indeed cannot be represented because the 2's complement representation of signed integers isn't symmetric. Think about it... If you have 32 bits in an int , that gives you 2 32 distinct values from INT_MIN to INT_MAX . That

How does libc provide functions with two names?

这一生的挚爱 提交于 2019-11-28 04:48:17
问题 Before the advent of direct binding (-B direct) libc provided many functions with two names. For example, getpwent() and _getpwent(). These two names referred to exactly the same function in libc. How does libc make two function names point to the same implementation? I think it should not be as easy as writing the same code twice though. 回答1: It's done via weak aliases a "nonstandard" linker trick that's been around since early unices and that's supported by all unix compilers/linkers I know

Are posix regcomp and regexec threadsafe? In specific, on GNU libc?

强颜欢笑 提交于 2019-11-28 03:02:35
问题 Two separate questions here really: Can I use regexes in a multithreaded program without locking and, if so, can I use the same regex_t at the same time in multiple threads? I can't find an answer on Google or the manpages. 回答1: http://www.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html 2.9.1 Thread-Safety All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the following functions1 need not be thread-safe. ... regexec and regcomp are not in that

Getting GCC to compile without inserting call to memcpy

坚强是说给别人听的谎言 提交于 2019-11-27 23:42:28
I'm currently using GCC 4.5.3, compiled for PowerPC 440, and am compiling some code that doesn't require libc. I don't have any direct calls to memcpy(), but the compiler seems to be inserting one during the build. There are linker options like -nostdlib, -nostartfiles, -nodefaultlibs but I'm unable to use them as I'm not doing the linking phase. I'm only compiling. With something like this: $ powerpc-440-eabi-gcc -O2 -g -c -o output.o input.c If I check the output.o with nm, I see a reference to memcpy: $ powerpc-440-eabi-nm output.o | grep memcpy U memcpy $ The GCC man page makes it clear

Get the time zone GMT offset in C

微笑、不失礼 提交于 2019-11-27 21:21:12
I'm using the standard mktime function to turn a struct tm into an epoch time value. The tm fields are populated locally, and I need to get the epoch time as GMT. tm has a gmtoff field to allow you to set the local GMT offset in seconds for just this purpose. But I can't figure out how to get that information. Surely there must be a standard function somewhere that will return the offset? How does localtime do it? Just do the following: #define _GNU_SOURCE /* for tm_gmtoff and tm_zone */ #include <stdio.h> #include <time.h> /* Checking errors returned by system calls was omitted for the sake