Need assitance in using fgets() 
Author Message
 Need assitance in using fgets()

I'm trying to create a small C program that will prompt a user for a
filename in unix and print the results of the find for him.  Below is
what I have so far.  But what I would like to do is read the results
into an array or something of that nature.  I would like to have the
user look at the found files, and be able to choose the one he wants
to open, copy, mv, etc...

Any help on this would be greatly appreciated.
Ken

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char buf[ 500 ];
char command[ 500 ];
char result[ 500 ];
char file[ 500 ];
FILE *output = NULL;

int main()
{
        printf( "Please enter a filename you would like to work on>"
);
        scanf( "%s", file );

sprintf( command, "find /somedir -follow -name \"%s\" -exec ll {}
\\;", file );

     output = popen(command, "r");

        if(output)
        {

                while(fgets(buf, 300, output) != NULL)
                {
                        puts(buf);
                }

                pclose(output);

        }

/* Somewhere here I would like to see the output of the above command
   and be able to type (1) for the first file etc....  */

        return 0;

Quote:
}

--



Wed, 14 Sep 2005 15:59:56 GMT  
 Need assitance in using fgets()
Quote:

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

Be aware that unistd.h is not a standard C header - the function in it are
Unix only
Quote:
> #include <unistd.h>

Why are these global? Also, use MAX_PATH rather than 500 for a file buffer.
Quote:
> char buf[ 500 ];
> char command[ 500 ];
> char result[ 500 ];
> char file[ 500 ];
> FILE *output = NULL;

> int main()
> {

End your printf with an "\n" to force output to be written to screen.
Or fflush(stdout).
Quote:
>         printf( "Please enter a filename you would like to work on>"
> );

This isn't safe since if the user types in more than 499 characters you will
overflow your buffer. fgets(file, MAX_PATH, stdin) will read at most
MAX_PATH characters into the buffer. However a newline will be appended if
the filename fits in the buffer, not if it doesn't. You need to check for
this newline, remove it if it is there, and if not give some sort of error
message.
eg
  if( file[strlen(file)-1] == '\n' )
    file[strlen(file)-1] = 0;
 else
{
   fprintf(stderr, "Filename too long\n");
   exit(EXIT_FAILURE);
Quote:
}
>         scanf( "%s", file );

> sprintf( command, "find /somedir -follow -name \"%s\" -exec ll {}
> \\;", file );

I don't know what popen() does so it is hard to comment here. I will assume
you have set up the command correctly.
Quote:
>      output = popen(command, "r");

>         if(output)
>         {

Why is this constant 300? Is it some limit in the pipe? Otherwise looks like
good use of fgets(). However check the terminal conditions for your pipe -
does popen() cause a read error when you try to read past the end of input,
or does it cause fgets() to hang indefinitely, looking for new characters to
appear?

Quote:
>                 while(fgets(buf, 300, output) != NULL)
>                 {
>                         puts(buf);
>                 }

>                 pclose(output);

>         }

> /* Somewhere here I would like to see the output of the above command
>    and be able to type (1) for the first file etc....  */

>         return 0;

> }
> --


--



Sun, 18 Sep 2005 02:31:44 GMT  
 Need assitance in using fgets()

Quote:

> /* Somewhere here I would like to see the output of the above command
>    and be able to type (1) for the first file etc....  */

popen() returns a stdio stream pointer (FILE *) which can be
used like any other stdio stream to read what the process has
output.

I'd suggest using <dirent.h> functions instead of piping from
the "find" command.  For one thing, it's more universal;
although not covered by the basic C standard, it is POSIX
standard and implementations are available for most hosted
OSes.
--



Sun, 18 Sep 2005 02:31:48 GMT  
 Need assitance in using fgets()
in comp.lang.c.moderated i read:

Quote:
>        printf( "Please enter a filename you would like to work on>" );

you need to call fflush(stdout) here, since you aren't terminating the
string with \n and stdout is typically line buffered.

Quote:
>        scanf( "%s", file );

the `s' specifier stops at whitespace and should always be provided with an
upper limit to prevent buffer overrun.  also, you should check the return
value.

Quote:
>/* Somewhere here I would like to see the output of the above command
>   and be able to type (1) for the first file etc....  */

you need more program logic.

--
bringing you boring signatures for 17 years
--



Sun, 18 Sep 2005 02:32:06 GMT  
 Need assitance in using fgets()

Quote:

> I'm trying to create a small C program that will prompt a user for a
> filename in unix and print the results of the find for him.  Below is
> what I have so far.  But what I would like to do is read the results
> into an array or something of that nature.  I would like to have the
> user look at the found files, and be able to choose the one he wants
> to open, copy, mv, etc...

<code snipped>

First of all - I can see you are using some Unix specific
constructs in your code, so you can benefit from taking
a look at comp.unix.programmer

Reading files with fgets() is good style. What exactly is
the problem ?

If you want an array of lines, you'll have to use a char**
Verify the input and insert the new line into an array.
Naturally, memory handling issues come in view, but it
depends on what you expect from your input.

--
Email: ________ at yahoo dot com
(the username is spur4444)

Website: http://www.geocities.com/spur4444/
--



Sun, 18 Sep 2005 02:32:13 GMT  
 Need assitance in using fgets()

Quote:
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>#include <unistd.h>

 ^^^^^^^^^^^^^^^^^^^
No such thing in standard C.

Quote:
>char buf[ 500 ];
>char command[ 500 ];
>char result[ 500 ];
>char file[ 500 ];
>FILE *output = NULL;

>int main()
>{
>        printf( "Please enter a filename you would like to work on>"

Since stdout is not flushed, the user may not see the prompt by the
time scanf is called.

Quote:
>);
>        scanf( "%s", file );

1. What if the user "types" 500 characters or more?

2. What if the file name contains spaces?

Quote:
>sprintf( command, "find /somedir -follow -name \"%s\" -exec ll {}
>\\;", file );

The embedded newline is not going to do you any good.

Quote:
>     output = popen(command, "r");

               ^^^^^
No such thing in standard C.

Quote:
>        if(output)
>        {

>                while(fgets(buf, 300, output) != NULL)
>                {
>                        puts(buf);

You're combining one newline from fgets with one from puts.  Your users
won't appreciate that.

Quote:
>                }

>                pclose(output);

                 ^^^^^^
No such thing in standard C.

Quote:
>        }

>/* Somewhere here I would like to see the output of the above command
>   and be able to type (1) for the first file etc....  */

You have (very badly) written the code for displaying the output.  Write
the code for the rest.

Quote:
>        return 0;

>}

BTW, after fixing the already mentioned bugs, you may want to repost your
question to comp.unix.programmmer, preferably in a more coherent phrasing.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Sun, 18 Sep 2005 02:32:51 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Need assitance in using fgets()

2. annoying fgets problem <fgets>

3. need some more help with fgets, stdin, EOF

4. help needed with fgets()

5. Help needed - fgets and EOF

6. Help needed - fgets and EOF

7. Using getchar() instead of fgets or scanf

8. using fgets() -- dynamically allocating string size

9. Using fgets and sscanf Line into Strings

10. Using fgets on a network socket

11. flushing stdin after using fgets( foo, 100, stdin )

12. Using fgets() for interactive use

 

 
Powered by phpBB® Forum Software