Linux C Programming Skills

Source: Internet
Author: User
Tags htons
Creation: 16:54:00
Author: unlinux
From: http://www.Unlinux.com

1. Get the file information:
Stat (char * filename, struct stat * BUF );
Struct stat {
Dev_t st_dev;/* Device */
Ino_t st_ino;/* node */
Mode_t st_mode;/* mode */
Nlink_t st_nlink;/* hard connection */
Uid_t st_uid;/* User ID */
Gid_t st_gid;/* Group ID */
Dev_t st_rdev;/* device type */
Off_t st_off;/* Number of file bytes */
Unsigned long st_blksize;/* block size */
Unsigned long st_blocks;/* number of blocks */
Time_t st_atime;/* last access time */
Time_t st_mtime;/* last modification time */
Time_t st_ctime;/* last change time (attribute )*/
};

Struct statfs
{
Long f_type;/* file system type */
Long f_bsize;/* block size */
Long f_blocks;/* number of blocks */
Long f_bfree;/* idle block ()*/
Long f_bavail;/* Available block */
Long f_files;/* total file nodes */
Long f_ffree;/* idle file node */
Fsid_t f_fsid;/* File System ID */
Long f_namelen;/* Maximum File Name Length */
Long f_spare [6];/* spare for later */
};

2. Get the file access permission or determine whether the file exists:
Int access (char * filename, int mode );

3. Get the current time:
Time_t t; char * asctime (localtime (& T ));
Or
Time (& T); char * ctime (& T );
The string format is wed Mar 12 10:07:53 2003.

4. Calculate the time difference between two moments
Double difftime (time_t time2, time_t time1 );

5. delete an object:
Int unlink (char * pathname );
Int remove (char * pathname );

6. delete a directory:
Int rmdir (const char * pathname );

7. Obtain the name of the current directory:
Char * getcwd (char * Buf, size_t size); Buf returns the current path name.

8. Get the directory information:
Dir * opendir (const char * pathname );
Int closedir (dir * DIR );
Struct dirent * readdir (dir * DIR );
Struct dirent
{
Long d_ino;/* inode Number */
Off_t d_off;/* offset to this dirent */
Unsigned short d_reclen;/* length of this d_name */
Char d_name [name_max + 1];/* file name (null-terminated )*/
};

9. strerror (errno); the function returns a string with the specified error message.

10. Obtain the number of all files (including directories) in the current path.
Struct dirent ** namelist;
Int num = scandir (".", & namelist, 0, alphasort)

11./etc/lD. So. conf: location where shared libraries are searched
View the shared libraries called by the execution File
Shell> ldd a. Out
Shared library management tool. This command is generally run after the shared library is updated.
Shell> ldconfig

12. view the file execution speed
Shell> time./A. Out

13. Change the file access permission
Int chmod (const char * path, mode_t mode );

14. Change the file size
Int chsize (INT handle, long size );

15. convert a floating point number to a string
Char ECVT (double value, int ndigit, int * decpt, int * sign );

16. Check file termination
Int EOF (int * handle );

17. Check the file Terminator on the stream
Int feof (File * stream );

18. Detect stream errors
Int ferror (File * stream );

19. Load and run functions of other programs
Int execl (char * pathname, char * arg0, arg1,..., argn, null );
Int execle (char * pathname, char * arg0, arg1,..., argn, null,
Char * envp []);
Int execlp (char * pathname, char * arg0, arg1,..., null );
Int execple (char * pathname, char * arg0, arg1,..., null,
Char * envp []);
Int execv (char * pathname, char * argv []);
Int execve (char * pathname, char * argv [], char * envp []);
Int execvp (char * pathname, char * argv []);
Int execvpe (char * pathname, char * argv [], char * envp []);

20. exponential functions
Double exp (Double X );

21. struct sockaddr
{
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14 bytes of Protocol address */
};
Struct sockaddr_in
{
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* Internet address */
Unsigned char sin_zero [8];/* same size as struct sockaddr */
};
Struct in_addr
{
Unsigned long s_addr;
};
S_addr stores IP addresses in byte sequence.
Sin_zero is an empty byte reserved to keep sockaddr and sockaddr_in data structures of the same size.

