C Validating >=1 numeric argv[]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chris3020
    New Member
    • May 2022
    • 15

    C Validating >=1 numeric argv[]

    I have a simple utility expecting three command-line arguments:
    ./myutil -k 123 45

    argv[1] is a text flag of some sort: memcmp() against allowed flags; works!

    argv[2] and argv[3] need to sit in unsigned 32-bit integers and are required to be >= 1.

    For now I'm using:
    strstr() to look for "-" ...and assuming absence of "-" means +ve number-as-string.
    atoi() to get assumed +ve number into unsigned thisuint, thatuint
    Then checking thisuint >= 1, thatuint >=1
    It works for the test cases I've tried, but it seems mmmmmm clumsy.

    Is there a better way?
    As stated, this is a "simple utility": 1000 lines of validation code is the wrong choice.

    Chris
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Do you really need 1000 lines of code to do this validation in C? I do not have enough experience with C, but that seems too much. Maybe you can post the code that you wrote to get tips on how to improve the code.

    Comment

    • Chris3020
      New Member
      • May 2022
      • 15

      #3
      @Luuk

      I admit to being crazy, but I'm not quite crazy enough to devote 1000 lines of code to input validation for an integer!
      Was just looking for a neat way that didn't involve a huge (1000 lines...) library designed to handle every nit-picky facet of argv[] parsing.

      Since posting, I remembered abs() ...doh!

      Input validation is now simple:
      Code:
        unsigned this = abs(atoi(argv[2]));
        unsigned that = abs(atoi(argv[3]));
        if (this == 0 || that == 0){
          puts("\n USAGE both this & that must be > 0\n exit...");
          exit(EXIT_FAILURE);
        }
      Chris
      Last edited by Chris3020; Aug 17 '22, 07:23 PM. Reason: delete un wanted <space>

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        When entering `-123456789012345 67890` as parameter, te value `-2147483648` is returned... 😢😉

        Comment

        • dev7060
          Recognized Expert Contributor
          • Mar 2017
          • 655

          #5
          I have a simple utility expecting three command-line arguments:
          ./myutil -k 123 45

          argv[1] is a text flag of some sort: memcmp() against allowed flags; works!

          argv[2] and argv[3] need to sit in unsigned 32-bit integers and are required to be >= 1.

          For now I'm using:
          strstr() to look for "-" ...and assuming absence of "-" means +ve number-as-string.
          atoi() to get assumed +ve number into unsigned thisuint, thatuint
          Then checking thisuint >= 1, thatuint >=1
          It works for the test cases I've tried, but it seems mmmmmm clumsy.

          Is there a better way?
          As stated, this is a "simple utility": 1000 lines of validation code is the wrong choice.
          What specific validations are you looking for and why? How have you tested user input before? Who is the target user? There could be many things that may not go right. Using the primitive way is probably reasonable according to the use case like dealing with simple use cases (very few known flags, assumption that valid args are present in the correct order, format, sequence, type, range, count, etc). If it gets more complex than that or if the aim is to have a robust code and add flexibility and make sure everything is handled the right way, using a library would probably be a better choice. Having a well-tested library to take care of that stuff may allow to better focus on the mainstream part of the code.

          From the provided context, I guess a few nested if-else blocks may be enough. Maybe show the complete validation part of the code for optimization.

          Comment

          • Chris3020
            New Member
            • May 2022
            • 15

            #6
            ...sorry for radio-silence: some stuff came up in the real-world.

            Turned out that I was the only user for the C-version of the utility!
            A friend asked for something to calculate the product of the first N prime-numbers after some number X.
            ...run-times of a few micro-seconds did not overcome his dislike of the command-line.
            Once the algorithm was ironed out in C it got translated into VBA! He's happier with that.

            @Luuk: thanks for the fun overflow illustration. When I have a moment I'll check what happens when an unsigned int overflows.
            @dev7060: yes, for anything more serious I will try to find a decent library. It hasn't come up yet: my teach-myself-C exercise is a rework of something I did in Perl a year or two ago (too slow AND a lousy algorithm). C-version runs with hard-coded parameters for now. Three more modules to complete ...I hope by 27 December to mark the 1st anniversary of trying to learn C.

            Chris

            Comment

            Working...