Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: standard error codes Date: Thu, 12 Jan 2017 11:39:51 -0800 Organization: None to speak of Lines: 73 Message-ID: References: <96e16709-18eb-4399-86c4-a6f30a767df3@googlegroups.com> <64011b84-2f8d-44f2-902a-0eacfcdf630b@googlegroups.com> <11823fb6-613e-4283-95e8-dd930e152418@googlegroups.com> <546841f1-b478-44dc-abc0-22ca4f46e553@googlegroups.com> <87o9zdtgzo.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="768745921b79448f5a531b86aeb60d71"; logging-data="31379"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19X4RXKwQd44FLLE0MOMFUc" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:u3NqHMX2sNu13soD6Zd3FP4VOzY= sha1:dPLp6Z0oZpCq6gDba9F7LXxdqGY= Xref: csiph.com comp.lang.c:101541 Richard Heathfield writes: > On 12/01/17 17:28, Keith Thompson wrote: >> David Brown writes: >>> On 11/01/17 22:38, Gareth Owen wrote: >>>> BartC writes: >>>>> It just reinforces my point that strcmp by itself is unintuitive. >>> >>> I would say that strcmp is a useful function, but the name is not ideal. >>> It would make more sense, IMHO, if it were called "strdiff". >> >> strdiff would be a good name for a function that returns a Boolean >> result indicating whether its argument strings differ. strcmp can >> be used that way, but IMHO the name strdiff would be more misleading >> than strcmp. What it really does is *compare* two strings. >> >> It's nice for a function that returns a result to have a name >> that relfects the meaning of that result, but it's not always >> practical. > > Actually, I agree with David here: strdiff would be a better name, for > the same reason that diff(1) is well-named (see below). I still disagree, for the reasons I've stated, but it's not a big deal. >> And in the case of strcmp, you can think of the >> negative/zero/positive result it returns as a *comparison*, which >> makes the name strcmp, perfectly sensible. It's not the most >> intuitive name, but it's more than good enough. > > Yes. Although strdiff would be a better name, strcmp will do. > >> (A somewhat irrelevant example: the Unix "diff" command returns >> a result indicating success if the two files do *not* differ. >> That probably bothered me for a couple of minutes when I first >> learned about it.) > > diff returns 0 if the diff is 0. What's to be bothered about? The status returned by a command is not a count, it's a success/failure indication, with an opportunity to distinguish among different kinds of failures (at least on Unix-like systems, where EXIT_SUCCESS==0 and any non-zero status (modulo 256) denotes failure). The shell's "if" statement treats a successful status of 0 as a true condition. One might naively assume that: if diff file1 file2 ; then echo yes else echo no fi would print "yes" if the files differ. (csh/tcsh use different syntax, but the same idea holds.) And typically the output of the diff command is of more interest than its status. Again, this is not a big deal, just a small thing you have to learn if you want to use the diff command. It's far more important to be aware that the command returns 0 (success) if the files are the same, 1 (generic failure) if they differ, and 2 if there's an error, than to worry about whether that makes sense given the command's name. The fact that 0 and 1 sometimes mean false and true, respectively, and sometimes mean success and failure, can be confusing. There are good reasons the convention is the way it is, and even if there weren't we'd still just have to deal with it. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"