Example:
Struct sockaddr_in SA;
SA. sin_family = af_inet;
SA. sin_port = htons (3490 );
SA. sin_addr.s_addr = inet_addr ("132.241.5.10 ");
Bzero (& (SA. sin_zero), 8 );
NOTE: If SA. sin_addr.s_addr = inaddr_any, no IP address is specified (for server programs)

22. # define unix_path_max 108.
Struct sockaddr_un
{
Sa_family_t sun_family;/* af_unix */
Char sun_path [unix_path_max];/* path */
};

23. IP address conversion function:
Unsigned long inet_addr (const char * CP );
Inet_addr converts a dot-decimal IP address string to a 32-bit IP address (Network byte order)

Char * inet_ntoa (struct in_addr in );
Inet_ntoa converts a 32-bit IP address into a dotted-decimal IP address string.

These two functions are inverse functions.

Byte sequence conversion:
Htons () -- "host to network short"
Htonl () -- "host to network long"
Ntohs () -- "network to host short"
Ntohl () -- "network to host long"

24. Obtain the CPU and memory usage of the Current Machine
Getrusage

25. Common flags and mode parameters for open
Int file_flag = o_wronly | o_append | o_creat;
Int file_mode = s_irusr | s_iwusr | s_irgrp | s_iroth;

26. Commonly Used symbols in makefile:
Predefine variable meaning
$ * The name of the target file that does not contain the extension.
$ @ Complete target name
$ % If the target is an archive member, this variable indicates the name of the target archive member. For example, if the target name
For mytarget. So (image. O), $ @ is mytarget. So, and $ % is image. O.

$ + All dependent files are separated by spaces and appear sequentially, which may contain repeated dependent files.
$ <Name of the first dependent file.
$? All dependent files are separated by spaces. The modified date of these dependent files is later than the object creation date.
$ ^ All dependent files are separated by spaces and do not contain duplicate dependent files.

The name of the Ar archive maintenance program. The default value is ar.
Arflags archive maintenance program options.
The name of the as assembler. The default value is.
Asflags assembler options.
The name of the cc c compiler. The default value is CC.
Ccflags C compiler options.
Name of the cpp c pre-compiler. The default value is $ (CC)-E.
Cppflags C pre-compiled options.
The name of the cxx C ++ compiler. The default value is g ++.
Cxxflags C ++ compiler options.
The name of the FC Fortran compiler. The default value is f77.
Fflags Fortran compiler options.

Use the variable object to represent all. O files:
Objects: = $ (wildcard *. O)

Make-N or -- just-print indicates that only the command is displayed, but the command is not executed.
Make-S or -- slient indicates that the command display is completely forbidden.
Make-I or -- ignore-errors indicates that all commands in makefile will ignore the error.
Make-K or -- keep-going indicates that if the command in a rule has an error, the execution of the rule will be terminated, but other rules will continue to be executed.

Get the variable plat directly using shell in makefile
Use a method in make to define variables. This method uses the ": =" Operator
Plat: = $ (shell uname-)

To define a variable whose value is a space, we can:
Nullstring: =
Space: = $ (nullstring) # End of the line

Foo? = Bar:
If Foo has not been defined, the value of the variable foo is bar. If Foo has been previously defined, this statement will not do anything, which is equivalent:
Ifeq ($ (origin Foo), undefined)
Foo = bar
Endif

Foo: = A. o B. O C. O
Bar: = $ (FOO:. O =. c)
We first define a "$ (FOO)" variable, and the second line means to take all the values in "$ (FOO)" as ". O "string" end "is replaced with". C ", so the value of" $ (bar) "is". c B. c. C ".

If you want to set the parameter value in makefile, you can use the "Override" indicator. Its syntax is:
Override <variable >=< value>
Override <variable >:= <value>
Of course, you can also append:
Override <variable> + = <more text>

The system environment variables during the make operation can be loaded into the MAKEFILE file when the make operation starts,
However, if the makefile has defined this variable, or the variable is imported by the make command line,
Then the system environment variable value will be overwritten.
If make specifies the "-e" parameter, the system environment variable overwrites the variable defined in makefile.
Reprinted from: http://www.unlinux.com/doc/program/20051026/2391.html

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.