UNIX Advanced Environment Programming learning Note (ii): Files and directories

Source: Internet
Author: User
Tags lstat

1 Stat,fstat,lstat function
#include <sys/stat.h>int stat (constcharstruct stat *restrict buf); int fstat (intstruct stat * buf); int lstat (constcharstruct stat *restrict buf);


Successful return 0, failure return-1

Stat Returns the information structure of the file specified on the pathname, FSTAT returns the information structure of the file descriptor for the specified file, LSTAT returns the information structure of the symbolic connection file, not the information structure of the file to which the symbolic connection points

2 Lstat Exercises
#include <stdio.h>#include<sys/stat.h>intMainintargcChar*argv[]) {  Char*ptr; inti; structstat buf;  for(i=1; i<argc;i++) {printf ("%s:", Argv[i]); if((Lstat (Argv[i], &buf)) <0) {printf ("Error:lstat"); Continue; }      if(S_isreg (buf.st_mode)) PTR="Regular"; Else if(S_isdir (buf.st_mode)) PTR="Directory"; Else if(S_ISCHR (buf.st_mode)) PTR="Character Special"; Else if(S_ISBLK (buf.st_mode)) PTR="Block Special"; Else if(S_isfifo (buf.st_mode)) PTR="FIFO"; Else if(S_islnk (buf.st_mode)) PTR="Link"; Else if(S_issock (buf.st_mode)) PTR="Socket"; printf ("%s\n", PTR); }  return 0;}


3 Owner and Group owner


The owner of the file is identified by St_uid and the group owner is identified by St_gid


4 The user ID for creating a file is set to a valid user ID for the process, the group ID may be a valid group ID for the process, or it may be a valid group ID for the directory where the file resides
5 Access functions to test the actual user ID for a file

#include <unistd.h>int access (constChar* pathname,int mode);


Successful return 0, failure return-1
Value of Mode
R_OK, W_OK, X_OK, F_OK (test file exists)


6 chmod and Fchmod functions, changing file access permissions

#include <sys/stat.h>int chmod (constChar* pathname, mode_t mode); int fchmode (int filedes, mode_t mode)


Successful return 0, failure return-1

Instance:
Chmode ("Bar", S_IRUSR | S_IWUSR | S_irgrp | S_iroth)
Increase user Read and write permissions for bar, and group and other Read permissions


7 Stick bit


When this bit is set, the program is first executed and ended, the program body part is still saved in the swap area, so that the next execution program can be loaded into the memory area faster, because the swap area occupies contiguous disk space, and the general data is often stored randomly
S_ISVTX = SaVed TeXt


8 Chown, Fchown, lchown, changing file User ID and group ID
#include <unistd.h>int chown (constChar * Pathname, uid_t owner, gid_t Group)  int Fchown (int  filedes, uid_t owner, gid_t Group)int lchown (const  Char* Pathname, uid_t owner, gid_t Group)



UNIX Advanced Environment Programming learning Note (ii): Files and directories

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.