[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: using long long and printf("%m") in debian



su, 2006-08-20 kello 12:48 +0200, Hendrik Sattler kirjoitti:
> And there is always perror() which is something like:
>   printf("%s%s%s\n",(s?s:""),(s?": ":""),strerror(errno));
> So, using something like:
>   perror(__FUNCTION__);
> of
>   perror(NULL);
> is probably a good idea.

Not if you care about the quality of your error messages.

__FUNCTION__ isn't useful at all to the user, unless the user happens to
be a programmer and looks up the function in the source code.

        fprintf(stderr, "Could not read file: %s: %s\n", filename,
        strerror(errno));

Please consider the above a minimal requirement for an error message: it
reports the operation that was attempted (reading a file), the target of
the operation (the filename), and the error message.

perror is, on the whole, useful only in throwaway programs, or programs
whose users like guessing.

PS. Be careful of preserving the value of errno, if you call any other
functions between the failed operation and strerror. This is wrong:

        if (fgets(buf, sizeof(buf), f) == NULL) {
                fclose(f);
                fprintf(..., strerror(errno));
        }
        
Any library function may change the value of errno. Do this instead:

        if (fgets(buf, sizeof(buf), f) == NULL) {
                int e = errno;
                fclose(f);
                fprintf(..., strerror(e));
        }

PPS. This is getting off-topic for debian-devel, but I don't know where
to redirect further discussion, either. I'm not on any general
programming mailing lists.

-- 
Boilerplate programming mean tools lack power.



Reply to: