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?: -
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <dirent.h>
-
-
void print_help(){
-
printf("NAME\n");
-
printf(" fp - search pattern string in file(s)\n\n");
-
printf("SYNOPSIS\n");
-
printf(" fp [options] PATTERN FILE\n\n");
-
printf("DESCRIPTION\n");
-
printf(" fp fp searches given input files (or the files in the given path) for lines\n");
-
printf(" containing the given PATTERN (string). By default, fp prints only the matching\n");
-
printf(" lines.\n\n");
-
printf("OPTIONS\n");
-
printf(" --help\n");
-
printf(" Output this help message.\n\n");
-
printf(" -i, --ignore-case\n");
-
printf(" Ignore case. Lowercase letters in the pattern, matches uppercase letters\n");
-
printf(" in the file, and vice versa.\n\n");
-
printf(" -n, --line-number\n");
-
printf(" Precedes each line, containing the PATTERN, by its line number in the\n");
-
printf(" file.\n\n");
-
printf(" -R, -r, --recursive\n");
-
printf(" Read and search all files under each directory represented by FILE string\n");
-
printf(" (path string in this case), recursively.\n\n");
-
printf(" -w, --word\n");
-
printf(" The PATTERN must match a whole word.\n");
-
}
-
-
int main(int argc, char *argv[]){
-
int i,j;
-
int i_control = 0,n_control = 0, r_control = 0, w_control = 0; //controls will be 1 if that parameter is given
-
char *path; //file path
-
char *pattern; //search pattern
-
char *argument; //argv[i]
-
char *str; //rest of argument after 2 dashes
-
-
printf("Argument count: %d\n",argc);
-
for(i = 1; i < argc; i++){ //i!=0 at the beginning,because argv[0]=="fp",we skip it
-
argument = (char*)malloc(strlen(argv[i]));
-
str = (char*)malloc(strlen(argument)-1);
-
strcpy(argument,argv[i]);
-
printf("Argument = %s\n",argument);
-
if(argument[0] == '-'){
-
printf("argument[0] = %c\n",argument[0]);
-
if(argument[1] == '-'){ //command with 2 dashes
-
printf("argument[1] = %c\n",argument[1]);
-
printf("strlen_argument = %d\n", strlen(argument));
-
for(j = 0; j < strlen(argument)-2; j++){ //program terminates here with segmentation fault
-
str[j] = argument[j+2];
-
}
-
str[strlen(argument)-2] = '\0';
-
printf("str = %s\n", str);
-
if(strcmp(str,"ignore-case")==0){
-
i_control = 1;
-
}
-
else if(strcmp(str,"line-number")==0){
-
n_control = 1;
-
}
-
else if(strcmp(str,"recursive")==0){
-
r_control = 1;
-
}
-
else if(strcmp(str,"word")==0){
-
w_control = 1;
-
}
-
else if(strcmp(str,"help")==0){
-
print_help();
-
}
-
} //if
-
else{ //command with 1 dash
-
for(j = 1; j < strlen(argument); j++){ //j!=0 at the beginning,because argument[0]=='-',we skip it
-
if(argument[j] == 'i'){
-
i_control = 1;
-
}
-
else if(argument[j] == 'n'){
-
n_control = 1;
-
}
-
else if(argument[j] == 'r' || argument[j] == 'R'){
-
r_control = 1;
-
}
-
else if(argument[j] == 'w'){
-
w_control = 1;
-
}
-
} //for
-
} //else
-
} //if
-
free(argument);
-
free(str);
-
} //for
-
/* pattern = argv[argc-2];
-
printf("Pattern: %s\n", pattern);
-
path = argv[argc-1];
-
printf("Path: %s\n", path);
-
*/
-
return 0;
-
}
-
-
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
in your statements -
argument = (char*)malloc(strlen(argv[i]));
-
str = (char*)malloc(strlen(argument)-1);
-
strcpy(argument,argv[i]);
-
you malloc() str before copying argv[1] into argument
it should be -
argument = (char*)malloc(strlen(argv[i]));
-
strcpy(argument,argv[i]);
-
str = (char*)malloc(strlen(argument)-1);
-
thank you very much.it solved my problem
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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)?...
|
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()...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |