View Issue Details

IDProjectCategoryView StatusLast Update
00011791003.1(2016/18)/Issue7+TC2Base Definitions and Headerspublic2024-06-11 09:08
Reportercgraff Assigned To 
PrioritynormalSeverityEditorialTypeClarification Requested
Status ClosedResolutionAccepted 
NameChristopher M. Graff
Organization
User Reference
Sectiongetopt
Page Number0
Line Number0
Interp Status---
Final Accepted Text
Summary0001179: Code example for getopt usage has mismatched brackets wth no final bracket for main()
DescriptionThe example below has no final bracket for main(). This is obviously not a major issue, but it would be nice if the code was a bit easier to follow so as not tu use much cognitive overhead for developers.

#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(2);
    }
    for ( ; optind < argc; optind++) {
        if (access(argv[optind], R_OK)) {
    . . .
}
Desired ActionIt would be nice to change the code snippet example to one that compiles when the brackets are commented out or filled in with code. Or to at least indent the final mismached bracket so that it does not appear to be the final bracket for main(). Here is my suggestion:

#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(2);
    }
    for ( ; optind < argc; optind++) {
        if (access(argv[optind], R_OK)) {
            . . .
        }
    }
}
Tagstc3-2008

Relationships

related to 0001180 Closed getopt example uses exit(2) and should be EXIT_FAILURE 

Activities

Don Cragun

2019-02-14 17:21

manager   bugnote:0004253

Note that the only change suggested here is the addition of two closing braces.

Issue History

Date Modified Username Field Change
2017-12-20 22:31 cgraff New Issue
2017-12-20 22:31 cgraff Name => Christopher M. Graff
2017-12-20 22:31 cgraff Section => getopt
2017-12-20 22:31 cgraff Page Number => 0
2017-12-20 22:31 cgraff Line Number => 0
2019-02-14 17:20 Don Cragun Interp Status => ---
2019-02-14 17:20 Don Cragun Status New => Resolved
2019-02-14 17:20 Don Cragun Resolution Open => Accepted
2019-02-14 17:21 Don Cragun Tag Attached: tc3-2008
2019-02-14 17:21 Don Cragun Note Added: 0004253
2019-02-14 17:25 eblake Relationship added related to 0001180
2019-11-08 10:54 geoffclare Status Resolved => Applied
2024-06-11 09:08 agadmin Status Applied => Closed