1{
2    This file is part of the Free Component Library (FCL)
3    Copyright (c) 1999-2002 by the Free Pascal development team
4
5    libc unit for Nintendo DS
6    Copyright (c) 2006 by Francesco Lombardi
7
8    See the file COPYING.FPC, included in this distribution,
9    for details about the copyright.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 *****************************************************************************}
16
17type
18  time_t = longint;
19  ptime_t = ^time_t;
20
21  Ptm = ^tm;
22  tm = record
23    tm_sec: longint;
24    tm_min: longint;
25    tm_hour: longint;
26    tm_mday: longint;
27    tm_mon: longint;
28    tm_year: longint;
29    tm_wday: longint;
30    tm_yday: longint;
31    tm_isdst: longint;
32  end;
33
34
35(* Some libc functions *)
36//function printf(format: Pchar; args: array of const): longint; cdecl; external;
37function printf(format: Pchar): longint; cdecl; varargs; external;
38//function sprintf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
39function sprintf(s: Pchar; format: Pchar): longint; varargs; cdecl; external;
40//function iprintf(format: Pchar; args: array of const): longint; cdecl; external;
41function iprintf(format: Pchar): longint; varargs; cdecl; external;
42//function scanf(format: Pchar; args: array of const): longint; cdecl; external;
43function scanf(format: Pchar): longint; cdecl; varargs; external;
44//function sscanf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
45function sscanf(s: Pchar; format: Pchar): longint; cdecl; varargs; external;
46function strcmp(s1: Pchar; s2: Pchar): longint; cdecl; external;
47
48function malloc(size: integer): pointer; cdecl; external;
49function realloc(ptr: pointer; size: integer): pointer; cdecl; external;
50procedure free(ptr: pointer); cdecl; external;
51function memcpy(dest: pointer; src: pointer; n: integer): pointer; cdecl; external;
52
53function gmtime(timer: ptime_t): ptm; cdecl; external;
54function time(timer: ptime_t): time_t; cdecl; external;
55
56type
57  TSort = function (const a, b: pointer): integer;
58procedure qsort(__base: pointer; __nmemb: integer; __size: integer; __compar: TSort); cdecl; external;
59
60var
61  errno: plongint; external name '__errno';
62
63type
64{
65  _FILE = record
66    firstCluster: longword;
67    length: longword;
68    curPos: longword;
69    curClus: longword;                       // Current cluster to read from
70    curSect: integer;                     // Current sector within cluster
71    curByte: integer;                     // Current byte within sector
72    readBuffer: array [0..511] of byte;   // Buffer used for unaligned reads
73    appClus: longword;                       // Cluster to append to
74    appSect: integer;                     // Sector within cluster for appending
75    appByte: integer;                     // Byte within sector for appending
76    read: boolean;                        // Can read from file
77    write: boolean;                       // Can write to file
78    append: boolean;                      // Can append to file
79    inUse: boolean;                       // This file is open
80    dirEntSector: longword;                  // The sector where the directory entry is stored
81    dirEntOffset: integer;                // The offset within the directory sector
82  end;
83  P_FILE = ^_FILE;
84 }
85
86   P_iobuf = ^_iobuf;
87   _iobuf = record
88        reserved : longint;
89     end;
90   _FILE = _iobuf;
91   P_FILE = ^_FILE;
92   PP_FILE = ^P_FILE;
93
94const
95  SEEK_SET = 0;
96  SEEK_CUR = 1;
97  SEEK_END = 2;
98   R_OK = 1;
99   W_OK = 2;
100   X_OK = 4;
101   F_OK = 8;
102   L_SET = SEEK_SET;
103   L_INCR = SEEK_CUR;
104   L_XTND = SEEK_END;
105   EFF_ONLY_OK = 8;
106   STDIN_FILENO = 0;
107   STDOUT_FILENO = 1;
108   STDERR_FILENO = 2;
109
110(*
111  ------------------------------------------------------------------------------
112    Directory iterator for mantaining state between dir* calls
113  ------------------------------------------------------------------------------
114*)
115type
116  DIR_ITER = record
117    device: longint;
118    dirStruct: pointer;
119  end;
120  PDIR_ITER = ^DIR_ITER;
121
122  stat = record
123    st_dev: longint;
124    st_ino: longword;
125    st_mode : longword;
126    st_nlink : word;
127    st_uid : word;
128    st_gid : word;
129    st_rdev : longint;
130    st_size : longint;
131    st_atime : longint;
132
133    st_spare1: longint;
134    st_mtime: longint;
135    st_spare2: longint;
136    st_ctime: longint;
137    st_spare3: longint;
138    st_blksize: longint;
139    st_blocks: longint;
140    st_spare4: array [0..1] of longint;
141  end;
142  TStat = stat;
143  PStat = ^stat;
144
145const
146  _IFMT    = 0170000;   // type of file
147  _IFDIR   = 0040000;   // directory
148  _IFCHR   = 0020000; 	// character special
149  _IFBLK   = 0060000; 	// block special
150  _IFREG   = 0100000; 	// regular
151  _IFLNK   = 0120000; 	// symbolic link
152  _IFSOCK  = 0140000; 	// socket
153  _IFIFO   = 0010000; 	// fifo
154
155  S_BLKSIZE = 1024;  // size of a block
156
157  S_ISUID = 0004000; // set user id on execution
158  S_ISGID = 0002000; // set group id on execution
159
160  NAME_MAX = 767;
161
162function S_ISBLK(m: longint): boolean; inline;
163function S_ISCHR(m: longint): boolean; inline;
164function S_ISDIR(m: longint): boolean; inline;
165function S_ISFIFO(m: longint): boolean; inline;
166function S_ISREG(m: longint): boolean; inline;
167function S_ISLNK(m: longint): boolean; inline;
168function S_ISSOCK(m: longint): boolean; inline;
169
170
171type
172  dirent = record
173    d_ino: longint;
174    d_name: array [0..NAME_MAX] of char;
175  end;
176  PDirent = ^dirent;
177  PPDirent = ^PDirent;
178
179  DIR = record
180    position: longint;
181    dirData: PDIR_ITER;
182    fileData: dirent;
183  end;
184  PDIR = ^DIR;
185
186(* DIR handling *)
187function closedir(dirp: PDIR): longint; cdecl; external;
188function opendir(const dirname: pchar): PDIR; cdecl; external;
189function readdir(dirp: PDIR): PDirent; cdecl; external;
190function readdir_r(dirp: PDIR; entry: PDirent; result: PPDirent): longint; cdecl; external;
191procedure rewinddir(dirp: PDIR); cdecl; external;
192procedure seekdir(dirp: PDIR; loc: longint); cdecl; external;
193function telldir(dirp: PDIR): longint; cdecl; external;
194
195function diropen(const path: pchar): PDIR_ITER; cdecl; external;
196function dirreset(dirState: PDIR_ITER): longint; cdecl; external;
197function dirnext(dirState: PDIR_ITER; filename: pchar; filestat: Pstat): longint; cdecl; external;
198function dirclose(dirState: PDIR_ITER): longint; cdecl; external;
199
200(* File handling *)
201function fopen(filename: Pchar; modes: Pchar): P_FILE; cdecl; external;
202function fread(ptr: pointer; size: longint; n: longint; stream: P_FILE): longint; cdecl; external;
203function fread(var ptr; size: longint; n: longint; var stream: _FILE): longint; cdecl; external;
204function fwrite(ptr: pointer; size: longint; n: longint; s: P_FILE): longint; cdecl; external;
205function fwrite(var ptr; size: longint; n: longint; var s: _FILE): longint; cdecl; external;
206function ftell(stream: P_FILE): longint; cdecl; external;
207function ftell(var stream: _FILE): longint; cdecl; external;
208function fseek(stream: P_FILE; off: longint; whence: longint): longint; cdecl; external;
209function fseek(var stream: _FILE; off: longint; whence: longint): longint; cdecl; external;
210function fclose(stream: P_FILE): longint; cdecl; external;
211function fclose(var stream: _FILE): longint; cdecl; external;
212function isatty(fildes: longint): longint; cdecl; external;
213function fileno(para1: P_FILE): longint; cdecl; external;
214function fileno(var para1: _FILE): longint; cdecl; external;
215function fstat(fildes: longint; buf: PStat): longint; cdecl; external;
216function fstat(fildes: longint; var buf: TStat): longint; cdecl; external;
217function _stat(__file: Pchar; var __buf: Tstat): longint; cdecl; external name 'stat';
218function _stat(path: Pchar; buf: Pstat): longint; cdecl; external name 'stat';
219function ftruncate(fildes: longint; len: longint): longint; cdecl; external;
220function unlink(path: Pchar): longint; cdecl; external;
221function rename(para1: Pchar; para2: Pchar): longint; cdecl; external;
222
223function _open(path: Pchar; oflag: longint): longint; cdecl; external name 'open';
224function _open(path: Pchar; oflag, mode: longint): longint; cdecl; external name 'open';
225function _read(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'read';
226function _read(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'read';
227function _write(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'write';
228function _write(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'write';
229function _lseek(fildes:longint; offset:longint; whence:longint):longint;cdecl;external name 'lseek';
230function _close(fildes:longint): longint; cdecl; external name 'close';
231function _unlink(path:Pchar): longint; cdecl; external name 'unlink';
232function _rename(para1: Pchar; para2: Pchar): longint; cdecl; external name 'rename';
233function _access(path: Pchar; mode: longint): longint; cdecl; external name 'access';
234function _tell(fildes: longint): longint; cdecl; external name 'tell';
235function _isatty(fildes: longint): longint; cdecl; external name 'isatty';
236function _truncate(fildes: longint; len: longint): longint; cdecl; external name 'truncate';
237
238
239
240const
241 F_GETFL      = 1;        // get file status flags
242 F_SETFL      = 2;        // set file status flags
243 F_DUPFD      = 3;        // duplicate file descriptor
244 F_GETFD      = 4;        // get file descriptor flags
245 F_SETFD      = 5;        // set file descriptor flags
246 F_SETLK      = 6;        // set record locking info
247 F_SETLK64    = 16;       // set record locking info (64-bit)
248 F_GETLK      = 7;        // get record locking info
249 F_GETLK64    = 17;       // get record locking info (64-bit)
250 F_SETLKW     = 8;        // get record locking info; wait if blocked
251 F_SETLKW64   = 18;       // get record locking info (64-bit)
252 F_CLOEXEC    = 9;        // close on execute
253
254// values for 'l_type' field of 'struct flock'...
255 F_RDLCK      = 1;        // shared or read lock
256 F_WRLCK      = 2;        // exclusive or write lock
257 F_UNLCK      = 3;        // unlock
258
259// values for 'oflag' in open()...
260 O_RDONLY     =$00000000; // open for read only
261 O_WRONLY     =$00000001; // open for write only
262 O_RDWR       =$00000002; // open for read and write
263 O_ACCMODE    =$00000003; // access flags mask
264 O_reserved1  =$00000004; // reserved
265 O_reserved2  =$00000008; // reserved
266 O_APPEND     =$00000010; // writes done at end of file
267 O_CREAT      =$00000020; // create new file
268 O_TRUNC      =$00000040; // truncate existing file
269 O_EXCL       =$00000080; // exclusive open
270 O_NOCTTY     =$00000100; // no controlling terminal--unsupported
271 O_BINARY     =$00000200; // binary file--all files
272 O_NDELAY     =$00000400; // nonblocking flag
273 O_reserved3  =$00000800; // reserved
274 O_SYNC       =$00001000; // synchronized I/O file integrity
275 O_DSYNC      =$00002000; // synchronized I/O data integrity
276 O_RSYNC      =$00004000; // synchronized read I/O
277 O_NONBLOCK   = O_NDELAY; // alias
278 FD_CLOEXEC   =$00008000; // parent closes after call to process()
279 O_UPDATE     =$00010000; // keep legacy files updated
280 O_FIFO       =$00100000; // opening one end of a FIFO [non-standard]
281
282// value for third argument when 'cmd' is F_SETFL in fcntl()...
283 FNDELAY      = O_NDELAY;   // fcntl() non-blocking I/O
284
285// 'shflag' values for sopen()...
286 SH_DENYRW    = $00000010; // deny read/write mode
287 SH_DENYWR    = $00000020; // deny write mode
288 SH_DENYRD    = $00000030; // deny read mode
289 SH_DENYNO    = $00000040; // deny none mode
290
291Const
292
293Sys_EPERM       = 1;    { Operation not permitted }
294Sys_ENOENT      = 2;    { No such file or directory }
295Sys_ESRCH       = 3;    { No such process }
296Sys_EINTR       = 4;    { Interrupted system call }
297Sys_EIO = 5;    { I/O error }
298Sys_ENXIO       = 6;    { No such device or address }
299Sys_E2BIG       = 7;    { Arg list too long }
300Sys_ENOEXEC     = 8;    { Exec format error }
301Sys_EBADF       = 9;    { Bad file number }
302Sys_ECHILD      = 10;   { No child processes }
303Sys_EAGAIN      = 11;   { Try again }
304Sys_ENOMEM      = 12;   { Out of memory }
305Sys_EACCES      = 13;   { Permission denied }
306Sys_EFAULT      = 14;   { Bad address }
307Sys_ENOTBLK     = 15;   { Block device required, NOT POSIX! }
308Sys_EBUSY       = 16;   { Device or resource busy }
309Sys_EEXIST      = 17;   { File exists }
310Sys_EXDEV       = 18;   { Cross-device link }
311Sys_ENODEV      = 19;   { No such device }
312Sys_ENOTDIR     = 20;   { Not a directory }
313Sys_EISDIR      = 21;   { Is a directory }
314Sys_EINVAL      = 22;   { Invalid argument }
315Sys_ENFILE      = 23;   { File table overflow }
316Sys_EMFILE      = 24;   { Too many open files }
317Sys_ENOTTY      = 25;   { Not a typewriter }
318Sys_ETXTBSY     = 26;   { Text file busy. The new process was
319                      a pure procedure (shared text) file which was
320                      open for writing by another process, or file
321                      which was open for writing by another process,
322                      or while the pure procedure file was being
323                      executed an open(2) call requested write access
324                      requested write access.}
325Sys_EFBIG       = 27;   { File too large }
326Sys_ENOSPC      = 28;   { No space left on device }
327Sys_ESPIPE      = 29;   { Illegal seek }
328Sys_EROFS       = 30;   { Read-only file system }
329Sys_EMLINK      = 31;   { Too many links }
330Sys_EPIPE       = 32;   { Broken pipe }
331Sys_EDOM        = 33;   { Math argument out of domain of func }
332Sys_ERANGE      = 34;   { Math result not representable }
333Sys_EDEADLK     = 35;   { Resource deadlock would occur }
334Sys_ENAMETOOLONG= 36;   { File name too long }
335Sys_ENOLCK      = 37;   { No record locks available }
336Sys_ENOSYS      = 38;   { Function not implemented }
337Sys_ENOTEMPTY= 39;      { Directory not empty }
338Sys_ELOOP       = 40;   { Too many symbolic links encountered }
339Sys_EWOULDBLOCK = Sys_EAGAIN;   { Operation would block }
340Sys_ENOMSG      = 42;   { No message of desired type }
341Sys_EIDRM       = 43;   { Identifier removed }
342Sys_ECHRNG      = 44;   { Channel number out of range }
343Sys_EL2NSYNC= 45;       { Level 2 not synchronized }
344Sys_EL3HLT      = 46;   { Level 3 halted }
345Sys_EL3RST      = 47;   { Level 3 reset }
346Sys_ELNRNG      = 48;   { Link number out of range }
347Sys_EUNATCH     = 49;   { Protocol driver not attached }
348Sys_ENOCSI      = 50;   { No CSI structure available }
349Sys_EL2HLT      = 51;   { Level 2 halted }
350Sys_EBADE       = 52;   { Invalid exchange }
351Sys_EBADR       = 53;   { Invalid request descriptor }
352Sys_EXFULL      = 54;   { Exchange full }
353Sys_ENOANO      = 55;   { No anode }
354Sys_EBADRQC     = 56;   { Invalid request code }
355Sys_EBADSLT     = 57;   { Invalid slot }
356Sys_EDEADLOCK= 58;      { File locking deadlock error }
357Sys_EBFONT      = 59;   { Bad font file format }
358Sys_ENOSTR      = 60;   { Device not a stream }
359Sys_ENODATA     = 61;   { No data available }
360Sys_ETIME       = 62;   { Timer expired }
361Sys_ENOSR       = 63;   { Out of streams resources }
362Sys_ENONET      = 64;   { Machine is not on the network }
363Sys_ENOPKG      = 65;   { Package not installed }
364Sys_EREMOTE     = 66;   { Object is remote }
365Sys_ENOLINK     = 67;   { Link has been severed }
366Sys_EADV        = 68;   { Advertise error }
367Sys_ESRMNT      = 69;   { Srmount error }
368Sys_ECOMM       = 70;   { Communication error on send }
369Sys_EPROTO      = 71;   { Protocol error }
370Sys_EMULTIHOP= 72;      { Multihop attempted }
371Sys_EDOTDOT     = 73;   { RFS specific error }
372Sys_EBADMSG     = 74;   { Not a data message }
373Sys_EOVERFLOW= 75;      { Value too large for defined data type }
374Sys_ENOTUNIQ= 76;       { Name not unique on network }
375Sys_EBADFD      = 77;   { File descriptor in bad state }
376Sys_EREMCHG     = 78;   { Remote address changed }
377Sys_ELIBACC     = 79;   { Can not access a needed shared library }
378Sys_ELIBBAD     = 80;   { Accessing a corrupted shared library }
379Sys_ELIBSCN     = 81;   { .lib section in a.out corrupted }
380Sys_ELIBMAX     = 82;   { Attempting to link in too many shared libraries }
381Sys_ELIBEXEC= 83;       { Cannot exec a shared library directly }
382Sys_EILSEQ      = 84;   { Illegal byte sequence }
383Sys_ERESTART= 85;       { Interrupted system call should be restarted }
384Sys_ESTRPIPE= 86;       { Streams pipe error }
385Sys_EUSERS      = 87;   { Too many users }
386Sys_ENOTSOCK= 88;       { Socket operation on non-socket }
387Sys_EDESTADDRREQ= 89;   { Destination address required }
388Sys_EMSGSIZE= 90;       { Message too long }
389Sys_EPROTOTYPE= 91;     { Protocol wrong type for socket }
390Sys_ENOPROTOOPT= 92;    { Protocol not available }
391Sys_EPROTONOSUPPORT= 93;        { Protocol not supported }
392Sys_ESOCKTNOSUPPORT= 94;        { Socket type not supported }
393Sys_EOPNOTSUPP= 95;     { Operation not supported on transport endpoint }
394Sys_EPFNOSUPPORT= 96;   { Protocol family not supported }
395Sys_EAFNOSUPPORT= 97;   { Address family not supported by protocol }
396Sys_EADDRINUSE= 98;     { Address already in use }
397Sys_EADDRNOTAVAIL= 99;  { Cannot assign requested address }
398Sys_ENETDOWN= 100;      { Network is down }
399Sys_ENETUNREACH= 101;   { Network is unreachable }
400Sys_ENETRESET= 102;     { Network dropped connection because of reset }
401Sys_ECONNABORTED= 103;  { Software caused connection abort }
402Sys_ECONNRESET= 104;    { Connection reset by peer }
403Sys_ENOBUFS     = 105;  { No buffer space available }
404Sys_EISCONN     = 106;  { Transport endpoint is already connected }
405Sys_ENOTCONN= 107;      { Transport endpoint is not connected }
406Sys_ESHUTDOWN= 108;     { Cannot send after transport endpoint shutdown }
407Sys_ETOOMANYREFS= 109;  { Too many references: cannot splice }
408Sys_ETIMEDOUT= 110;     { Connection timed out }
409Sys_ECONNREFUSED= 111;  { Connection refused }
410Sys_EHOSTDOWN= 112;     { Host is down }
411Sys_EHOSTUNREACH= 113;  { No route to host }
412Sys_EALREADY= 114;      { Operation already in progress }
413Sys_EINPROGRESS= 115;   { Operation now in progress }
414Sys_ESTALE      = 116;  { Stale NFS file handle }
415Sys_EUCLEAN     = 117;  { Structure needs cleaning }
416Sys_ENOTNAM     = 118;  { Not a XENIX named type file }
417Sys_ENAVAIL     = 119;  { No XENIX semaphores available }
418Sys_EISNAM      = 120;  { Is a named type file }
419Sys_EREMOTEIO= 121;     { Remote I/O error }
420Sys_EDQUOT      = 122;  { Quota exceeded }
421
422
423{ This value was suggested by Daniel
424  based on infos from www.linuxassembly.org }
425
426Sys_ERROR_MAX = $fff;
427