File: glob.c

package info (click to toggle)
lua-posix 36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,720 kB
  • sloc: ansic: 5,462; makefile: 21; sh: 6
file content (113 lines) | stat: -rw-r--r-- 2,696 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * POSIX library for Lua 5.1, 5.2, 5.3 & 5.4.
 * Copyright (C) 2013-2025 Gary V. Vaughan
 * Copyright (C) 2010-2013 Reuben Thomas <rrt@sc3d.org>
 * Copyright (C) 2008-2010 Natanael Copa <natanael.copa@gmail.com>
 * Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
 * Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
 * Based on original by Claudio Terra for Lua 3.x.
 * With contributions by Roberto Ierusalimschy.
 * With documentation from Steve Donovan 2012
 */
/***
 Generate pathnames matching a shell-style pattern.

 Functions generating a table of filenames that match a shell-style
 pattern string.

@module posix.glob
*/

#include <glob.h>

#include "_helpers.c"


/***
Find all files in this directory matching a shell pattern.
@function glob
@string[opt="*"] pat shell glob pattern
@param flags bitwise inclusive OR of zero or more of `GLOB_ERR`, `GLOB_MARK`
 and `GLOB_NOCHECK`
@treturn[1] table matching filenames, if successful
@treturn[2] nil
@treturn[2] one of `GLOB_ABORTED`, `GLOB_NOMATCH` or `GLOB_NOSPACE`
@see glob(3)
@see glob.lua
*/
static int
Pglob(lua_State *L)
{
	const char *pattern = optstring(L, 1, "*");
	int flags = checkint(L, 2);
	int globret = 0;
	glob_t globres;

	checknargs(L, 2);
	if ((globret = glob(pattern, flags, NULL, &globres)) != 0)
	{
		lua_pushnil(L);
		lua_pushinteger(L, globret);
		return 2;
	}
	else
	{
		unsigned int i;
		lua_newtable(L);
		for (i=1; i<=globres.gl_pathc; i++)
		{
			lua_pushstring(L, globres.gl_pathv[i-1]);
			lua_rawseti(L, -2, i);
		}
		globfree(&globres);
		return 1;
	}
}


static const luaL_Reg posix_glob_fns[] =
{
	LPOSIX_FUNC( Pglob		),
	{NULL, NULL}
};


/***
Constants.
@section constants
*/

/***
Glob constants.
@table posix.glob
@int GLOB_MARK append slashes to matches that are directories.
@int GLOB_ERR instead of ignoring non-readable directories, return GLOB_ABORTED
@int GLOB_ABORTED encountered a non-readable directory
@int GLOB_NOCHECK return the original pattern if there are no matches
@int GLOB_NOMATCH pattern does not match any existing pathname
@int GLOB_NOSPACE not enough memory to continue
@usage
  -- Print glob constants supported on this host.
  for name, value in pairs (require "posix.glob") do
    if type (value) == "number" then
      print (name, value)
    end
  end
*/


LUALIB_API int
luaopen_posix_glob(lua_State *L)
{
	luaL_newlib(L, posix_glob_fns);
	lua_pushstring(L, LPOSIX_VERSION_STRING("glob"));
	lua_setfield(L, -2, "version");
	LPOSIX_CONST( GLOB_ERR );
	LPOSIX_CONST( GLOB_MARK );
	LPOSIX_CONST( GLOB_NOCHECK );
	LPOSIX_CONST( GLOB_ABORTED );
	LPOSIX_CONST( GLOB_NOMATCH );
	LPOSIX_CONST( GLOB_NOSPACE );

	return 1;
}