posix

Why do both `htons` and `ntohs` exist?

五迷三道 提交于 2021-02-20 10:12:57
问题 I am not sure why htons and ntohs both exist in the standard library. They do exactly the same thing -- unless I'm confused somehow! The same goes for htonl and ntohl . 回答1: They make for self-documenting code that tells the reader whether the data is in host- or network- order. 回答2: This is in case a machine has some sort of unusual endianness besides big-endian or little-endian that isn't one or more simple byte swaps. For example, if the value 0x0A0B0C0D was represented internally as 0B 0C

Why do both `htons` and `ntohs` exist?

安稳与你 提交于 2021-02-20 10:11:14
问题 I am not sure why htons and ntohs both exist in the standard library. They do exactly the same thing -- unless I'm confused somehow! The same goes for htonl and ntohl . 回答1: They make for self-documenting code that tells the reader whether the data is in host- or network- order. 回答2: This is in case a machine has some sort of unusual endianness besides big-endian or little-endian that isn't one or more simple byte swaps. For example, if the value 0x0A0B0C0D was represented internally as 0B 0C

Is ./*/ portable?

家住魔仙堡 提交于 2021-02-19 04:27:06
问题 I often use ./*/ in a for loop like for d in ./*/; do : # do something with dirs done to match all non-hidden directories in current working directory, but I'm not really sure if this is a portable way to do that. I have bash, dash and ksh installed on my system and it works with all, but since POSIX spec doesn't say anything about it (or it says implicitly, and I missed it) I think I can't rely on it. I also checked POSIX bug reports, but to no avail, there's no mention of it there as well.

How to determine if a path is inside a directory? (POSIX)

不羁岁月 提交于 2021-02-18 23:00:39
问题 In C, using POSIX calls, how can I determine if a path is inside a target directory? For example, a web server has its root directory in /srv , this is getcwd() for the daemon. When parsing a request for /index.html , it returns the contents of /srv/index.html . How can I filter out requests for paths outside of /srv ? /../etc/passwd , /valid/../../etc/passwd , etc. Splitting the path at / and rejecting any array containing .. will break valid accesses /srv/valid/../index.html . Is there a

How can I determine if the operating system is POSIX in C?

送分小仙女□ 提交于 2021-02-17 08:46:34
问题 Related questions How can I detect the operating system in C/C++? How can I find out what operating system I am running under in GCC or in ANSI C? I'd be fine If I can know if I'm running on POSIX. UPDATE: It doesn't make a difference to me whether it is at compile time or run time. I'm using this in a debug routine, so performance isn't that important. I'm looking for the path separator. Windows & Unix/Linux/BSD would be fine. And, I'm trying to find the basename on a path. I found some

How to get further information on SIGFPE signal?

亡梦爱人 提交于 2021-02-16 20:11:13
问题 This is from The GNU C Library Reference Manual int SIGFPE The SIGFPE signal reports a fatal arithmetic error. This signal actually covers all arithmetic errors, including division by zero and overflow. BSD systems provide the SIGFPE handler with an extra argument that distinguishes various causes of the exception. In order to access this argument, you must define the handler to accept two arguments, which means you must cast it to a one-argument function type in order to establish the

How to get further information on SIGFPE signal?

末鹿安然 提交于 2021-02-16 20:11:07
问题 This is from The GNU C Library Reference Manual int SIGFPE The SIGFPE signal reports a fatal arithmetic error. This signal actually covers all arithmetic errors, including division by zero and overflow. BSD systems provide the SIGFPE handler with an extra argument that distinguishes various causes of the exception. In order to access this argument, you must define the handler to accept two arguments, which means you must cast it to a one-argument function type in order to establish the

Why are there multiple C functions for case-insensitive comparison

混江龙づ霸主 提交于 2021-02-16 14:18:17
问题 For comparing any strings without considering their case, there are various C library functions such as strcasecmp() , stricmp() and stricmpi() . What is the difference between these? 回答1: There are multiple ways to do many things primarily because the standards process lags behind implementations. People see the need for a function (in this case, case insensitive string comparison) and some compiler writers/library writers implement a function called strcmpi , while another group implements

Posix_spawn performance when capturing process output

人走茶凉 提交于 2021-02-11 06:22:57
问题 I am trying to use posix_spawn instead of fork/exec to get some performance gain. My current project is written in Python, so I used this Python binding. Also I tried some of its forks, and after that I wrote my own posix_spawn binding in Cython (to get rid of some dependencies), but obtained almost the same results. There is indeed a significant speed-up when I just need to run processes without capturing stdout/stderr. But when I do need it (for my project it is necessary), the posix_spawn

fnmatch usage with complicate pattern C

半城伤御伤魂 提交于 2021-02-10 15:17:10
问题 I'm only able to match simple patterns like: "[0-9]" with fnmatch("[0-9]", tocheck, 0) . If I try something more complicated with ? or . or even a combination of these how do I use fnmatch ? I saw there are some flags that can do the trick, but I don't know how to use because I'm fairly new to C. EDIT: I saw the comment asking to give more details: #include <stdio.h> #include <fnmatch.h> int main(int argc, char **argv) { const char *patternOne = "[0-9]"; const char *patternTwo = ".?[a-z0-9]*?