MCPcopy Create free account
hub / github.com/creatale/node-dv / getFilenamesInDirectory

Function getFilenamesInDirectory

deps/leptonica/src/sarray1.c:1857–1908  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1855#ifndef _WIN32
1856
1857SARRAY *
1858getFilenamesInDirectory(const char *dirname)
1859{
1860char dir[PATH_MAX + 1];
1861char *realdir, *stat_path, *ignore;
1862size_t size;
1863SARRAY *safiles;
1864DIR *pdir;
1865struct dirent *pdirentry;
1866int dfd, stat_ret;
1867struct stat st;
1868
1869 PROCNAME("getFilenamesInDirectory");
1870
1871 if (!dirname)
1872 return (SARRAY *)ERROR_PTR("dirname not defined", procName, NULL);
1873
1874 /* It's nice to ignore directories. fstatat() works with relative
1875 directory paths, but stat() requires using the absolute path.
1876 Also, do not pass NULL as the second parameter to realpath();
1877 use a buffer of sufficient size. */
1878 ignore = realpath(dirname, dir); /* see note above */
1879 realdir = genPathname(dir, NULL);
1880 if ((pdir = opendir(realdir)) == NULL) {
1881 LEPT_FREE(realdir);
1882 return (SARRAY *)ERROR_PTR("pdir not opened", procName, NULL);
1883 }
1884 safiles = sarrayCreate(0);
1885 dfd = dirfd(pdir);
1886 while ((pdirentry = readdir(pdir))) {
1887#if HAVE_FSTATAT
1888 stat_ret = fstatat(dfd, pdirentry->d_name, &st, 0);
1889#else
1890 size = strlen(realdir) + strlen(pdirentry->d_name) + 2;
1891 if (size > PATH_MAX) {
1892 L_ERROR("size = %lu too large; skipping\n", procName,
1893 (unsigned long)size);
1894 continue;
1895 }
1896 stat_path = (char *)LEPT_CALLOC(size, 1);
1897 snprintf(stat_path, size, "%s/%s", realdir, pdirentry->d_name);
1898 stat_ret = stat(stat_path, &st);
1899 LEPT_FREE(stat_path);
1900#endif
1901 if (stat_ret == 0 && S_ISDIR(st.st_mode))
1902 continue;
1903 sarrayAddString(safiles, pdirentry->d_name, L_COPY);
1904 }
1905 closedir(pdir);
1906 LEPT_FREE(realdir);
1907 return safiles;
1908}
1909
1910#else /* _WIN32 */
1911

Callers 2

lept_rmdirFunction · 0.85

Calls 10

genPathnameFunction · 0.85
opendirFunction · 0.85
sarrayCreateFunction · 0.85
readdirFunction · 0.85
sarrayAddStringFunction · 0.85
closedirFunction · 0.85
stringJoinFunction · 0.85
sarrayDestroyFunction · 0.85
convertSepCharsInPathFunction · 0.85
statClass · 0.70

Tested by

no test coverage detected