about summary refs log tree commit diff stats
path: root/dev/c/lib.html
blob: f5f6accf1f4183852625c51e61fcf913583b315c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
        <meta charset='utf-8'>
        <title>Libraries</title>
    </head>
    <body>
        <a href="index.html">C Index</a>


        <h1>Libraries</h1>

        <h2 id="basic">Basic</h2>

        <dl>
            <dt>@include &lt;unistd.h&gt;</dt>
            <dd>fork, pipe and I/O primitives (read, write, close, etc.)
            + primitve types like uid_t, pid_t etc</dd>

            <dt>#include &lt;stdlib.h&gt;</dt>
            <dd>Standard lib, contains primitives for number conversion
            and memory allocation</dd>
            <dt>#include &lt;stdio.h&gt;</dt>
            <dd>Basic i/o lib: printf etc</dd>
            <dd>glibc</dd>

            <dt>#include &lt;string.h&gt;</dt>
            <dd>String manipulations</dd>
            <dd>glibc</dd>

            <dt>#include &lt;time.h&gt;</dt>
            <dd>Time related functions</dd>
            <dd>glibc</dd>

            <dt>#include &lt;signal.h&gt;</dt>
            <dd>Signal handling</dd>
            <dd>glibc</dd>

            <dt>#include &lt;stdbool.h&gt;</dt>
            <dd>Boolean type</dd>
            <dd>glibc</dd>

            <dt>#include &lt;math.h&gt;</dt>
            <dd>Math functions</dd>
            <dd>glibc</dd>
        </dl>

        <h2 id="advanced">Advanced</h2>

        <dl>
            <dt>#include &lt;sys/socket.h&gt;</dt>
            <dd>Socket connections</dd>
            <dd>glibc</dd>

            <dt>#include &lt;sys/types.h&gt;</dt>
            <dd>Primitive types like uid_t, pid_t etc</dd>
            <dd>glibc</dd>

            <dt>#include &lt;netinet/in.h&gt;</dt>
            <dd> internet address family</dd>
            <dd>glibc</dd>

            <dt>#include &lt;arpa/inet.h&gt;</dt>
            <dd> definitions for internet operations</dd>
            <dd>glibc</dd>

            <dt>#include &lt;pthread.h&gt;</dt>
            <dd> threads</dd>
            <dd>glibc</dd>

            <dt>#include &lt;stdatomic.h&gt;</dt>
            <dd> mutual exclusion locks</dd>
            <dd>gcc</dd>
        </dl>

        <h2 id="random-numbers">Random Numbers</h2>
        <pre>
        // seed with current time:
        // time_t t;
        // srand(time(&t));
        void srand(unsigned seed);

        int rand(void);
        </pre>

        <h2 id="signals">Signals</h2>

        <pre>
        sig_t signal(int sig, sig_t func);

        int raise(int sig);

        int kill(pid_t pid, int sig);
        </pre>

        <h2 id="sorting">Sorting</h2>

        <pre>
        void qsort(void* values, size_t num_items, size_t item_size, int (*comparefunc)(const void*, const void*));
        </pre>

        <h2 id="strings">Strings</h2>

        <pre>
        // SUCCESS: pointer to destination string
        char *strcpy(char *dest, const char *src);

        // result == 0 -&gt; strings are equal
        // result &lt; 0 -&gt; str1 less than str2
        // result &gt; 0 -&gt; str2 less than str1
        int strcmp(const char *str1, const char *str2);;

        // result == NULL -&gt; 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 &lt; 0
        // result = 0 inside child
        // result &gt; 0 inside parent
        pid_t fork(void);

        // ERROR: result &lt; 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 &gt; 0
        // ERROR: result == EOF
        int fputs(const char *restrict s, FILE *restrict stream);
        </pre>

        <h2 id="shared-memory">Shared Memory</h2>

        <pre>
        // ERROR: result &lt; 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 &lt; 0
        // SUCCESS: result == 0
        int shmdt(const void *shmaddr);
        </pre>

        <h2 id="networking">Networking</h2>

        <pre>
        $ pkginfo -o socket.h
        </pre>

        <pre>
        #include "&lt;sys/socket.h&gt;"
        </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 &lt; 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 &lt; 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 &gt; 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 &gt; 0
        int pthread_join(pthread_t tid, void **ret);

        void pthread_exit(void *value_ptr);
        </pre>

        <a href="index.html">C Index</a>

        <p>
        This is part of the Tribu System Documentation.
        Copyright (C) 2020
        Tribu Team.
        See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
        for copying conditions.</p>
    </body>
</html>