LuaPosix: Difference between revisions

From Alpine Linux
Jump to navigation Jump to search
(→‎getcwd: more readable example)
(→‎getpassword: better examples)
Line 213: Line 213:


==getpassword==
==getpassword==
get password file entry
'''INPUT'''
'''INPUT'''
*NAME or UID
*NAME or UID
Line 218: Line 219:
*TABLE- values from /etc/passwd
*TABLE- values from /etc/passwd
uid,name,gid,password,gecos,dir,shell
uid,name,gid,password,gecos,dir,shell
  bar = require "posix"
  for a,b in pairs(posix.getpasswd("root")) do
bobo = bar.getpasswd("root")
  print(a,b)
for a,b in pairs(bobo) do print(a,b) end
end
  --uid 0
 
  --name root
Outputs:
  --gid 0
  uid     0
  --passwd x
  name   root
  --gecos root
  gid     0
  --dir /root/
  passwd x
  --shell /bin/bash
  gecos   root
  dir     /root
shell  /bin/bash
 
  print(posix.getpasswd("root", "shell"))
Output:
/bin/sh


==getprocessid==
==getprocessid==

Revision as of 23:33, 11 November 2007

Lua Posix

This is a list of the Lua Posix functions. Included is helpful snippets of code. Install lua posix. To get this list do:

require "posix"
for a in pairs(posix) do 
  print(a)
end

access

check real user's permissions for a file

SYNOPSIS

posix.access(pathname, mode)

DESCRIPTION

access() checks whether the calling process can access the file pathname. If pathname is a symbolic link, it is dereferenced.

The mode specifies the accessibility check(s) to be performed, and is either the string "f", or one or more of "r", "w", and "x". "f" tests for the existence of the file. "r", "w", and "x" test whether the file exists and grants read, write, and execute permissions, respectively.

RETURN VALUES

On success (all requested permissions granted), zero is returned. On error (at least one char in mode asked for a permission that is denied, or some other error occurred), nil, errorstr and errno is returned.

require "posix"
status, errstr, errno = posix.access("/etc/passwd", "rw")

Reference: http://swoolley.org/man.cgi/access

chdir

INPUT

  • Path - where do you want to change directory to

OUPUT

  • 0 if success
    • nil if failed
bar = require "posix"
bar.chdir("/etc/")
--print(bar.getcwd()) to see where you are

0 if success, nil if failure. If you

print(bar.chdir("/var/lob"))
-- it will print the system error message also
nil     /var/lob: No such file or directory     2


The access function returns three values altogether; the following example may help to distinguish their use:

lpos = require "posix"
ptr, msg, code = lpos.access("/etc/dhcp")
if ptr == nil then
  print("msg  = " .. msg)
  print("code = " .. code)
else
  print("ptr  = " .. ptr)
end

chmod

INPUT

  • PATH - where is the file or directory
    • MODE - what mode to you want to change on the file

OUPUT

  • string - 0 if sucess, nil failure
bar = require "posix"
bar.chmod("/etc/passwd", "a=rwx")
--looks like you need to use the ugoa format for this, not numbers

chown

INPUT

  • Path-location of file or directory
  • UID - User id number
  • GID - Group id number

OUPUT

  • String - 0 is success and nil for failure
