Next: 9. The MMX unit
Up: Unit reference for Free
Previous: 7. The IPC unit.
Subsections
This chapter describes the LINUX unit for Free Pascal. The unit was written
by Michaël van Canneyt. It works only on the Linux operating system.
This chapter is divided in 2 sections:
- The first section lists all constants, types and variables, as listed
in the interface section of the LINUX unit.
- The second section describes all procedures and functions in the LINUX
unit.
8.1.1 Types
PGlob and TGlob are 2 types used in the Glob function:
PGlob = ^TGlob;
TGlob = record
Name : PChar;
Next : PGlob;
end;
The following types are used in the signal-processing procedures.
{$Packrecords 1}
SignalHandler = Procedure ( Sig : Integer);cdecl;
PSignalHandler = SignalHandler;
SignalRestorer = Procedure;cdecl;
PSignalrestorer = SignalRestorer;
SigActionRec = Record
Sa_Handler : Signalhandler;
Sa_Mask : Longint;
Sa_flags : Integer;
Sa_Restorer : SignalRestorer;
end;
PSigActionRec = ^SigActionRec;
Stat is used to store information about a file. It is defined in the
syscalls unit.
stat = record
dev : word;
pad1 : word;
ino : longint;
mode : word;
nlink : word;
uid : word;
gid : word;
rdev : word;
pad2 : word;
size : longint;
blksze : Longint;
blocks : Longint;
atime : Longint;
unused1 : longint;
mtime : Longint;
unused2 : longint;
ctime : Longint;
unused3 : longint;
unused4 : longint;
unused5 : longint;
end;
Statfs is used to store information about a filesystem. It is defined in
the syscalls unit.
statfs = record
fstype : longint;
bsize : longint;
blocks : longint;
bfree : longint;
bavail : longint;
files : longint;
ffree : longint;
fsid : longint;
namelen : longint;
spare : array [0..6] of longint;
end
Dir and PDir are used in the OpenDir and ReadDir
functions.
TDir =record
fd : integer;
loc : longint;
size : integer;
buf : pdirent;
nextoff: longint;
dd_max : integer;
lock : pointer;
end;
PDir =^TDir;
Dirent, PDirent are used in the ReadDir function to return files in a directory.
PDirent = ^Dirent;
Dirent = Record
ino,
off : longint;
reclen : word;
name : string[255]
end;
Termio and Termios are used with iotcl() calls for terminal handling.
Const NCCS = 19;
NCC = 8;
Type termio = record
c_iflag, { input mode flags }
c_oflag, { output mode flags }
c_cflag, { control mode flags }
c_lflag : Word; { local mode flags }
c_line : Word; { line discipline - careful, only High byte in use}
c_cc : array [0..NCC-1] of char; { control characters }
end;
termios = record
c_iflag, { input mode flags }
c_oflag, { output mode flags }
c_cflag, { control mode flags }
c_lflag : Cardinal; { local mode flags }
c_line : char; { line discipline }
c_cc : array [0..NCCS-1] of char; { control characters }
end;
Utimbuf is used in the Utime call to set access and modificaton time
of a file.
utimbuf = record
actime,modtime : Longint;
end;
For the Select call, the following 4 types are needed:
FDSet = Array [0..31] of longint;
PFDSet = ^FDSet;
TimeVal = Record
sec,usec : Longint;
end;
PTimeVal = ^TimeVal;
The Uname function uses the utsname to return information about
the current kernel :
utsname =record
sysname,nodename,release,
version,machine,domainname : Array[0..64] of char;
end;
Its elements are null-terminated C style strings, you cannot access them
directly !
Linuxerror is the variable in which the procedures in the linux unit
report errors.
LinuxError : Longint;
StdErr Is a Text variable, corresponding to Standard Error or
diagnostic output. It is connected to file descriptor 2. It can be freely
used, and will be closed on exit.
StdErr : Text;
Constants for setting/getting process priorities :
Prio_Process = 0;
Prio_PGrp = 1;
Prio_User = 2;
For testing access rights:
R_OK = 4;
W_OK = 2;
X_OK = 1;
F_OK = 0;
For signal handling functions :
SA_NOCLDSTOP = 1;
SA_SHIRQ = $04000000;
SA_STACK = $08000000;
SA_RESTART = $10000000;
SA_INTERRUPT = $20000000;
SA_NOMASK = $40000000;
SA_ONESHOT = $80000000;
SIG_BLOCK = 0;
SIG_UNBLOCK = 1;
SIG_SETMASK = 2;
SIG_DFL = 0 ;
SIG_IGN = 1 ;
SIG_ERR = -1;
SIGHUP = 1;
SIGINT = 2;
SIGQUIT = 3;
SIGILL = 4;
SIGTRAP = 5;
SIGABRT = 6;
SIGIOT = 6;
SIGBUS = 7;
SIGFPE = 8;
SIGKILL = 9;
SIGUSR1 = 10;
SIGSEGV = 11;
SIGUSR2 = 12;
SIGPIPE = 13;
SIGALRM = 14;
SIGTERM = 15;
SIGSTKFLT = 16;
SIGCHLD = 17;
SIGCONT = 18;
SIGSTOP = 19;
SIGTSTP = 20;
SIGTTIN = 21;
SIGTTOU = 22;
SIGURG = 23;
SIGXCPU = 24;
SIGXFSZ = 25;
SIGVTALRM = 26;
SIGPROF = 27;
SIGWINCH = 28;
SIGIO = 29;
SIGPOLL = SIGIO;
SIGPWR = 30;
SIGUNUSED = 31;
For file control mechanism :
F_GetFd = 1;
F_SetFd = 2;
F_GetFl = 3;
F_SetFl = 4;
F_GetLk = 5;
F_SetLk = 6;
F_SetLkW = 7;
F_GetOwn = 8;
F_SetOwn = 9;
For Terminal handling :
TCGETS = $5401 ;
TCSETS = $5402 ;
TCSETSW = $5403 ;
TCSETSF = $5404 ;
TCGETA = $5405 ;
TCSETA = $5406 ;
TCSETAW = $5407 ;
TCSETAF = $5408 ;
TCSBRK = $5409 ;
TCXONC = $540A ;
TCFLSH = $540B ;
TIOCEXCL = $540C ;
TIOCNXCL = $540D ;
TIOCSCTTY = $540E ;
TIOCGPGRP = $540F ;
TIOCSPGRP = $5410 ;
TIOCOUTQ = $5411 ;
TIOCSTI = $5412 ;
TIOCGWINSZ = $5413 ;
TIOCSWINSZ = $5414 ;
TIOCMGET = $5415 ;
TIOCMBIS = $5416 ;
TIOCMBIC = $5417 ;
TIOCMSET = $5418 ;
TIOCGSOFTCAR = $5419 ;
TIOCSSOFTCAR = $541A ;
FIONREAD = $541B ;
TIOCINQ = FIONREAD;
TIOCLINUX = $541C ;
TIOCCONS = $541D ;
TIOCGSERIAL = $541E ;
TIOCSSERIAL = $541F ;
TIOCPKT = $5420 ;
FIONBIO = $5421 ;
TIOCNOTTY = $5422 ;
TIOCSETD = $5423 ;
TIOCGETD = $5424 ;
TCSBRKP = $5425 ;
TIOCTTYGSTRUCT = $5426 ;
FIONCLEX = $5450 ;
FIOCLEX = $5451 ;
FIOASYNC = $5452 ;
TIOCSERCONFIG = $5453 ;
TIOCSERGWILD = $5454 ;
TIOCSERSWILD = $5455 ;
TIOCGLCKTRMIOS = $5456 ;
TIOCSLCKTRMIOS = $5457 ;
TIOCSERGSTRUCT = $5458 ;
TIOCSERGETLSR = $5459 ;
TIOCSERGETMULTI = $545A ;
TIOCSERSETMULTI = $545B ;
TIOCMIWAIT = $545C ;
TIOCGICOUNT = $545D ;
TIOCPKT_DATA = 0;
TIOCPKT_FLUSHREAD = 1;
TIOCPKT_FLUSHWRITE = 2;
TIOCPKT_STOP = 4;
TIOCPKT_START = 8;
TIOCPKT_NOSTOP = 16;
TIOCPKT_DOSTOP = 32;
Other than that, all constants for setting the speed and control flags of a
terminal line, as described in the termios (2) man
page, are defined in the linux unit. It would take too much place to list
them here.
To check the mode field of a stat record, you ca use the
following constants :
{ Constants to check stat.mode }
STAT_IFMT = $f000; {00170000}
STAT_IFSOCK = $c000; {0140000}
STAT_IFLNK = $a000; {0120000}
STAT_IFREG = $8000; {0100000}
STAT_IFBLK = $6000; {0060000}
STAT_IFDIR = $4000; {0040000}
STAT_IFCHR = $2000; {0020000}
STAT_IFIFO = $1000; {0010000}
STAT_ISUID = $0800; {0004000}
STAT_ISGID = $0400; {0002000}
STAT_ISVTX = $0200; {0001000}
{ Constants to check permissions }
STAT_IRWXO = $7;
STAT_IROTH = $4;
STAT_IWOTH = $2;
STAT_IXOTH = $1;
STAT_IRWXG = STAT_IRWXO shl 3;
STAT_IRGRP = STAT_IROTH shl 3;
STAT_IWGRP = STAT_IWOTH shl 3;
STAT_IXGRP = STAT_IXOTH shl 3;
STAT_IRWXU = STAT_IRWXO shl 6;
STAT_IRUSR = STAT_IROTH shl 6;
STAT_IWUSR = STAT_IWOTH shl 6;
STAT_IXUSR = STAT_IXOTH shl 6;
You can test the type of a filesystem returned by a FSStat call with
the following constants:
fs_old_ext2 = $ef51;
fs_ext2 = $ef53;
fs_ext = $137d;
fs_iso = $9660;
fs_minix = $137f;
fs_minix_30 = $138f;
fs_minux_V2 = $2468;
fs_msdos = $4d44;
fs_nfs = $6969;
fs_proc = $9fa0;
fs_xia = $012FD16D;
the FLock call uses the following mode constants :
LOCK_SH = 1;
LOCK_EX = 2;
LOCK_UN = 8;
LOCK_NB = 4;
8.2.1 Access
-
Declaration
-
Function Access (Path : Pathstr; Mode : integer) : Boolean;
-
Description
Tests user's access rights on the specified file. Mode is a mask existing of
one or more of
- R_OK
- User has read rights.
- W_OK
- User has write rights.
- X_OK
- User has execute rights.
- F_OK
- User has search rights in the directory where the file is.
The test is done with the real user ID, instead of the effective user ID.
If access is denied, or an error occurred, false is returned.
-
Errors
- LinuxError is used to report errors:
- sys_eaccess
- The requested access is denied, either to the file or one
of the directories in its path.
- sys_einval
- Mode was incorrect.
- sys_enoent
- A directory component in Path doesn't exist or is a
dangling symbolic link.
- sys_enotdir
- A directory component in Path is not a directory.
- sys_enomem
- Insufficient kernel memory.
- sys_eloop
- Path has a circular symbolic link.
-
See also
- Chown, Chmod, Access (2)
-
Example
Program Example26;
{ Program to demonstrate the Access function. }
Uses linux;
begin
if Access ('/etc/passwd',W_OK) then
begin
Writeln ('Better check your system.');
Writeln ('I can write to the /etc/passwd file !');
end;
end.
8.2.2 AssignPipe
-
Declaration
-
Procedure AssignPipe (Pipe_in, Pipe_out : Text);
-
Description
AssignePipe creates a pipe, i.e. two file objects, one for input, one for output.
What is written to Pipe_out, can be read from Pipe_in.
Reading and writing happens through the usual Readln(Pipe_in,...) and
Writeln (Pipe_out,...) procedures.
-
Errors
- LinuxError is used to report errors:
- sys_emfile
- Too many file descriptors for this process.
- sys_enfile
- The system file table is full.
-
See also
- POpen, MkFifo, pipe (2)
-
Example
Program Example36;
{ Program to demonstrate the AssignPipe function. }
Uses linux;
Var pipi,pipo : Text;
s : String;
begin
Writeln ('Assigning Pipes.');
assignpipe(pipi,pipo);
if linuxerror<>0 then
Writeln('Error assigning pipes !');
Writeln ('Writing to pipe, and flushing.');
Writeln (pipo,'This is a textstring');close(pipo);
Writeln ('Reading from pipe.');
While not eof(pipi) do
begin
Readln (pipi,s);
Writeln ('Read from pipe : ',s);
end;
close (pipi);
writeln ('Closed pipes.');
writeln
end.
8.2.3 AssignStream
-
Declaration
-
Procedure AssignStream (StreamIn,StreamOut : Text; Const prog : String);
-
Description
AssignStream creates a 2 pipes, i.e. two file objects, one for input, one for
output, the other ends of these pipes are connected to standard input and and
output of Prog. Prog is the name of a program (including path)
with options, which will be executed.
What is written to StreamOut, will go to the standard input of
Prog. Whatever is written by Prog to it's standard output be read from
StreamIn.
Reading and writing happens through the usual Readln(StreamIn,...) and
Writeln (StreamOut,...) procedures.
Remark: You should not use Reset or Rewrite on a
file opened with POpen. This will close the file before re-opening
it again, thereby closing the connection with the program.
-
Errors
- LinuxError is used to report errors:
- sys_emfile
- Too many file descriptors for this process.
- sys_enfile
- The system file table is full.
Other errors include the ones by the fork and exec programs
-
See also
- AssignPipe, POpen,pipe (2)
-
Example
Program Example38;
{ Program to demonstrate the AssignStream function. }
Uses linux;
Var Si,So : Text;
S : String;
i : longint;
begin
if not (paramstr(1)='-son') then
begin
Writeln ('Calling son');
Assignstream (Si,So,'./ex38 -son');
if linuxerror<>0 then
begin
writeln ('AssignStream failed !');
halt(1);
end;
Writeln ('Speaking to son');
For i:=1 to 10 do
begin
writeln (so,'Hello son !');
if ioresult<>0 then writeln ('Can''t speak to son...');
end;
For i:=1 to 3 do writeln (so,'Hello chap !');
close (so);
while not eof(si) do
begin
readln (si,s);
writeln ('Father: Son said : ',S);
end;
Writeln ('Stopped conversation');
Close (Si);
Writeln ('Put down phone');
end
Else
begin
Writeln ('This is the son ');
While not eof (input) do
begin
readln (s);
if pos ('Hello son !',S)<>0 then
Writeln ('Hello Dad !')
else
writeln ('Who are you ?');
end;
close (output);
end
end.
8.2.4 BaseName
-
Declaration
-
Function BaseName (Const Path;Suf : Pathstr) : Pathstr;
-
Description
Returns the filename part of Path, stripping off Suf if it
exists.
The filename part is the whole name if Path contains no slash,
or the part of Path after the last slash.
The last character of the result is not a slash, unless the directory is the
root directory.
-
Errors
- None.
-
See also
- DirName, FExpand, Basename (1)
-
Example
Program Example48;
{ Program to demonstrate the BaseName function. }
Uses linux;
Var S : String;
begin
S:=FExpand(Paramstr(0));
Writeln ('This program is called : ',Basename(S,''));
end.
8.2.5 CFMakeRaw
-
Declaration
-
Procedure CFMakeRaw (var Tios:TermIOS);
-
Description
CFMakeRaw
Sets the flags in the Termios structure Tios to a state so that
the terminal will function in Raw Mode.
-
Errors
- None.
-
See also
- CFSetOSpeed, CFSetISpeed, termios (2)
For an example, see TCGetAttr.
8.2.6 CFSetISpeed
-
Declaration
-
Procedure CFSetISpeed (var Tios:TermIOS;Speed:Longint);
-
Description
CFSetISpeed
Sets the input baudrate in the TermIOS structure Tios to
Speed.
-
Errors
- None.
-
See also
- CFSetOSpeed, CFMakeRaw, termios (2)
8.2.7 CFSetOSpeed
-
Declaration
-
Procedure CFSetOSpeed (var Tios:TermIOS;Speed:Longint);
-
Description
CFSetOSpeed
Sets the output baudrate in the Termios structure Tios to
Speed.
-
Errors
- None.
-
See also
- CFSetISpeed, CFMakeRaw, termios (2)
8.2.8 Chown
-
Declaration
-
Function Chown (Path : Pathstr;NewUid,NewGid : Longint) : Boolean;
-
Description
Chown sets the User ID and Group ID of the file in Path to NewUid,
NewGid.
The function returns True if the call was succesfull, False if the call
failed.
-
Errors
Errors are returned in LinuxError.
- sys_eperm
- The effective UID doesn't match the ownership of the file,
and is not zero. Owner or group were not specified correctly.
- sys_eaccess
- One of the directories in Path has no
search (=execute) permission.
- sys_enoent
- A directory entry in Path does
not exist or is a symbolic link pointing to a non-existent directory.
- sys_enotdir
- A directory entry in OldPath or NewPath is
nor a directory.
- sys_enomem
- Insufficient kernel memory.
- sys_erofs
- The file is on a read-only filesystem.
- sys_eloop
- Path has a reference to a circular
symbolic link, i.e. a symbolic link, whose expansion points to itself.
-
See also
- Chmod, Access, Chown (() 2)
-
Example
Program Example24;
{ Program to demonstrate the Chown function. }
Uses linux;
Var UID,GID : Longint;
F : Text;
begin
Writeln ('This will only work if you are root.');
Write ('Enter a UID : ');readln(UID);
Write ('Enter a GID : ');readln(GID);
Assign (f,'test.txt');
Rewrite (f);
Writeln (f,'The owner of this file should become : ');
Writeln (f,'UID : ',UID);
Writeln (f,'GID : ',GID);
Close (F);
if not Chown ('test.txt',UID,GID) then
if LinuxError=Sys_EPERM then
Writeln ('You are not root !')
else
Writeln ('Chmod failed with exit code : ',LinuxError)
else
Writeln ('Changed owner successfully !');
end.
8.2.9 Chmod
-
Declaration
-
Function Chmod (Path : Pathstr;NewMode : Longint) : Boolean;
-
Description
Chmod
Sets the Mode bits of the file in Path to NewMode. Newmode can be
specified by 'or'-ing the following:
- S_ISUID
- Set user ID on execution.
- S_ISGID
- Set Group ID on execution.
- S_ISVTX
- Set sticky bit.
- S_IRUSR
- Read by owner.
- S_IWUSR
- Write by owner.
- S_IXUSR
- Execute by owner.
- S_IRGRP
- Read by group.
- S_IWGRP
- Write by group.
- S_IXGRP
- Execute by group.
- S_IROTH
- Read by others.
- S_IWOTH
- Write by others.
- S_IXOTH
- Execute by others.
- S_IRWXO
- Read, write, execute by others.
- S_IRWXG
- Read, write, execute by groups.
- S_IRWXU
- Read, write, execute by user.
-
Errors
Errors are returned in LinuxError.
- sys_eperm
- The effective UID doesn't match the ownership of the file,
and is not zero. Owner or group were not specified correctly.
- sys_eaccess
- One of the directories in Path has no
search (=execute) permission.
- sys_enoent
- A directory entry in Path does
not exist or is a symbolic link pointing to a non-existent directory.
- sys_enotdir
- A directory entry in OldPath or NewPath is
nor a directory.
- sys_enomem
- Insufficient kernel memory.
- sys_erofs
- The file is on a read-only filesystem.
- sys_eloop
- Path has a reference to a circular
symbolic link, i.e. a symbolic link, whose expansion points to itself.
-
See also
- Chown, Access, Chmod (() 2)
-
Example
Program Example23;
{ Program to demonstrate the Chmod function. }
Uses linux;
Var F : Text;
begin
{ Create a file }
Assign (f,'testex21');
Rewrite (F);
Writeln (f,'#!/bin/sh');
Writeln (f,'echo Some text for this file');
Close (F);
{ Octal() makes the correct number from a
number that LOOKS octal }
Chmod ('testex21',octal (777));
{ File is now executable }
execl ('./testex21');
end.
8.2.10 CloseDir
-
Declaration
-
Function CloseDir (p:pdir) : integer;
-
Description
CloseDir closes the directory pointed to by p.
It returns zero if the directory was closed succesfully, -1 otherwise.
-
Errors
- Errors are returned in LinuxError.
-
See also
- OpenDir, ReadDir, SeekDir, TellDir,
closedir (3)
For an example, see OpenDir.
8.2.11 DirName
-
Declaration
-
Function DirName (Const Path : Pathstr) : Pathstr;
-
Description
Returns the directory part of Path.
The directory is the part of Path before the last slash,
or empty if there is no slash.
The last character of the result is not a slash, unless the directory is the
root directory.
-
Errors
- None.
-
See also
- BaseName, FExpand, Dirname (1)
-
Example
Program Example47;
{ Program to demonstrate the DirName function. }
Uses linux;
Var S : String;
begin
S:=FExpand(Paramstr(0));
Writeln ('This program is in directory : ',Dirname(S));
end.
8.2.12 Dup
-
Declaration
-
Procedure Dup (Var OldFile, NewFile : Text);
-
Description
Makes NewFile an exact copy of OldFile, after having flushed the
buffer of OldFile. Due to the buffering mechanism of Pascal, this has not
the same functionality as the dup (2) call in C. The internal Pascal
buffers are not the same after this call, but when the buffers are flushed
(e.g. after output), the output is sent to the same file.
Doing an lseek will, however, work as in C, i.e. doing a lseek will change the
fileposition in both files.
-
Errors
- Linuxerror is used to report errors.
- sys_ebadf
- OldFile hasn't been assigned.
- sys_emfile
- Maximum number of open files for the process is reached.
-
See also
- Dup2, Dup (2)
-
Example
program Example31;
{ Program to demonstrate the Dup function. }
uses linux;
var f : text;
begin
if not dup (output,f) then
Writeln ('Dup Failed !');
writeln ('This is written to stdout.');
writeln (f,'This is written to the dup file, and flushed');flush(f);
writeln
end.
8.2.13 Dup2
-
Declaration
-
Procedure Dup2 (Var OldFile, NewFile : Text);
-
Description
Makes NewFile an exact copy of OldFile, after having flushed the
buffer of OldFile. NewFile can be an assigned file.
If newfile was open, it is closed first.
Due to the buffering mechanism of Pascal, this has not
the same functionality as the dup2 (2) call in C. The internal Pascal
buffers are not the same after this call, but when the buffers are flushed
(e.g. after output), the output is sent to the same file.
Doing an lseek will, however, work as in C, i.e. doing a lseek will change the
fileposition in both files.
-
Errors
- Linuxerror is used to report errors.
- sys_ebadf
- OldFile hasn't been assigned.
- sys_emfile
- Maximum number of open files for the process is reached.
-
See also
- Dup, Dup2 (2)
-
Example
program Example31;
{ Program to demonstrate the Dup function. }
uses linux;
var f : text;
i : longint;
begin
Assign (f,'text.txt');
Rewrite (F);
For i:=1 to 10 do writeln (F,'Line : ',i);
if not dup2 (output,f) then
Writeln ('Dup2 Failed !');
writeln ('This is written to stdout.');
writeln (f,'This is written to the dup file, and flushed');
flush(f);
writeln;
{ Remove file. Comment this if you want to check flushing.}
Unlink ('text.txt');
end.
8.2.14 EpochToLocal
-
Declaration
-
Procedure EpochToLocal (Epoch : Longint; var Year,Month,Day,Hour,Minute,Second : Word);
-
Description
Converts the epoch time (=Number of seconds since 00:00:00 , January 1,
1970, corrected for your time zone ) to local date and time.
-
Errors
- None
-
See also
- GetEpochTime, LocalToEpoch, GetTime,GetDate
-
Example
Program Example3;
{ Program to demonstrate the EpochToLocal function. }
Uses linux;
Var Year,month,day,hour,minute,seconds : Word;
begin
EpochToLocal (GetEpochTime,Year,month,day,hour,minute,seconds);
Writeln ('Current date : ',Day:2,'/',Month:2,'/',Year:4);
Writeln ('Current time : ',Hour:2,':',minute:2,':',seconds:2);
end.
8.2.15 Execl
-
Declaration
-
Procedure Execl (Path : pathstr);
-
Description
Replaces the currently running program with the program, specified in
path. Path is split into a command and it's options.
The executable in path is NOT searched in the path.
The current environment is passed to the program.
On success, execl does not return.
-
Errors
- Errors are reported in LinuxError:
- sys_eacces
- File is not a regular file, or has no execute permission.
A compononent of the path has no search permission.
- sys_eperm
- The file system is mounted noexec.
- sys_e2big
- Argument list too big.
- sys_enoexec
- The magic number in the file is incorrect.
- sys_enoent
- The file does not exist.
- sys_enomem
- Not enough memory for kernel, or to split command line.
- sys_enotdir
- A component of the path is not a directory.
- sys_eloop
- The path contains a circular reference (via symlinks).
-
See also
- Execve, Execv, Execvp, Execle,
Execlp, Fork, execvp (3)
-
Example
Program Example10;
{ Program to demonstrate the Execl function. }
Uses linux, strings;
begin
{ Execute 'ls -l', with current environment. }
{ 'ls' is NOT looked for in PATH environment variable.}
Execl ('/bin/ls -l');
end.
8.2.16 Execle
-
Declaration
-
Procedure Execle (Path : pathstr, Ep : ppchar);
-
Description
Replaces the currently running program with the program, specified in
path. Path is split into a command and it's options.
The executable in path is searched in the path, if it isn't
an absolute filename.
The environment in ep is passed to the program.
On success, execle does not return.
-
Errors
- Errors are reported in LinuxError:
- sys_eacces
- File is not a regular file, or has no execute permission.
A compononent of the path has no search permission.
- sys_eperm
- The file system is mounted noexec.
- sys_e2big
- Argument list too big.
- sys_enoexec
- The magic number in the file is incorrect.
- sys_enoent
- The file does not exist.
- sys_enomem
- Not enough memory for kernel, or to split command line.
- sys_enotdir
- A component of the path is not a directory.
- sys_eloop
- The path contains a circular reference (via symlinks).
-
See also
- Execve, Execv, Execvp,
Execl, Execlp, Fork, execvp (3)
-
Example
Program Example11;
{ Program to demonstrate the Execle function. }
Uses linux, strings;
begin
{ Execute 'ls -l', with current environment. }
{ 'ls' is NOT looked for in PATH environment variable.}
{ envp is defined in the system unit.}
Execle ('/bin/ls -l',envp);
end.
8.2.17 Execlp
-
Declaration
-
Procedure Execlp (Path : pathstr);
-
Description
Replaces the currently running program with the program, specified in
path. Path is split into a command and it's options.
The executable in path is searched in the path, if it isn't
an absolute filename.
The current environment is passed to the program.
On success, execlp does not return.
-
Errors
- Errors are reported in LinuxError:
- sys_eacces
- File is not a regular file, or has no execute permission.
A compononent of the path has no search permission.
- sys_eperm
- The file system is mounted noexec.
- sys_e2big
- Argument list too big.
- sys_enoexec
- The magic number in the file is incorrect.
- sys_enoent
- The file does not exist.
- sys_enomem
- Not enough memory for kernel, or to split command line.
- sys_enotdir
- A component of the path is not a directory.
- sys_eloop
- The path contains a circular reference (via symlinks).
-
See also
- Execve, Execv, Execvp, Execle,
Execl, Fork, execvp (3)
-
Example
Program Example12;
{ Program to demonstrate the Execlp function. }
Uses linux, strings;
begin
{ Execute 'ls -l', with current environment. }
{ 'ls' is looked for in PATH environment variable.}
{ envp is defined in the system unit.}
Execlp ('ls -l',envp);
end.
8.2.18 Execv
-
Declaration
-
Procedure Execv (Path : pathstr; args : ppchar);
-
Description
Replaces the currently running program with the program, specified in
path.
It gives the program the options in args.
This is a pointer to an array of pointers to null-terminated
strings. The last pointer in this array should be nil.
The current environment is passed to the program.
On success, execv does not return.
-
Errors
- Errors are reported in LinuxError:
- sys_eacces
- File is not a regular file, or has no execute permission.
A compononent of the path has no search permission.
- sys_eperm
- The file system is mounted noexec.
- sys_e2big
- Argument list too big.
- sys_enoexec
- The magic number in the file is incorrect.
- sys_enoent
- The file does not exist.
- sys_enomem
- Not enough memory for kernel.
- sys_enotdir
- A component of the path is not a directory.
- sys_eloop
- The path contains a circular reference (via symlinks).
-
See also
- Execve, Execvp, Execle,
Execl, Execlp, Fork, execv (3)
-
Example
Program Example8;
{ Program to demonstrate the Execv function. }
Uses linux, strings;
Const Arg0 : PChar = '/bin/ls';
Arg1 : Pchar = '-l';
Var PP : PPchar;
begin
GetMem (PP,3*SizeOf(Pchar));
PP[0]:=Arg0;
PP[1]:=Arg1;
PP[3]:=Nil;
{ Execute '/bin/ls -l', with current environment }
Execv ('/bin/ls',pp);
end.
8.2.19 Execve
-
Declaration
-
Procedure Execve (Path : pathstr; args,ep : ppchar);
-
Description
Replaces the currently running program with the program, specified in
path.
It gives the program the options in args, and the environment in
ep. They are pointers to an array of pointers to null-terminated
strings. The last pointer in this array should be nil.
On success, execve does not return.
-
Errors
- Errors are reported in LinuxError:
- eacces
- File is not a regular file, or has no execute permission.
A compononent of the path has no search permission.
- sys_ eperm
- The file system is mounted noexec.
- sys_ e2big
- Argument list too big.
- sys_ enoexec
- The magic number in the file is incorrect.
- sys_ enoent
- The file does not exist.
- sys_ enomem
- Not enough memory for kernel.
- sys_ enotdir
- A component of the path is not a directory.
- sys_ eloop
- The path contains a circular reference (via symlinks).
-
See also
- Execve, Execv, Execvp Execle,
Execl, Execlp, Fork, execve (2)
-
Example
Program Example7;
{ Program to demonstrate the Execve function. }
Uses linux, strings;
Const Arg0 : PChar = '/bin/ls';
Arg1 : Pchar = '-l';
Var PP : PPchar;
begin
GetMem (PP,3*SizeOf(Pchar));
PP[0]:=Arg0;
PP[1]:=Arg1;
PP[3]:=Nil;
{ Execute '/bin/ls -l', with current environment }
{ Envp is defined in system.inc }
ExecVe ('/bin/ls',pp,envp);
end.
8.2.20 Execvp
-
Declaration
-
Procedure Execvp (Path : pathstr; args : ppchar);
-
Description
Replaces the currently running program with the program, specified in
path. The executable in path is searched in the path, if it isn't
an absolute filename.
It gives the program the options in args. This is a pointer to an array of pointers to null-terminated
strings. The last pointer in this array should be nil.
The current environment is passed to the program.
On success, execvp does not return.
-
Errors
- Errors are reported in LinuxError:
- sys_eacces
- File is not a regular file, or has no execute permission.
A compononent of the path has no search permission.
- sys_eperm
- The file system is mounted noexec.
- sys_e2big
- Argument list too big.
- sys_enoexec
- The magic number in the file is incorrect.
- sys_enoent
- The file does not exist.
- sys_enomem
- Not enough memory for kernel.
- sys_enotdir
- A component of the path is not a directory.
- sys_eloop
- The path contains a circular reference (via symlinks).
-
See also
- Execve, Execv, Execle,
Execl, Execlp, Fork, execvp (3)
-
Example
Program Example9;
{ Program to demonstrate the Execvp function. }
Uses linux, strings;
Const Arg0 : PChar = 'ls';
Arg1 : Pchar = '-l';
Var PP : PPchar;
begin
GetMem (PP,3*SizeOf(Pchar));
PP[0]:=Arg0;
PP[1]:=Arg1;
PP[3]:=Nil;
{ Execute 'ls -l', with current environment. }
{ 'ls' is looked for in PATH environment variable.}
{ Envp is defined in the system unit. }
Execvp ('ls',pp,envp);
end.
8.2.21 FD_ZERO
-
Declaration
-
Procedure FD_ZERO (var fds:fdSet);
-
Description
FD_ZERO clears all the filedescriptors in the file descriptor
set fds.
-
Errors
- None.
-
See also
- Select,
SelectText,
GetFS,
FD_Clr,
FD_Set,
FD_IsSet
For an example, see Select.
8.2.22 FD_Clr
-
Declaration
-
Procedure FD_Clr (fd:longint;var fds:fdSet);
-
Description
FD_Clr clears file descriptor fd in filedescriptor s
et fds.
-
Errors
- None.
-
See also
- Select,
SelectText,
GetFS,
FD_ZERO,
FD_Set,
FD_IsSet
For an example, see Select.
8.2.23 FD_IsSet
-
Declaration
-
Function FD_IsSet (fd:longint;var fds:fdSet) : boolean;
-
Description
FD_Set Checks whether file descriptor fd in filedescriptor set fds
is set.
-
Errors
- None.
-
See also
- Select, SelectText, GetFS,
FD_ZERO,
FD_Clr,
FD_Set
For an example, see Select.
8.2.24 FD_Set
-
Declaration
-
Procedure FD_Set (fd:longint;var fds:fdSet);
-
Description
FD_Set sets file descriptor fd in filedescriptor set fds.
-
Errors
- None.
-
See also
- Select, SelectText, GetFS,FD_ZERO,
FD_Clr, FD_IsSet
For an example, see Select.
8.2.25 fdClose
-
Declaration
-
Function fdClose (fd:longint) : boolean;
-
Description
fdClose closes a file with file descriptor Fd. The function
returns True if the file was closed successfully, False
otherwise.
-
Errors
- Errors are returned in LinuxError
-
See also
- fdOpen, fdRead, fdWrite,fdTruncate,
fdFlush, seefFdSeek
For an example, see fdOpen.
8.2.26 fdFlush
-
Declaration
-
Function fdFlush (fd:Longint) : boolean;
-
Description
fdflush flushes the Linux kernel file buffer, so the file is actually
written to disk. This is NOT the same as the internal buffer, maintained by
Free Pascal.
The function returns True if the call was successful, false if
an error occurred.
-
Errors
- Errors are returned in LinuxError.
-
See also
- fdOpen, fdClose, fdRead,fdWrite,
fdTruncate, fdSeek
For an example, see fdRead.
8.2.27 fdOpen
-
Declaration
-
Function fdOpen (Var PathName;flags:longint[; Mode: longint]) : longint;
-
Description
fdOpen opens a file in pathname with flags flags a ORed combination of
Open_Accmode, Open_RdOnly, Open_WrOnly, Open_RdWr, Open_Creat,
Open_Excl, Open_NoCtty, Open_Trunc, Open_Append, Open_NonBlock,
Open_NDelay, Open_Sync PathName can be of type PChar or
String
The optional mode argument specifies the permissions to set when opening
the file. This is modified by the umask setting. The real permissions are
Mode and not umask.
The return value of the function is the filedescriptor, or a negative
value if there was an error.
-
Errors
- Errors are returned in LinuxError
-
See also
- fdClose, fdRead, fdWrite,fdTruncate,
fdFlush, fdSeek
-
Example
Program Example19;
{ Program to demonstrate the fdOpen, fdwrite and fdCLose functions. }
Uses linux;
Const Line : String[80] = 'This is easy writing !';
Var FD : Longint;
begin
FD:=fdOpen ('Test.dat',Open_WrOnly or Open_Creat);
if FD>0 then
begin
if length(Line)<>fdwrite (FD,Line[1],Length(Line)) then
Writeln ('Error when writing to file !');
fdClose(FD);
end;
end.
8.2.28 fdRead
-
Declaration
-
Function fdRead (fd:longint;var buf;size:longint : longint;
-
Description
fdRead reads at most size bytes from the file descriptor
fd, and stores them in buf.
The function returns the number of bytes actually read, or -1 if
an error occurred.
No checking on the length of buf is done.
-
Errors
- Errors are returned in LinuxError.
-
See also
- fdOpen, fdClose, fdWrite,fdTruncate,
fdFlush, fdSeek
-
Example
Program Example20;
{ Program to demonstrate the fdRead and fdTruncate functions. }
Uses linux;
Const Data : string[10] = '12345687890';
Var FD : Longint;
l : longint;
begin
FD:=fdOpen('test.dat',open_wronly or open_creat,octal(666));
if fd>0 then
begin
{ Fill file with data }
for l:=1 to 10 do
if fdWrite (FD,Data[1],10)<>10 then
begin
writeln ('Error when writing !');
halt(1);
end;
fdClose(FD);
FD:=fdOpen('test.dat',open_rdonly);
{ Read data again }
If FD>0 then
begin
For l:=1 to 5 do
if fdRead (FD,Data[1],10)<>10 then
begin
Writeln ('Error when Reading !');
Halt(2);
end;
fdCLose(FD);
{ Truncating file at 60 bytes }
{ For truncating, file must be open or write }
FD:=fdOpen('test.dat',open_wronly,octal(666));
if FD>0 then
begin
if not fdTruncate(FD,60) then
Writeln('Error when truncating !');
fdClose (FD);
end;
end;
end;
end.
8.2.29 fdSeek
-
Declaration
-
Function fdSeek (fd,Pos,SeekType:longint : longint;
-
Description
fdSeek sets the current fileposition of file fd to
Pos, starting from SeekType, which can be one of the following:
- Seek_Set
- Pos is the absolute position in the file.
- Seek_Cur
- Pos is relative to the current position.
- Seek_end
- Pos is relative to the end of the file.
The function returns the new fileposition, or -1 of an error occurred.
-
Errors
- Errors are returned in LinuxError.
-
See also
- fdOpen, fdWrite, fdClose,
fdRead,fdTruncate,
fdFlush
For an example, see fdOpen.
8.2.30 fdTruncate
-
Declaration
-
Function fdTruncate (fd,size:longint) : boolean;
-
Description
fdTruncate sets the length of a file in fd on size
bytes, where size must be less than or equal to the current length of
the file in fd.
The function returns True if the call was successful, false if
an error occurred.
-
Errors
- Errors are returned in LinuxError.
-
See also
- fdOpen, fdClose, fdRead,fdWrite,fdFlush,
fdSeek
8.2.31 fdWrite
-
Declaration
-
Function fdWrite (fd:longint;var buf;size:longint : longint;
-
Description
fdWrite writes at most size bytes from buf to
file descriptor fd.
The function returns the number of bytes actually written, or -1 if an error
occurred.
-
Errors
- Errors are returned in LinuxError.
-
See also
- fdOpen, fdClose, fdRead,fdTruncate,
fdSeek, fdFlush
8.2.32 FExpand
-
Declaration
-
Function FExpand (Const Path: Pathstr) : pathstr;
-
Description
Expands Path to a full path, starting from root,
eliminating directory references such as . and .. from the result.
-
Errors
- None
-
See also
- BaseName,DirName
-
Example
Program Example45;
{ Program to demonstrate the FExpand function. }
Uses linux;
begin
Writeln ('This program is in : ',FExpand(Paramstr(0)));
end.
8.2.33 FLock
-
Declaration
-
Procedure FLock (Var F; Mode : longint);
-
Description
FLock implements file locking. it sets or removes a lock on the file
F. F can be of type Text or File, or it can be a LINUX
filedescriptor (a longint)
Mode can be one of the following constants :
- LOCK_SH
- sets a shared lock.
- LOCK_EX
- sets an exclusive lock.
- LOCK_UN
- unlocks the file.
- LOCK_NB
- This can be OR-ed together with the other. If this is done
the application doesn't block when locking.
-
Errors
- Errors are reported in LinuxError.
-
See also
- Fcntl, flock (2)
8.2.34 FSStat
-
Declaration
-
Function FSStat (Path : Pathstr; Var Info : statfs) : Boolean;
-
Description
Return in Info information about the filesystem on which the file
Path resides. Info is of type statfs.
The function returns True if the call was succesfull, False if the call
failed.
-
Errors
- LinuxError is used to report errors.
- sys_enotdir
- A component of Path is not a directory.
- sys_einval
- Invalid character in Path.
- sys_enoent
- Path does not exist.
- sys_eaccess
- Search permission is denied for component in
Path.
- sys_eloop
- A circular symbolic link was encountered in Path.
- sys_eio
- An error occurred while reading from the filesystem.
-
See also
- FStat, LStat, statfs (2)
-
Example
program Example30;
{ Program to demonstrate the FSStat function. }
uses linux;
var s : string;
info : statfs;
begin
writeln ('Info about current partition : ');
s:='.';
while s<>'q' do
begin
if not fsstat (s,info) then
begin
writeln('Fstat failed. Errno : ',linuxerror);
halt (1);
end;
writeln;
writeln ('Result of fsstat on file ''',s,'''.');
writeln ('fstype : ',info.fstype);
writeln ('bsize : ',info.bsize);
writeln ('bfree : ',info.bfree);
writeln ('bavail : ',info.bavail);
writeln ('files : ',info.files);
writeln ('ffree : ',info.ffree);
writeln ('fsid : ',info.fsid);
writeln ('Namelen : ',info.namelen);
write ('Type name of file to do fsstat. (q quits) :');
readln (s)
end;
end.
8.2.35 FSearch
-
Declaration
-
Function FSearch (Path : pathstr;DirList : string) : Pathstr;
-
Description
Searches in DirList, a colon separated list of directories,
for a file named Path. It then returns a path to the found file.
-
Errors
- An empty string if no such file was found.
-
See also
- BaseName, DirName, FExpand
-
Example
Program Example46;
{ Program to demonstrate the FSearch function. }
Uses linux,strings;
begin
Writeln ('ls is in : ',FSearch ('ls',strpas(Getenv('PATH'))));
end.
8.2.36 FStat
-
Declaration
-
Function FStat (Path : Pathstr; Var Info : stat) : Boolean;
-
Description
FStat gets information about the file specified in Path, and stores it in
Info, which is of type stat.
The function returns True if the call was succesfull, False if the call
failed.
-
Errors
- LinuxError is used to report errors.
- sys_enoent
- Path does not exist.
-
See also
- FSStat, LStat, stat (2)
-
Example
program example28;
{ Program to demonstrate the FStat function. }
uses linux;
var f : text;
i : byte;
info : stat;
begin
{ Make a file }
assign (f,'test.fil');
rewrite (f);
for i:=1 to 10 do writeln (f,'Testline # ',i);
close (f);
{ Do the call on made file. }
if not fstat ('test.fil',info) then
begin
writeln('Fstat failed. Errno : ',linuxerror);
halt (1);
end;
writeln;
writeln ('Result of fstat on file ''test.fil''.');
writeln ('Inode : ',info.ino);
writeln ('Mode : ',info.mode);
writeln ('nlink : ',info.nlink);
writeln ('uid : ',info.uid);
writeln ('gid : ',info.gid);
writeln ('rdev : ',info.rdev);
writeln ('Size : ',info.size);
writeln ('Blksize : ',info.blksze);
writeln ('Blocks : ',info.blocks);
writeln ('atime : ',info.atime);
writeln ('mtime : ',info.mtime);
writeln ('ctime : ',info.ctime);
{ Remove file }
erase (f);
end.
8.2.37 Fcntl
-
Declaration
-
Function Fcntl (Fd : text, Cmd : Integer) : Integer;
-
Description
Read a file's attributes. Fd is an assigned file.
Cmd speciefies what to do, and is one of the following:
- F_GetFd
- Read the close_on_exec flag. If the low-order bit is 0, then
the file will remain open across execve calls.
- F_GetFl
- Read the descriptor's flags.
- F_GetOwn
- Get the Process ID of the owner of a socket.
-
Errors
LinuxError is used to report errors.
- sys_ebadf
- Fd has a bad file descriptor.
-
See also
- Fcntl, Fcntl (2)
8.2.38 Fcntl
-
Declaration
-
Procedure Fcntl (Fd : text, Cmd : Integer; Arg : longint);
-
Description
Read or Set a file's attributes. Fd is an assigned file.
Cmd speciefies what to do, and is one of the following:
- F_SetFd
- Set the close_on_exec flag of Fd. (only the least
siginificant bit is used).
- F_GetLk
- Return the flock record that prevents this process from
obtaining the lock, or set the l_type field of the lock of there is no
obstruction. Arg is a pointer to a flock record.
- F_SetLk
- Set the lock or clear it (depending on l_type in the
flock structure). if the lock is held by another process, an error
occurs.
- F_GetLkw
- Same as for F_Setlk, but wait until the lock is
released.
- F_SetOwn
- Set the Process or process group that owns a socket.
-
Errors
LinuxError is used to report errors.
- sys_ebadf
- Fd has a bad file descriptor.
- sys_eagain or sys_eaccess
- For F_SetLk, if the lock is
held by another process.
-
See also
- Fcntl, Fcntl (2)
8.2.39 Fork
-
Declaration
-
Function Fork : Longint;
-
Description
Fork creates a child process which is a copy of the parent process.
Fork returns the process ID in the parent process, and zero in the child's
process. (you can get the parent's PID with GetPPid).
-
Errors
- On error, -1 is returned to the parent, and no child is created.
- sys_eagain
- Not enough memory to create child process.
-
See also
- Execve, fork (2)
-
Example
Program Example14;
{ Program to demonstrate the Fork and WaitPidfunction. }
Uses linux;
Var PID, ExitStatus : Longint;
begin
Writeln ('Spawning a child');
PID:=Fork;
If PID=0 then
begin
Writeln ('Hello From the Child !!');
Writeln ('Exiting with exit status 1 !');
Halt (1);
end
Else
begin
Writeln ('Spawned child with PID : ',PID);
WaitPid (PID,@ExitStatus,0);
Writeln ('Child exited with status : ',ExitStatus shr 8);
end;
end.
8.2.40 GetDate
-
Declaration
-
Procedure GetDate (Var Year, Month, Day : Word) ;
-
Description
Returns the current day.
-
Errors
- None
-
See also
- GetEpochTime, GetTime, EpochToLocal
-
Example
Program Example6;
{ Program to demonstrate the GetDate function. }
Uses linux;
Var Year, Month, Day : Word;
begin
GetDate (Year, Month, Day);
Writeln ('Date : ',Day:2,'/',Month:2,'/',Year:4);
end.
8.2.41 GetDomainName
-
Declaration
-
Function GetDomainName : String;
-
Description
Get the domain name of the machine on which the process is running.
An empty string is returned if the domain is not set.
-
Errors
- None.
-
See also
- GetHostName,seemGetdomainname2
-
Example
Program Example39;
{ Program to demonstrate the GetDomainName function. }
Uses linux;
begin
Writeln ('Domain name of this machine is : ',GetDomainName);
end.
8.2.42 GetEGid
-
Declaration
-
Function GetEGid : Longint;
-
Description
Get the effective group ID of the currently running process.
-
Errors
- None.
-
See also
- GetGid, getegid (2)
-
Example
Program Example18;
{ Program to demonstrate the GetGid and GetEGid functions. }
Uses linux;
begin
writeln ('Group Id = ',getgid,' Effective group Id = ',getegid);
end.
8.2.43 GetEUid
-
Declaration
-
Function GetEUid : Longint;
-
Description
Get the effective user ID of the currently running process.
-
Errors
- None.
-
See also
- GetEUid, geteuid (2)
-
Example
Program Example17;
{ Program to demonstrate the GetUid and GetEUid functions. }
Uses linux;
begin
writeln ('User Id = ',getuid,' Effective user Id = ',geteuid);
end.
8.2.44 GetEnv
-
Declaration
-
Function GetEnv (P : String) : PChar;
-
Description
Returns the value of the environment variable in P. If the variable is
not defined, nil is returned. The value of the environment variable may be
the empty string.
A PChar is returned to accomodate for strings longer than 255 bytes,
TERMCAP and LS_COLORS, for instance.
-
Errors
- None.
-
See also
- sh (1) , csh (1)
-
Example
Program Example41;
{ Program to demonstrate the GetEnv function. }
Uses linux;
begin
Writeln ('Path is : ',Getenv('PATH'));
end.
8.2.45 GetEpochTime
-
Declaration
-
Function GetEpochTime : longint;
-
Description
returns the number of seconds since 00:00:00 gmt, january 1, 1970.
it is adjusted to the local time zone, but not to DST.
-
Errors
- no errors
-
See also
- EpochToLocal, GetTime, time (2)
-
Example
Program Example1;
{ Program to demonstrate the GetEpochTime function. }
Uses linux;
begin
Write ('Secs past the start of the Epoch (00:00 1/1/1980) : ');
Writeln (GetEpochTime);
end.
8.2.46 GetFS
-
Declaration
-
Function GetFS (Var F : Any File Type) : Longint;
-
Description
GetFS returns the file selector that the kernel provided for your
file. In principle you don' need this file selector. Only for some calls
it is needed, such as the Select call or so.
-
Errors
- In case the file was not opened, then -1 is returned.
-
See also
- Select
-
Example
Program Example33;
{ Program to demonstrate the SelectText function. }
Uses linux;
Var tv : TimeVal;
begin
Writeln ('Press the <ENTER> to continue the program.');
{ Wait until File descriptor 0 (=Input) changes }
SelectText (Input,nil);
{ Get rid of <ENTER> in buffer }
readln;
Writeln ('Press <ENTER> key in less than 2 seconds...');
tv.sec:=2;
tv.usec:=0;
if SelectText (Input,@tv)>0 then
Writeln ('Thank you !')
else
Writeln ('Too late !');
end.
8.2.47 GetGid
-
Declaration
-
Function GetGid : Longint;
-
Description
Get the real group ID of the currently running process.
-
Errors
- None.
-
See also
- GetEGid, getgid (2)
-
Example
Program Example18;
{ Program to demonstrate the GetGid and GetEGid functions. }
Uses linux;
begin
writeln ('Group Id = ',getgid,' Effective group Id = ',getegid);
end.
8.2.48 GetHostName
-
Declaration
-
Function GetHostName : String;
-
Description
Get the hostname of the machine on which the process is running.
An empty string is returned if hostname is not set.
-
Errors
- None.
-
See also
- GetDomainName,seemGethostname2
-
Example
Program Example40;
{ Program to demonstrate the GetHostName function. }
Uses linux;
begin
Writeln ('Name of this machine is : ',GetHostName);
end.
8.2.49 GetPid
-
Declaration
-
Function GetPid : Longint;
-
Description
Get the Process ID of the currently running process.
-
Errors
- None.
-
See also
- GetPPid, getpid (2)
-
Example
Program Example16;
{ Program to demonstrate the GetPid, GetPPid function. }
Uses linux;
begin
Writeln ('Process Id = ',getpid,' Parent process Id = ',getppid);
end.
8.2.50 GetPPid
-
Declaration
-
Function GetPPid : Longint;
-
Description
Get the Process ID of the parent process.
-
Errors
- None.
-
See also
- GetPid, getppid (2)
-
Example
Program Example16;
{ Program to demonstrate the GetPid, GetPPid function. }
Uses linux;
begin
Writeln ('Process Id = ',getpid,' Parent process Id = ',getppid);
end.
8.2.51 GetPriority
-
Declaration
-
Function GetPriority (Which,Who : Integer) : Integer;
-
Description
GetPriority returns the priority with which a process is running.
Which process(es) is determined by the Which and Who variables.
Which can be one of the pre-defined Prio_Process, Prio_PGrp,
Prio_User, in which case Who is the process ID, Process group ID or
User ID, respectively.
-
Errors
Error checking must be done on LinuxError, since a priority can be negative.
- sys_esrch
- No process found using which and who.
- sys_einval
- Which was not one of Prio_Process, Prio_Grp
or Prio_User.
-
See also
- SetPriority, Nice, Getpriority (2)
For an example, see Nice.
8.2.52 GetTime
-
Declaration
-
Procedure GetTime (Var Hour,Minute, Second : Word) ;
-
Description
Returns the current time of the day.
-
Errors
- None
-
See also
- GetEpochTime, GetDate, EpochToLocal
-
Example
Program Example5;
{ Program to demonstrate the GetTime function. }
Uses linux;
Var Hour, Minute, Second : Word;
begin
GetTime (Hour, Minute, Second);
Writeln ('Time : ',Hour:2,':',Minute:2,':',Second:2);
end.
8.2.53 GetUid
-
Declaration
-
Function GetUid : Longint;
-
Description
Get the real user ID of the currently running process.
-
Errors
- None.
-
See also
- GetEUid, getuid (2)
-
Example
Program Example17;
{ Program to demonstrate the GetUid and GetEUid functions. }
Uses linux;
begin
writeln ('User Id = ',getuid,' Effective user Id = ',geteuid);
end.
8.2.54 Glob
-
Declaration
-
Function Glob (Const Path : Pathstr) : PGlob;
-
Description
Glob returns a pointer to a glob structure which contains all filenames which
exist and match the pattern in Path.
The pattern can contain wildcard characters, which have their
usual meaning.
-
Errors
- Returns nil on error, and LinuxError is set.
- sys_enomem
- No memory on heap for glob structure.
- others
- As returned by the opendir call, and sys_readdir.
-
See also
- GlobFree, Glob (3)
-
Example
Program Example49;
{ Program to demonstrate the Glob and GlobFree functions. }
Uses linux;
Var G1,G2 : PGlob;
begin
G1:=Glob ('*');
if LinuxError=0 then
begin
G2:=G1;
Writeln ('Files in this directory : ');
While g2<>Nil do
begin
Writeln (g2^.name);
g2:=g2^.next;
end;
GlobFree (g1);
end;
end.
8.2.55 GlobFree
-
Declaration
-
Procedure GlobFree (Var P : Pglob);
-
Description
Releases the memory, occupied by a pglob structure. P is set to nil.
-
Errors
- None
-
See also
- Glob
For an example, see Glob.
8.2.56 IOCtl
-
Declaration
-
Procedure IOCtl (Handle,Ndx: Longint; Data: Pointer);
-
Description
This is a general interface to the Unix/ LINUX ioctl call.
It performs various operations on the filedescriptor Handle.
Ndx describes the operation to perform.
Data points to data needed for the Ndx function.
The structure of this data is function-dependent, so we don't elaborate on
this here.
For more information on this, see various manual pages under linux.
-
Errors
Errors are reported in LinuxError. They are very dependent on the used
function, that's why we don't list them here
-
See also
- ioctl (2)
-
Example
Program Example54;
uses Linux;
{ Program to demonstrate the IOCtl function. }
var
tios : Termios;
begin
IOCtl(1,TCGETS,@tios);
WriteLn('Input Flags : $',hexstr(tios.c_iflag,8));
WriteLn('Output Flags : $',hexstr(tios.c_oflag,8));
WriteLn('Line Flags : $',hexstr(tios.c_lflag,8));
WriteLn('Control Flags: $',hexstr(tios.c_cflag,8));
end.
8.2.57 IOperm
-
Declaration
-
Function IOperm (From,Num : Cadinal; Value : Longint) : boolean;
-
Description
IOperm
sets permissions on Num ports starting with port From to
Value. The function returns True if the call was successfull,
False otherwise.
Remark:
- This works ONLY as root.
- Only the first 0x03ff ports can be set.
- When doing a Fork, the permissions are reset. When doing a
Execve they are kept.
-
Errors
- Errors are returned in LinuxError
-
See also
- ioperm (2)
8.2.58 IsATTY
-
Declaration
-
Function IsATTY (var f) : Boolean;
-
Description
Check if the filehandle described by f is a terminal.
f can be of type
- longint for file handles;
- Text for text variables such as input etc.
Returns True if f is a terminal, False otherwise.
-
Errors
- No errors are reported
-
See also
- IOCtl,TTYName
8.2.59 S_ISBLK
-
Declaration
-
Function S_ISBLK (m:integer) : boolean;
-
Description
S_ISBLK checks the file mode m to see whether the file is a
block device file. If so it returns True.
-
Errors
- FStat,
S_ISLNK,
S_ISREG,
S_ISDIR,
S_ISCHR,
S_ISFIFO,
S_ISSOCK
-
See also
- ISLNK.
8.2.60 S_ISCHR
-
Declaration
-
Function S_ISCHR (m:integer) : boolean;
-
Description
S_ISCHR checks the file mode m to see whether the file is a
character device file. If so it returns True.
-
Errors
- FStat,
S_ISLNK,
S_ISREG,
S_ISDIR,
S_ISBLK,
S_ISFIFO,
S_ISSOCK
-
See also
- ISLNK.
8.2.61 S_ISDIR
-
Declaration
-
Function S_ISDIR (m:integer) : boolean;
-
Description
S_ISDIR checks the file mode m to see whether the file is a
directory. If so it returns True
-
Errors
- FStat,
S_ISLNK,
S_ISREG,
S_ISCHR,
S_ISBLK,
S_ISFIFO,
S_ISSOCK
-
See also
- ISLNK.
8.2.62 S_ISFIFO
-
Declaration
-
Function S_ISFIFO (m:integer) : boolean;
-
Description
S_ISFIFO checks the file mode m to see whether the file is a
fifo (a named pipe). If so it returns True.
-
Errors
- FStat,
S_ISLNK,
S_ISREG,
S_ISDIR,
S_ISCHR,
S_ISBLK,
S_ISSOCK
-
See also
- ISLNK.
8.2.63 S_ISLNK
-
Declaration
-
Function S_ISLNK (m:integer) : boolean;
-
Description
S_ISLNK checks the file mode m to see whether the file is a
symbolic link. If so it returns True
-
Errors
- FStat,
S_ISREG,
S_ISDIR,
S_ISCHR,
S_ISBLK,
S_ISFIFO,
S_ISSOCK
-
See also
- linuxex/ex53.pp
-
Example
Program Example53;
{ Program to demonstrate the S_ISLNK function. }
Uses linux;
Var Info : Stat;
begin
if FStat (paramstr(1),info) then
begin
if S_ISLNK(info.mode) then
Writeln ('File is a link');
if S_ISREG(info.mode) then
Writeln ('File is a regular file');
if S_ISDIR(info.mode) then
Writeln ('File is a directory');
if S_ISCHR(info.mode) then
Writeln ('File is a character device file');
if S_ISBLK(info.mode) then
Writeln ('File is a block device file');
if S_ISFIFO(info.mode) then
Writeln ('File is a named pipe (FIFO)');
if S_ISSOCK(info.mode) then
Writeln ('File is a socket');
end;
end.
8.2.64 S_ISREG
-
Declaration
-
Function S_ISREG (m:integer) : boolean;
-
Description
S_ISREG checks the file mode m to see whether the file is a
regular file. If so it returns True
-
Errors
- FStat,
S_ISLNK,
S_ISDIR,
S_ISCHR,
S_ISBLK,
S_ISFIFO,
S_ISSOCK
-
See also
- ISLNK.
8.2.65 S_ISSOCK
-
Declaration
-
Function S_ISSOCK (m:integer) : boolean;
-
Description
S_ISSOCK checks the file mode m to see whether the file is a
socket. If so it returns True.
-
Errors
- FStat,
S_ISLNK,
S_ISREG,
S_ISDIR,
S_ISCHR,
S_ISBLK,
S_ISFIFO
-
See also
- ISLNK.
8.2.66 Kill
-
Declaration
-
Function Kill Pid : Longint; Sig : Integer) : Integer;
-
Description
Send a signal Sig to a process or process group. If Pid>0 then
the signal is sent to Pid, if it equals -1, then the signal is sent to
all processes except process 1. If Pid<-1 then the signal is sent to
process group -Pid.
The return value is zero, except in case three, where the return value is the
number of processes to which the signal was sent.
-
Errors
- LinuxError is used to report errors:
- sys_einval
- An invalid signal is sent.
- sys_esrch
- The Pid or process group don't exist.
- sys_eperm
- The effective userid of the current process doesn't math
the one of process Pid.
-
See also
- SigAction, Signal, Kill (2)
8.2.67 LStat
-
Declaration
-
Function LStat (Path : Pathstr; Var Info : stat) : Boolean;
-
Description
LStat gets information about the link specified in Path, and stores it in
Info, which is of type stat. Contrary to FStat, it stores
information about the link, not about the file the link points to.
The function returns True if the call was succesfull, False if the call
failed.
-
Errors
- LinuxError is used to report errors.
- sys_enoent
- Path does not exist.
-
See also
- FStat, FSStat, stat (2)
-
Example
program example29;
{ Program to demonstrate the LStat function. }
uses linux;
var f : text;
i : byte;
info : stat;
begin
{ Make a file }
assign (f,'test.fil');
rewrite (f);
for i:=1 to 10 do writeln (f,'Testline # ',i);
close (f);
{ Do the call on made file. }
if not fstat ('test.fil',info) then
begin
writeln('Fstat failed. Errno : ',linuxerror);
halt (1);
end;
writeln;
writeln ('Result of fstat on file ''test.fil''.');
writeln ('Inode : ',info.ino);
writeln ('Mode : ',info.mode);
writeln ('nlink : ',info.nlink);
writeln ('uid : ',info.uid);
writeln ('gid : ',info.gid);
writeln ('rdev : ',info.rdev);
writeln ('Size : ',info.size);
writeln ('Blksize : ',info.blksze);
writeln ('Blocks : ',info.blocks);
writeln ('atime : ',info.atime);
writeln ('mtime : ',info.mtime);
writeln ('ctime : ',info.ctime);
If not SymLink ('test.fil','test.lnk') then
writeln ('Link failed ! Errno :',linuxerror);
if not lstat ('test.lnk',info) then
begin
writeln('LStat failed. Errno : ',linuxerror);
halt (1);
end;
writeln;
writeln ('Result of fstat on file ''test.lnk''.');
writeln ('Inode : ',info.ino);
writeln ('Mode : ',info.mode);
writeln ('nlink : ',info.nlink);
writeln ('uid : ',info.uid);
writeln ('gid : ',info.gid);
writeln ('rdev : ',info.rdev);
writeln ('Size : ',info.size);
writeln ('Blksize : ',info.blksze);
writeln ('Blocks : ',info.blocks);
writeln ('atime : ',info.atime);
writeln ('mtime : ',info.mtime);
writeln ('ctime : ',info.ctime);
{ Remove file and link }
erase (f);
unlink ('test.lnk');
end.
8.2.68 Link
-
Declaration
-
Function Link (OldPath,NewPath : pathstr) : Boolean;
-
Description
Link makes NewPath point to the same file als OldPath. The two files
then have the same inode number. This is known as a 'hard' link.
The function returns True if the call was succesfull, False if the call
failed.
-
Errors
- Errors are returned in LinuxError.
- sys_exdev
- OldPath and NewPath are not on the same
filesystem.
- sys_eperm
- The filesystem containing oldpath and newpath doesn't
support linking files.
- sys_eaccess
- Write access for the directory containing Newpath
is disallowed, or one of the directories in OldPath or NewPath has no
search (=execute) permission.
- sys_enoent
- A directory entry in OldPath or NewPath does
not exist or is a symbolic link pointing to a non-existent directory.
- sys_enotdir
- A directory entry in OldPath or NewPath is
nor a directory.
- sys_enomem
- Insufficient kernel memory.
- sys_erofs
- The files are on a read-only filesystem.
- sys_eexist
- NewPath already exists.
- sys_emlink
- OldPath has reached maximal link count.
- sys_eloop
- OldPath or NewPath has a reference to a circular
symbolic link, i.e. a symbolic link, whose expansion points to itself.
- sys_enospc
- The device containing NewPath has no room for anothe
entry.
- sys_eperm
- OldPath points to . or .. of a directory.
-
See also
- SymLink, UnLink, Link (2)
-
Example
Program Example21;
{ Program to demonstrate the Link and UnLink functions. }
Uses linux;
Var F : Text;
S : String;
begin
Assign (F,'test.txt');
Rewrite (F);
Writeln (F,'This is written to test.txt');
Close(f);
{ new.txt and test.txt are now the same file }
if not Link ('test.txt','new.txt') then
writeln ('Error when linking !');
{ Removing test.txt still leaves new.txt }
If not Unlink ('test.txt') then
Writeln ('Error when unlinking !');
Assign (f,'new.txt');
Reset (F);
While not EOF(f) do
begin
Readln(F,S);
Writeln ('> ',s);
end;
Close (f);
{ Remove new.txt also }
If not Unlink ('new.txt') then
Writeln ('Error when unlinking !');
end.
8.2.69 LocalToEpoch
-
Declaration
-
Function LocalToEpoch (Year,Month,Day,Hour,Minute,Second : Word) : longint;
-
Description
Converts the Local time to epoch time (=Number of seconds since 00:00:00 , January 1,
1970 ).
-
Errors
- None
-
See also
- GetEpochTime, EpochToLocal, GetTime,GetDate
-
Example
Program Example4;
{ Program to demonstrate the LocalToEpoch function. }
Uses linux;
Var year,month,day,hour,minute,second : Word;
begin
Write ('Year : ');readln(Year);
Write ('Month : ');readln(Month);
Write ('Day : ');readln(Day);
Write ('Hour : ');readln(Hour);
Write ('Minute : ');readln(Minute);
Write ('Seonds : ');readln(Second);
Write ('This is : ');
Write (LocalToEpoch(year,month,day,hour,minute,second));
Writeln (' seconds past 00:00 1/1/1980');
end.
8.2.70 MkFifo
-
Declaration
-
Function MkFifo (PathName: String; Mode : Longint) : Boolean;
-
Description
MkFifo creates named a named pipe in the filesystem, with name
PathName and mode Mode.
-
Errors
- LinuxError is used to report errors:
- sys_emfile
- Too many file descriptors for this process.
- sys_enfile
- The system file table is full.
-
See also
- POpen, MkFifo, mkfifo (4)
8.2.71 Nice
-
Declaration
-
Procedure Nice ( N : Integer);
-
Description
Nice adds -N to the priority of the running process. The lower the
priority numerically, the less the process is favored.
Only the superuser can specify a negative N, i.e. increase the rate at
which the process is run.
-
Errors
- Errors are returned in LinuxError
- sys_eperm
- A non-superuser tried to specify a negative N, i.e.
do a priority increase.
-
See also
- GetPriority, SetPriority, Nice (2)
-
Example
Program Example15;
{ Program to demonstrate the Nice and Get/SetPriority functions. }
Uses linux;
begin
writeln ('Setting priority to 5');
setpriority (prio_process,getpid,5);
writeln ('New priority = ',getpriority (prio_process,getpid));
writeln ('Doing nice 10');
nice (10);
writeln ('New Priority = ',getpriority (prio_process,getpid));
end.
8.2.72 OpenDir
-
Declaration
-
Function OpenDir (f:pchar) : pdir;
-
Description
OpenDir opens the directory f, and returns a pdir
pointer to a Dir record, which can be used to read the directory
structure. If the directory cannot be opened, nil is returned.
-
Errors
- Errors are returned in LinuxError.
-
See also
- CloseDir, ReadDir, SeekDir, TellDir,
opendir (3)
-
Example
Program Example35;
{ Program to demonstrate the
OpenDir,ReadDir, SeekDir and TellDir functions. }
Uses linux;
Var TheDir : PDir;
ADirent : PDirent;
Entry : Longint;
begin
TheDir:=OpenDir('./.');
Repeat
Entry:=TellDir(TheDir);
ADirent:=ReadDir (TheDir);
If ADirent<>Nil then
With ADirent^ do
begin
Writeln ('Entry No : ',Entry);
Writeln ('Inode : ',ino);
Writeln ('Offset : ',off);
Writeln ('Reclen : ',reclen);
Writeln ('Name : ',pchar(@name[0]));
end;
Until ADirent=Nil;
Repeat
Write ('Entry No. you would like to see again (-1 to stop): ');
ReadLn (Entry);
If Entry<>-1 then
begin
SeekDir (TheDir,Entry);
ADirent:=ReadDir (TheDir);
If ADirent<>Nil then
With ADirent^ do
begin
Writeln ('Entry No : ',Entry);
Writeln ('Inode : ',ino);
Writeln ('Offset : ',off);
Writeln ('Reclen : ',reclen);
Writeln ('Name : ',pchar(@name[0]));
end;
end;
Until Entry=-1;
CloseDir (TheDir);
end.
8.2.73 PClose
-
Declaration
-
Function PClose (Var F : FileType) : longint;
-
Description
PClose closes a file opened with POpen. It waits for the
command to complete, and then returns the exit status of the command.
-
Errors
- LinuxError is used to report errors. If it is different from zero,
the exit status is not valid.
-
See also
- POpen
For an example, see POpen
8.2.74 POpen
-
Declaration
-
Procedure POpen (Var F : FileType; Cmd : pathstr; rw : char);
-
Description
Popen runs the command specified in Cmd,
and redirects the standard in or output of the
command to the other end of the pipe F. The parameter rw
indicates the direction of the pipe. If it is set to 'W', then F can
be used to write data, which will then be read by the command from stdinput.
If it is set to 'R', then the standard output of the command can be
read from F. F should be reset or rewritten prior to using it.
F can be of type Text or File.
A file opened with POpen can be closed with Close, but also
with PClose. The result is the same, but PClose returns the
exit status of the command Cmd.
-
Errors
- Errors are reported in LinuxError and are essentially those of the
Execve, Dup and AssignPipe commands.
-
See also
- AssignPipe, popen (3) , PClose
-
Example
Program Example37;
{ Program to demonstrate the Popen function. }
uses linux;
var f : text;
i : longint;
begin
writeln ('Creating a shell script to which echoes its arguments');
writeln ('and input back to stdout');
assign (f,'test21a');
rewrite (f);
writeln (f,'#!/bin/sh');
writeln (f,'echo this is the child speaking.... ');
writeln (f,'echo got arguments \*"$*"\*');
writeln (f,'cat');
writeln (f,'exit 2');
writeln (f);
close (f);
chmod ('test21a',octal (755));
popen (f,'./test21a arg1 arg2','W');
if linuxerror<>0 then
writeln ('error from POpen : Linuxerror : ', Linuxerror);
for i:=1 to 10 do
writeln (f,'This is written to the pipe, and should appear on stdout.');
Flush(f);
Writeln ('The script exited with status : ',PClose (f));
writeln;
writeln ('Press <return> to remove shell script.');
readln;
assign (f,'test21a');
erase (f)
end.
8.2.75 ReadDir
-
Declaration
-
Function ReadDir (p:pdir) : pdirent;
-
Description
ReadDir reads the next entry in the directory pointed to by p.
It returns a pdirent pointer to a structure describing the entry.
If the next entry can't be read, Nil is returned.
-
Errors
- Errors are returned in LinuxError.
-
See also
- CloseDir, OpenDir, SeekDir, TellDir,
readdir (3)
For an example, see OpenDir.
8.2.76 SeekDir
-
Declaration
-
Procedure SeekDir (p:pdir;off:longint);
-
Description
SeekDir sets the directory pointer to the off-th entry in the
directory structure pointed to by p.
-
Errors
- Errors are returned in LinuxError.
-
See also
- CloseDir, ReadDir, OpenDir, TellDir,
seekdir (3)
For an example, see OpenDir.
8.2.77 Select
-
Declaration
-
Function Select (N : Longint;
var readfds,writefds,exceptfds : PFDset;
Var Timeout) : Longint;
-
Description
Select checks one of the file descriptors in the FDSets to see if its
status changed.
readfds, writefds and exceptfds are pointers to arrays of 256
bits. If you want a file descriptor to be checked, you set the
corresponding element in the array to 1. The other elements in the array
must be set to zero. Three arrays are passed : The entries in readfds
are checked to see if characters become available for reading. The entries
in writefds are checked to see if it is OK to write to them, while
entries in exceptfds are cheked to see if an exception occorred on
them.
You can use the functions FD_ZERO, FD_Clr,
FD_Set, FD_IsSet to manipulate the individual elements of a set.
The pointers can be nil.
N is the largest index of a nonzero entry plus 1. (= the largest
file-descriptor + 1).
TimeOut can be used to set a time limit.
If TimeOut can be two types :
- TimeOut is of type PTime and contains a
zero time, the call returns immediately. If TimeOut is Nil, the
kernel will wait forever, or until a status changed.
- TimeOut is of type Longint. If it is -1, this has the same
effect as a Timeout of type PTime which is Nil.
Otherwise, TimeOut contains a time in milliseconds.
When the TimeOut is reached, or one of the file descriptors has changed,
the Select call returns. On return, it will have modified the entries
in the array which have actually changed, and it returns the number of
entries that have been changed. If the timout was reached, and no decsriptor
changed, zero is returned; The arrays of indexes are undefined after that.
On error, -1 is returned.
-
Errors
- On error, the function returns -1, and Errors are reported in LinuxError :
- SYS_EBADF
- An invalid descriptot was specified in one of the sets.
- SYS_EINTR
- A non blocked signal was caught.
- SYS_EINVAL
- N is negative or too big.
- SYS_ENOMEM
- Select was unable to allocate memory for its
internal tables.
-
See also
- SelectText, GetFS,
FD_ZERO,
FD_Clr,
FD_Set,
FD_IsSet
-
Example
Program Example33;
{ Program to demonstrate the Select function. }
Uses linux;
Var FDS : FDSet;
begin
FD_Zero (FDS);
FD_Set (0,FDS);
Writeln ('Press the <ENTER> to continue the program.');
{ Wait until File descriptor 0 (=Input) changes }
Select (1,@FDS,nil,nil,nil);
{ Get rid of <ENTER> in buffer }
readln;
Writeln ('Press <ENTER> key in less than 2 seconds...');
FD_Zero (FDS);
FD_Set (0,FDS);
if Select (1,@FDS,nil,nil,2000)>0 then
Writeln ('Thank you !')
{ FD_ISSET(0,FDS) would be true here. }
else
Writeln ('Too late !');
end.
8.2.78 SelectText
-
Declaration
-
Function SelectText ( var T : Text; TimeOut :PTime) : Longint;
-
Description
SelectText executes the Select call on a file of type
Text. You can specify a timeout in TimeOut. The SelectText call
determines itself whether it should check for read or write, depending on
how the file was opened : With Reset it is checked for reading, with
Rewrite and Append it is checked for writing.
-
Errors
- See Select. SYS_EBADF can also mean that the file wasn't
opened.
-
See also
- Select, GetFS
8.2.79 SetPriority
-
Declaration
-
Function SetPriority (Which,Who,Prio : Integer) : Integer;
-
Description
SetPriority sets the priority with which a process is running.
Which process(es) is determined by the Which and Who variables.
Which can be one of the pre-defined Prio_Process, Prio_PGrp,
Prio_User, in which case Who is the process ID, Process group ID or
User ID, respectively.
Prio is a value in the range -20 to 20.
-
Errors
Error checking must be done on LinuxError, since a priority can be negative.
- sys_esrch
- No process found using which and who.
- sys_einval
- Which was not one of Prio_Process, Prio_Grp
or Prio_User.
- sys_eperm
- A process was found, but neither its effective or real
user ID match the effective user ID of the caller.
- sys_eacces
- A non-superuser tried to a priority increase.
-
See also
- GetPriority, Nice, Setpriority (2)
For an example, see Nice.
8.2.80 Shell
-
Declaration
-
Function Shell (Command : String) : Longint;
-
Description
Shell invokes the bash shell (/bin/sh), and feeds it the
command Command (using the -c option). The function then waits
for the command to complete, and then returns the exit
status of the command, or 127 if it could not complete the Fork
or Execve calls.
-
Errors
- Errors are reported in LinuxError.
-
See also
- POpen, Fork, Execve, system (3)
-
Example
program example56;
uses linux;
{ Program to demonstrate the Shell function }
Var S : Longint;
begin
Writeln ('Output of ls -l *.pp');
S:=Shell ('ls -l *.pp');
Writeln ('Command exited wwith status : ',S);
end.
8.2.81 SigAction
-
Declaration
-
Procedure SigAction (Signum : Integer; Var Act,OldAct : PSigActionRec);
-
Description
Changes the action to take upon receipt of a signal. Act and
Oldact are pointers to a SigActionRec record.
SigNum specifies the signal, and can be any signal except
SIGKILL or SIGSTOP.
If Act is non-nil, then the new action for signal SigNum is taken
from it. If OldAct is non-nil, the old action is stored there.
Sa_Handler may be SIG_DFL for the default action or
SIG_IGN to ignore the signal.
Sa_Mask Specifies which signals should be ignord during the execution
of the signal handler.
Sa_Flags Speciefies a series of flags which modify the behaviour of
the signal handler. You can 'or' none or more of the following :
- SA_NOCLDSTOP
- If signum is SIGCHLD do not receive
notification when child processes stop.
- SA_ONESHOT or SA_RESETHAND
- Restore the signal action to the default
state once the signal handler has been called.
- SA_RESTART
- For compatibility with BSD signals.
- SA_NOMASK or SA_NODEFER
- Do not prevent the signal from being received
from within its own signal handler.
-
Errors
- LinuxError is used to report errors.
- sys_einval
- an invalid signal was specified, or it was
SIGKILL or SIGSTOP.
- sys_efault
- Act,OldAct point outside this process address space
- sys_eintr
- System call was interrupted.
-
See also
SigProcMask, SigPending, SigSuspend, Kill,
Sigaction (2)
-
Example
Program example57;
{ Program to demonstrate the SigAction function.}
{
do a kill -USR1 pid from another terminal to see what happens.
replace pid with the real pid of this program.
You can get this pid by running 'ps'.
}
uses Linux;
Var
oa,na : PSigActionRec;
Procedure DoSig(sig : Longint);cdecl;
begin
writeln('Receiving signal: ',sig);
end;
begin
new(na);
new(oa);
na^.Sa_Handler:=@DoSig;
na^.Sa_Mask:=0;
na^.Sa_Flags:=0;
na^.Sa_Restorer:=Nil;
SigAction(SigUsr1,na,oa);
if LinuxError<>0 then
begin
writeln('Error: ',linuxerror,'.');
halt(1);
end;
Writeln ('Send USR1 signal or press <ENTER> to exit');
readln;
end.
8.2.82 SigPending
-
Declaration
-
Function SigPending : SigSet;
-
Description
Sigpending allows the examination of pending signals (which have been raised
while blocked.) The signal mask of pending signals is returned.
-
Errors
- None
-
See also
- SigAction, SigProcMask, SigSuspend, Signal,
Kill, Sigpending (2)
8.2.83 SigProcMask
-
Declaration
-
Procedure SigProcMask (How : Integer; SSet,OldSSet : PSigSet);
-
Description
Changes the list of currently blocked signals. The behaviour of the call
depends on How :
- SIG_BLOCK
- The set of blocked signals is the union of the current set
and the SSet argument.
- SIG_UNBLOCK
- The signals in SSet are removed from the set of
currently blocked signals.
- SIG_SETMASK
- The list of blocked signals is set so SSet.
If OldSSet is non-nil, then the old set is stored in it.
-
Errors
- LinuxError is used to report errors.
- sys_efault
- SSet or OldSSet point to an adress outside
the range of the process.
- sys_eintr
- System call was interrupted.
-
See also
- SigAction, SigPending, SigSuspend, Kill,
Sigprocmask (2)
8.2.84 SigSuspend
-
Declaration
-
Procedure SigSuspend (Mask : SigSet);
-
Description
SigSuspend temporarily replaces the signal mask for the process with the one
given in Mask, and then suspends the process until a signal is received.
-
Errors
- None
-
See also
- SigAction, SigProcMask, SigPending, Signal,
Kill, SigSuspend (2)
8.2.85 Signal
-
Declaration
-
Function Signal (SigNum : Integer; Handler : SignalHandler) : SignalHandler;
-
Description
Signal installs a new signal handler for signal SigNum. This call has
the same functionality as the SigAction call.
The return value for Signal is the old signal handler, or nil on error.
-
Errors
- LinuxError is used to report errors :
- SIG_ERR
- An error occurred.
-
See also
- SigAction,Kill, Signal (2)
-
Example
Program example58;
{ Program to demonstrate the Signal function.}
{
do a kill -USR1 pid from another terminal to see what happens.
replace pid with the real pid of this program.
You can get this pid by running 'ps'.
}
uses Linux;
Procedure DoSig(sig : Longint);cdecl;
begin
writeln('Receiving signal: ',sig);
end;
begin
SigNal(SigUsr1,@DoSig);
if LinuxError<>0 then
begin
writeln('Error: ',linuxerror,'.');
halt(1);
end;
Writeln ('Send USR1 signal or press <ENTER> to exit');
readln;
end.
8.2.86 SymLink
-
Declaration
-
Function SymLink (OldPath,NewPath : pathstr) : Boolean;
-
Description
SymLink makes Newpath point to the file in OldPath, which doesn't
necessarily exist. The two files DO NOT have the same inode number.
This is known as a 'soft' link.
The permissions of the link are irrelevant, as they are not used when
following the link. Ownership of the file is only checked in case of removal
or renaming of the link.
The function returns True if the call was succesfull, False if the call
failed.
-
Errors
- Errors are returned in LinuxError.
- sys_eperm
- The filesystem containing oldpath and newpath doesn't
support linking files.
- sys_eaccess
- Write access for the directory containing Newpath
is disallowed, or one of the directories in OldPath or NewPath has no
search (=execute) permission.
- sys_enoent
- A directory entry in OldPath or NewPath does
not exist or is a symbolic link pointing to a non-existent directory.
- sys_enotdir
- A directory entry in OldPath or NewPath is
nor a directory.
- sys_enomem
- Insufficient kernel memory.
- sys_erofs
- The files are on a read-only filesystem.
- sys_eexist
- NewPath already exists.
- sys_eloop
- OldPath or NewPath has a reference to a circular
symbolic link, i.e. a symbolic link, whose expansion points to itself.
- sys_enospc
- The device containing NewPath has no room for anothe
entry.
-
See also
- Link, UnLink, Symlink (2)
-
Example
Program Example22;
{ Program to demonstrate the SymLink and UnLink functions. }
Uses linux;
Var F : Text;
S : String;
begin
Assign (F,'test.txt');
Rewrite (F);
Writeln (F,'This is written to test.txt');
Close(f);
{ new.txt and test.txt are now the same file }
if not SymLink ('test.txt','new.txt') then
writeln ('Error when symlinking !');
{ Removing test.txt still leaves new.txt
Pointing now to a non-existent file ! }
If not Unlink ('test.txt') then
Writeln ('Error when unlinking !');
Assign (f,'new.txt');
{ This should fail, since the symbolic link
points to a non-existent file! }
{$i-}
Reset (F);
{$i+}
If IOResult=0 then
Writeln ('This shouldn''t happen');
{ Now remove new.txt also }
If not Unlink ('new.txt') then
Writeln ('Error when unlinking !');
end.
8.2.87 TCDrain
-
Declaration
-
Function TCDrain (Fd:longint) : Boolean;
-
Description
TCDrain
waits until all data to file descriptor Fd is transmitted.
The function returns True if the call was succesfull, False
otherwise.
-
Errors
- Errors are reported in LinuxError
-
See also
- termios (2)
8.2.88 TCFlow
-
Declaration
-
Function TCFlow (Fd,Act:longint) : Boolean;
-
Description
TCFlow
suspends/resumes transmission or reception of data to or from the file
descriptor Fd, depending
on the action Act. This can be one of the following pre-defined
values:
- TCOOFF
- suspend reception/transmission,
- TCOON
- resume reception/transmission,
- TCIOFF
- transmit a stop character to stop input from the terminal,
- TCION
- transmit start to resume input from the terminal.
The function returns True if the call was succesfull, False
otherwise.
-
Errors
- Errors are reported in LinuxError.
-
See also
- termios (2)
8.2.89 TCFlush
-
Declaration
-
Function TCFlush (Fd,QSel:longint) : Boolean;
-
Description
TCFlush
discards all data sent or received to/from file descriptor fd.
QSel indicates which queue
should be discard. It can be one of the following pre-defined values :
- TCIFLUSH
- input,
- TCOFLUSH
- output,
- TCIOFLUSH
- both input and output.
The function returns True if the call was succesfull, False
otherwise.
-
Errors
- Errors are reported in LinuxError.
-
See also
- termios (2)
8.2.90 TCGetAttr
-
Declaration
-
Function TCGetAttr (fd:longint;var tios:TermIOS) : Boolean;
-
Description
TCGetAttr
gets the terminal parameters from the terminal referred to by the file
descriptor fd and returns them in a TermIOS structure tios.
The function returns True if the call was succesfull, False
otherwise.
-
Errors
- Errors are reported in LinuxError
-
See also
- TCSetAttr, termios (2)
-
Example
Program Example55;
uses Linux;
{ Program to demonstrate the TCGetAttr/TCSetAttr/CFMakeRaw functions. }
procedure ShowTermios(var tios:Termios);
begin
WriteLn('Input Flags : $',hexstr(tios.c_iflag,8)+#13);
WriteLn('Output Flags : $',hexstr(tios.c_oflag,8));
WriteLn('Line Flags : $',hexstr(tios.c_lflag,8));
WriteLn('Control Flags: $',hexstr(tios.c_cflag,8));
end;
var
oldios,
tios : Termios;
begin
WriteLn('Old attributes:');
TCGetAttr(1,tios);
ShowTermios(tios);
oldios:=tios;
Writeln('Setting raw terminal mode');
CFMakeRaw(tios);
TCSetAttr(1,TCSANOW,tios);
WriteLn('Current attributes:');
TCGetAttr(1,tios);
ShowTermios(tios);
TCSetAttr(1,TCSANOW,oldios);
end.
8.2.91 TCGetPGrp
-
Declaration
-
Function TCGetPGrp (Fd:longint;var Id:longint) : boolean;
-
Description
TCGetPGrp
returns the process group ID of a foreground process group in Id
The function returns True if the call was succesfull, False
otherwise
-
Errors
- Errors are reported in LinuxError
-
See also
- termios (2)
8.2.92 TCSendBreak
-
Declaration
-
Function TCSendBreak (Fd,Duration:longint) : Boolean;
-
Description
TCSendBreak
Sends zero-valued bits on an asynchrone serial connection decsribed by
file-descriptor Fd, for duration Duration.
The function returns True if the action was performed successfully,
False otherwise.
-
Errors
- Errors are reported in LinuxError.
-
See also
- termios (2)
8.2.93 TCSetAttr
-
Declaration
-
Function TCSetAttr (Fd:longint;OptAct:longint;var Tios:TermIOS) : Boolean;
-
Description
TCSetAttr
Sets the terminal parameters you specify in a TermIOS structure
Tios for the terminal
referred to by the file descriptor Fd. OptAct specifies an
optional action when the set need to be done,
this could be one of the following pre-defined values:
- TCSANOW
- set immediately.
- TCSADRAIN
- wait for output.
- TCSAFLUSH
- wait for output and discard all input not yet read.
The function Returns True if the call was succesfull, False
otherwise.
-
Errors
- Errors are reported in LinuxError.
-
See also
- TCGetAttr, termios (2)
For an example, see TCGetAttr.
8.2.94 TCSetPGrp
-
Declaration
-
Function TCSetPGrp (Fd,Id:longint) : boolean;
-
Description
TCSetPGrp Sets the Process Group Id to Id.
The function returns True if the call was successful, False
otherwise.
-
Errors
- Errors are returned in LinuxError.
-
See also
- TCGetPGrp, termios (2)
For an example, see TCGetPGrp.
8.2.95 TTYName
-
Declaration
-
Function TTYName (var f) : String;
-
Description
Returns the name of the terminal pointed to by f. f
must be a terminal. f can be of type:
- longint for file handles;
- Text for text variables such as input etc.
-
Errors
- Returns an empty string in case of an error. Linuxerror may be set
to indicate what error occurred, but this is uncertain.
-
See also
- IsATTY,IOCtl
8.2.96 TellDir
-
Declaration
-
Function TellDir (p:pdir) : longint;
-
Description
TellDir returns the current location in the directory structure
pointed to by p. It returns -1 on failure.
-
Errors
- Errors are returned in LinuxError.
-
See also
- CloseDir, ReadDir, SeekDir, OpenDir,
telldir (3)
For an example, see OpenDir.
8.2.97 Umask
-
Declaration
-
Function Umask (Mask : Integer) : Integer;
-
Description
Change the file creation mask for the current user to Mask. The
current mask is returned.
-
Errors
- None
-
See also
- Chmod, Umask (2)
-
Example
Program Example27;
{ Program to demonstrate the Umask function. }
Uses linux;
begin
Writeln ('Old Umask was : ',Umask(Octal(111)));
WRiteln ('New Umask is : ',Octal(111));
end.
8.2.98 Uname
-
Declaration
-
Procedure Uname (var unamerec:utsname);
-
Description
Uname gets the name and configuration of the current LINUX kernel,
and returns it in unamerec.
-
Errors
- LinuxError is used to report errors.
-
See also
- GetHostName, GetDomainName, uname (2)
8.2.99 UnLink
-
Declaration
-
Function UnLink (Var Path) : Boolean;
-
Description
UnLink decreases the link count on file Path. Path can be
of type PathStr or PChar. If the link count is zero, the
file is removed from the disk.
The function returns True if the call was succesfull, False if the call
failed.
-
Errors
- Errors are returned in LinuxError.
- sys_eaccess
- You have no write access right in the directory
containing Path, or you have no search permission in one of the
directory components of Path.
- sys_eperm
- The directory containing pathname has the sticky-bit
set and the process's effective uid is neither the uid of the
file to be deleted nor that of the directory containing it.
- sys_enoent
- A component of the path doesn't exist.
- sys_enotdir
- A directory component of the path is not a directory.
- sys_eisdir
- Path refers to a directory.
- sys_enomem
- Insufficient kernel memory.
- sys_erofs
- Path is on a read-only filesystem.
-
See also
- Link, SymLink, Unlink (2)
For an example, see Link.
8.2.100 Utime
-
Declaration
-
Function Utime (path : pathstr; utim : utimbuf) : Boolean;
-
Description
Utime sets the access and modification times of a file.
the utimbuf record contains 2 fields, actime, and modtime,
both of type Longint. They should be filled with an epoch-like time,
specifying, respectively, the last access time, and the last modification
time.
For some filesystem (most notably, FAT), these times are the same.
-
Errors
- Errors are returned in LinuxError.
- sys_eaccess
- One of the directories in Path has no
search (=execute) permission.
- sys_enoent
- A directory entry in Path does
not exist or is a symbolic link pointing to a non-existent directory.
Other errors may occur, but aren't documented.
-
See also
- GetEpochTime, Chown, Access, utime (() 2)
-
Example
Program Example25;
{ Program to demonstrate the UTime function. }
Uses linux;
Var utim : utimbuf;
year,month,day,hour,minute,second : Word;
begin
{ Set access and modification time of executable source }
GetTime (hour,minute,second);
GetDate (year,month,day);
utim.actime:=LocalToEpoch(year,month,day,hour,minute,second);
utim.modtime:=utim.actime;
if not Utime('ex25.pp',utim) then
writeln ('Call to UTime failed !')
else
begin
Write ('Set access and modification times to : ');
Write (Hour:2,':',minute:2,':',second,', ');
Writeln (Day:2,'/',month:2,'/',year:4);
end;
end.
8.2.101 WaitPid
-
Declaration
-
Function WaitPid (Pid : longint; Status : pointer; Options : Integer) : Longint;
-
Description
WaitPid waits for a child process with process ID Pid to exit. The
value of Pid can be one of the following:
- Pid < -1
- Causes WaitPid to wait for any child process whose
process group ID equals the absolute value of pid.
- Pid = -1
- Causes WaitPid to wait for any child process.
- Pid = 0
- Causes WaitPid to wait for any child process whose
process group ID equals the one of the calling
process.
- Pid > 0
- Causes WaitPid to wait for the child whose process ID
equals the value of Pid.
The Options parameter can be used to specify further how WaitPid
behaves:
- WNOHANG
- Causes Waitpid to return immediately if no child has
exited.
- WUNTRACED
- Causes WaitPid to return also for children which are
stopped, but whose status has not yet been reported.
Upon return, it returns the exit status of the process, or -1 in case of
failure.
-
Errors
- Errors are returned in LinuxError.
-
See also
- Fork, Execve, waitpid (2)
for an example, see Fork.
root
1999-06-10