From 5d7a23e5d38048d72243a18b60b6f5f7f3d805d2 Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Wed, 7 Mar 2018 15:37:11 +0000 Subject: dev c revision --- dev/c/elements.html | 78 ++++++++++++++++++ dev/c/index.html | 229 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 240 insertions(+), 67 deletions(-) create mode 100644 dev/c/elements.html (limited to 'dev/c') diff --git a/dev/c/elements.html b/dev/c/elements.html new file mode 100644 index 0000000..d6950d2 --- /dev/null +++ b/dev/c/elements.html @@ -0,0 +1,78 @@ + + + + + Types & Elements + + + Development Index + +

Types & Elements

+ +

Types

+ +
+
char
+
Integer, one byte.
+
int
+
Integer.
+
float
+
Single precision floating point.
+
double
+
Double precision floating point.
+
void
+
Absence of type.
+
+ +

Identifiers

+ +

Variable names, functions, data types and preprocessor macros. + Are allowed letters, decimal digits and underscore.

+ +

Keywords

+ +

Reserved identifiers part of C language.;

+ +

auto break case char const continue default do double else enum + extern float for goto if int long register return short signed + sizeof static struct switch typedef union unsigned void volatile + while

+ +

restrict inline _Bool _Complex _Imaginary

+ +

__FUNCTION__ __PRETTY_FUNCTION__ __alignof __alignof__ __asm + __asm__ __attribute __attribute__ __builtin_offsetof __builtin_va_arg + __complex __complex__ __const __extension__ __func__ __imag __imag__ + __inline __inline__ __label__ __null __real __real__ + __restrict __restrict__ __signed __signed__ __thread __typeof + __volatile __volatile__

+ +

Constants

+ +

Constants are identifiers with fixed values that can't change + during execution. Data types of constants; integer, character, + double, strings.

+ +

Operators

+ +

Operators do operations such as; addition or subtraction.

+ +

Separators

+ +

Language elements separators;

+

{} [] () ; , . :

+ +

White Space

+ +

White space is ignored except when separating elements.

+ + Development Index + +

+ This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

+ + diff --git a/dev/c/index.html b/dev/c/index.html index 1c3b478..3e8651f 100644 --- a/dev/c/index.html +++ b/dev/c/index.html @@ -1,102 +1,197 @@ - - C & GDB + + C & GDB - Development Index + Development Index -

C & GDB

+

C & GDB

-

C program with autotools -

+

Hello World

-
-        $ touch NEWS README AUTHORS ChangeLog
-        $ mkdir -p src/bin src/lib
-        
+

Create file hello.c with;

-

- GDB Quick Start, - Learning C with GDB - and Memory Layout and the Stack - are great sources of introductory information. -

+
+	#include <stdio.h>
 
-        

To use gdb you need to compile program with -g flag. To - debug a program;

+ int main() { + printf("Hello World!"); + return 0; + } +
-
-        gdb program
-        
+

Compile;

-

If the program needs arguments you can set it;

+
+	$ gcc -Wall hello.c -o hello
+	
-
-        (gdb)set args -parameter1 -parameter2
-        
+

Run;

-

To start the program you can type run, this way gdb - will try to run the program until the end. If program - crash, gdb will stop it for debuging.

+
+	$./hello
+	Hello World!
+	
-
-        (gdb) run
-        
+

Multiple Sources

-
-        n - execute next line
-        s - step in next line
-        b - backtrace
-        info locals
-        print
-        x
-        
+

To organize code in multiple files split above + example in main.c, hello.c and hello.h. Content of + main.c;

-

SysCalls

+
+	#include "hello.h"
 
-        
catch syscall open
+ int main() { + hello("world"); + return 0; + } +
-

Threads

+

Header file contains declaration of the function hello, + content of hello.h;

-

When new thread is created you receive - a notification. To get information about - threads;

+
+	void hello(const char* name);
+	
-
-        info threads
-        
+

Implementation of hello function in hello.c;

-

To select thread;

+
+	#include <stdio.h>
+	#include "hello.h"
 
-        
-        thread 1
-        
+ void hello(const char* name) { + printf("Hello, %s!\n", name); + } +
+ +

Compile;

+ +
+	$ gcc -Wall main.c hello.c -o hello
+	
+ +

Makefile

+ +

Make reads a Makefile by default on current directory, + Makefile defines targets, for example executables and their + dependencies, for example object files and source files.

+ +

Create Makefile;

