News for hackers. Stuff that matters.
Re:'NSA addition to ifconfig' by poizan42 on Saturday May 28, 2016 @01:18PM (#11792415)
Completely offtopic, but am I the only one who don't like the usage of ! in the strcmp checks? To me conceptually !cond is checking for falsity, but the return value from strcmp is not logically a boolean
Re:'NSA addition to ifconfig' by bpineau on Saturday May 28, 2016 @01:24PM (#11792432)
It's just a shorthand for '== 0' (since !0 is true, and !<non-zero> is false). strcmp(3) returns 0 when the two strings are identical. So "if (!strcmp(a, b))" means "if the strings a and b are the same.
Re:'NSA addition to ifconfig' by anyfoo on Saturday May 28, 2016 @02:26PM (#11792704)
He understands that, but he does not like to use this shorthand for strcmp, because it conveys a conflicting meaning. I'm much the same, and I prefer explicitly writing "== 0" in those situations.
Re:'NSA addition to ifconfig' by protomyth on Saturday May 28, 2016 @02:46PM (#11792805)
I'm not sure I see it as conflicting meaning versus how C treats boolean expressions. Logical, I want to know if the strings are not equal and that uses boolean syntax to tell me that as opposed to comparing to zero which doesn't convey the meaning of the statement as fast.
Re:'NSA addition to ifconfig' by lfowles on Saturday May 28, 2016 @05:02PM (#11793490)
It's an artifact of implementation. In some zany world, strcmp could return EQUAL_ENUM. What you are ideally expressing is that you want to know that both strings are equal as determined by strcmp, not that the strcmp return value is nonzero. Now it just so happens that EQUAL_ENUM=0 in this world, so a side effect is that !strcmp(...) will still work. And we also have plenty of examples using this shorthand that it doesn't seem unnatural to a lot of devs. (Doesn't make it any less boots-on-head strange... when in Rome, do as the Romans do...)
Re:'NSA addition to ifconfig' by protomyth on Saturday May 28, 2016 @05:16PM (#11793562)
I'm not saying its not screwed up, and I'm sure some enums would be interesting, but they are using it in a logical context so I feel like the ! makes a fair amount of sense.

I do wonder what would have happened if truth had been defined like it is in Forth[1]? That would make me go with the enums.

1) False is 0 (all bits set to 0), and True is -1 (all bits set to 1 giving a twos-complement -1).

Re:'NSA addition to ifconfig' by int0x80 on Sunday May 29, 2016 @07:11AM (#11795937)
In the general case I'm with you, but I think !strcmp is a very common idiom that makes me(and most C programmers) go "string equality". There is also the streq(a, b) camp that #define streq !strcmp.
Re:'NSA addition to ifconfig' by moosterv on Saturday May 28, 2016 @04:58PM (#11793470)
I agree. I also hate var++; on a line by itself. I'd prefer var += 1;