logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

fstatat.3p (12451B)


  1. '\" et
  2. .TH FSTATAT "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. fstatat,
  12. lstat,
  13. stat
  14. \(em get file status
  15. .SH SYNOPSIS
  16. .LP
  17. .nf
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. .P
  21. int fstatat(int fd, const char *restrict \fIpath\fP,
  22. struct stat *restrict \fIbuf\fP, int \fIflag\fP);
  23. int lstat(const char *restrict \fIpath\fP, struct stat *restrict \fIbuf\fP);
  24. int stat(const char *restrict \fIpath\fP, struct stat *restrict \fIbuf\fP);
  25. .fi
  26. .SH DESCRIPTION
  27. The
  28. \fIstat\fR()
  29. function shall obtain information about the named file and write it
  30. to the area pointed to by the
  31. .IR buf
  32. argument. The
  33. .IR path
  34. argument points to a pathname naming a file. Read, write, or execute
  35. permission of the named file is not required. An implementation that
  36. provides additional or alternate file access control mechanisms may,
  37. under implementation-defined conditions, cause
  38. \fIstat\fR()
  39. to fail. In particular, the system may deny the existence of the file
  40. specified by
  41. .IR path .
  42. .P
  43. If the named file is a symbolic link, the
  44. \fIstat\fR()
  45. function shall continue pathname resolution using the contents of the
  46. symbolic link, and shall return information pertaining to the resulting
  47. file if the file exists.
  48. .P
  49. The
  50. .IR buf
  51. argument is a pointer to a
  52. .BR stat
  53. structure, as defined in the
  54. .IR <sys/stat.h>
  55. header, into which information is placed concerning the file.
  56. .P
  57. The
  58. \fIstat\fR()
  59. function shall update any time-related fields (as described in the Base Definitions volume of POSIX.1\(hy2017,
  60. .IR "Section 4.9" ", " "File Times Update"),
  61. before writing into the
  62. .BR stat
  63. structure.
  64. .P
  65. If the named file is a shared memory object, the implementation
  66. shall update in the
  67. .BR stat
  68. structure pointed to by the
  69. .IR buf
  70. argument the
  71. .IR st_uid ,
  72. .IR st_gid ,
  73. .IR st_size ,
  74. and
  75. .IR st_mode
  76. fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and
  77. S_IWOTH file permission bits need be valid. The implementation may
  78. update other fields and flags.
  79. .P
  80. If the named file is a typed memory object, the implementation
  81. shall update in the
  82. .BR stat
  83. structure pointed to by the
  84. .IR buf
  85. argument the
  86. .IR st_uid ,
  87. .IR st_gid ,
  88. .IR st_size ,
  89. and
  90. .IR st_mode
  91. fields, and only the S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and
  92. S_IWOTH file permission bits need be valid. The implementation may
  93. update other fields and flags.
  94. .P
  95. For all other file types defined in this volume of POSIX.1\(hy2017, the structure members
  96. .IR st_mode ,
  97. .IR st_ino ,
  98. .IR st_dev ,
  99. .IR st_uid ,
  100. .IR st_gid ,
  101. .IR st_atim ,
  102. .IR st_ctim ,
  103. and
  104. .IR st_mtim
  105. shall have meaningful values and the value of the member
  106. .IR st_nlink
  107. shall be set to the number of links to the file.
  108. .P
  109. The
  110. \fIlstat\fR()
  111. function shall be equivalent to
  112. \fIstat\fR(),
  113. except when
  114. .IR path
  115. refers to a symbolic link. In that case
  116. \fIlstat\fR()
  117. shall return information about the link, while
  118. \fIstat\fR()
  119. shall return information about the file the link references.
  120. .P
  121. For symbolic links, the
  122. .IR st_mode
  123. member shall contain meaningful information when used with the file
  124. type macros. The file mode bits in
  125. .IR st_mode
  126. are unspecified. The structure members
  127. .IR st_ino ,
  128. .IR st_dev ,
  129. .IR st_uid ,
  130. .IR st_gid ,
  131. .IR st_atim ,
  132. .IR st_ctim ,
  133. and
  134. .IR st_mtim
  135. shall have meaningful values and the value of the
  136. .IR st_nlink
  137. member shall be set to the number of (hard) links to the symbolic link.
  138. The value of the
  139. .IR st_size
  140. member shall be set to the length of the pathname contained in the
  141. symbolic link not including any terminating null byte.
  142. .P
  143. The
  144. \fIfstatat\fR()
  145. function shall be equivalent to the
  146. \fIstat\fR()
  147. or
  148. \fIlstat\fR()
  149. function, depending on the value of
  150. .IR flag
  151. (see below), except in the case where
  152. .IR path
  153. specifies a relative path. In this case the status shall be retrieved
  154. from a file relative to the directory associated with the file descriptor
  155. .IR fd
  156. instead of the current working directory. If the access mode of the
  157. open file description associated with the file descriptor is not
  158. O_SEARCH, the function shall check whether directory searches
  159. are permitted using the current permissions of the directory
  160. underlying the file descriptor. If the access mode is O_SEARCH,
  161. the function shall not perform the check.
  162. .P
  163. Values for
  164. .IR flag
  165. are constructed by a bitwise-inclusive OR of flags from the following
  166. list, defined in
  167. .IR <fcntl.h> :
  168. .IP AT_SYMLINK_NOFOLLOW 6
  169. .br
  170. If
  171. .IR path
  172. names a symbolic link, the status of the symbolic link is returned.
  173. .P
  174. If
  175. \fIfstatat\fR()
  176. is passed the special value AT_FDCWD in the
  177. .IR fd
  178. parameter, the current working directory shall be used and the behavior
  179. shall be identical to a call to
  180. \fIstat\fR()
  181. or
  182. \fIlstat\fR()
  183. respectively, depending on whether or not the AT_SYMLINK_NOFOLLOW bit
  184. is set in
  185. .IR flag .
  186. .SH "RETURN VALUE"
  187. Upon successful completion, these functions shall return 0.
  188. Otherwise, these functions shall return \-1 and set
  189. .IR errno
  190. to indicate the error.
  191. .SH ERRORS
  192. These functions shall fail if:
  193. .TP
  194. .BR EACCES
  195. Search permission is denied for a component of the path prefix.
  196. .TP
  197. .BR EIO
  198. An error occurred while reading from the file system.
  199. .TP
  200. .BR ELOOP
  201. A loop exists in symbolic links encountered during resolution of the
  202. .IR path
  203. argument.
  204. .TP
  205. .BR ENAMETOOLONG
  206. .br
  207. The length of a component of a pathname is longer than
  208. {NAME_MAX}.
  209. .TP
  210. .BR ENOENT
  211. A component of
  212. .IR path
  213. does not name an existing file or
  214. .IR path
  215. is an empty string.
  216. .TP
  217. .BR ENOTDIR
  218. A component of the path prefix names an existing file that is neither
  219. a directory nor a symbolic link to a directory, or the
  220. .IR path
  221. argument contains at least one non-\c
  222. <slash>
  223. character and ends with one or more trailing
  224. <slash>
  225. characters and the last pathname component names an existing file
  226. that is neither a directory nor a symbolic link to a directory.
  227. .TP
  228. .BR EOVERFLOW
  229. The file size in bytes or the number of blocks allocated to the file or
  230. the file serial number cannot be represented correctly in the structure
  231. pointed to by
  232. .IR buf .
  233. .P
  234. The
  235. \fIfstatat\fR()
  236. function shall fail if:
  237. .TP
  238. .BR EACCES
  239. The access mode of the open file description associated with
  240. .IR fd
  241. is not O_SEARCH and the permissions of the directory underlying
  242. .IR fd
  243. do not permit directory searches.
  244. .TP
  245. .BR EBADF
  246. The
  247. .IR path
  248. argument does not specify an absolute path and the
  249. .IR fd
  250. argument is neither AT_FDCWD nor a valid file descriptor open
  251. for reading or searching.
  252. .TP
  253. .BR ENOTDIR
  254. The
  255. .IR path
  256. argument is not an absolute path and
  257. .IR fd
  258. is a file descriptor associated with a non-directory file.
  259. .P
  260. These functions may fail if:
  261. .TP
  262. .BR ELOOP
  263. More than
  264. {SYMLOOP_MAX}
  265. symbolic links were encountered during resolution of the
  266. .IR path
  267. argument.
  268. .TP
  269. .BR ENAMETOOLONG
  270. .br
  271. The length of a pathname exceeds
  272. {PATH_MAX},
  273. or pathname resolution of a symbolic link produced an intermediate
  274. result with a length that exceeds
  275. {PATH_MAX}.
  276. .TP
  277. .BR EOVERFLOW
  278. A value to be stored would overflow one of the members of the
  279. .BR stat
  280. structure.
  281. .br
  282. .P
  283. The
  284. \fIfstatat\fR()
  285. function may fail if:
  286. .TP
  287. .BR EINVAL
  288. The value of the
  289. .IR flag
  290. argument is not valid.
  291. .LP
  292. .IR "The following sections are informative."
  293. .SH EXAMPLES
  294. .SS "Obtaining File Status Information"
  295. .P
  296. The following example shows how to obtain file status information for a
  297. file named
  298. .BR /home/cnd/mod1 .
  299. The structure variable
  300. .IR buffer
  301. is defined for the
  302. .BR stat
  303. structure.
  304. .sp
  305. .RS 4
  306. .nf
  307. #include <sys/types.h>
  308. #include <sys/stat.h>
  309. #include <fcntl.h>
  310. .P
  311. struct stat buffer;
  312. int status;
  313. \&...
  314. status = stat("/home/cnd/mod1", &buffer);
  315. .fi
  316. .P
  317. .RE
  318. .SS "Getting Directory Information"
  319. .P
  320. The following example fragment gets status information for each entry
  321. in a directory. The call to the
  322. \fIstat\fR()
  323. function stores file information in the
  324. .BR stat
  325. structure pointed to by
  326. .IR statbuf .
  327. The lines that follow the
  328. \fIstat\fR()
  329. call format the fields in the
  330. .BR stat
  331. structure for presentation to the user of the program.
  332. .sp
  333. .RS 4
  334. .nf
  335. #include <sys/types.h>
  336. #include <sys/stat.h>
  337. #include <dirent.h>
  338. #include <pwd.h>
  339. #include <grp.h>
  340. #include <time.h>
  341. #include <locale.h>
  342. #include <langinfo.h>
  343. #include <stdio.h>
  344. #include <stdint.h>
  345. .P
  346. struct dirent *dp;
  347. struct stat statbuf;
  348. struct passwd *pwd;
  349. struct group *grp;
  350. struct tm *tm;
  351. char datestring[256];
  352. \&...
  353. /* Loop through directory entries. */
  354. while ((dp = readdir(dir)) != NULL) {
  355. .P
  356. /* Get entry\(aqs information. */
  357. if (stat(dp->d_name, &statbuf) == -1)
  358. continue;
  359. .P
  360. /* Print out type, permissions, and number of links. */
  361. printf("%10.10s", sperm (statbuf.st_mode));
  362. printf("%4d", statbuf.st_nlink);
  363. .P
  364. /* Print out owner\(aqs name if it is found using getpwuid(). */
  365. if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
  366. printf(" %-8.8s", pwd->pw_name);
  367. else
  368. printf(" %-8d", statbuf.st_uid);
  369. .P
  370. /* Print out group name if it is found using getgrgid(). */
  371. if ((grp = getgrgid(statbuf.st_gid)) != NULL)
  372. printf(" %-8.8s", grp->gr_name);
  373. else
  374. printf(" %-8d", statbuf.st_gid);
  375. .P
  376. /* Print size of file. */
  377. printf(" %9jd", (intmax_t)statbuf.st_size);
  378. .P
  379. tm = localtime(&statbuf.st_mtime);
  380. .P
  381. /* Get localized date string. */
  382. strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
  383. .P
  384. printf(" %s %s\en", datestring, dp->d_name);
  385. }
  386. .fi
  387. .P
  388. .RE
  389. .SS "Obtaining Symbolic Link Status Information"
  390. .P
  391. The following example shows how to obtain status information for a
  392. symbolic link named
  393. .BR /modules/pass1 .
  394. The structure variable
  395. .IR buffer
  396. is defined for the
  397. .BR stat
  398. structure. If the
  399. .IR path
  400. argument specified the pathname for the file pointed to by the symbolic
  401. link (\c
  402. .BR /home/cnd/mod1 ),
  403. the results of calling the function would be the same as those returned
  404. by a call to the
  405. \fIstat\fR()
  406. function.
  407. .sp
  408. .RS 4
  409. .nf
  410. #include <sys/stat.h>
  411. .P
  412. struct stat buffer;
  413. int status;
  414. \&...
  415. status = lstat("/modules/pass1", &buffer);
  416. .fi
  417. .P
  418. .RE
  419. .SH "APPLICATION USAGE"
  420. None.
  421. .SH RATIONALE
  422. The intent of the paragraph describing ``additional or alternate file
  423. access control mechanisms'' is to allow a secure implementation where a
  424. process
  425. with a label that does not dominate the file's label cannot perform a
  426. \fIstat\fR()
  427. function. This is not related to read permission; a process with a
  428. label that dominates the file's label does not need read permission.
  429. An implementation that supports write-up operations could fail
  430. \fIfstat\fR()
  431. function calls even though it has a valid file descriptor open for
  432. writing.
  433. .P
  434. The purpose of the
  435. \fIfstatat\fR()
  436. function is to obtain the status of files in directories other than the
  437. current working directory without exposure to race conditions. Any part
  438. of the path of a file could be changed in parallel to a call to
  439. \fIstat\fR(),
  440. resulting in unspecified behavior. By opening a file descriptor for the
  441. target directory and using the
  442. \fIfstatat\fR()
  443. function it can be guaranteed that the file for which status is returned
  444. is located relative to the desired directory.
  445. .SH "FUTURE DIRECTIONS"
  446. None.
  447. .SH "SEE ALSO"
  448. .IR "\fIaccess\fR\^(\|)",
  449. .IR "\fIchmod\fR\^(\|)",
  450. .IR "\fIfdopendir\fR\^(\|)",
  451. .IR "\fIfstat\fR\^(\|)",
  452. .IR "\fImknod\fR\^(\|)",
  453. .IR "\fIreadlink\fR\^(\|)",
  454. .IR "\fIsymlink\fR\^(\|)"
  455. .P
  456. The Base Definitions volume of POSIX.1\(hy2017,
  457. .IR "Section 4.9" ", " "File Times Update",
  458. .IR "\fB<fcntl.h>\fP",
  459. .IR "\fB<sys_stat.h>\fP",
  460. .IR "\fB<sys_types.h>\fP"
  461. .\"
  462. .SH COPYRIGHT
  463. Portions of this text are reprinted and reproduced in electronic form
  464. from IEEE Std 1003.1-2017, Standard for Information Technology
  465. -- Portable Operating System Interface (POSIX), The Open Group Base
  466. Specifications Issue 7, 2018 Edition,
  467. Copyright (C) 2018 by the Institute of
  468. Electrical and Electronics Engineers, Inc and The Open Group.
  469. In the event of any discrepancy between this version and the original IEEE and
  470. The Open Group Standard, the original IEEE and The Open Group Standard
  471. is the referee document. The original Standard can be obtained online at
  472. http://www.opengroup.org/unix/online.html .
  473. .PP
  474. Any typographical or formatting errors that appear
  475. in this page are most likely
  476. to have been introduced during the conversion of the source files to
  477. man page format. To report such errors, see
  478. https://www.kernel.org/doc/man-pages/reporting_bugs.html .