473,382 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

segmentation fault in linux

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
3 4511
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
horace1
1,510 Expert 1GB
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
thank you very much.it solved my problem
Nov 8 '06 #4

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

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
5
by: Allan | last post by:
Hi all, I've a C program which was originally developed under Sun Solaris(OS,2.6,gcc 2.95). I'm trying to migrate it to our linux system(Mandrake 9.0,2.4.19,gcc 3.2). Following is the...
9
by: Narendran Kumaraguru Nathan | last post by:
Hi all, I am fairly experianced in C. I am writing a program in which I'm getting a segmentation fault. The problem is that it is getting the segmentation fault when executing calloc. I tried...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
3
by: Kiran | last post by:
Hello All, In my program, I have a main thread which is the GUI (wxPython) and then a thread which goes and reads data from a socket. The reason this is in a different thread is because the data...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
2
by: Nagaraj | last post by:
Hi all, I am new to Linux platform. I am writing some C programs on Linux platform. It gives the segmentation fault error, what is segmentation fault and how to remove it. please reply.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.