+ +
+	CC=gcc
+	CFLAGS=-Wall
+
+	hello: main.o hello.o
 
-        

Stopping and Starting - multi-thread programs

+ clean: + rm -f hello main.o hello.o +
+ +
+	$ touch NEWS README AUTHORS ChangeLog
+	
+ +

Debug

+ +

To use gdb you need to compile program with -g flag. Change + Makefile

+ +
+	CC=gcc
+	CFLAGS=-Wall -g
+
+	hello: main.o hello.o
+
+	clean:
+		rm -f hello main.o hello.o
+	
+ +
+	$ gdb hello
+	
+ +

Set break point;

+ +
+	(gdb) break main
+	
+ +

To start the program you can type run, this way gdb + will try to run the program until the end. If program + crash, gdb will stop it for debuging. Start program;

+ +
+	(gdb) run
+	
+ +

Step in next line;

+ +
+	(gdb) s
+	
+ +

Execute next line;

+ +
+	(gdb) n
+	
+ +

To start gdb in TUI mode press;

+ +
+
Ctrl-x A
+
Enter or leave TUI.
+
Ctrl-x 1
+
TUI with only one window.
+
Ctrl-x 2
+
TUI with more than two windows.
+
Ctrl-x o
+
Change active window.
+
Ctrl-x s
+
TUI single key mode.
+
Ctrl-L s
+
Refresh screen.
+
Up
+
Scroll
+
Down
+
Scroll
+
Left
+
Scroll
+
Right
+
Scroll
+
+ +

Run again and step in hello function, to print + variable "name" value and type;

-        break linespec thread threadno
+        (gdb) print name
+        $1 = 0x4005b0 "world"
+        (gdb)
         
-

Strace

+

Print variable name type;

-        strace -c ./program
+        (gdb) ptype name
+        type = const char *
+        (gdb)
         
- Development Index -

- This is part of the c9-doc Manual. - Copyright (C) 2016 - c9 team. - See the file Gnu Free Documentation License - for copying conditions.

- +

Variable is a string constant, + continue reading Elements.

+ Development Index +

+ This is part of the c9-doc Manual. + Copyright (C) 2016 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

- - -- cgit 1.4.1-2-gfad0 From 21613ef6f34273fd50876f153f9899dab7eccada Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Wed, 7 Mar 2018 16:58:05 +0000 Subject: dev c added datatypes and debugging --- dev/c/datatypes.html | 35 +++++++++++++++++++++++++++ dev/c/debugging.html | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ dev/index.html | 1 + 3 files changed, 103 insertions(+) create mode 100644 dev/c/datatypes.html create mode 100644 dev/c/debugging.html (limited to 'dev/c') diff --git a/dev/c/datatypes.html b/dev/c/datatypes.html new file mode 100644 index 0000000..6d6b76d --- /dev/null +++ b/dev/c/datatypes.html @@ -0,0 +1,35 @@ + + + + + Datatypes + + + Development Index + +

Datatypes

+ +

Data types

+ +

Integer

+

Real number

+

Complex number

+

Enumeration

+

Unions

+

Structures

+

Arrays

+

Pointers

+

Incomplete types

+

Type qualifiers

+

Storage class

+ + Development Index + +

+ This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

+ + diff --git a/dev/c/debugging.html b/dev/c/debugging.html new file mode 100644 index 0000000..d1f425b --- /dev/null +++ b/dev/c/debugging.html @@ -0,0 +1,67 @@ + + + + + Debugging + + + Development Index + +

Debugging

+ +

If the program needs arguments you can set it;

+ +
+        (gdb)set args -parameter1 -parameter2
+        
+ + +
+        b - backtrace
+        info locals
+        print
+        x
+        catch syscall open
+        
+ +

When new thread is created you receive + a notification. To get information about + threads;

+ +
+        info threads
+        
+ +

To select thread;

+ +
+        thread 1
+        
+ +
+        break linespec thread threadno
+        
+ +
+        strace -c ./program
+        
+ + +

C program with autotools + GDB Quick Start, + Learning C with GDB + and Memory Layout and the Stack + are great sources of introductory information. + Stopping and Starting + multi-thread programs

+ + Development Index + +

+ This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

