scrut(1): further error-proofing
This commit is contained in:
		
							parent
							
								
									da190f713c
								
							
						
					
					
						commit
						98c4d94f6d
					
				| @ -17,6 +17,7 @@ | |||||||
|  * along with this program. If not, see https://www.gnu.org/licenses/.
 |  * along with this program. If not, see https://www.gnu.org/licenses/.
 | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
|  | #include <assert.h> /* assert(3) */ | ||||||
| #include <stdio.h> /* fprintf(3), stderr, NULL */ | #include <stdio.h> /* fprintf(3), stderr, NULL */ | ||||||
| #include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */ | #include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */ | ||||||
| #include <string.h> /* memset(3), strchr(3) */ | #include <string.h> /* memset(3), strchr(3) */ | ||||||
| @ -29,6 +30,7 @@ | |||||||
| 
 | 
 | ||||||
| char *program_name = "scrut"; | char *program_name = "scrut"; | ||||||
| #define OPTS "bcdefgkprsuwxLS" | #define OPTS "bcdefgkprsuwxLS" | ||||||
|  | /* this is an array so main:sel's size can be known at compile time */ | ||||||
| static char opts[] = OPTS; | static char opts[] = OPTS; | ||||||
| 
 | 
 | ||||||
| static int | static int | ||||||
| @ -58,7 +60,10 @@ int main(int argc, char *argv[]) { | |||||||
| 		memset(sel, '\0', sizeof sel); | 		memset(sel, '\0', sizeof sel); | ||||||
| 		for (int c; (c = getopt(argc, argv, opts)) != -1;) { | 		for (int c; (c = getopt(argc, argv, opts)) != -1;) { | ||||||
| 			if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); } | 			if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); } | ||||||
| 			else { sel[p - opts] = c; } | 			else { | ||||||
|  | 				assert(p - opts < sizeof sel / sizeof *sel); /* bounds check */ | ||||||
|  | 				sel[p - opts] = c; | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		/* straighten out selections; permute out nulls */ | 		/* straighten out selections; permute out nulls */ | ||||||
| @ -73,7 +78,7 @@ int main(int argc, char *argv[]) { | |||||||
| 
 | 
 | ||||||
| 	if (optind == argc) { return usage(argv[0]); } | 	if (optind == argc) { return usage(argv[0]); } | ||||||
| 
 | 
 | ||||||
| 	for (argv += optind ; *argv != NULL; ++argv) { | 	for (argv += optind ; *argv != NULL; argv = &argv[1]) { | ||||||
| 		struct stat buf; | 		struct stat buf; | ||||||
| 
 | 
 | ||||||
| #ifdef __OpenBSD__ | #ifdef __OpenBSD__ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user