#848231 bugs in scandir example program

Package:
manpages-dev
Source:
manpages
Submitter:
Ian Jackson
Date:
2022-01-26 19:03:04 UTC
Severity:
minor
Tags:
#848231#5
Date:
2016-12-15 12:55:54 UTC
From:
To:
I cut and pasted the example program from scandir(3) and found that it
had some bugs which caused compiler warnings with -Wall.  Here is a
patch (to the example program, not to the nroff) to fix them.

Thanks,
Ian.
--- t.c.orig	2016-12-15 12:52:33.181859142 +0000
+++ t.c	2016-12-15 12:55:08.939521298 +0000
@@ -1,6 +1,8 @@
        #define _SVID_SOURCE
        /* print files in current directory in reverse order */
        #include <dirent.h>
+       #include <stdio.h>
+       #include <stdlib.h>

        int
        main(void)
@@ -18,4 +20,5 @@
                }
                free(namelist);
            }
+           return 0;
        }

#848231#10
Date:
2016-12-15 13:31:28 UTC
From:
To:
tags 848231 fixed-upstream
thanks

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").

Cheers,

Michael

#848231#17
Date:
2016-12-15 13:56:26 UTC
From:
To:
Michael Kerrisk (man-pages) writes ("Re: Bug#848231: bugs in scandir example program"):
...>

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.)

Ian.

#848231#22
Date:
2016-12-15 15:59:38 UTC
From:
To:
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

#848231#27
Date:
2016-12-15 16:08:08 UTC
From:
To:
Michael Kerrisk (man-pages) writes ("Re: Bug#848231: bugs in scandir example program"):

Oh, right.  Jolly good.

Thanks,
Ian.

#848231#32
Date:
2022-01-26 19:02:22 UTC
From:
To:
Confirmed to be present in man-pages-4.10 (first packaged in Debian in
4.10-1) and also confirmed working, hence this bug could be closed
accordingly.

Cheers,
Flo