posix

How can adding a header increase portability? (sys/time.h)

北战南征 提交于 2019-12-20 03:14:49
问题 I just noticed this line in the getrusage man page: Including <sys/time.h> is not required these days, but increases portability. (Indeed, struct timeval is defined in <sys/time.h> ) What? Since struct rusage contains struct timeval as a member, surely sys/resource.h must include sys/time.h or the type would be incomplete and unusable? How could this comment ever have made sense? How could it ever have not been necessary? How could portability have ever been helped? 回答1: In general, it was

How can I unit/integration test a program's ANSI escape code behavior?

青春壹個敷衍的年華 提交于 2019-12-20 03:08:46
问题 I've started adding some coloring and other functionality (line resets, etc.) to my application and would like to have some unit tests covering the behavior. I know I could just assert that the output contains the appropriate \e[... codes, but that's brittle. For one, it would fail if it were swapped to \033 or otherwise refactored in trivial but not identical ways. More to the point however, testing the sequence of characters doesn't really do what I want. I want to assert or verify that the

How can I unit/integration test a program's ANSI escape code behavior?

柔情痞子 提交于 2019-12-20 03:08:13
问题 I've started adding some coloring and other functionality (line resets, etc.) to my application and would like to have some unit tests covering the behavior. I know I could just assert that the output contains the appropriate \e[... codes, but that's brittle. For one, it would fail if it were swapped to \033 or otherwise refactored in trivial but not identical ways. More to the point however, testing the sequence of characters doesn't really do what I want. I want to assert or verify that the

Simple UTF8->UTF16 string conversion with iconv

廉价感情. 提交于 2019-12-20 02:11:12
问题 I want to write a function to convert a UTF8 string to UTF16 (little-endian). The problem is, the iconv function does not seem to let you know in advance how many bytes you'll need to store the output string. My solution is to start by allocating 2*strlen(utf8) , and then run iconv in a loop, increasing the size of that buffer with realloc if necessary: static int utf8_to_utf16le(char *utf8, char **utf16, int *utf16_len) { iconv_t cd; char *inbuf, *outbuf; size_t inbytesleft, outbytesleft,

Simple UTF8->UTF16 string conversion with iconv

风流意气都作罢 提交于 2019-12-20 02:11:10
问题 I want to write a function to convert a UTF8 string to UTF16 (little-endian). The problem is, the iconv function does not seem to let you know in advance how many bytes you'll need to store the output string. My solution is to start by allocating 2*strlen(utf8) , and then run iconv in a loop, increasing the size of that buffer with realloc if necessary: static int utf8_to_utf16le(char *utf8, char **utf16, int *utf16_len) { iconv_t cd; char *inbuf, *outbuf; size_t inbytesleft, outbytesleft,

Where does the recursive variable expansion in bash/shell numeric contexts come from?

只谈情不闲聊 提交于 2019-12-20 01:08:51
问题 The POSIX spec states with regard to Arithmetic Expansion that [i]f the shell variable x contains a value that forms a valid integer constant, optionally including a leading plus or minus sign, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value. Which is a reasonable shortcut and cleans up complicated expressions rather nicely. bash (versions 3.2.25(1)-release from CentOS 5 and 4.3.33(1)-release from debian unstable) as well as ksh ( Version AJM 93t+ 2010-06-21

fallocate vs posix_fallocate

只谈情不闲聊 提交于 2019-12-19 15:33:46
问题 I am debating which function to use between posix_fallocate and fallocate . posix_fallocate writes a file right away (initializes the characters to NULL). However, fallocate does not change the file size (when using FALLOC_FL_KEEP_SIZE flag). Based on my experimentation, it seems that fallocate does not write NULL or zero characters to the file. Can someone please comment based on your experience? Thanks for your time. 回答1: Having files that take up more storage space than their displayed

Can it be assumed that `pthread_cond_signal` will wake the signaled thread atomically with regards to the mutex bond to the condition variable?

╄→尐↘猪︶ㄣ 提交于 2019-12-19 11:53:37
问题 Quoting POSIX: The pthread_cond_broadcast() or pthread_cond_signal() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling pthread_cond_broadcast() or pthread_cond_signal() . "If predictable scheduling behavior is required".

objective-c NSFilePosixPermissions to human readable NSString

匆匆过客 提交于 2019-12-19 06:56:08
问题 is there a way to get human readable string (@"drwxr-xr-x" for example) from an NSFilePosixPermissions integer ? 回答1: The file system permissions attribute is simply an unsigned long value. The code below could obviously be made more efficient but it shows [more or less] what needs to be done to get the string you want: // The indices of the items in the permsArray correspond to the POSIX // permissions. Essentially each bit of the POSIX permissions represents // a read, write, or execute bit

how to determine if pipe can be written

两盒软妹~` 提交于 2019-12-19 05:37:09
问题 Is there a way (in C, or preferably in Perl) to determine whether a named pipe may be written - i.e. there is an active reading process It seems that if I open for write nonblocking, the open returns at once but a select for write also returns immediately. The goal is for the writing process to just carry on (i.e. skip sending) if the reading end is not ready 回答1: Chances are that you are not paying attention to the return codes from open . If you open a FIFO for writing and as non-blocking