diff options
-rw-r--r-- | dev/c/lib.html | 344 | ||||
-rw-r--r-- | dev/index.html | 2 |
2 files changed, 218 insertions, 128 deletions
diff --git a/dev/c/lib.html b/dev/c/lib.html index d1c9c28..fb4682b 100644 --- a/dev/c/lib.html +++ b/dev/c/lib.html @@ -1,167 +1,255 @@ <!DOCTYPE html> <html dir="ltr" lang="en"> <head> - <meta charset='utf-8'> - <title>Libraries</title> + <meta charset='utf-8'> + <title>Libraries</title> </head> <body> - <a href="../index.html">Development Index</a> + <a href="../index.html">Development Index</a> - <h1>Libraries</h1> + <h1>Libraries</h1> - <h2>Basic</h2> + <h2 id="basic">Basic</h2> - <dl> - <dt>include <unistd.h></dt> - <dd>fork, pipe and I/O primitives (read, write, close, etc.) - + primitve types like uid_t, pid_t etc</dd> + <dl> + <dt>include <unistd.h></dt> + <dd>fork, pipe and I/O primitives (read, write, close, etc.) + + primitve types like uid_t, pid_t etc</dd> - <dt>#include <stdlib.h></dt> - <dd>Standard lib, contains primitves for number conversion - and memory allocation</dd> - <dt>#include <stdio.h></dt> - <dd>Basic i/o lib: printf etc</dd> + <dt>#include <stdlib.h></dt> + <dd>Standard lib, contains primitves for number conversion + and memory allocation</dd> + <dt>#include <stdio.h></dt> + <dd>Basic i/o lib: printf etc</dd> + <dd>glibc</dd> - <dt>#include <string.h></dt> - <dd>String manipulations</dd> + <dt>#include <string.h></dt> + <dd>String manipulations</dd> + <dd>glibc</dd> - <dt>#include <time.h></dt> - <dd>Time related functions</dd> + <dt>#include <time.h></dt> + <dd>Time related functions</dd> + <dd>glibc</dd> - <dt>#include <signal.h></dt> - <dd>Signal handling</dd> + <dt>#include <signal.h></dt> + <dd>Signal handling</dd> + <dd>glibc</dd> - <dt>#include <stdbool.h></dt> - <dd>Boolean type</dd> + <dt>#include <stdbool.h></dt> + <dd>Boolean type</dd> + <dd>glibc</dd> - <dt>#include <math.h></dt> - <dd>Math functions</dd> - </dl> + <dt>#include <math.h></dt> + <dd>Math functions</dd> + <dd>glibc</dd> + </dl> - <h2>Advanced</h2> - <dl> - <dt>#include <sys/socket.h></dt> - <dd> socket connections</dd> - <dt>#include <sys/types.h></dt> - <dd> primitive types like uid_t, pid_t etc</dd> + <h2 id="advanced">Advanced</h2> - <dt>#include <netinet/in.h></dt> - <dd> internet address family</dd> + <dl> + <dt>#include <sys/socket.h></dt> + <dd>Socket connections</dd> + <dd>glibc</dd> - <dt>#include <arpa/inet.h></dt> - <dd> definitions for internet operations</dd> + <dt>#include <sys/types.h></dt> + <dd>Primitive types like uid_t, pid_t etc</dd> + <dd>glibc</dd> - <dt>#include <pthread.h></dt> - <dd> threads</dd> + <dt>#include <netinet/in.h></dt> + <dd> internet address family</dd> + <dd>glibc</dd> - <dt>#include <stdatomic.h></dt> - <dd> mutual exclusion locks</dd> + <dt>#include <arpa/inet.h></dt> + <dd> definitions for internet operations</dd> + <dd>glibc</dd> - </dl> + <dt>#include <pthread.h></dt> + <dd> threads</dd> + <dd>glibc</dd> - <h2>Stdio</h2> + <dt>#include <stdatomic.h></dt> + <dd> mutual exclusion locks</dd> + <dd>gcc</dd> + </dl> - <h3>File Modes</h3> + <h2 id="random-numbers">Random Numbers</h2> + <pre> + // seed with current time: + // time_t t; + // srand(time(&t)); + void srand(unsigned seed); - <pre> - mode Description - "r" Opens a file for reading. The file must exist. - "w" Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file. - "a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist. - "r" Opens a file to update both reading and writing. The file must exist. - "w" Creates an empty file for both reading and writing. - "a" Opens a file for reading and appending. - </pre> + int rand(void); + </pre> - <pre> - // ERROR: result == NULL (also when EOF is reached) - char *fgets(char *str, int strlen, FILE *stream); + <h2 id="signals">Signals</h2> - // ERROR: result == NULL - FILE *fopen(const char *filename, const char *mode); + <pre> + sig_t signal(int sig, sig_t func); - // result == EOF when finished reading the stream - // SUCCESS: number of matched items on success - int fscanf(FILE *stream, const char *format, ...); + int raise(int sig); - // ERROR: result == EOF - // SUCCESS: result == 0 - int fclose(FILE *stream); + int kill(pid_t pid, int sig); + </pre> - // ERROR: result == -1 - // SUCCESS: number of bytes written - ssize_t write(int fildes, const void *buf, size_t nbyte); + <h2 id="sorting">Sorting</h2> - // ERROR: result == -1 - // EOF when finished reading - // SUCCESS: number of bytes read - ssize_t read(int fildes, void *buf, size_t nbyte); + <pre> + void qsort(void* values, size_t num_items, size_t item_size, int (*comparefunc)(const void*, const void*)); + </pre> - // SUCCESS: result > 0 - // ERROR: result == EOF - int fputs(const char *restrict s, FILE *restrict stream); - </pre> + <h2 id="strings">Strings</h2> - <h2>Strings</h2> + <pre> + // SUCCESS: pointer to destination string + char *strcpy(char *dest, const char *src); - <pre> - // SUCCESS: pointer to destination string - char *strcpy(char *dest, const char *src); + // result == 0 -> strings are equal + // result < 0 -> str1 less than str2 + // result > 0 -> str2 less than str1 + int strcmp(const char *str1, const char *str2);; - // result == 0 -> strings are equal - // result < 0 -> str1 less than str2 - // result > 0 -> str2 less than str1 - int strcmp(const char *str1, const char *str2);; + // result == NULL -> no tokens left to retrieve + // else: pointer to last token found + char *strtok(char *str, const char *delim); + + // NULL if no match + // else: pointer to first occurence in string + char *strstr(const char *string, const char *substring); + + // ERROR: result == 0 + int atoi(const char *str); + </pre> + + <h2 id="inter-process-communication">Inter Process Communication</h2> + <pre> + // ERROR: result < 0 + // result = 0 inside child + // result > 0 inside parent + pid_t fork(void); + + // ERROR: result < 0 + // SUCCESS: result == 0 + int pipe(int fd[2]); + </pre> + + <h2 id="file-io">File IO</h2> + <h3>File Modes</h3> + + <pre> + mode Description + "r" Opens a file for reading. The file must exist. + "w" Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file. + "a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist. + "r" Opens a file to update both reading and writing. The file must exist. + "w" Creates an empty file for both reading and writing. + "a" Opens a file for reading and appending. + </pre> + + <pre> + // ERROR: result == NULL (also when EOF is reached) + char *fgets(char *str, int strlen, FILE *stream); + + // ERROR: result == NULL + FILE *fopen(const char *filename, const char *mode); + + // result == EOF when finished reading the stream + // SUCCESS: number of matched items on success + int fscanf(FILE *stream, const char *format, ...); + + // ERROR: result == EOF + // SUCCESS: result == 0 + int fclose(FILE *stream); + + // ERROR: result == -1 + // SUCCESS: number of bytes written + ssize_t write(int fildes, const void *buf, size_t nbyte); + + // ERROR: result == -1 + // EOF when finished reading + // SUCCESS: number of bytes read + ssize_t read(int fildes, void *buf, size_t nbyte); + + // SUCCESS: result > 0 + // ERROR: result == EOF + int fputs(const char *restrict s, FILE *restrict stream); + </pre> - // result == NULL -> no tokens left to retrieve - // else: pointer to last token found - char *strtok(char *str, const char *delim); + <h2 id="shared-memory">Shared Memory</h2> - // NULL if no match - // else: pointer to first occurence in string - char *strstr(const char *string, const char *substring); - - // ERROR: result == 0 - int atoi(const char *str); - </pre> - - <h2>Networking</h2> - - <pre> - // ERROR: result == -1 - // example: sock = socket(AF_INET, SOCK_STREAM, 0); - int socket(int domain, int type, int protocol); - - // sockaddr_in struct (man ip 4) - struct sockaddr_in server; - server.sin_family = PF_INET; - server.sin_addr.s_addr = INADDR_ANY; - server.sin_port = htons(8080); - - // ERROR: result < 0 - // SUCCESS: result == 0 - // example: bind(sock, (struct sockaddr *) &server, sizeof(server)) - int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); - - // ERROR: result < 0 - // SUCCESS: filedeskriptor for accepted socket - // example: fd = accept(sock, (struct sockaddr *) &client, &client_len); - int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); - - // SUCCESS: result == 0 - // ERROR: result == -1 - int listen(int socket, int backlog); - </pre> - - <a href="../index.html">Development Index</a> - - <p> - This is part of the c9-doc Manual. - Copyright (C) 2018 - c9 team. - See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> - for copying conditions.</p> + <pre> + // ERROR: result < 0 + // SUCCESS: result == shmid + int shmget(key_t key, size_t size, int shmflg); + + // ERROR: result == NULL + void *shmat(int shmid, const void *shmaddr, int shmflg); + + // ERROR: result < 0 + // SUCCESS: result == 0 + int shmdt(const void *shmaddr); + </pre> + + <h2 id="networking">Networking</h2> + + <pre> + $ pkginfo -o socket.h + </pre> + + <pre> + #include "<sys/socket.h>" + </pre> + + <pre> + // ERROR: result == -1 + // example: sock = socket(AF_INET, SOCK_STREAM, 0); + int socket(int domain, int type, int protocol); + + // sockaddr_in struct (man ip 4) + struct sockaddr_in server; + server.sin_family = PF_INET; + server.sin_addr.s_addr = INADDR_ANY; + server.sin_port = htons(8080); + + // ERROR: result < 0 + // SUCCESS: result == 0 + // example: bind(sock, (struct sockaddr *) &server, sizeof(server)) + int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); + + // ERROR: result < 0 + // SUCCESS: filedeskriptor for accepted socket + // example: fd = accept(sock, (struct sockaddr *) &client, &client_len); + int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); + + // SUCCESS: result == 0 + // ERROR: result == -1 + int listen(int socket, int backlog); + </pre> + + <h2 id="threads">Threads</h2> + + <pre> + // ERROR: result > 0 + // SUCCESS: res == 0 + // example: pthread_create(&threads[t], NULL, printHello, (void*)t); + int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); + + // SUCCESS: result == 0 + // ERROR: result > 0 + int pthread_join(pthread_t tid, void **ret); + + void pthread_exit(void *value_ptr); + </pre> + + <a href="../index.html">Development Index</a> + + <p> + This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a> + for copying conditions.</p> </body> </html> diff --git a/dev/index.html b/dev/index.html index ab00a3e..1f11b84 100644 --- a/dev/index.html +++ b/dev/index.html @@ -80,6 +80,8 @@ <li><a href="">Input & Output</a></li> <li><a href="c/lib.html">Libraries</a> <ul> + <li><a href="c/lib.html#basic">Basic libraries</a></li> + <li><a href="c/lib.html#advanced">Advanced libraries</a></li> <li><a href="c/lib.html#random-numbers">Random Numbers</a></li> <li><a href="c/lib.html#signals">Signals</a></li> <li><a href="c/lib.html#sorting">Sorting</a></li> |