By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

segmentation fault in linux

P: 7
i'm coding simple version of 'grep' function.i have a code which works in windows but not in linux.can you look at it and help me?:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dirent.h>
  6.  
  7. void print_help(){
  8.         printf("NAME\n");
  9.         printf("      fp - search pattern string in file(s)\n\n");
  10.         printf("SYNOPSIS\n");
  11.         printf("      fp [options] PATTERN FILE\n\n");
  12.         printf("DESCRIPTION\n");
  13.         printf("      fp fp searches given input files (or the files in the given path) for lines\n");
  14.         printf("      containing the given PATTERN (string). By default, fp prints only the matching\n");
  15.         printf("      lines.\n\n");
  16.         printf("OPTIONS\n");
  17.         printf("      --help\n");
  18.         printf("            Output this help message.\n\n");
  19.         printf("      -i, --ignore-case\n");
  20.         printf("            Ignore case. Lowercase letters in the pattern, matches uppercase letters\n");
  21.         printf("            in the file, and vice versa.\n\n");
  22.         printf("      -n, --line-number\n");
  23.         printf("            Precedes each line, containing the PATTERN, by its line number in the\n");
  24.         printf("            file.\n\n");
  25.         printf("      -R, -r, --recursive\n");
  26.         printf("            Read and search all files under each directory represented by FILE string\n");
  27.         printf("            (path string in this case), recursively.\n\n");
  28.         printf("      -w, --word\n");
  29.         printf("            The PATTERN must match a whole word.\n");
  30. }
  31.  
  32. int main(int argc, char *argv[]){
  33.         int i,j;
  34.         int i_control = 0,n_control = 0, r_control = 0, w_control = 0; //controls will be 1 if that parameter is given
  35.         char *path; //file path
  36.         char *pattern; //search pattern
  37.         char *argument; //argv[i]
  38.         char *str; //rest of argument after 2 dashes
  39.  
  40.         printf("Argument count: %d\n",argc);
  41.         for(i = 1; i < argc; i++){ //i!=0 at the beginning,because argv[0]=="fp",we skip it
  42.                 argument = (char*)malloc(strlen(argv[i]));
  43.                 str = (char*)malloc(strlen(argument)-1);
  44.                 strcpy(argument,argv[i]);
  45.                 printf("Argument = %s\n",argument);
  46.                 if(argument[0] == '-'){
  47.                         printf("argument[0] = %c\n",argument[0]);
  48.                         if(argument[1] == '-'){ //command with 2 dashes
  49.                                 printf("argument[1] = %c\n",argument[1]);
  50.                                 printf("strlen_argument = %d\n", strlen(argument));
  51.                                 for(j = 0; j < strlen(argument)-2; j++){ //program terminates here with segmentation fault
  52.                                         str[j] = argument[j+2];
  53.                                 }
  54.                                 str[strlen(argument)-2] = '\0';
  55.                                 printf("str = %s\n", str);
  56.                                 if(strcmp(str,"ignore-case")==0){
  57.                                         i_control = 1;
  58.                                 }
  59.                                 else if(strcmp(str,"line-number")==0){
  60.                                         n_control = 1;
  61.                                 }
  62.                                 else if(strcmp(str,"recursive")==0){
  63.                                         r_control = 1;
  64.                                 }
  65.                                 else if(strcmp(str,"word")==0){
  66.                                         w_control = 1;
  67.                                 }
  68.                                 else if(strcmp(str,"help")==0){
  69.                                         print_help();
  70.                                 }
  71.                         } //if
  72.                         else{ //command with 1 dash
  73.                                 for(j = 1; j < strlen(argument); j++){ //j!=0 at the beginning,because argument[0]=='-',we skip it
  74.                                         if(argument[j] == 'i'){
  75.                                                 i_control = 1;
  76.                                         }
  77.                                         else if(argument[j] == 'n'){
  78.                                                 n_control = 1;
  79.                                         }
  80.                                         else if(argument[j] == 'r' || argument[j] == 'R'){
  81.                                                 r_control = 1;
  82.                                         }
  83.                                         else if(argument[j] == 'w'){
  84.                                                 w_control = 1;
  85.                                         }
  86.                                 } //for
  87.                         } //else
  88.                 } //if
  89.                 free(argument);
  90.                 free(str);
  91.         } //for
  92. /*      pattern = argv[argc-2];
  93.         printf("Pattern: %s\n", pattern);
  94.         path = argv[argc-1];
  95.         printf("Path: %s\n", path);
  96. */
  97.         return 0;
  98. }
  99.  
  100.  
Nov 8 '06 #1
Share this Question
Share on Google+
3 Replies


P: 7
as you see i'm not finished yet.i'm working on getting parameters from command line.
sample executions of program:
fp -i -wn --recursive
fp --help
fp --ignore-case -nwR
Nov 8 '06 #2

Expert 100+
P: 1,510
in your statements
Expand|Select|Wrap|Line Numbers
  1.     argument = (char*)malloc(strlen(argv[i]));
  2.     str = (char*)malloc(strlen(argument)-1);
  3.     strcpy(argument,argv[i]);
  4.  
you malloc() str before copying argv[1] into argument

it should be
Expand|Select|Wrap|Line Numbers
  1.     argument = (char*)malloc(strlen(argv[i]));
  2.     strcpy(argument,argv[i]);
  3.     str = (char*)malloc(strlen(argument)-1);
  4.  
Nov 8 '06 #3

P: 7
thank you very much.it solved my problem
Nov 8 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.