I'm currently working on this course assignment and I'm trying to set up code to search within a database file (really its just a text file named a1.db). I hear we can use the popen function to implement shell code within a C program so here's what I tried:

----------------
outside main:

<all imports>
FILE *read_fp
char *command; ;
char *input;

input is a variable in my program

<in a signal handler outside main (called by child)>
command = "grep '[B]input[/B]' a1.dv\n"
if ( !(read_fp = (FILE*)popen(command,"r")) ) {
        (record not found) [read_fp is null] }
else {
        (record found)
     }

What I want to happen is that it would search my database file(a1.db) for a user-specific word (chosen at runtime and set to variable 'input'). If that word is present, I want to display a message saying 'record found' or something and if not, then the other message saying not found.

The problem:
I'm trying to figure out a way to make that 'input' variable work, as I know right now it just takes it as the word 'input' instead of a variable. I heard snprintf was a solution, so I tried this code before doing the popen code above:

command:

command = "grep 'input' a1.db\n";
snprintf(command,sizeof command, "grep '%s' a1.db\n", input);

Also tried:

snprintf(NULL,0, "grep '%s' a1.db\n", input);

command wasn't set in this case.

The thing is, I don't really understand how snprintf works. I read some definitions here and there but I still don't really get it.

Another method I tried was concatenating, but that just crashed my program everytime saying Segmentation fault (core dumped) right after getting the input variable from me and right before doing the strncpy/strcat (I tried both) commands. It's also not a single concatenation, as you would concatenate these strings:

String1: "grep '"
String2: input (the variable delcared as 'char input' outside main)
String3: "' a1.db\n"
Final String: "grep 'input' a1.db\n"

---------------------------

(outside main) -- unncessary variables for this question removed
  <imports>
  char *input;
  FILE *read_fp;
  char *command;
  char *tmp1,*tmp2,*tmp3;

  (within main)

  printf("Please enter the string to enter/search within the database!\n");
  fgets(input,30,stdin);


  (never reaches to this point due to the crash after variable is entered)

  tmp1= "grep '";
  tmp2 = "' a1.db/n";
  printf("settings tmp1 and 2");
  tmp3 = input;
  printf("now strncyping!");
  tmp1 = strncpy(tmp1,tmp3,30);
  tmp1 = strncpy(tmp1,tmp2,10);
  printf("strnycpy done!!");
  command = tmp1;

When I don't use the concatenation method, my program works fine, except that it doesn't actually search for the string and I get the same result 'not found' or 'found' in every case.
-----------------------------

Any help is appreciated :D All I need to do is get the variable working with popen...simple and short code would be nice!

I'm not sure but I do not think "char *input" reserves any space for input just defines a pointer. so as soon as typing starts you are writing into a undefined memory location. You would most likely receive some kind of runtime error.

Edited 5 Years Ago by jnawrocki: n/a

Yeah, you have to allocate space for the strings using a function like malloc() or calloc(). Or you could also just declare your strings like char input[100] or something like that. Also, you should use // or /* and */ to indicate your comments.

Edited 5 Years Ago by chrjs: n/a

This article has been dead for over six months. Start a new discussion instead.