Home > Archive > Unix Programming > June 2007 > popen/fgets issue
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ian Collins 2007-06-16, 7:05 pm |
| I'm lot sure whether this is Solaris only, but I have an application
that reads a group of files form a directory and processes them. I use
"ls -m xx*" to get the list of files starting with xx. The problem is
using popen and fgets to read the result gives me multiple lines where
running the ls from the command line gives one line (as expected). Test
case:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
const size_t bufferSize = 2048;
FILE* ptr;
char buf[bufferSize];
if( (ptr = popen( "ls -m /tmp/test/xx*", "r" )) != NULL)
{
while( fgets( buf, bufferSize, ptr ) != NULL )
{
printf("%d %s", strlen(buf), buf );
}
return pclose( ptr );
}
return EXIT_FAILURE;
}
../a.out
69 /tmp/test/xx, /tmp/test/xx.merge_left33, /tmp/test/xx.merge_right43,
21 /tmp/test/xx.working
What is causing the line wrap? Piping the ls output through wc also
shoes there are two lines.
ls -m /tmp/test/xx* | wc
2 4 90
--
Ian Collins.
| |
| Barry Margolin 2007-06-16, 10:10 pm |
| In article <5dja1fF359klnU7@mid.individual.net>,
Ian Collins <ian-news@hotmail.com> wrote:
> I'm lot sure whether this is Solaris only, but I have an application
> that reads a group of files form a directory and processes them. I use
> "ls -m xx*" to get the list of files starting with xx. The problem is
> using popen and fgets to read the result gives me multiple lines where
> running the ls from the command line gives one line (as expected). Test
> case:
The -m option means to put as many files as will fit on a line,
separated by commas. Why don't you expect this? Why are you using the
-m option if you don't want this behavior?
On my system ls uses the COLUMNS environment variable if it's set, to
determine the line size. If not, then if output is to a terminal it
uses the terminal's width (as set by 'stty columns'), otherwise it
appears to default to 80 columns. So if you don't have COLUMNS set,
you'll see different behavior when sending to a pipe (as used by popen)
or writing to the terminal if the terminal width is different from 80.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main()
> {
> const size_t bufferSize = 2048;
> FILE* ptr;
> char buf[bufferSize];
>
> if( (ptr = popen( "ls -m /tmp/test/xx*", "r" )) != NULL)
> {
> while( fgets( buf, bufferSize, ptr ) != NULL )
> {
> printf("%d %s", strlen(buf), buf );
> }
> return pclose( ptr );
> }
> return EXIT_FAILURE;
> }
>
> ./a.out
> 69 /tmp/test/xx, /tmp/test/xx.merge_left33, /tmp/test/xx.merge_right43,
> 21 /tmp/test/xx.working
>
> What is causing the line wrap? Piping the ls output through wc also
> shoes there are two lines.
>
> ls -m /tmp/test/xx* | wc
> 2 4 90
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Ian Collins 2007-06-16, 10:10 pm |
| Barry Margolin wrote:
> In article <5dja1fF359klnU7@mid.individual.net>,
> Ian Collins <ian-news@hotmail.com> wrote:
>
>
> The -m option means to put as many files as will fit on a line,
> separated by commas. Why don't you expect this? Why are you using the
> -m option if you don't want this behavior?
>
I did, the Solaris man page simply says "Streams output format. Files
are listed across the page, separated by commas." which is what I
wanted, all the matching files in one line. I use wide terminals, to
the list in question always fitted.
> On my system ls uses the COLUMNS environment variable if it's set, to
> determine the line size.
Solaris does the same, thanks.
--
Ian Collins.
| |
| Barry Margolin 2007-06-18, 4:15 am |
| In article <5djk50F359klnU11@mid.individual.net>,
Ian Collins <ian-news@hotmail.com> wrote:
> Barry Margolin wrote:
> I did, the Solaris man page simply says "Streams output format. Files
> are listed across the page, separated by commas." which is what I
> wanted, all the matching files in one line. I use wide terminals, to
> the list in question always fitted.
I misread the original question, I thought you were expecting each file
on a different line, not all the files on one line.
I guess you weren't listing lots of files -- even on a wide terminal,
you'll eventually overflow to the next line.
>
>
> Solaris does the same, thanks.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
|
|
|
|
|