• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @defgroup unistd Unistd
3  * @ingroup libc
4  */
5 
6 #ifndef	_UNISTD_H
7 #define	_UNISTD_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <features.h>
14 
15 #define STDIN_FILENO  0
16 #define STDOUT_FILENO 1
17 #define STDERR_FILENO 2
18 
19 #define SEEK_SET 0
20 #define SEEK_CUR 1
21 #define SEEK_END 2
22 
23 #ifdef __cplusplus
24 #define NULL 0L
25 #else
26 #ifndef NULL
27 #define NULL ((void*)0)
28 #endif
29 #endif
30 
31 #define __NEED_size_t
32 #define __NEED_ssize_t
33 #define __NEED_uid_t
34 #define __NEED_gid_t
35 #define __NEED_off_t
36 #define __NEED_pid_t
37 #define __NEED_intptr_t
38 #define __NEED_useconds_t
39 #ifdef __LITEOS__
40 #define __NEED_int64_t
41 #endif
42 
43 #include <bits/alltypes.h>
44 
45 int pipe(int [2]);
46 int pipe2(int [2], int);
47 
48 /**
49  * @ingroup unistd
50  *
51  * @par Description:
52  * The close() function shall close a file descriptor, so that it no longer refers to any file and may be reused.
53  * Any record locks held on the file it was associated with, and owned by the process, are removed.
54  * If fd is the last copy of a particular file descriptor the resources associated with it are freed;
55  * if the descriptor was the last reference to a file which has been removed using unlink() the file is deleted.
56  *
57  * @param __fd [IN] file descriptor to close.
58  *
59  * @attention
60  * <ul>
61  * <li>None.</li>
62  * </ul>
63  *
64  * @retval #0  close file successful.
65  * @retval #-1 An error is encountered and close file failed.
66  *
67  * @par Errors
68  * <ul>
69  * <li><b>EBADF</b>: The __fd argument is not a open file descriptor, or value is not within valid range.</li>
70  * <li><b>ENOENT</b>: Can't find volume in fat or can't find internal node in jffs.</li>
71  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
72  * <li><b>ENODEV</b>: The partition of disk can't find.</li>
73  * <li><b>EPERM</b>: Other errors.</li>
74  * </ul>
75  *
76  * @par Dependency:
77  * <ul><li>unistd.h</li></ul>
78  * @see dup | fclose | fopen | ioctl | open | unlink
79  */
80 int close(int);
81 int posix_close(int, int);
82 
83 /**
84  * @ingroup unistd
85  *
86  * @par Description:
87  * The dup() function shall duplicate an open file descriptor. It provides an alternative interface
88  * to the service provided by fcntl() using the F_DUPFD command. The call dup(fildes) shall be equivalent to:
89  * fcntl(fildes, F_DUPFD, 0);
90  *
91  * @param __oldfd [IN] file descriptor to duplicate.
92  *
93  * @attention
94  * <ul>
95  * <li>The dup() function doesn't support to duplicate a socket descriptor.</li>
96  * </ul>
97  *
98  * @retval #int  A non-negative integer namely the file descriptor shall be returned upon successful completion.
99  * @retval #-1 An error is encountered and duplicate file descriptor failed.
100  *
101  * @par Errors
102  * <ul>
103  * <li><b>EBADF</b>: __oldfd is not a valid open file descriptor, or it is a socket descriptor.</li>
104  * <li><b>ENOMEM</b>: Out of memory.</li>
105  * <li><b>EMFILE</b>: Too many open files and allocate a file descriptor failed.</li>
106  * <li><b>ENOENT</b>: Can't find the inode by the pathname associated with __oldfd.</li>
107  * <li><b>ENOSYS</b>: The current file system doesn't support the dup() function.</li>
108  * <li><b>ENODEV</b>: The mount is not healthy.</li>
109  * </ul>
110  *
111  * @par Dependency:
112  * <ul><li>unistd.h</li></ul>
113  * @see close | fcntl | open
114  */
115 int dup(int);
116 
117 /**
118  * @ingroup unistd
119  *
120  * @par Description:
121  * The dup2() function shall cause the file descriptor __newfd to refer to the same open file description
122  * as the file descriptor __oldfd and to share any locks, and shall return __newfd.
123  * If __newfd is already a valid open file descriptor, it shall be closed first, unless __oldfd is equal to __newfd
124  * in which case dup2() shall return __newfd without closing it.
125  *
126  * @attention
127  * <ul>
128  * <li>The dup2() function doesn't support to clone a socket descriptor.</li>
129  * <li>If __newfd is already a valid opend descriptor, it shall be closed first, even if the file system doesn't
130  * support dup2().</li>
131  * </ul>
132  *
133  * @retval #int  A non-negative integer namely the file descriptor shall be returned upon successful completion.
134  * @retval #-1 An error is encountered and duplicate file descriptor failed.
135  *
136  * @par Errors
137  * <ul>
138  * <li><b>EBADF</b>: __oldfd is not a valid open file descriptor, or it is a socket descriptor.</li>
139  * <li><b>ENOMEM</b>: Out of memory.</li>
140  * <li><b>ENOENT</b>: Can't find the inode by the pathname associated with __oldfd.</li>
141  * <li><b>EINTR</b>: The dup2() function was interrupted by a signal.</li>
142  * <li><b>ENOSYS</b>: The current file system doesn't support the dup() function.</li>
143  * <li><b>ENODEV</b>: The mount is not healthy or no such device.</li>
144  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
145  * </ul>
146  *
147  * @par Dependency:
148  * <ul><li>unistd.h</li></ul>
149  * @see close | fcntl | open
150  */
151 int dup2(int, int);
152 int dup3(int, int, int);
153 off_t lseek(int, off_t, int);
154 
155 /**
156  * @ingroup unistd
157  *
158  * @par Description:
159  * The fsync() function shall request that all data for the open file descriptor named by fildes is to
160  * be transferred to the storage device associated with the file described by fildes.
161  * The nature of the transfer is implementation-defined. The fsync() function shall not return until
162  * the system has completed that action or until an error is detected.
163  *
164  * @param  __fd [IN] An open file descriptor that refers to a device.
165  *
166  * @attention
167  * <ul>
168  * <li>None.</li>
169  * </ul>
170  *
171  * @retval #int The fsync() function shall return 0 upon successful completion.
172  * Otherwise, the function shall return -1 and set errno.
173  *
174  * @par Errors
175  * <ul>
176  * <li><b>EBADF</b>: The __fd argument is not a valid open file descriptor.</li>
177  * <li><b>ENOSYS</b>: The function is not supported.</li>
178  * <li><b>EINVAL</b>: The __fd argument is not valid for this device.</li>
179  * <li><b>ENOENT</b>: The file can't found.</li>
180  * <li><b>ENODEV</b>: The device is not existed.</li>
181  * <li><b>EACCES</b>: The file permission is denied.</li>
182  * </ul>
183  *
184  * @par Dependency:
185  * <ul><li>unistd.h</li></ul>
186  * @see sync
187  */
188 int fsync(int);
189 int fdatasync(int);
190 
191 /**
192  * @ingroup unistd
193  *
194  * @par Description:
195  * The read() function shall attempt to read __count bytes from the file associated with the open file descriptor,
196  * __fd, into the buffer pointed to by __buf.
197  *
198  * @param __fd    [IN]     file descriptor (or socket descriptor) to read from
199  * @param __buf   [IN/OUT] user-provided to save the data
200  * @param __count [IN]     The maximum size of the user-provided buffer
201  *
202  * @attention
203  * <ul>
204  * <li>None.</li>
205  * </ul>
206  *
207  * @retval #ssize_t  A positive non-zero number of bytes read on suceess.
208  * @retval #0        End-of-file.
209  * @retval #-1       On failure with errno set appropriately.
210  *
211  * @par Errors
212  * <ul>
213  * <li><b>EBADF</b>: __fd is not a valid open file descriptor, or the file system doesn't support read().</li>
214  * <li><b>EFAULT</b>: __buf is outside your accessible address space.</li>
215  * <li><b>EACCES</b>: The file is a write-only file.</li>
216  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
217  * <li><b>ENOMEM</b>: Out of memory.</li>
218  * <li><b>EINVAL</b>: __count is greater than {YAFFS_MAX_FILE_SIZE} in yaffs filesystem.</li>
219  * <li><b>ENODEV</b>: The mount is not healthy.</li>
220  * </ul>
221  *
222  * @par Dependency:
223  * <ul><li>unistd.h</li></ul>
224  * @see  fcntl | ioctl | lseek | open | readv
225  */
226 ssize_t read(int, void *, size_t);
227 
228 /**
229  * @ingroup unistd
230  *
231  * @par Description:
232  * The write() function shall attempt to write __count bytes from the buffer pointed to by __buf to the file
233  * associated with the open file descriptor, __fd.
234  *
235  * @param __fd    [IN] file descriptor (or socket descriptor) to write to
236  * @param __buf   [IN] Data to write
237  * @param __count [IN] Length of data to write
238  *
239  * @attention
240  * <ul>
241  * <li>On a regular file or other file capable of seeking, the actual writing of data shall proceed from the position
242  * in the file indicated by the file offset associated with fildes. Before successful return from write(), the file
243  * offset shall be incremented by the number of bytes actually written. On a regular file, if the position
244  * of the last byte written is greater than or equal to the length of the file, the length of the file shall be
245  * set to this position plus one.</li>
246  * <li>If the O_APPEND flag of the file status flags is set, the file offset shall be set to the end of the file
247  * prior to each write and no intervening file modification operation shall occur between changing the file offset
248  * and the write operation.</li>
249  * </ul>
250  *
251  * @retval #ssize_t  On success, the number of bytes  written are returned (zero indicates nothing was written).
252  * @retval #-1       On error, -1 is returned, and errno is set appropriately.
253  *
254  * @par Errors
255  * <ul>
256  * <li><b>EBADF</b>: __fd is not a valid open file descriptor, or the file system doesn't support write().</li>
257  * <li><b>EFAULT</b>: __buf is outside your accessible address space.</li>
258  * <li><b>EACCES/EROFS</b>: The file is a read-only file or the file system is read-only.</li>
259  * <li><b>EIO</b>: A physical I/O error has occurred.</li>
260  * <li><b>ENOMEM</b>: Out of memory.</li>
261  * <li><b>EINTR</b>: The write operation was terminated due to the receipt of a signal, no data was transferred.</li>
262  * <li><b>ENOSPC</b>: There was no free space remaining on the device containing the file.</li>
263  * <li><b>EAGAIN</b>: Non-blocking I/O has been selected using O_NONBLOCK and the write would block.</li>
264  * <li><b>EFBIG</b>: An attempt was made to write a file that exceeds the implementation defined maximum file
265  * size or the process' file size limit, or to write at a position past the maximum allowed offset.</li>
266  * <li><b>EINVAL</b>: The current position of file is less than zero.</li>
267  * <li><b>ENODEV</b>: The mount is not healthy.</li>
268  * </ul>
269  *
270  * @par Dependency:
271  * <ul><li>unistd.h</li></ul>
272  * @see  fcntl | ioctl | lseek | open | readv
273  */
274 ssize_t write(int, const void *, size_t);
275 
276 /**
277  * @ingroup unistd
278  *
279  * @par Description:
280  * The pread() function shall be equivalent to read(), except that it shall read from a given position in the file
281  * without changing the file offset. The first three arguments to pread() are the same as read() with the addition
282  * of a fourth argument __offset for the desired position inside the file. An attempt to perform a pread() on a file
283  * that is incapable of seeking shall result in an error.
284  *
285  * @param __fd     [IN]     file descriptor (or socket descriptor) to read from
286  * @param __buf    [IN/OUT] user-provided to save the data
287  * @param __count  [IN]     The maximum size of the user-provided buffer
288  * @param __offset [IN]     The file offset
289  *
290  * @attention
291  * <ul>
292  * <li>None.</li>
293  * </ul>
294  *
295  * @retval #ssize_t  A positive non-zero number of bytes read on suceess.
296  * @retval #0        End-of-file.
297  * @retval #-1       On failure with errno set appropriately.
298  *
299  * @par Errors
300  * <ul>
301  * <li><b>EBADF</b>: __fd is not a valid open file descriptor, or the file system doesn't support read().</li>
302  * <li><b>EFAULT</b>: __buf is outside your accessible address space.</li>
303  * <li><b>EACCES</b>: The file is a write-only file.</li>
304  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
305  * <li><b>ENOMEM</b>: Out of memory.</li>
306  * <li><b>EINVAL</b>: __count is greater than {YAFFS_MAX_FILE_SIZE} in yaffs filesystem.</li>
307  * <li><b>ENODEV</b>: The mount is not healthy.</li>
308  * </ul>
309  *
310  * @par Dependency:
311  * <ul><li>unistd.h</li></ul>
312  * @see  fcntl | ioctl | lseek | open | readv
313  */
314 ssize_t pread(int, void *, size_t, off_t);
315 
316 /**
317  * @ingroup unistd
318  *
319  * @par Description:
320  * The pwrite() function shall be equivalent to write(), except that it writes into a given position
321  * and does not change the file offset (regardless of whether O_APPEND is set). The first three arguments to pwrite()
322  * are the same as write() with the addition of a fourth argument __offset for the desired position inside the file.
323  * An attempt to perform a pwrite() on a file that is incapable of seeking shall result in an error.
324  *
325  * @param __fd     [IN] file descriptor (or socket descriptor) to write to
326  * @param __buf    [IN] Data to write
327  * @param __count  [IN] Length of data to write
328  * @param __offset [IN] The file offset to write from
329  *
330  * @attention
331  * <ul>
332  * <li>None.</li>
333  * </ul>
334  *
335  * @retval #ssize_t  On success, the number of bytes  written are returned (zero indicates nothing was written).
336  * @retval #-1       On error, -1 is returned, and errno is set appropriately.
337  *
338  * @par Errors
339  * <ul>
340  * <li><b>EBADF</b>: __fd is not a valid open file descriptor, or the file system doesn't support write().</li>
341  * <li><b>EFAULT</b>: __buf is outside your accessible address space.</li>
342  * <li><b>EACCES/EROFS</b>: The file is a read-only file or the file system is read-only.</li>
343  * <li><b>EIO</b>: A physical I/O error has occurred.</li>
344  * <li><b>ENOMEM</b>: Out of memory.</li>
345  * <li><b>EINTR</b>: The write operation was terminated due to the receipt of a signal, no data was transferred.</li>
346  * <li><b>ENOSPC</b>: There was no free space remaining on the device containing the file.</li>
347  * <li><b>EAGAIN</b>: Non-blocking I/O has been selected using O_NONBLOCK and the write would block.</li>
348  * <li><b>EFBIG</b>: An attempt was made to write a file that exceeds the implementation defined maximum file
349  * size or the process' file size limit, or to write at a position past the maximum allowed offset.</li>
350  * <li><b>EINVAL</b>: The current position of file is less than zero.</li>
351  * <li><b>ENODEV</b>: The mount is not healthy.</li>
352  * </ul>
353  *
354  * @par Dependency:
355  * <ul><li>unistd.h</li></ul>
356  * @see  fcntl | ioctl | lseek | open | readv
357  */
358 ssize_t pwrite(int, const void *, size_t, off_t);
359 
360 int chown(const char *, uid_t, gid_t);
361 int fchown(int, uid_t, gid_t);
362 int lchown(const char *, uid_t, gid_t);
363 int fchownat(int, const char *, uid_t, gid_t, int);
364 
365 int link(const char *, const char *);
366 int linkat(int, const char *, int, const char *, int);
367 int symlink(const char *, const char *);
368 int symlinkat(const char *, int, const char *);
369 ssize_t readlink(const char *__restrict, char *__restrict, size_t);
370 ssize_t readlinkat(int, const char *__restrict, char *__restrict, size_t);
371 
372 /**
373  * @ingroup unistd
374  *
375  * @par Description:
376  * The unlink() function shall remove a link to file. When the file's link count becomes 0 and no process
377  * has the file open, the space occupied by the file shall be freed and the file shall no longer be accessible.
378  * If one or more processes have the file open when the last link is removed, the link shall be removed before unlink() returns,
379  * but the removal of the file contents shall be postponed until all references to the file are closed.
380  *
381  * @param  __path [IN] path of the file which need to be removed, shall not name a directory.
382  *
383  * @attention
384  * <ul>
385  * <li>If the unlink() function is called to remove a file in /dev, it shall just call the registered unlink hook function
386  * the file will still alive and won't be removed.</li>
387  * </ul>
388  *
389  * @retval #int Upon successful completion, these functions shall return 0.
390  * Otherwise, these functions shall return -1 and set errno to indicate the error.
391  * If -1 is returned, the named file shall not be changed.
392  *
393  *
394  * @par Errors
395  * <ul>
396  * <li><b>EACCES</b>: Search permission is denied for a component of the path prefix,
397  * or write permission is denied on the directory containing the directory entry to be removed,
398  * or remove a file in /dev which doesn't register the unlink function.</li>
399  * <li><b>ENAMETOOLONG</b>: The length of a component of a pathname is longer than {NAME_MAX}.</li>
400  * <li><b>ENOTEMPTY</b>: unlink a pseudo-file which is functioning as a directory and the directory is not empty.</li>
401  * <li><b>ENOENT</b>: A component of path does not name an existing file.</li>
402  * <li><b>EINVAL</b>: The path is not valid.</li>
403  * <li><b>ENOMEM</b>: Out of memory.</li>
404  * <li><b>EISDIR</b>: The path is a directory.</li>
405  * </ul>
406  *
407  * @par Dependency:
408  * <ul><li>unistd.h</li></ul>
409  * @see close | remove | rename | rmdir
410  */
411 int unlink(const char *);
412 int unlinkat(int, const char *, int);
413 
414 /**
415  * @ingroup unistd
416  *
417  * @par Description:
418  * The rmdir() function shall remove a directory whose name is given by __path.
419  * The directory shall be removed only if it is an empty directory.
420  *
421  * @attention
422  * <ul>
423  * <li>None.</li>
424  * </ul>
425  *
426  * @retval #0  Remove directory successful.
427  * @retval #-1 An error is encountered and remove the directory failed.
428  *
429  * @par Errors
430  * <ul>
431  * <li><b>EINVAL</b>: The path is a null pointer or an empty string.</li>
432  * <li><b>ENAMETOOLONG</b>: The length of a component of a pathname is longer than {NAME_MAX}.</li>
433  * <li><b>ENOENT</b>: A component of the path does not exist.</li>
434  * <li><b>EPERM</b>: The path represent a mount point, or the path is neither a directory nor a file.</li>
435  * <li><b>ENOTEMPTY</b>: The path argument names a directory that is not an empty directory.</li>
436  * <li><b>ENOTDIR</b>: A component of path names an existing file instead of a directory.</li>
437  * <li><b>ENODEV</b>: The mount is not healthy.</li>
438  * <li><b>ENOMEM</b>: Out of memory.</li>
439  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
440  * <li><b>EROFS</b>: The physical drive is write protected with fat filesystem.</li>
441  * <li><b>EACCES</b>: The directory entry to be removed resides on a read-only file system, or remove a directory
442  * in proc.</li>
443  * <li><b>EBUSY</b>: Remove a root of file system, or the directory is not empty of ramfs.</li>
444  * <li><b>ENOSYS
445 </b>: The file system doesn't support to rmdir.</li>
446  * </ul>
447  *
448  * @par Dependency:
449  * <ul><li>unistd.h</li></ul>
450  * @see mkdir | remove | rename | unlink
451  */
452 int rmdir(const char *);
453 int truncate(const char *, off_t);
454 
455 /**
456  * @ingroup unistd
457  *
458  * @par Description:
459  * The ftruncate() function truncates the file size to the length bytes.
460  * If  the  file  previously was larger than this size, the extra data is lost.
461  * If the file previously was shorter, it is extended, and the extended part reads as disk data.
462  * @attention
463  * <ul>
464  * <li>Now only FAT32 support this function.</li>
465  * </ul>
466  *
467  * @retval #int The ftruncate() function shall return 0 upon successful completion.
468  * Otherwise, the function shall return -1 and set errno.
469  *
470  * @par Errors
471  * <ul>
472  * <li><b>EBADF</b>: The __fd argument is not a valid open file descriptor.</li>
473  * <li><b>ENOSYS</b>: The function is not supported.</li>
474  * <li><b>EINVAL</b>: The length argument was less than 0 or
475  * The length argument was greater than the maximum file size.</li>
476  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
477  * <li><b>EACCES</b>: The file permission is denied.</li>
478  * <li><b>ENODEV</b>: The device is not existed.</li>
479  * </ul>
480  *
481  * @par Dependency:
482  * <ul><li>unistd.h</li></ul>
483  * @see fallocate
484  */
485 int ftruncate(int, off_t);
486 
487 #define F_OK 0
488 #define R_OK 4
489 #define W_OK 2
490 #define X_OK 1
491 
492 /**
493  * @ingroup unistd
494  *
495  * @par Description:
496  * The access() function shall check the file named by the pathname pointed to by the __path argument
497  * for accessibility according to the bit pattern contained in __mode.
498  *
499  * @param  __path [IN] path of the file which need to be checked.
500  * @param  __mode [IN] The value of __mode is either the bitwise-inclusive OR of the access permissions to be checked
501  *                     (R_OK, W_OK, X_OK) or the existence test (F_OK).
502  *
503  * @attention
504  * <ul>
505  * <li>None.</li>
506  * </ul>
507  *
508  * @retval #int The access() function shall return 0 upon successful completion.
509  * Otherwise, the function shall return -1 and set errno.
510  *
511  * @par Errors
512  * <ul>
513  * <li><b>EACCES</b>: Permission bits of the file mode do not permit the requested access,
514  * or search permission is denied on a component of the path prefix.</li>
515  * <li><b>ENAMETOOLONG</b>: The length of a component of a pathname is longer than {NAME_MAX}.</li>
516  * <li><b>EFAULT</b>: Bad address.</li>
517  * <li><b>ENOENT</b>: A component of the path does not exist, or the path is an empty string.</li>
518  * <li><b>ENOMEM</b>: Out of memory.</li>
519  * <li><b>ENOTDIR</b>: A component of the path is not a directory.</li>
520  * </ul>
521  *
522  * @par Dependency:
523  * <ul><li>unistd.h</li></ul>
524  * @see None
525  */
526 int access(const char *, int);
527 int faccessat(int, const char *, int, int);
528 
529 /**
530  * @ingroup unistd
531  *
532  * @par Description:
533  * The chdir() function shall cause the directory named by the pathname pointed to by the __path argument to become
534  * the current working directory; that is, the starting point for path searches for pathnames not beginning with '/'.
535  *
536  * @param  __path [IN] path of the file which need to be set as the current working directory.
537  *
538  * @attention
539  * <ul>
540  * <li>None.</li>
541  * </ul>
542  *
543  * @retval #int Upon successful completion, 0 shall be returned.
544  * Otherwise, -1 shall be returned, the current working directory shall remain unchanged,
545  * and errno shall be set to indicate the error.
546  *
547  * @par Errors
548  * <ul>
549  * <li><b>EINVAL</b>: The path is an empty string.</li>
550  * <li><b>ENAMETOOLONG</b>: The length of a component of a pathname is longer than {NAME_MAX}.</li>
551  * <li><b>ENOENT</b>: A component of the path does not exist,
552  * or length of __path is longer than {PATH_MAX}.</li>
553  * <li><b>ENOMEM</b>: Out of memory.</li>
554  * <li><b>ENOSPC</b>: Out of memory in ramfs filesystem.</li>
555  * <li><b>ENOTDIR</b>: A component of the path is not a directory.</li>
556  * <li><b>EMFILE</b>: Too many open file.</li>
557  * <li><b>EROFS</b>: The physical drive is write protected with fat filesystem.</li>
558  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
559  * <li><b>ENODEV</b>: The mount is not healthy.</li>
560  * <li><b>EPERM</b>: Other errors.</li>
561  * </ul>
562  *
563  * @par Dependency:
564  * <ul><li>unistd.h</li></ul>
565  * @see getcwd
566  */
567 int chdir(const char *);
568 int fchdir(int);
569 
570 /**
571  * @ingroup unistd
572  *
573  * @par Description:
574  * The getcwd() function shall place an absolute pathname of the current working directory
575  * in the array pointed to by __buf, and return __buf.
576  *
577  * @param __buf  [IN/OUT] Point to an array which place an absolute pathname of the current working directory and return.
578  * @param __size [IN]     The size in bytes of the character array pointed to by the __buf argument.
579  *
580  * @attention
581  * <ul>
582  * <li>None.</li>
583  * </ul>
584  *
585  * @retval #char* Return the __buf argument if upon successful completion.
586  * @retval #NULL  An error is encountered and the contents of the array pointed to by __buf are undefined.
587  *
588  * @par Errors
589  * <ul>
590  * <li><b>EINVAL</b>: The __buf is a NULL pointer.</li>
591  * <li><b>ERANGE</b>: The size argument is smaller than the length of the current working directory name + 1.</li>
592  * <li><b>ENAMETOOLONG</b>: Use memcpy_s to copy the current working directory name to __buf failed.</li>
593  * </ul>
594  *
595  * @par Dependency:
596  * <ul><li>unistd.h</li></ul>
597  * @see malloc
598  */
599 char *getcwd(char *, size_t);
600 
601 unsigned alarm(unsigned);
602 
603 /**
604  * @ingroup unistd
605  *
606  * @par Description:
607  * The sleep() function shall cause the calling thread to be suspended from execution until either the number of
608  * realtime seconds specified by the argument __seconds has elapsed or a signal is delivered to the calling thread
609  * and its action is to invoke a signal-catching function or to terminate the process.
610  * The suspension time may be longer than requested due to the scheduling of other activity by the system.
611  *
612  * @attention
613  * <ul>
614  * <li>None.</li>
615  * </ul>
616  *
617  * @retval #0             The requested time has elapsed.
618  * @retval #unsigned int  An error is encountered in LOS_TaskDelay() and the __seconds shall be returned.
619  *
620  * @par Dependency:
621  * <ul><li>unistd.h</li></ul>
622  * @see nanosleep
623  */
624 unsigned sleep(unsigned);
625 int pause(void);
626 
627 pid_t fork(void);
628 int execve(const char *, char *const [], char *const []);
629 int execv(const char *, char *const []);
630 int execle(const char *, const char *, ...);
631 int execl(const char *, const char *, ...);
632 int execvp(const char *, char *const []);
633 int execlp(const char *, const char *, ...);
634 int fexecve(int, char *const [], char *const []);
635 _Noreturn void _exit(int);
636 
637 /**
638  * @ingroup unistd
639  *
640  * @par Description:
641  * The getpid() function get the process ID of the calling process.
642  *
643  * @attention
644  * <ul>
645  * <li>None.</li>
646  * </ul>
647  *
648  * @retval #int The getpid() function shall return the process ID of the calling process.
649  * @par Dependency:
650  * <ul><li>unistd.h</li></ul>
651  * @see None
652  */
653 pid_t getpid(void);
654 pid_t getppid(void);
655 pid_t getpgrp(void);
656 pid_t getpgid(pid_t);
657 int setpgid(pid_t, pid_t);
658 pid_t setsid(void);
659 pid_t getsid(pid_t);
660 char *ttyname(int);
661 int ttyname_r(int, char *, size_t);
662 int isatty(int);
663 pid_t tcgetpgrp(int);
664 int tcsetpgrp(int, pid_t);
665 
666 uid_t getuid(void);
667 uid_t geteuid(void);
668 gid_t getgid(void);
669 gid_t getegid(void);
670 int getgroups(int, gid_t []);
671 int setuid(uid_t);
672 int seteuid(uid_t);
673 int setgid(gid_t);
674 int setegid(gid_t);
675 
676 char *getlogin(void);
677 int getlogin_r(char *, size_t);
678 int gethostname(char *, size_t);
679 char *ctermid(char *);
680 
681 int getopt(int, char * const [], const char *);
682 extern char *optarg;
683 extern int optind, opterr, optopt;
684 
685 long pathconf(const char *, int);
686 long fpathconf(int, int);
687 long sysconf(int);
688 size_t confstr(int, char *, size_t);
689 
690 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
691 #define F_ULOCK 0
692 #define F_LOCK  1
693 #define F_TLOCK 2
694 #define F_TEST  3
695 int setreuid(uid_t, uid_t);
696 int setregid(gid_t, gid_t);
697 int lockf(int, int, off_t);
698 long gethostid(void);
699 int nice(int);
700 
701 /**
702  * @ingroup unistd
703  *
704  * @par Description:
705  * The sync() function shall request that all data in bcache to be transferred to the storage device for disk 0 and 1.
706  *
707  * @attention
708  * <ul>
709  * <li>The sync() function in liteos can only flush all data in bcache into storage device, but can't flush
710  * the data in memory, and it shall not return until the system completed that action or until an error is detected.
711  * But in POSIX, The sync() function shall cause all information in memory that updates file systems to be scheduled
712  * for writing out to all file systems, and the writing, although scheduled, is not necessarily complete
713  * upon return from sync().</li>
714  * <li>The sync() function is only useful for fat filesystem and the bcache shall be enabled,
715  * other filesystems don't need to sync.</li>
716  * <li>The sync() function
717  * </ul>
718  *
719  * @retval #void None.
720  *
721  * @par Dependency:
722  * <ul><li>unistd.h</li></ul>
723  * @see fsync
724  */
725 void sync(void);
726 pid_t setpgrp(void);
727 char *crypt(const char *, const char *);
728 void encrypt(char *, int);
729 void swab(const void *__restrict, void *__restrict, ssize_t);
730 #endif
731 
732 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) \
733  || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700)
734 
735 /**
736  * @ingroup unistd
737  *
738  * @par Description:
739  * The usleep() function shall cause the calling thread to be suspended from execution until either the number of
740  * microsecond specified by the argument __usec has elapsed or a signal is delivered to the calling thread
741  * and its action is to invoke a signal-catching function or to terminate the process.
742  * The suspension time may be longer than requested due to the scheduling of other activity by the system.
743  *
744  * @attention
745  * <ul>
746  * <li>If __usec is less than per Millisecond and is not equal to zero,
747  * the suspended time shall be set to 1 millisecond.</li>
748  * <li>The realtime to sleep is __usec/1000 millisecond.</li>
749  * </ul>
750  *
751  * @retval #0   The requested time has elapsed.
752  * @retval #-1  An error is encountered in LOS_TaskDelay().
753  *
754  * @par Dependency:
755  * <ul><li>unistd.h</li></ul>
756  * @see sleep
757  */
758 int usleep(unsigned);
759 unsigned ualarm(unsigned, unsigned);
760 #endif
761 
762 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
763 #define L_SET 0
764 #define L_INCR 1
765 #define L_XTND 2
766 int brk(void *);
767 void *sbrk(intptr_t);
768 pid_t vfork(void);
769 int vhangup(void);
770 int chroot(const char *);
771 int getpagesize(void);
772 int getdtablesize(void);
773 int sethostname(const char *, size_t);
774 int getdomainname(char *, size_t);
775 int setdomainname(const char *, size_t);
776 int setgroups(size_t, const gid_t *);
777 char *getpass(const char *);
778 int daemon(int, int);
779 void setusershell(void);
780 void endusershell(void);
781 char *getusershell(void);
782 int acct(const char *);
783 long syscall(long, ...);
784 int execvpe(const char *, char *const [], char *const []);
785 int issetugid(void);
786 int getentropy(void *, size_t);
787 extern int optreset;
788 #endif
789 
790 #ifdef _GNU_SOURCE
791 extern char **environ;
792 int setresuid(uid_t, uid_t, uid_t);
793 int setresgid(gid_t, gid_t, gid_t);
794 int getresuid(uid_t *, uid_t *, uid_t *);
795 int getresgid(gid_t *, gid_t *, gid_t *);
796 char *get_current_dir_name(void);
797 int syncfs(int);
798 int euidaccess(const char *, int);
799 int eaccess(const char *, int);
800 ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned);
801 #endif
802 
803 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
804 #ifdef __LITEOS__
805 #define off64_t int64_t
806 off64_t lseek64(int, off64_t, int);
807 ssize_t pread64(int, void *, size_t, off64_t);
808 ssize_t pwrite64(int, const void *, size_t, off64_t);
809 int truncate64(const char *, off64_t);
810 int ftruncate64(int, off64_t);
811 int lockf64(int, int, off64_t);
812 #else
813 #define lseek64 lseek
814 #define pread64 pread
815 #define pwrite64 pwrite
816 #define truncate64 truncate
817 #define ftruncate64 ftruncate
818 #define lockf64 lockf
819 #define off64_t off_t
820 #endif
821 #endif
822 
823 #define POSIX_CLOSE_RESTART     0
824 
825 #define _XOPEN_VERSION          700
826 #define _XOPEN_UNIX             1
827 #define _XOPEN_ENH_I18N         1
828 
829 #define _POSIX_VERSION          200809L
830 #define _POSIX2_VERSION         _POSIX_VERSION
831 
832 #define _POSIX_ADVISORY_INFO    _POSIX_VERSION
833 #define _POSIX_CHOWN_RESTRICTED 1
834 #define _POSIX_IPV6             _POSIX_VERSION
835 #define _POSIX_JOB_CONTROL      1
836 #define _POSIX_MAPPED_FILES     _POSIX_VERSION
837 #define _POSIX_MEMLOCK          _POSIX_VERSION
838 #define _POSIX_MEMLOCK_RANGE    _POSIX_VERSION
839 #define _POSIX_MEMORY_PROTECTION _POSIX_VERSION
840 #define _POSIX_MESSAGE_PASSING  _POSIX_VERSION
841 #define _POSIX_FSYNC            _POSIX_VERSION
842 #define _POSIX_NO_TRUNC         1
843 #define _POSIX_RAW_SOCKETS      _POSIX_VERSION
844 #define _POSIX_REALTIME_SIGNALS _POSIX_VERSION
845 #define _POSIX_REGEXP           1
846 #define _POSIX_SAVED_IDS        1
847 #define _POSIX_SHELL            1
848 #define _POSIX_SPAWN            _POSIX_VERSION
849 #define _POSIX_VDISABLE         0
850 
851 #define _POSIX_THREADS          _POSIX_VERSION
852 #define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
853 #define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION
854 #define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION
855 #define _POSIX_THREAD_ATTR_STACKSIZE _POSIX_VERSION
856 #define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
857 #define _POSIX_THREAD_CPUTIME   _POSIX_VERSION
858 #define _POSIX_TIMERS           _POSIX_VERSION
859 #define _POSIX_TIMEOUTS         _POSIX_VERSION
860 #define _POSIX_MONOTONIC_CLOCK  _POSIX_VERSION
861 #define _POSIX_CPUTIME          _POSIX_VERSION
862 #define _POSIX_CLOCK_SELECTION  _POSIX_VERSION
863 #define _POSIX_BARRIERS         _POSIX_VERSION
864 #define _POSIX_SPIN_LOCKS       _POSIX_VERSION
865 #define _POSIX_READER_WRITER_LOCKS _POSIX_VERSION
866 #define _POSIX_ASYNCHRONOUS_IO  _POSIX_VERSION
867 #define _POSIX_SEMAPHORES       _POSIX_VERSION
868 #define _POSIX_SHARED_MEMORY_OBJECTS _POSIX_VERSION
869 
870 #define _POSIX2_C_BIND          _POSIX_VERSION
871 
872 #include <bits/posix.h>
873 
874 
875 
876 #define _PC_LINK_MAX	0
877 #define _PC_MAX_CANON	1
878 #define _PC_MAX_INPUT	2
879 #define _PC_NAME_MAX	3
880 #define _PC_PATH_MAX	4
881 #define _PC_PIPE_BUF	5
882 #define _PC_CHOWN_RESTRICTED	6
883 #define _PC_NO_TRUNC	7
884 #define _PC_VDISABLE	8
885 #define _PC_SYNC_IO	9
886 #define _PC_ASYNC_IO	10
887 #define _PC_PRIO_IO	11
888 #define _PC_SOCK_MAXBUF	12
889 #define _PC_FILESIZEBITS	13
890 #define _PC_REC_INCR_XFER_SIZE	14
891 #define _PC_REC_MAX_XFER_SIZE	15
892 #define _PC_REC_MIN_XFER_SIZE	16
893 #define _PC_REC_XFER_ALIGN	17
894 #define _PC_ALLOC_SIZE_MIN	18
895 #define _PC_SYMLINK_MAX	19
896 #define _PC_2_SYMLINKS	20
897 
898 #define _SC_ARG_MAX	0
899 #define _SC_CHILD_MAX	1
900 #define _SC_CLK_TCK	2
901 #define _SC_NGROUPS_MAX	3
902 #define _SC_OPEN_MAX	4
903 #define _SC_STREAM_MAX	5
904 #define _SC_TZNAME_MAX	6
905 #define _SC_JOB_CONTROL	7
906 #define _SC_SAVED_IDS	8
907 #define _SC_REALTIME_SIGNALS	9
908 #define _SC_PRIORITY_SCHEDULING	10
909 #define _SC_TIMERS	11
910 #define _SC_ASYNCHRONOUS_IO	12
911 #define _SC_PRIORITIZED_IO	13
912 #define _SC_SYNCHRONIZED_IO	14
913 #define _SC_FSYNC	15
914 #define _SC_MAPPED_FILES	16
915 #define _SC_MEMLOCK	17
916 #define _SC_MEMLOCK_RANGE	18
917 #define _SC_MEMORY_PROTECTION	19
918 #define _SC_MESSAGE_PASSING	20
919 #define _SC_SEMAPHORES	21
920 #define _SC_SHARED_MEMORY_OBJECTS	22
921 #define _SC_AIO_LISTIO_MAX	23
922 #define _SC_AIO_MAX	24
923 #define _SC_AIO_PRIO_DELTA_MAX	25
924 #define _SC_DELAYTIMER_MAX	26
925 #define _SC_MQ_OPEN_MAX	27
926 #define _SC_MQ_PRIO_MAX	28
927 #define _SC_VERSION	29
928 #define _SC_PAGE_SIZE	30
929 #define _SC_PAGESIZE	30 /* !! */
930 #define _SC_RTSIG_MAX	31
931 #define _SC_SEM_NSEMS_MAX	32
932 #define _SC_SEM_VALUE_MAX	33
933 #define _SC_SIGQUEUE_MAX	34
934 #define _SC_TIMER_MAX	35
935 #define _SC_BC_BASE_MAX	36
936 #define _SC_BC_DIM_MAX	37
937 #define _SC_BC_SCALE_MAX	38
938 #define _SC_BC_STRING_MAX	39
939 #define _SC_COLL_WEIGHTS_MAX	40
940 #define _SC_EXPR_NEST_MAX	42
941 #define _SC_LINE_MAX	43
942 #define _SC_RE_DUP_MAX	44
943 #define _SC_2_VERSION	46
944 #define _SC_2_C_BIND	47
945 #define _SC_2_C_DEV	48
946 #define _SC_2_FORT_DEV	49
947 #define _SC_2_FORT_RUN	50
948 #define _SC_2_SW_DEV	51
949 #define _SC_2_LOCALEDEF	52
950 #define _SC_UIO_MAXIOV	60 /* !! */
951 #define _SC_IOV_MAX	60
952 #define _SC_THREADS	67
953 #define _SC_THREAD_SAFE_FUNCTIONS	68
954 #define _SC_GETGR_R_SIZE_MAX	69
955 #define _SC_GETPW_R_SIZE_MAX	70
956 #define _SC_LOGIN_NAME_MAX	71
957 #define _SC_TTY_NAME_MAX	72
958 #define _SC_THREAD_DESTRUCTOR_ITERATIONS	73
959 #define _SC_THREAD_KEYS_MAX	74
960 #define _SC_THREAD_STACK_MIN	75
961 #define _SC_THREAD_THREADS_MAX	76
962 #define _SC_THREAD_ATTR_STACKADDR	77
963 #define _SC_THREAD_ATTR_STACKSIZE	78
964 #define _SC_THREAD_PRIORITY_SCHEDULING	79
965 #define _SC_THREAD_PRIO_INHERIT	80
966 #define _SC_THREAD_PRIO_PROTECT	81
967 #define _SC_THREAD_PROCESS_SHARED	82
968 #define _SC_NPROCESSORS_CONF	83
969 #define _SC_NPROCESSORS_ONLN	84
970 #define _SC_PHYS_PAGES	85
971 #define _SC_AVPHYS_PAGES	86
972 #define _SC_ATEXIT_MAX	87
973 #define _SC_PASS_MAX	88
974 #define _SC_XOPEN_VERSION	89
975 #define _SC_XOPEN_XCU_VERSION	90
976 #define _SC_XOPEN_UNIX	91
977 #define _SC_XOPEN_CRYPT	92
978 #define _SC_XOPEN_ENH_I18N	93
979 #define _SC_XOPEN_SHM	94
980 #define _SC_2_CHAR_TERM	95
981 #define _SC_2_UPE	97
982 #define _SC_XOPEN_XPG2	98
983 #define _SC_XOPEN_XPG3	99
984 #define _SC_XOPEN_XPG4	100
985 #define _SC_NZERO	109
986 #define _SC_XBS5_ILP32_OFF32	125
987 #define _SC_XBS5_ILP32_OFFBIG	126
988 #define _SC_XBS5_LP64_OFF64	127
989 #define _SC_XBS5_LPBIG_OFFBIG	128
990 #define _SC_XOPEN_LEGACY	129
991 #define _SC_XOPEN_REALTIME	130
992 #define _SC_XOPEN_REALTIME_THREADS	131
993 #define _SC_ADVISORY_INFO	132
994 #define _SC_BARRIERS	133
995 #define _SC_CLOCK_SELECTION	137
996 #define _SC_CPUTIME	138
997 #define _SC_THREAD_CPUTIME	139
998 #define _SC_MONOTONIC_CLOCK	149
999 #define _SC_READER_WRITER_LOCKS	153
1000 #define _SC_SPIN_LOCKS	154
1001 #define _SC_REGEXP	155
1002 #define _SC_SHELL	157
1003 #define _SC_SPAWN	159
1004 #define _SC_SPORADIC_SERVER	160
1005 #define _SC_THREAD_SPORADIC_SERVER	161
1006 #define _SC_TIMEOUTS	164
1007 #define _SC_TYPED_MEMORY_OBJECTS	165
1008 #define _SC_2_PBS	168
1009 #define _SC_2_PBS_ACCOUNTING	169
1010 #define _SC_2_PBS_LOCATE	170
1011 #define _SC_2_PBS_MESSAGE	171
1012 #define _SC_2_PBS_TRACK	172
1013 #define _SC_SYMLOOP_MAX	173
1014 #define _SC_STREAMS	174
1015 #define _SC_2_PBS_CHECKPOINT	175
1016 #define _SC_V6_ILP32_OFF32	176
1017 #define _SC_V6_ILP32_OFFBIG	177
1018 #define _SC_V6_LP64_OFF64	178
1019 #define _SC_V6_LPBIG_OFFBIG	179
1020 #define _SC_HOST_NAME_MAX	180
1021 #define _SC_TRACE	181
1022 #define _SC_TRACE_EVENT_FILTER	182
1023 #define _SC_TRACE_INHERIT	183
1024 #define _SC_TRACE_LOG	184
1025 
1026 #define _SC_IPV6	235
1027 #define _SC_RAW_SOCKETS	236
1028 #define _SC_V7_ILP32_OFF32	237
1029 #define _SC_V7_ILP32_OFFBIG	238
1030 #define _SC_V7_LP64_OFF64	239
1031 #define _SC_V7_LPBIG_OFFBIG	240
1032 #define _SC_SS_REPL_MAX	241
1033 #define _SC_TRACE_EVENT_NAME_MAX	242
1034 #define _SC_TRACE_NAME_MAX	243
1035 #define _SC_TRACE_SYS_MAX	244
1036 #define _SC_TRACE_USER_EVENT_MAX	245
1037 #define _SC_XOPEN_STREAMS	246
1038 #define _SC_THREAD_ROBUST_PRIO_INHERIT	247
1039 #define _SC_THREAD_ROBUST_PRIO_PROTECT	248
1040 
1041 #define _CS_PATH	0
1042 #define _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS	1
1043 #define _CS_GNU_LIBC_VERSION	2
1044 #define _CS_GNU_LIBPTHREAD_VERSION	3
1045 #define _CS_POSIX_V5_WIDTH_RESTRICTED_ENVS	4
1046 #define _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS	5
1047 
1048 #define _CS_POSIX_V6_ILP32_OFF32_CFLAGS	1116
1049 #define _CS_POSIX_V6_ILP32_OFF32_LDFLAGS	1117
1050 #define _CS_POSIX_V6_ILP32_OFF32_LIBS	1118
1051 #define _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS	1119
1052 #define _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS	1120
1053 #define _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS	1121
1054 #define _CS_POSIX_V6_ILP32_OFFBIG_LIBS	1122
1055 #define _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS	1123
1056 #define _CS_POSIX_V6_LP64_OFF64_CFLAGS	1124
1057 #define _CS_POSIX_V6_LP64_OFF64_LDFLAGS	1125
1058 #define _CS_POSIX_V6_LP64_OFF64_LIBS	1126
1059 #define _CS_POSIX_V6_LP64_OFF64_LINTFLAGS	1127
1060 #define _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS	1128
1061 #define _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS	1129
1062 #define _CS_POSIX_V6_LPBIG_OFFBIG_LIBS	1130
1063 #define _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS	1131
1064 #define _CS_POSIX_V7_ILP32_OFF32_CFLAGS	1132
1065 #define _CS_POSIX_V7_ILP32_OFF32_LDFLAGS	1133
1066 #define _CS_POSIX_V7_ILP32_OFF32_LIBS	1134
1067 #define _CS_POSIX_V7_ILP32_OFF32_LINTFLAGS	1135
1068 #define _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS	1136
1069 #define _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS	1137
1070 #define _CS_POSIX_V7_ILP32_OFFBIG_LIBS	1138
1071 #define _CS_POSIX_V7_ILP32_OFFBIG_LINTFLAGS	1139
1072 #define _CS_POSIX_V7_LP64_OFF64_CFLAGS	1140
1073 #define _CS_POSIX_V7_LP64_OFF64_LDFLAGS	1141
1074 #define _CS_POSIX_V7_LP64_OFF64_LIBS	1142
1075 #define _CS_POSIX_V7_LP64_OFF64_LINTFLAGS	1143
1076 #define _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS	1144
1077 #define _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS	1145
1078 #define _CS_POSIX_V7_LPBIG_OFFBIG_LIBS	1146
1079 #define _CS_POSIX_V7_LPBIG_OFFBIG_LINTFLAGS	1147
1080 #define _CS_V6_ENV	1148
1081 #define _CS_V7_ENV	1149
1082 
1083 #ifdef __cplusplus
1084 }
1085 #endif
1086 
1087 #endif
1088