📜 ⬆️ ⬇️

Write on C as a gentleman


“Code Monkey like Fritos
Code Monkey like Tab and Mountain Dew
Code Monkey very simple man
With big warm fuzzy secret heart:
Code Monkey like you
Code Monkey like you »

- Jonathan Coulton - Code Monkey

I think that Jonathan Coulton's gorgeous song is familiar to many, and this is a life situation when “Rob say Code Monkey very diligent”, but “his output stink” and “his code not 'functional' or 'elegant'”.

The C language, which gave us so much useful software, was slowly squeezed out of the desktop and enterprise by such high-level giants as Java and C # and occupied the niche of system programming. And everything is good, but the system analysts are very repulsed peculiar guys. Tasks that sometimes arise before them, even with their formulation, are able to drive into horror mere mortals. In fact, just like some solutions.
')
Today we will talk about some useful practices that I learned from the depths of C system programming. Go.

Items will range from the most fundamental and obvious (targeted at novices in the C language) to the most specific, but useful. If you feel that you know it - scroll further.

Practice I: Observe the uniform Code Style and the fundamental principles of "good form"


The function accepts the INPUT variable as an argument, parses it into the IncomingValues ​​array, and returns result_to_return? Stop bydlokod!

The first thing that a beginner gives out is the failure to follow a single style of writing code within a particular application. Next comes the disregard for the rules of "good tone".

Here are some of the most common recommendations for C code design:




P.S. : , , , Code Style, . - .

II:


— .
, , , , :

  1. , , .

    file1.c, mySUPER_COOL_header.h ..
    main.c — , graph_const.h — .

  2. include.

    :

    • project/
      • common.c
      • common.h
      • main.c
      • network.h
      • networking.c
      • networking_v6.c
      • packet.c
      • packet.h
      • Makefile

    , . , 9 , , , 39. - . , — , GUI, , , Github/Gitlab/Bitbucket?

    , ? , :

    • project/
      • include/
        • common.h
        • network.h
        • packet.h

      • common.c
      • main.c
      • networking.c
      • networking_v6.c
      • packet.c
      • Makefile

    , include . Makefile (include , Makefile):

    @$(CC) $(OBJS) -o networkd -L$(ROMFS)/lib -linteraction -Wall -lpthread -I ./include

  3. .c .

    , , // — . , — .

  4. , . Makefile :

    .PHONY clean build
    build:
        cd sound/ && make clean && make 
        cd graphics/ && make clean && make
        cd engine/ && make clean && make
    sound:
        cd sound/ && make clean && make
    graphics:
        cd graphics/ && make clean && make
    engine:
        cd engine/ && make clean && make
    clean:
        cd sound/ && make clean
        cd engine/ && make clean
        cd greaphics/ && make clean

III: -


- (-) . , , , errno .

( — ), , «» . -, , , .

— . , (+ ) .

. — :

int sock_one = 0, sock_two = 0, sock_three = 0;
/*     ,   , 
 *     
 */
if ((socket_one = socket(AF_INET , SOCK_STREAM , 0)) <= 0) { 
    perror("socket one");
    exit(EXIT_ERROR_CODE);
}
if ((socket_two = socket(AF_INET , SOCK_DGRAM , 0)) <= 0) { 
    perror("socket two");
    exit(EXIT_ERROR_CODE);
}
if ((socket_three = socket(PF_INET , SOCK_RAW , 0)) <= 0) { 
    perror("socket three");
    exit(EXIT_ERROR_CODE);
} 

, , ? .

/* -  ... */
int Socket(int domain, int type, int proto) {
    int desk = socket(domain, type, proto);
    if (desk <= 0) {
        perror("socket");
        exit(EXIT_ERROR_CODE);
    }
    return desk;
}
/* ......... n   -    ......... */
int socket_one = 0, socket_two = 0, soket_three = 0;
socket_one = Socket(AF_INET , SOCK_STREAM , 0);
socket_two = Socket(AF_INET , SOCK_DGRAM , 0);
socket_three = Socket(PF_INET , SOCK_RAW , 0);

, - ( «» ), .

, . , .

, , , . — :)

IV: keywords


keywords . , , . , — , .

, , . , , . :


. — .

V: . valgrind.


, , , .

Valgrind — , , . , , , , , , . .
+ .

.

VI: ,


busybox 1.21. , busybox, -.

UPD: «» busybox. themiron , , — , . «» busybox, .

, busybox.

busybox , . , — .

busybox. udhcpc — DHCP :





, , , . .

, . , . . .

.

, !

Source: https://habr.com/ru/post/325678/


All Articles