UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » getting return values from a Fn call after a fork

#1 2010-03-27 09:39 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: getting return values from a Fn call after a fork

int main(int argc, char* argv[]){

   system ("clear");

   int fd1, fd2;
   int i;
   int lineNumber = 0;
   int parent;
   char *str1;
   char *str2;

              /* Make sure the appropriate number of arguments are present */
   if (argc < 3){
      fprintf(stderr, "Usage: %s filename(s) string1 string2.\n", argv[0]);
      exit (-1);
   }

      /* Assign the last two arguments of command line to string variables */
   str1 = argv[argc-2];
   str2 = argv[argc-1];

             /* Descriptive line showing name and the two string arguments */
   fprintf(stdout, "Program written by XXXXXXXXXX\n");
   fprintf(stdout, "%s %s\n\n", str1, str2);

                        /* Make sure there are at least 2 string arguments */
   if (((fd1 = open(str2, O_RDONLY)) == -1) &&
         (fd2 = open(str1, O_RDONLY) > 0)){
      fprintf(stderr,"You need at least two string arguments.\n");
      close(fd1);
      close(fd2);
      exit(-1);
   }
                       /* create the number of process based on the number */
                                   /* of files present on the command line */
   for (i = 1; i <= (argc-3)-1; i++)
      if((parent = fork()) == -1){
         perror("Failed to fork");
         exit(-1);
      } else if (parent){
         break;
      }
  
   if ((lineNumber == searchForTwoStrings(argv[i], str1, str2)) == -1)
      return 1;
   else
      fprintf(stdout, "%s : %d\n", argv[i], lineNumber);
   }
   return 0;
}

"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#2 2010-03-27 10:31 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: getting return values from a Fn call after a fork

If I have to guess:

if ((lineNumber == searchForTwoStrings(argv[i], str1, str2)) == -1)
      return 1;

should be:

lineNumber = searchForTwoStrings(argv[i], str1, str2);
   if (lineNumber == -1)
      return 1;

I personally hate assignments combined with checks in one statement.
It's confusing, ugly, and invites errors. It's a necessary evil in while loops,
but that is the only place they may be ever used IMHO.

Not sure what the purpose is of this crazy construction either:

if (((fd1 = open(str2, O_RDONLY)) == -1) &&
         (fd2 = open(str1, O_RDONLY) > 0)){

I would write it as:

fd1 = open(str1, O_RDONLY);
   fd2 = open(str2, O_RDONLY);
   if (fd1 == -1 || fd2 == -1){

Besides that the error message is misleading and the close() calls unnecessary.

EDIT: Seems you're checking if the last two arguments are files or not?!
That will cause some head scratching when a user enters a search string
that happens to be the same as some random file. Just trust the user, the
alternative is madness. And next time just use lstat(2) or access(2) instead
of open(2).

Offline

#3 2010-03-27 10:56 PM

Ronin
Member
From: San Antonio, TX
Registered: 2009-03-09
Posts: 21

Re: getting return values from a Fn call after a fork

Thanks, that did it. Will take advice to heart and I thought we weren't supposed to trust the user to do the right thing? :)


"Science without religion is lame. Religion without science is blind."
-Albert Einstein

Offline

#4 2010-03-27 11:33 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: getting return values from a Fn call after a fork

With your interface your program can't know what correct input is or not.
Either trust the user input or change the interface.

Offline

#5 2010-03-28 08:23 PM

Jaybe08
Guest

Re: getting return values from a Fn call after a fork

Please show what your Search for two Strings looks like.

#6 2010-03-29 11:09 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: getting return values from a Fn call after a fork

Offline

  • Index
  • » C
  • » getting return values from a Fn call after a fork

Board footer

Powered by FluxBB