+ + diff --git a/dev/index.html b/dev/index.html index 2dcc209..d84fa36 100644 --- a/dev/index.html +++ b/dev/index.html @@ -77,6 +77,7 @@
  • Control Flow
  • Functions
  • Input & Output
  • +
  • Debugging
  • System Development
  • -- cgit 1.4.1-2-gfad0 From 52f5effca30b476b427c2d3a7518c91c0e292fa4 Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Wed, 7 Mar 2018 22:33:00 +0000 Subject: dev c datatypes initial document --- dev/c/datatypes.html | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++- dev/index.html | 2 +- 2 files changed, 107 insertions(+), 2 deletions(-) (limited to 'dev/c') diff --git a/dev/c/datatypes.html b/dev/c/datatypes.html index 6d6b76d..a149c24 100644 --- a/dev/c/datatypes.html +++ b/dev/c/datatypes.html @@ -12,12 +12,117 @@

    Data types

    Integer

    +

    Allowed types are char and int;

    +
    +
    signed char
    +
    8 bit, from -128 to 127.
    +
    unsigned char
    +
    8 bit, from 0 to 255.
    +
    char
    +
    8 bit, used to store ASCII, ex; 'h', '\n', etc...
    +
    short int
    +
    16 bit, from -32,768 to 32,767.
    +
    unsigned short int
    +
    16 bit, from 0 to 65,535.
    +
    int
    +
    32 bit, from -2,147,483,648 to 2,147,483,647.
    +
    unsigned int
    +
    32 bit, from 0 to 4,294,967,295.
    +
    long int
    +
    32 bit, from -2,147,483,648 to 2,147,483,647.
    +
    unsigned long int
    +
    32 bit, from 0 to 4,294,967,295.
    +
    long long int
    +
    64 bit, from 9,223,372,036,854,775,808 + to 9,223,372,036,854,775,807.
    +
    unsigned long long int
    +
    64 bit, from 0 to 18,446,744,073,709,551,615.
    +
    +

    Real number

    + +

    Real numbers are fractional numbers, all floating points + are signed. Check float.h.

    + +
    +
    float
    +
    From 1e-37 to 1e37
    +
    double
    +
    Largest of floating point.
    +
    long double
    +
    The long double data maybe larger than double
    +
    +

    Complex number

    -

    Enumeration

    + +

    Complex Number Types

    + +

    Enumeration

    + +
    +        enum colors {black, orange, blue} color;
    +        enum colors color;
    +        
    +

    Unions

    + +
    +        union accounts {
    +            int id;
    +            float value;
    +        } first_account, second_account;
    +        
    +

    Structures

    + +
    +        struct point {
    +            int x, y;
    +        } first_point;
    +        struct point second_point;
    +        
    +

    Arrays

    + +
    +        int account_array[9];
    +        int account_array[] = {0, 1, 2, 3};
    +        int account_array[] = {0, 1, 2, [9] = 9};
    +        
    + +
    +        int account_array[2][9] { {1,2,3},{9,8,7} }
    +        
    + +

    Initializing string with individual characters the + null character must be defined;

    + +
    +        char black[] = {'b', 'l', 'a', 'c', 'k', '\0'}
    +        
    + +
    +        char black[] = "black";
    +        
    + +
    +        union accounts {
    +            int id;
    +            float value;
    +        };
    +        union accounts accounts_array[9];
    +        accounts_array[0].id = 8;
    +        
    + +
    +        struct point {
    +            int x, y;
    +        };
    +        struct point point_array[9];
    +        point_array[0].x=2;
    +        point_array[0].y=3;
    +        
    +

    Pointers

    Incomplete types

    Type qualifiers

    diff --git a/dev/index.html b/dev/index.html index d84fa36..afd147e 100644 --- a/dev/index.html +++ b/dev/index.html @@ -62,7 +62,7 @@
  • Integer
  • Real number
  • Complex number
  • -
  • Enumeration
  • +
  • Enumeration
  • Unions
  • Structures
  • Arrays
  • -- cgit 1.4.1-2-gfad0 From 77fe64b172a6d1847ec80b6fa34ac06816a4030d Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Mon, 12 Mar 2018 14:34:16 +0000 Subject: dev c added initial libraries --- dev/c/datatypes.html | 44 ++++++- dev/c/debugging.html | 32 +++++ dev/c/elements.html | 19 +-- dev/c/index.html | 44 ++----- dev/c/lib.html | 167 ++++++++++++++++++++++++++ dev/index.html | 324 +++++++++++++++++++++++++++------------------------ 6 files changed, 419 insertions(+), 211 deletions(-) create mode 100644 dev/c/lib.html (limited to 'dev/c') diff --git a/dev/c/datatypes.html b/dev/c/datatypes.html index a149c24..35b6ca4 100644 --- a/dev/c/datatypes.html +++ b/dev/c/datatypes.html @@ -9,6 +9,22 @@

    Datatypes

    +

    Types

    + +
    +
    char
    +
    Integer, one byte.
    +
    int
    +
    Integer.
    +
    float
    +
    Single precision floating point.
    +
    double
    +
    Double precision floating point.
    +
    void
    +
    Absence of type.
    +
    + +

    Data types

    Integer

    @@ -128,13 +144,35 @@

    Type qualifiers

    Storage class

    - Development Index +

    Format Type Specifiers

    -

    - This is part of the c9-doc Manual. +

    +
    %c
    +
    Character
    +
    %s
    +
    String of characters
    +
    %d
    +
    Decimal integer
    +
    %f
    +
    Decimal floating point
    +
    %llu
    +
    unsigned long long
    +
    %o
    +
    Signed octal
    +
    %u
    +
    Unsigned decimal integer
    +
    %x
    +
    Unsigned hexadecimal integer
    +
    %p
    +
    Pointer address
    +
    + + Development Index +

    This is part of the c9 Manual. Copyright (C) 2018 c9 team. See the file Gnu Free Documentation License for copying conditions.

    + diff --git a/dev/c/debugging.html b/dev/c/debugging.html index d1f425b..4e5931f 100644 --- a/dev/c/debugging.html +++ b/dev/c/debugging.html @@ -15,10 +15,42 @@ (gdb)set args -parameter1 -parameter2 +

    To start gdb in TUI mode press;

    + +
    +
    Ctrl-x A
    +
    Enter or leave TUI.
    +
    Ctrl-x 0
    +
    TUI with only one window.
    +
    Ctrl-x 2
    +
    TUI with more than two windows.
    +
    Ctrl-x o
    +
    Change active window.
    +
    Ctrl-x s
    +
    TUI single key mode.
    +
    Ctrl-L s
    +
    Refresh screen.
    +
    Up
    +
    Scroll
    +
    Down
    +
    Scroll
    +
    Left
    +
    Scroll
    +
    Right
    +
    Scroll
    +
    + +
    +        (gdb) info win
    +        (gdb) fs next
    +        (gdb) fs SRC
    +        
    +
             b - backtrace
             info locals
    +        display
             print
             x
             catch syscall open
    diff --git a/dev/c/elements.html b/dev/c/elements.html
    index d6950d2..c5eb87e 100644
    --- a/dev/c/elements.html
    +++ b/dev/c/elements.html
    @@ -2,27 +2,12 @@
     
         
             
    -        Types & Elements
    +        Elements
         
         
             Development Index
     
    -        

    Types & Elements

    - -

    Types

    - -
    -
    char
    -
    Integer, one byte.
    -
    int
    -
    Integer.
    -
    float
    -
    Single precision floating point.
    -
    double
    -
    Double precision floating point.
    -
    void
    -
    Absence of type.
    -
    +

    Elements

    Identifiers

    diff --git a/dev/c/index.html b/dev/c/index.html index 3e8651f..aeff035 100644 --- a/dev/c/index.html +++ b/dev/c/index.html @@ -135,39 +135,7 @@ (gdb) s
    -

    Execute next line;

    - -
    -	(gdb) n
    -	
    - -

    To start gdb in TUI mode press;

    - -
    -
    Ctrl-x A
    -
    Enter or leave TUI.
    -
    Ctrl-x 1
    -
    TUI with only one window.
    -
    Ctrl-x 2
    -
    TUI with more than two windows.
    -
    Ctrl-x o
    -
    Change active window.
    -
    Ctrl-x s
    -
    TUI single key mode.
    -
    Ctrl-L s
    -
    Refresh screen.
    -
    Up
    -
    Scroll
    -
    Down
    -
    Scroll
    -
    Left
    -
    Scroll
    -
    Right
    -
    Scroll
    -
    - -

    Run again and step in hello function, to print - variable "name" value and type;

    +

    Print variable "name" value;

             (gdb) print name
    @@ -175,7 +143,7 @@
             (gdb)
             
    -

    Print variable name type;

    +

    Print variable "name" type;

             (gdb) ptype name
    @@ -183,8 +151,12 @@
             (gdb)
             
    -

    Variable is a string constant, - continue reading Elements.

    +

    Variable is a string constant. + Execute next line to end;

    + +
    +	(gdb) n
    +	
    Development Index

    diff --git a/dev/c/lib.html b/dev/c/lib.html new file mode 100644 index 0000000..d1c9c28 --- /dev/null +++ b/dev/c/lib.html @@ -0,0 +1,167 @@ + + + + + Libraries + + + Development Index + + +

    Libraries

    + +

    Basic

    + +
    +
    include <unistd.h>
    +
    fork, pipe and I/O primitives (read, write, close, etc.) + + primitve types like uid_t, pid_t etc
    + +
    #include <stdlib.h>
    +
    Standard lib, contains primitves for number conversion + and memory allocation
    +
    #include <stdio.h>
    +
    Basic i/o lib: printf etc
    + +
    #include <string.h>
    +
    String manipulations
    + +
    #include <time.h>
    +
    Time related functions
    + +
    #include <signal.h>
    +
    Signal handling
    + +
    #include <stdbool.h>
    +
    Boolean type
    + +
    #include <math.h>
    +
    Math functions
    +
    + +

    Advanced

    +
    +
    #include <sys/socket.h>
    +
    socket connections
    +
    #include <sys/types.h>
    +
    primitive types like uid_t, pid_t etc
    + +
    #include <netinet/in.h>
    +
    internet address family
    + +
    #include <arpa/inet.h>
    +
    definitions for internet operations
    + +
    #include <pthread.h>
    +
    threads
    + +
    #include <stdatomic.h>
    +
    mutual exclusion locks
    + +
    + +

    Stdio

    + +

    File Modes

    + +
    +	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.
    +	
    + +
    +	// 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);
    +	
    + +

    Strings

    + +
    +	// 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 == 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);
    +	
    + +

    Networking

    + +
    +	// 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);
    +	
    + + Development Index + +

    + This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

    + + diff --git a/dev/index.html b/dev/index.html index afd147e..ab00a3e 100644 --- a/dev/index.html +++ b/dev/index.html @@ -1,164 +1,178 @@ - - Development + + Development - Documentation Index -

    Development

    - -

    Tools for development and debugging

    - -

    Git

    - -

    Git is a distributed version control system, this document is meant to be distributed using git.

    - - - -

    C & GDB

    - -

    C is functional compiled language created by Dennis Ritchie. BSD, Linux and Minix kernels use this language - as primary language.

    - - - - - -

    Shell Script

    - -

    Script files that start with "#!/bin/sh" use dash (in crux), - /bin/sh is a link to dash, while files that start with "#!/bin/bash" - use bash;

    - - - -

    Python

    - - -

    Perl

    - - - -

    JavaScript

    - - -

    PHP

    - - - - - Documentation Index -

    - This is part of the c9-doc Manual. - Copyright (C) 2017 - c9 team. - See the file Gnu Free Documentation License - for copying conditions.

    - - + Documentation Index +

    Development

    + +

    Tools for development and debugging

    + +

    Git

    + +

    Git is a distributed version control system, this document is meant to be distributed using git.

    + + + +

    C & GDB

    + +

    C is functional compiled language created by Dennis Ritchie. BSD, Linux and Minix kernels use this language + as primary language.

    + + + + + +

    Shell Script

    + +

    Script files that start with "#!/bin/sh" use dash (in crux), + /bin/sh is a link to dash, while files that start with "#!/bin/bash" + use bash;

    + + + +

    Python

    + + +

    Perl

    + + + +

    JavaScript

    + + +

    PHP

    + + + + + Documentation Index +

    + This is part of the c9-doc Manual. + Copyright (C) 2017 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

    -- cgit 1.4.1-2-gfad0 From 7867e4ea5ca255b610d69a4f47d45083717ac542 Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Tue, 3 Apr 2018 02:12:08 +0100 Subject: dev c lib and index revision --- dev/c/lib.html | 344 ++++++++++++++++++++++++++++++++++++--------------------- dev/index.html | 2 + 2 files changed, 218 insertions(+), 128 deletions(-) (limited to 'dev/c') 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 @@ - - Libraries + + Libraries - Development Index + Development Index -

    Libraries

    +

    Libraries

    -

    Basic

    +

    Basic

    -
    -
    include <unistd.h>
    -
    fork, pipe and I/O primitives (read, write, close, etc.) - + primitve types like uid_t, pid_t etc
    +
    +
    include <unistd.h>
    +
    fork, pipe and I/O primitives (read, write, close, etc.) + + primitve types like uid_t, pid_t etc
    -
    #include <stdlib.h>
    -
    Standard lib, contains primitves for number conversion - and memory allocation
    -
    #include <stdio.h>
    -
    Basic i/o lib: printf etc
    +
    #include <stdlib.h>
    +
    Standard lib, contains primitves for number conversion + and memory allocation
    +
    #include <stdio.h>
    +
    Basic i/o lib: printf etc
    +
    glibc
    -
    #include <string.h>
    -
    String manipulations
    +
    #include <string.h>
    +
    String manipulations
    +
    glibc
    -
    #include <time.h>
    -
    Time related functions
    +
    #include <time.h>
    +
    Time related functions
    +
    glibc
    -
    #include <signal.h>
    -
    Signal handling
    +
    #include <signal.h>
    +
    Signal handling
    +
    glibc
    -
    #include <stdbool.h>
    -
    Boolean type
    +
    #include <stdbool.h>
    +
    Boolean type
    +
    glibc
    -
    #include <math.h>
    -
    Math functions
    -
    +
    #include <math.h>
    +
    Math functions
    +
    glibc
    +
    -

    Advanced

    -
    -
    #include <sys/socket.h>
    -
    socket connections
    -
    #include <sys/types.h>
    -
    primitive types like uid_t, pid_t etc
    +

    Advanced

    -
    #include <netinet/in.h>
    -
    internet address family
    +
    +
    #include <sys/socket.h>
    +
    Socket connections
    +
    glibc
    -
    #include <arpa/inet.h>
    -
    definitions for internet operations
    +
    #include <sys/types.h>
    +
    Primitive types like uid_t, pid_t etc
    +
    glibc
    -
    #include <pthread.h>
    -
    threads
    +
    #include <netinet/in.h>
    +
    internet address family
    +
    glibc
    -
    #include <stdatomic.h>
    -
    mutual exclusion locks
    +
    #include <arpa/inet.h>
    +
    definitions for internet operations
    +
    glibc
    -
    +
    #include <pthread.h>
    +
    threads
    +
    glibc
    -

    Stdio

    +
    #include <stdatomic.h>
    +
    mutual exclusion locks
    +
    gcc
    +
    -

    File Modes

    +

    Random Numbers

    +
    +        // seed with current time:
    +        // time_t t;
    +        // srand(time(&t));
    +        void srand(unsigned seed);
     
    -	
    -	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.
    -	
    + int rand(void); +
    -
    -	// ERROR: result == NULL (also when EOF is reached)
    -	char *fgets(char *str, int strlen, FILE *stream);
    +        

    Signals

    - // ERROR: result == NULL - FILE *fopen(const char *filename, const char *mode); +
    +        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);
    +        
    - // ERROR: result == -1 - // SUCCESS: number of bytes written - ssize_t write(int fildes, const void *buf, size_t nbyte); +

    Sorting

    - // ERROR: result == -1 - // EOF when finished reading - // SUCCESS: number of bytes read - ssize_t read(int fildes, void *buf, size_t nbyte); +
    +        void qsort(void* values, size_t num_items, size_t item_size, int (*comparefunc)(const void*, const void*));
    +        
    - // SUCCESS: result > 0 - // ERROR: result == EOF - int fputs(const char *restrict s, FILE *restrict stream); -
    +

    Strings

    -

    Strings

    +
    +        // SUCCESS: pointer to destination string
    +        char *strcpy(char *dest, const char *src);
     
    -	
    -	// 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);
    +        
    + +

    Inter Process Communication

    +
    +        // 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]);
    +        
    + +

    File IO

    +

    File Modes

    + +
    +        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.
    +        
    + +
    +        // 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);
    +        
    - // result == NULL -> no tokens left to retrieve - // else: pointer to last token found - char *strtok(char *str, const char *delim); +

    Shared Memory

    - // 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); -
    - -

    Networking

    - -
    -	// 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);
    -	
    - - Development Index - -

    - This is part of the c9-doc Manual. - Copyright (C) 2018 - c9 team. - See the file Gnu Free Documentation License - for copying conditions.

    +
    +        // 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);
    +        
    + +

    Networking

    + +
    +        $ pkginfo -o socket.h
    +        
    + +
    +        #include "<sys/socket.h>"
    +        
    + +
    +        // 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);
    +        
    + +

    Threads

    + +
    +        // 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);
    +        
    + + Development Index + +

    + This is part of the c9-doc Manual. + Copyright (C) 2018 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

    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 @@
  • Input & Output
  • Libraries