Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active December 28, 2015 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeOam/2c7e4db4e29187f8e656 to your computer and use it in GitHub Desktop.
Save JeOam/2c7e4db4e29187f8e656 to your computer and use it in GitHub Desktop.
C Common Fuction

The GNU C Library


The table below shows the section numbers of the manual followed by the types of pages they contain.

1   Executable programs or shell commands
2   System calls (functions provided by the kernel)
3   Library calls (functions within program libraries)
4   Special files (usually found in /dev)
5   File formats and conventions eg /etc/passwd
6   Games
7   Miscellaneous  (including  macro  packages  and  conventions), e.g.
    man(7), groff(7)
8   System administration commands (usually only for root)
9   Kernel routines [Non standard]
@JeOam
Copy link
Author

JeOam commented Dec 20, 2015

int atoi(const char *str): converts the string argument str to an integer (type int).

@JeOam
Copy link
Author

JeOam commented Dec 20, 2015

#include <arpa/inet.h>
/*
 * converts the unsigned integer hostlong from host byte order to network byte order.
 */
uint32_t htonl(uint32_t hostlong);
/*
 * converts the unsigned short integer hostshort from host byte order to network byte order.
 */
uint16_t htons(uint16_t hostshort);
/*
 * convert IPv4 and IPv6 addresses from text to binary form
 */
int inet_pton(int af, const char *src, void *dst);
/*
 * convert IPv4 and IPv6 addresses from binary to text form
 */
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
  • pton = printable to numeric

@JeOam
Copy link
Author

JeOam commented Dec 20, 2015

size_t strlen(const char *str): computes the length of the string str up to, but not including the terminating null character.

@JeOam
Copy link
Author

JeOam commented Dec 20, 2015

int fputs(const char *str, FILE *stream): writes a string to the specified stream up to but not including the null character.

@JeOam
Copy link
Author

JeOam commented Dec 23, 2015

#include <unistd.h> 
int pause(void); 

The simple way to wait until a signal arrives is to call pause.

If the signal causes a handler function to be executed, then pause returns. This is considered an unsuccessful return (since “successful” behavior would be to suspend the program forever), so the return value is -1.

@JeOam
Copy link
Author

JeOam commented Dec 23, 2015

Get the process ID:

#include <unistd.h>
pid_t getpid(void);

@JeOam
Copy link
Author

JeOam commented Dec 23, 2015

Number of last error:

#include <errno.h>
errno

@JeOam
Copy link
Author

JeOam commented Dec 25, 2015

#include <sys/socket.h>
/*
 * returns the current address to which the socket sockfd is bound, in the buffer pointed to by addr. 
*/
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
/*
 * returns the address of the peer connected to the socket sockfd, in the buffer pointed to by addr.
 */
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

@JeOam
Copy link
Author

JeOam commented Dec 25, 2015

#include <pthread.h>
/*
 * The `pthread_create()` function starts a new thread in the calling process.  
 * The new thread starts execution by invoking `start_routine()`; arg is passed as the sole argument of `start_routine()`.
 */
int pthread_create(pthread_t *thread, 
                              const pthread_attr_t *attr,
                              void *(*start_routine) (void *), 
                              void *arg);
/*
 * obtain ID of the calling thread
 */
pthread_t pthread_self(void);
/*
 * marks the thread identified by thread as detached.  When a detached thread terminates, 
 * its resources are automatically released back to the system without the need for 
 * another thread to join with the terminated thread.
 */
int pthread_detach(pthread_t thread);

At any point in time, a thread is either joinable or detached (default state is joinable). Joinable threads must be reaped or killed by other threads (using pthread_join) in order to free memory resources. Detached threads cannot be reaped or killed by other threads, and resources are automatically reaped on termination. So unless threads need to synchronize among themselves, it is better to call

pthread_detach(pthread_self());

instead of pthread_join.

Refs: POSIX Threads

@JeOam
Copy link
Author

JeOam commented Dec 27, 2015

#include <stdio.h>
void perror(const char *s);
perror(str);

is equivalent to

if (str)
    fprintf(stderr, "%s: %s\n", str, strerror(errno));
else
    fprintf(stderr, "%s\n", strerror(errno));

@JeOam
Copy link
Author

JeOam commented Dec 27, 2015

include <bits/socket.h>
// Maximum queue length specifiable by listen.
SOMAXCONN

Find the SOMAXCONN definition:

find /usr/include -name \*.h -exec grep SOMAXCONN {} /dev/null \;

@JeOam
Copy link
Author

JeOam commented Dec 28, 2015

#include <sys/epoll.h> 
/*
 * 函数功能:操作某个epoll文件描述符;
 *
 * 参数:
 * epfd:由epoll_create创建的epoll文件描述符;
 * op:是操作方式,有以下三种操作方式:
 *      EPOLL_CTL_ADD   将fd注册到epfd中;
 *      EPOLL_CTL_MOD   修改已在epfd中注册的fd事件;
 *      EPOLL_CTL_DEL   将fd从epfd中删除;
 * fd:是关联的文件描述符;
 * event:指向struct epoll_event 结构,表示需要监听fd的某种事件;
 *
 * 返回值:若成功则返回0,否则出错返回-1;
 */
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
/*

 */
/* struct epoll_event 结构体定义如下 */
typedef union epoll_data {
void        *ptr;
int          fd;
uint32_t     u32;
uint64_t     u64;
} epoll_data_t;

struct epoll_event {
uint32_t     events;      /* Epoll events */
epoll_data_t data;        /* User data variable */
};
/*
 * 其中events有如下的取值:
 *  EPOLLIN         表示对应的文件描述符可读;
 *  EPOLLOUT        表示对应的文件描述符可写;
 *  EPOLLPRI        表示对应的文件描述符有紧急数据可读;
 *  EPOLLERR        表示对应的文件描述符发生错误;
 *  EPOLLHUP        表示对应的文件描述符被挂载;
 *  EPOLLET         表示将EPOLL设置为边缘触发模式(Edge Triggered);
 *  EPOLLONESHOT    表示只监听一次事件,当监听此次事件完成,若想继续监听,则需再次把该套接字描述符加入到EPOLL队列中;
 */
/*
 * 函数功能:收集在epoll监听事件中已发生的事件;
 *
 * 参数:
 * epfd:由epoll_create创建的epoll文件描述符;
 * events:指向epoll_event结构体,用于保存已发生的事件;
 * maxevents:每次能处理的最大事件数;
 * timeout:等待IO 事件发生的超时时间:-1相当于阻塞,即不会立即返回;0相当于非阻塞,即立即返回;
 *
 * 返回值:若成功则返回所发生的事件数,否则出错返回-1;
 */
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);

via epoll 解析

@JeOam
Copy link
Author

JeOam commented Dec 28, 2015

#include <netdb.h>
NI_MAXHOST
NI_MAXSERV

via getnameinfo(3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment