On 15 December 2016 at 14:56, Ian Jackson
<[email protected]> wrote:
> Michael Kerrisk (man-pages) writes ("Re: Bug#848231: bugs in scandir example
> program"):
>> tags 848231 fixed-upstream
>> thanks
> ...>
>> On 15 December 2016 at 13:55, Ian Jackson
>> > int
>> > main(void)
>> > @@ -18,4 +20,5 @@
>> > }
>> > free(namelist);
>> > }
>> > + return 0;
>> > }
>> >
>>
>> Thanks, Ian. Fixed pretty much as you suggest. The program doesn't
>> even compile as it was given! Looks like I injected the error after a
>> user report 4 years ago (changed "0" to "NULL").
>
> Thanks. But, 0 should be 0, not NULL.
>
> NULL is a null pointer constant but main returns int.
>
> (Even if main returned a pointer, 0 would be legal, because 0 is a
> valid nll pointer constant, but I guess for pedagogic reasons using
> NULL is probaby better in manpages where pointers are meant.)
We've misunderstood each other. Here's how I think i is:
1. You sent me a patch against a reasonably old version of the program
(pre 2012 upstream).
2. In 2012, I made this change in the code:
- n = scandir(".", &namelist, 0, alphasort);
+ n = scandir(".", &namelist, NULL, alphasort);
3. In that preceding change, I did not add a #include for <stdio.h>,
so the code would not even compile.
All fixed now. This is the current code:
#define _DEFAULT_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist, NULL, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
exit(EXIT_SUCCESS);
}
Cheers,
Michael
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/