Study notes: LINUXC sixth chapter under __linux

Source: Internet
Author: User

This chapter describes some of the basic file operations, the file contains two main aspects: (1) The file itself contains data, (2) file properties. Below, I briefly describe the file attribute operation, and in the study, practice process feel should pay attention to place.
File Property Actions:
1. Get file properties: In the shell directly with the command LS to obtain file properties in the program need to use the Stat/fstat/lstat function, these three functions are used to get the file information, see the following function declaration and its comments

/* get filename specified by filename *
/int stat (const char *filename, struct stat *buf);

/* Get descriptor Filedes Specify filename information *
/int fstat (int filedes, struct stat *buf);

/* For the symbolic link file, Lstat returns the link file itself, STAT returns the file information that the link refers to
/int lstat (const char *file_name, struct stat);

2. Set file properties:
The same can be done with some commands, such as chmod, under the shell. Modify file properties, such as Chmod/fchmod, through a series of functions in your program. Here the Umask function is mainly introduced.
The Umask function is used to set the masked word used when the file is created and returns the previous value (if: no error)
When a process creates a new file or directory, the actual access permissions for the new file are the result of mode and umask following the mode&~umask operation, the parameters of the Umask,umask function used by the Umask function to modify the process can be directly evaluated, or can be called by the system. For specific applications, please refer to the code and comments.

Umask (0);                 Do not mask any permissions
umask (S_IRWXO);           Masks all permissions for other users.

The following are some of the code I used to implement my own simplified version of the LS command through a function.
First, the LS command uses the sort function in many places. Here's how I use the quick sort time to sort the file names, so that when you get the file properties, you can simply pass the file name as the parameter to implement-T.
For more information, refer to the code:

/*file_name represents the filename, time is the left leftmost, right is the rightmost
/void Sort_time (char file_name[256][path_max+1], int left, int. , Char Time[256][path_max])
{
    int i=left;
    int j=right;
    Char key[80];     Time identification
    Char key1[80];    FileName identification

    strcpy (Key,time[left]);
    strcpy (Key1,file_name[left]);

    if (left >= right) {return
        ;
    }
    while (I<J) {
        while (i<j && strcmp (Key, Time[j)) <0) {
            j--;
        }
        strcpy (Time[i],time[j]);
        strcpy (File_name[i], file_name[j]);

        while (I<j && strcmp (Time[i], key) <=0) {
            i++;
        }
        strcpy (Time[j],time[i]);
        strcpy (File_name[j], file_name[i]);
    }
    strcpy (Time[i], key);
    strcpy (file_name[i],key1);
    Sort_time (file_name, left, i-1, time);   Recursive left
    sort_time (file_name, i+1, right, time); Recursive right
}

There is a very important function in the LS command-R, here is a brief description, please refer to the code and comments

void print_dir (char *path)//Receive path {dir *dir=null;
    struct Dirent *ptr;  Char filenames[2000];            Save path name int len=0;
    Gets the path length (by byte) Len=strlen (path);                   Dir=opendir (path);
    Open the Directory if (dir==null) {//To determine whether the return was successfully opened; } while (Ptr=readdir (dir)!=null)//delivery All files in this directory {/* Keep path correct/* strncpy (filenames, path, Len) 
        ;
        Filenames[len]= ' ";
        strcat (filenames, ptr->d_name);

        Filenames[len+strlen (ptr->d_name)]= ';
            if (ptr->d_type==8) {//=8 represents a file, the direct output file name is printf ("%s \", ptr->d_name);
            else if (Ptr->d_type = 4) {//=4 represents the directory, recursively gets all the file information in the directory printf ("%s", ptr->d_name);
        Print_dir (filenames); }
    }
}

There is a very important step: traversal must traverse all files in all directories, output directory name and filename. The main thing is a recursive process, layer by layer until a file is found to return to the next layer.

Above, is that I study linuxc sixth chapter of some notes, feel more important, error prone place also a lot of, want to see this blog if I think I have some place to write bad or wrong, can give me a reply, little woman grateful.

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.