Olufowobi Lawal <wolex...@...> wrote:
>
> Hi All!
> There are two possible argument to this programme (extract
> below) of the formats "192.148.14.1-192.148.14.30" and
> "192.148.14.1/21".  I can successfully extract the int's
> in either scenario but my error checking seems not intuitive
> enough. I was also wondering if there is a concise way of
> extracting this int's. and performing the error checking.

Parsing input is not one of C's strong points.

> And yeah, it's an HOMEWORK that needs perfection.

I only have nitpicks...

You should show your includes. Functions like printf and sscanf
are variadic and _require_ a prototype in scope.

> int main(int argc,char *argv[]){
>     
>     int i,ip_add[4],ip_add2[4];
>     int maskBit;
>     if( argc==1){

You should also care about argc > 2, and even argc == 0.

>         printf("NO INPUT!!\n");

A somewhat less than professional message. ;)

>         printf("Use --help for help\t -v for version number\n");

Error messages should go to stderr. I'd also avoid \t unless
you absolutely need to use it.

>         return 0;
>     }
>     else {
>         for(i = 1; i < argc; i++)
>         {
>             //if any of this options is input
>             if(!strcmp(argv[i], "--help") || !strcmp(argv[i],
> "/?") || !strcmp(argv[i], "-h"))

A little more whitespace would make this clearer.

>             {
>                 help();
>                 return 0;

You've encountered an error, but you return a success status.
This won't suit batch scripts too well.

>             }
>             if(!strcmp(argv[i], "--version")  || !strcmp(argv[i],
> "-v"))
>             {
>                 version();
>                 return 0;
>             }
>         }
>     }
> 
> 
>         //to improve, incorrect input formating        
>     if (sscanf(argv[1], "%d.%d.%d.%d-%d.%d.%d.%d",&ip_add[0],
> &ip_add[1],&ip_add[2],&ip_add[3],&ip_add2[0],&ip_add2[1],&ip_add2
> [2],&ip_add2[3])!=8){

I suggest %3d. If the user enters a number outside the range
of int, the behaviour of sscanf is undefined.

>     }  
>     else {

An empty if body seems awkward.

>     deaggregate(ip_add,ip_add2);

You should also show prototypes for your external functions.

>     return 0;
>     }
>     
>      if(  sscanf(argv[1],"%d.%d.%d.%d/%d",&ip_add[0],&ip_add
> [1],&ip_add[2],&ip_add[3],&maskBit)
>         !=5 ){
>        printf("Ip calculation can't be performed'\n");
>        printf("Deaggregation can't be performed'\n Check Input
> for errors\n");
>        return 0;
>         }
>         else ipcalc(ip_add,maskBit); 
>     return 0;
>   }

Given the pattern to match, one alternative might be along the
lines of... [untested]

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  const char *arg0 = "foo";

  void version(void)
  {
    printf("%s: version 1.0b\n", arg0);
    exit(0);
  }

  #define f0(x)    fprintf(fp, x "\n")
  #define f1(x, a) fprintf(fp, x "\n", a)

  void usage(FILE *fp)
  {
    f1("%s --version", arg0);
    f1("%s --help",    arg0);
    f1("%s ip-ip",     arg0);
    f1("%s ip/mask",   arg0);
    f0("");
    f0("Do something wonderful.");
    exit(fp == stderr ? EXIT_FAILURE : 0);
  }

  #define FB    "%4i"  /* beware! %4d may be safer */
  #define FIP   FB "." FB "." FB "." FB

  const char *sscan_ip(const char *s, int ip[4])
  {
    int n = 0, r;
    r = sscanf(s,
               FIP "%n",
               &ip[0], &ip[1], &ip[2], &ip[3],
               &n                           );
    return r == 4 ? s + n : 0;
  }

  int main(int argc, char **argv)
  {
    int ip[2][4];
    int mask;
    char c;
    const char *arg;

    if (argc != 2) usage(stderr);

    arg = argv[1];

    if (   strcmp(arg, "-h"    ) == 0
        || strcmp(arg, "--help") == 0
        || strcmp(arg, "/?"    ) == 0)
      usage(stdout);

    if (   strcmp(arg, "-v"       ) == 0
        || strcmp(arg, "--version") == 0)
      version();

    arg = sscan_ip(arg, ip[0]);
    if (!arg) usage(stderr);

    c = *arg++;

    if (c == '-')
    {
      if (!sscan_ip(arg, ip[1])) usage(stderr);

      printf("%d.%d.%d.%d-%d.%d.%d.%d\n",
             ip[0][0], ip[0][1], ip[0][2], ip[0][3],
             ip[1][0], ip[1][1], ip[1][2], ip[1][3]);
    }
    else if (c == '/')
    {
      if (sscanf(arg, FB, &mask) != 1) usage(stderr);

      printf("%d.%d.%d.%d/%d\n",
             ip[0][0], ip[0][1], ip[0][2], ip[0][3], mask);
    }
    else
      usage(stderr);

    return 0;
  }

-- 
Peter

Reply via email to