View Issue Details

IDProjectCategoryView StatusLast Update
00011801003.1(2016/18)/Issue7+TC2Base Definitions and Headerspublic2019-02-14 17:35
Reportercgraff Assigned To 
PrioritynormalSeverityEditorialTypeClarification Requested
Status ClosedResolutionRejected 
NameChristopher M. Graff
Organization
User Reference
Sectiongetopt
Page Number0
Line Number0
Interp Status---
Final Accepted Text
Summary0001180: getopt example uses exit(2) and should be EXIT_FAILURE
DescriptionThe usage of exit(2) in the getopt example seems like a non-standard exit code for a failed utility. Typically, I see either exit(1) or exit(EXIT_FAILURE) and the value of EXIT_FAILURE is generally 1 or exit(1).
if (errflg) {
    fprintf(stderr, "usage: . . . ");
    exit(2);
}
Desired ActionThis example is updated to include actions from my previous report about mismatched brackets and to use EXIT_FAILURE.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int
main(int argc, char *argv[ ])
{
    int c;
    int bflg = 0, aflg = 0, errflg = 0;
    char *ifile;
    char *ofile;
    . . .
    while ((c = getopt(argc, argv, ":abf:o:")) != -1) {
        switch(c) {
        case 'a':
            if (bflg)
                errflg++;
            else
                aflg++;
            break;
        case 'b':
            if (aflg)
                errflg++;
            else
                bflg++;
            break;
        case 'f':
            ifile = optarg;
            break;
        case 'o':
            ofile = optarg;
            break;
        case ':': /* -f or -o without operand */
            fprintf(stderr,
                "Option -%c requires an operand\n", optopt);
            errflg++;
            break;
        case '?':
            fprintf(stderr,
                "Unrecognized option: '-%c'\n", optopt);
            errflg++;
        }
    }
    if (errflg) {
        fprintf(stderr, "usage: . . . ");
        exit(EXIT_FAILURE);
    }
    for ( ; optind < argc; optind++) {
        if (access(argv[optind], R_OK)) {
            . . .
        }
    }
}
TagsNo tags attached.

Relationships

related to 0001179 Closed Code example for getopt usage has mismatched brackets wth no final bracket for main() 

Activities

geoffclare

2019-02-14 17:34

manager   bugnote:0004254

This was discussed in the February 14, 2019 teleconference. Using exit(1) or exit(2) are both valid examples, however utilities should not always use exit(1) for usage errors, since there are utilities for which exit(1) is not appropriate when there is a usage error. For example, in grep an exit status of 1 indicates no match whereas > 1 is an error. Therefore we do not want the example code to suggest that exit(1) should always be used for usage errors. As the existing example is valid, there is no reason to change to a different, but equally valid, example.

Issue History

Date Modified Username Field Change
2017-12-20 22:51 cgraff New Issue
2017-12-20 22:51 cgraff Name => Christopher M. Graff
2017-12-20 22:51 cgraff Section => getopt
2017-12-20 22:51 cgraff Page Number => 0
2017-12-20 22:51 cgraff Line Number => 0
2019-02-14 17:25 eblake Relationship added related to 0001179
2019-02-14 17:34 geoffclare Note Added: 0004254
2019-02-14 17:34 geoffclare Interp Status => ---
2019-02-14 17:34 geoffclare Status New => Closed
2019-02-14 17:34 geoffclare Resolution Open => Rejected
2019-02-14 17:35 eblake Summary getopt example uses exit(2) and should be EIXT_FAILURE => getopt example uses exit(2) and should be EXIT_FAILURE