Anonymous | Login | 2024-12-02 07:52 UTC |
Main | My View | View Issues | Change Log | Docs |
Viewing Issue Simple Details [ Jump to Notes ] | [ Issue History ] [ Print ] | ||||||
ID | Category | Severity | Type | Date Submitted | Last Update | ||
0001179 | [1003.1(2016/18)/Issue7+TC2] Base Definitions and Headers | Editorial | Clarification Requested | 2017-12-20 22:31 | 2024-06-11 09:08 | ||
Reporter | cgraff | View Status | public | ||||
Assigned To | |||||||
Priority | normal | Resolution | Accepted | ||||
Status | Closed | ||||||
Name | Christopher M. Graff | ||||||
Organization | |||||||
User Reference | |||||||
Section | getopt | ||||||
Page Number | 0 | ||||||
Line Number | 0 | ||||||
Interp Status | --- | ||||||
Final Accepted Text | |||||||
Summary | 0001179: Code example for getopt usage has mismatched brackets wth no final bracket for main() | ||||||
Description |
The 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 Action |
It 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)) { . . . } } } |
||||||
Tags | tc3-2008 | ||||||
Attached Files | |||||||
|
Relationships | ||||||
|
Notes | |
(0004253) Don Cragun (manager) 2019-02-14 17:21 |
Note that the only change suggested here is the addition of two closing braces. |
Mantis 1.1.6[^] Copyright © 2000 - 2008 Mantis Group |