MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / setpath

Function setpath

3rd/lua-5.4.3/src/loadlib.c:297–327  ·  view source on GitHub ↗

** Set a path */

Source from the content-addressed store, hash-verified

295** Set a path
296*/
297static void setpath (lua_State *L, const char *fieldname,
298 const char *envname,
299 const char *dft) {
300 const char *dftmark;
301 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
302 const char *path = getenv(nver); /* try versioned name */
303 if (path == NULL) /* no versioned environment variable? */
304 path = getenv(envname); /* try unversioned name */
305 if (path == NULL || noenv(L)) /* no environment variable? */
306 lua_pushstring(L, dft); /* use default */
307 else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL)
308 lua_pushstring(L, path); /* nothing to change */
309 else { /* path contains a ";;": insert default path in its place */
310 size_t len = strlen(path);
311 luaL_Buffer b;
312 luaL_buffinit(L, &b);
313 if (path < dftmark) { /* is there a prefix before ';;'? */
314 luaL_addlstring(&b, path, dftmark - path); /* add it */
315 luaL_addchar(&b, *LUA_PATH_SEP);
316 }
317 luaL_addstring(&b, dft); /* add default */
318 if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
319 luaL_addchar(&b, *LUA_PATH_SEP);
320 luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
321 }
322 luaL_pushresult(&b);
323 }
324 setprogdir(L);
325 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
326 lua_pop(L, 1); /* pop versioned variable name ('nver') */
327}
328
329/* }================================================================== */
330

Callers 1

luaopen_packageFunction · 0.85

Calls 9

lua_pushfstringFunction · 0.85
noenvFunction · 0.85
lua_pushstringFunction · 0.85
luaL_buffinitFunction · 0.85
luaL_addlstringFunction · 0.85
luaL_addstringFunction · 0.85
luaL_pushresultFunction · 0.85
setprogdirFunction · 0.85
lua_setfieldFunction · 0.85

Tested by

no test coverage detected