Thread: Issue with multi function utility program

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    1

    Question Issue with multi function utility program

    The following program is supposed to read from the command line( using argv[]) and execute either one of three functions:
    right(decides whether or not a triangle is a right triangle based on the side length: by typing mu -r [sidea] [sideb] [sidec]
    findtextfinds the sequence of a string inside of a given file: (ie,"hello") and lists the line number where it found it.
    count: (counts the number of tabs, words and backspaces).
    Findtext.c is functioning as it should:
    for example if i type ./mu -f [string] [file.txt]
    it successfully list the file and the line number in the file in which it was found.
    but when I run the same code with the -r(right) option it gives me the following segmentation fault:
    Program name: ./mu 3 Segmentation fault (core dumped)
    where am i going wrong in this code?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#include "count.h"
    //#include "right.h"
    //#include "findtext.h"
    
    #define STAND_ALONE 1
    void right(int, char **);
    void count(int, char **);
    void findtext(int, char **);
    #ifdef STAND_ALONE 
    int main(int argc, char *argv[])
    {
          
          printf("\n");
          printf("Can only use one option(-f, -c, -r) at once. sorry!\n");
          printf("\n");
            
    
             printf("Program name: %s\n", argv[0]);
    
        while ((argc > 1) && (argv[1][0] == '-'))
        {
            switch (argv[1][1])
            {
                case 'f': // findtext.c
                    printf("%s\n",&argv[1][2]);
                    findtext(argc, argv);
                                    break;
    
                case 'r': // right.c
                    printf("%s\n",&argv[1][2]);
                    right(argc, argv);
                    break;
                            case 'c': // count.c
                    printf("%s\n",&argv[1][2]);
                    count(argc,argv);
                    break;
    
                default:
                    printf("Wrong Argument: %s\n", argv[1]);
                    
            }
    
            ++argv;
            --argc;
        }
        return (0);
    }
    
    #endif
    
    void right(int argc, char *argv[]){
    int a;
    int b;
    int c;
    int largest;
    int a2;
    int b2;
    int c2;
    
    /*if(argc != 4){
    printf("please enter 3 sides, only \n");
    } */
    
    a = atoi(argv[2]);
    b = atoi(argv[3]);
    c = atoi(argv[4]);
    //printf("argv2:%s ",argv[2]);
    if((a <= 0 )|| (b <= 0) || (c <= 0))
         printf("Only positive values allowed\n"); exit(0);
    
    a2 = (a*a);
    b2 = (b*b);
    c2 = (c*c);
    
     
    if((c > a) && (c > b))
         largest = c;
    if((b > a) && (b > c))
         largest = b;
    if((a > b) && (a > c))
         largest = a;
    
    
    
    if(largest == a){
      printf("HEy hey hey!");
      if((b2 + c2) == a2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); } 
       else{printf("%s %s %s is not a right triangle\n",argv[3],argv[2],argv[4]);}
     }
    
    if(largest == b){
       printf("HEy");
      if((a2 + c2) == b2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); } 
       else{printf("%s %s %s is not a right triangle\n",argv[2],argv[3],argv[4]);}
       
     }
    
    if(largest == c){
      printf("yo");
       if((a2 + b2) == c2){ printf("%s %s %s is a right triangle\n",argv[2],argv[3],argv[4]); } 
       else{printf("%s %s %s is not a right triangle\n",argv[2],argv[3],argv[4]);}
       
     }
    
      
    
    
    } /* end method right() */
    
    
    
    void findtext(int argc, char *argv[]){
    FILE *fin;
    char buffer[100];
    int counter;
    char *ptr = buffer;
    char *result;
    
    //if(argc != 3) {printf("Usage: %s filename  argc:%d\n", argv[0], argc); exit(1);}
      fin = fopen(argv[3], "r");
      if(!fin) {printf("Unable to open %s\n", argv[2]); exit(1); }
      
     counter = 0; 
     while (fgets(buffer, 99, fin)){
       counter = counter + 1; 
       if(strstr(ptr,argv[2])){
          printf("%d. %s", counter, ptr);
          printf("\n");
           
         }
      
      }
      fclose (fin);
    
    
    }
    
    
    void count(int argc,  char **argv){
    FILE *fin;
    
    int lcounter = 0;
    int count = 0;
    char name[100];
    char ch;
    
    int word = 0;
    int nchar = 0;
    
    
    
    fin = fopen(argv[1],"r"); // open file
    
    if(fin==0){
    
    printf("Could not find specified file.\n");
    exit(0);
    }
    
    while((ch = getc(fin)) != EOF)
    {
    
      nchar++;
      if(ch == '\n')
        lcounter++;
      if(isspace(ch) || ch == '\t' || ch == '\n')
          word++;
    }
    
    printf("number of characters: %d\n",nchar);
    printf("Lines: %d\n",lcounter);
    printf("words: %d\n",word);
    printf("\n");
    
    fclose(fin);
     
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, compile with debug enabled.
    Code:
    $ gcc -g foo.c
    foo.c: In function ‘findtext’:
    foo.c:128:7: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default]
    Then use the debugger.
    Code:
    $ gdb -q ./a.out 
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) run -r 3 4 5
    Starting program: /home/sc/Documents/a.out -r 3 4 5
    
    Can only use one option(-f, -c, -r) at once. sorry!
    
    Program name: /home/sc/Documents/a.out
    
    [Inferior 1 (process 5147) exited normally]
    (gdb) run -r 3
    Starting program: /home/sc/Documents/a.out -r 3
    
    Can only use one option(-f, -c, -r) at once. sorry!
    
    Program name: /home/sc/Documents/a.out
    
    
    Program received signal SIGSEGV, Segmentation fault.
    ____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, group=<optimized out>, loc=0x7ffff7dd56a0) at ../stdlib/strtol_l.c:298
    298	../stdlib/strtol_l.c: No such file or directory.
    	in ../stdlib/strtol_l.c
    (gdb) bt
    #0  ____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, group=<optimized out>, loc=0x7ffff7dd56a0) at ../stdlib/strtol_l.c:298
    #1  0x00007ffff7a729a0 in atoi (nptr=<optimized out>) at atoi.c:28
    #2  0x00000000004009ba in right (argc=3, argv=0x7fffffffe208) at foo.c:67
    #3  0x000000000040090a in main (argc=3, argv=0x7fffffffe208) at foo.c:33
    (gdb) frame 2
    #2  0x00000000004009ba in right (argc=3, argv=0x7fffffffe208) at foo.c:67
    67	b = atoi(argv[3]);
    (gdb) print argv[3]
    $1 = 0x0
    The debugger catches the segfault, and allows you to examine the program state to help you figure out what you did wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utility of Function Objects
    By manasij7479 in forum Tech Board
    Replies: 16
    Last Post: 05-25-2011, 06:37 PM
  2. Issue with multi-threaded tcp server
    By mcmillhj in forum C Programming
    Replies: 4
    Last Post: 03-19-2011, 02:09 PM
  3. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  4. Please Try out my Utility Program!!!
    By dgprog in forum C++ Programming
    Replies: 19
    Last Post: 07-20-2002, 10:40 PM
  5. how to call the utility sort from within a C program?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 04:03 PM

Tags for this Thread