Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:43:49+00:00 2026-06-01T16:43:49+00:00

I have done my homework and studied other responses on this topic but none

  • 0

I have done my homework and studied other responses on this topic but none address my particular issue.

I want to remove the io library completely and the os only partially (let’s say i want to keep os.clock() and others)

How can I achieve this only from the C API.

Due to the nature of the project I am not allowed to modify the Lua headers and the scripts that will be sent to me. These are not under my control. The only thing I can modify is the interpreter.

doing something like this:

lua_pushnil(state_pointer);
lua_setglobal(state_pointer, "os.execute");

won’t help much because in the script the user can call os = require(‘os’) and get all the functions back

I am not allowed to disable the require function so this makes things harder.

Any ideas?

PS:More of a curiosity: if I do something like

luaopen_base(L);
luaopen_table(L);
luaopen_string(L);
luaopen_math(L);
luaopen_loadlib(L); (basically i'm loading every library by hand except os and io)

instead of

luaL_openlibs(L); (this loads all the libraries)

would os = require(‘os’) or io = require(‘io’) still work?


@Nicol Bolas don’t know if i’m doing something wrong but os = require(‘os’) & require(‘io’) just brings everything back.

my code:

luaL_openlibs(LuaInstance);     /* load the libs        */ 
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "io");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.execute");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.rename");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.remove");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.exit");

In my script i just do a

os = require('os')
io = require('io')

after this os functions and io functions all work. os.exit still closes my app and io.write works as usual

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:43:50+00:00Added an answer on June 1, 2026 at 4:43 pm

    won’t help much because in the script the user can call os = require(‘os’) and get all the functions back

    No, it won’t. Calling require(os) will simply return the os table. The same table you will have modified. So there’s no problem.

    So just modify the table after you register it. It will work, and it’s really easy to test that it does.

    luaopen_base(L);

    Be advised: luaopen_* are not regular C functions. They are Lua C functions; they’re functions that expect to be called via the standard Lua mechanisms. You can’t call them directly from C.

    In Lua 5.1, you must use push them on the stack and use lua_pcall or similar calling functions to call them. In Lua 5.2, you should use luaL_requiref, which will put their tables into the Lua require registry.


    Your code has two problems. First:

    lua_setglobal(LuaInstance, "io");
    lua_pushnil(LuaInstance);
    

    This does not actually change the table. It simply removes the reference to the table. If you want to change the table itself, then you must change the table. You have to get the io table and modify it. Walk the table and set each value in it to nil. Simply replacing the contents of the global variable called io will do nothing.

    However, if you want to prevent io from being used entirely, you shouldn’t register it to begin with.

    The second problem is this:

    lua_pushnil(LuaInstance);
    lua_setglobal(LuaInstance, "os.execute");
    

    This modifies the value of the global table who’s key is ["os.execute"]. It is the equivalent of this Lua code:

    _G["os.execute"] = nil
    

    This is not the same as:

    os.execute = nil;
    

    When you use os.execute in Lua, that means to take the global table (_G), find the value with the key named "os" and find the "execute" key within the table fetched from "os".

    When you do _G["os.execute"], what you’re saying is to take the global table and find the value with the key named "os.execute".

    See the difference?

    What you want to do is get the table stored in the global variable os and modify that table. You can’t use lua_setglobal, because the members of the os table are not globals; they’re members of a table. Yes, the table that they are stored in just so happens to be a global. But you can’t modify the members of a table stored in a global with lua_setglobal.

    You have to do this:

    lua_getglobal(L, "os");
    lua_pushnil(L);
    lua_setfield(L, -2, "execute");
    lua_pushnil(L);
    lua_setfield(L, -2, "rename");
    lua_pushnil(L);
    lua_setfield(L, -2, "remove");
    lua_pushnil(L);
    lua_setfield(L, -2, "exit");
    lua_pop(L, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have done some research into this topic, but found no relevant answers. What I
I have this homework problem where I need to use regex to remove every
Alright, i had this homework recently (don't worry, i've already done it, but in
I've done some homework on this issue and I am pretty sure it will
I have been dancing around this issue for awhile but it keeps coming up.
Here is my code. It's homework but i have done it all. But the
I have done some looking and I found this: http://www.codeproject.com/Articles/14439/The-ScrollableListBox-Custom-Control-for-ASP-NET-2 but to me it
If I have done my homework correctly, I have come to learn that Backbone
I know you don't like helping others in their homework but I have to
It might be for pedantic purposes but not homework. I have below question about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.