Package: qfits / 6.2.0-8

06-dfits-args.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--- qfits-6.2.0.orig/main/dfits.c
+++ qfits-6.2.0/main/dfits.c
@@ -58,7 +58,7 @@
  -----------------------------------------------------------------------------*/
 
 void usage(char * pname) ;
-void parse_cmd_line(int, char **, int *, int *, int *) ;
+int parse_cmd_line(int, char **, int *, int *, int *) ;
 int dump_fits_filter(FILE *, int) ;
 int dump_hdr_filter(FILE *, int) ;
 int dump_fits(char *, int) ;
@@ -74,11 +74,9 @@ int main(int argc, char *argv[])
     int        filter ;
     int        err ;
 
-    /* No arguments prints out a usage message */
-    if (argc<2) usage(argv[0]);
-
     /* Parse command-line options */
-    parse_cmd_line(argc, argv, &xtnum, &filter, &c_arg);
+    if (!parse_cmd_line(argc, argv, &xtnum, &filter, &c_arg))
+        usage(argv[0]);
 
     /* Filter mode: process data received from stdin */
     if (filter) {
@@ -126,7 +124,7 @@ void usage(char * pname)
     exit(1) ;
 }
 
-void parse_cmd_line(
+int parse_cmd_line(
         int         argc,
         char    **  argv,
         int     *    xtnum,
@@ -137,16 +135,22 @@ void parse_cmd_line(
     *xtnum  = -1 ;
     *c_arg  = argc-1 ;
 
+    /* No arguments, return an error */
+    if (argc<2) return 0;
+
     /* If '-' is on the command-line, it must be the last argument */
     if (!strcmp(argv[argc-1], "-")) *filter = 1 ;
     /* If -x xtnum is on the command-line, it must be the first two arguments */
     if (!strcmp(argv[1], "-x")) {
-        *xtnum = atoi(argv[2]);
+        char *endptr;
+        if (argc < 3) return 0;
+        *xtnum = strtol(argv[2], &endptr, 10);
+        if (*endptr != 0) return 0;
         *c_arg = 3 ;
     } else {
         *c_arg = 1 ;
     }
-    return ;
+    return 1;
 }
 
 /* Strip off all blank characters in a string from the right-side. */