bar = require "posix"
print(bar.chown("/etc/passwd",100,200)
--above will give back nil. Since we printed it will also give back the system errors.

ctermid

INPUT

  • NONE- DISPLAYS the terminal id

OUTPUT

  • String with the terminal id
bar = require "posix"
print(bar.ctermid())

dir

Directory listing INPUT

  • PATH - directory

OUTPUT

  • TABLE - table with the file name contents of the directory
    • Failure returns a nil value
bar = require "posix"
bobo = bar.dir("/var/log")
for a,b in ipairs(bobo) do print(b) end
-- the bobo table will output all the file names in /var/log/

errno

Display the error information

INPUT

  • NONE

OUPUT

  • Success, or Error message
bar = require "posix"
a,b = bar.dir("/var/foo"), bar.errno()
print(a,b)
-- a will be nil or 0, b will be No such file or directory or Success

exec

Replaces the running process with the command specified. Command line arguments are given as additional arguments. (To start a sub process, you probably want fork+exec, not just exec.)

INPUT

  • PATH-Location of binary
  • ARGs-options to pass to binary

OUTPUT

  • Strings
bar = require "posix"
bobo = bar.exec("/bin/ls", "-l", "/etc/")

files

Returns an interator function that loops over each file in the given directory

INPUT

  • PATH- Directory

OUPUT

require "posix"
for name in posix.files("/etc") do
    print (name)
end

fork

INPUT

  • None

OUTPUT returns: -1 on error, 0 to the child process, and the pid of the child to the parent.

require("posix")

print ("parent: my pid is: " .. posix.getpid("pid")) 

local pid = posix.fork () 

if ( pid == -1 ) then
        print ("parent: The fork failed.")
elseif ( pid == 0 ) then
        print ("child: Hello World! I am pid: " .. posix.getpid("pid") )
        print ("child: I'll sleep for 1 second ... ")
        posix.sleep(1)
        print ("child: Good bye");
else
        print ("parent: While the child sleeps, I'm still running.")
        print ("parent: waiting for child (pid:" .. pid .. ") to die...")
        posix.wait(pid)
        print ("parent: child died, but I'm still alive.")
        print ("parent: Good bye")
end
parent: my pid is: 11050
child: Hello World! I am pid: 11051
child: I'll sleep for 1 second ... 
parent: While the child sleeps, I'm still running.
parent: waiting for child (pid:11051) to die...
child: Good bye
parent: child died, but I'm still alive.
parent: Good bye

getcwd

Get current working directory INPUT

  • NONE

OUTPUT

  • String- contents of which is the current working directory
require "posix"
print(posix.getcwd())

getenv

INPUT

  • NONE

OUPUT

  • Table - Current Environment settings
bar = require "posix"
bobo = bar.getenv()
for a,b in pairs(bobo) do print(b) end
-- Varible - Value

getgroup

INPUT

  • GID, or groupname

OUPUT

  • TABLE -contents of which hold the group name,gid, and users
bar = require "posix"
bobo = bar.getgroup(1000)
for a,b in pairs(bobo) do print(a,b) end
--if you use pairs then bobo will print also the name and gid or the group
--if you use ipairs then just the group members
1 user1
2 user2
name wheel
gid 1

getlogin

Couldn't get it to work

INPUT

  • NONE

OUPUT

  • UNKNOWN

getpassword

get password file entry INPUT

  • NAME or UID

OUPUT

  • TABLE- values from /etc/passwd

uid,name,gid,password,gecos,dir,shell

for a,b in pairs(posix.getpasswd("root")) do
  print(a,b)
end

Outputs:

uid     0
name    root
gid     0
passwd  x
gecos   root
dir     /root
shell   /bin/bash
print(posix.getpasswd("root", "shell"))

Output:

/bin/sh

getprocessid

INPUT

  • Selector - either [egid,euid,gid,uid,pgrp,pid,ppid,NULL]

OUPUT

  • Number that matches the current process and the selector
bar = require "posix"
print(bar.getprocessid("gid"))
18456 
--it just printed the current process id for the script or interactive lua.


Is there a version difference here? AL 1.7.7 calls this "getpid" Nangel

kill

INPUT

  • PID- process identifier
  • Signal- to send to the process

OUPUT

bar = require "posix"
--kill your current process
bobo = bar.getprocessid("pid")
bar.kill(bobo,9)
---Signals looks to be the number signals accepted in Unix

link

Hard Links INPUT

  • Oldpath-
  • Newpath-

'OUTPUT If you want output 0 sucess, nil errors

bar = require "posix"
bobo = bar.link("/etc/passwd", "testfile")
-- 0 for success, nil for errors

mkdir

INPUT

  • PATH-path to new dir

OUTPUT If you are looking for output-

  • 0 for success
  • nil for failure
bar = require "posix"
bar.mkdir("/home/user/bobo")
--set the above to a variable to get output

mkfifo

INPUT

  • PATH- where to make the fifo

patchconf

get configuration values for files

SYNOPSIS

posix.pathconf(path, name)

DESCRIPTION

posix.pathconf() gets a value for configuration option name for the filename path. Setting name equal to one of the following strings returns the following configuration options:

  • link_max
    • returns the maximum number of links to the file. If path refer to a directory, then the value applies to the whole directory.
  • max_canon
    • returns the maximum length of a formatted input line, where path must refer to a terminal.
  • max_input
    • returns the maximum length of an input line, where path must refer to a terminal.
  • name_max
    • returns the maximum length of a filename in the directory path the process is allowed to create.
  • path_max
    • returns the maximum length of a relative pathname when path is the current working directory.
  • pipe_buf
    • returns the size of the pipe buffer, where path must refer to a FIFO.
  • chown_restricted
    • returns non-zero if the chown(2) call may not be used on this file. If path refer to a directory, then this applies to all files in that directory.
  • no_trunc
    • returns non-zero if accessing filenames longer than name_max generates an error.
  • vdisable
    • returns non-zero if special character processing can be disabled, where path must refer to a terminal.

RETURN VALUE

The limit is returned, if one exists. If name is omitted, a table of all limits is returned.

EXAMPLE

for i,j in pairs(posix.pathconf("/dev/tty" )) do
  print(i, j)
end

Will ouput:

pipe_buf        4096
link_max        127
path_max        4096
max_canon       255
chown_restricted        1
no_trunc        1
max_input       255
name_max        255
vdisable        0

REFERENCE

http://swoolley.org/man.cgi/pathconf

putenv

readlink

rmdir

setgid

setuid

sleep

stat

symlink

sysconf

times

ttyname

umask

uname

utime

wait


If you are using this under linux also get

setenv

unsetenv