113498266Sopenharmony_ci# curl C code style 213498266Sopenharmony_ci 313498266Sopenharmony_ciSource code that has a common style is easier to read than code that uses 413498266Sopenharmony_cidifferent styles in different places. It helps making the code feel like one 513498266Sopenharmony_cisingle code base. Easy-to-read is an important property of code and helps 613498266Sopenharmony_cimaking it easier to review when new things are added and it helps debugging 713498266Sopenharmony_cicode when developers are trying to figure out why things go wrong. A unified 813498266Sopenharmony_cistyle is more important than individual contributors having their own personal 913498266Sopenharmony_citastes satisfied. 1013498266Sopenharmony_ci 1113498266Sopenharmony_ciOur C code has a few style rules. Most of them are verified and upheld by the 1213498266Sopenharmony_ci`scripts/checksrc.pl` script. Invoked with `make checksrc` or even by default 1313498266Sopenharmony_ciby the build system when built after `./configure --enable-debug` has been 1413498266Sopenharmony_ciused. 1513498266Sopenharmony_ci 1613498266Sopenharmony_ciIt is normally not a problem for anyone to follow the guidelines, as you just 1713498266Sopenharmony_cineed to copy the style already used in the source code and there are no 1813498266Sopenharmony_ciparticularly unusual rules in our set of rules. 1913498266Sopenharmony_ci 2013498266Sopenharmony_ciWe also work hard on writing code that are warning-free on all the major 2113498266Sopenharmony_ciplatforms and in general on as many platforms as possible. Code that obviously 2213498266Sopenharmony_ciwill cause warnings will not be accepted as-is. 2313498266Sopenharmony_ci 2413498266Sopenharmony_ci## Naming 2513498266Sopenharmony_ci 2613498266Sopenharmony_ciTry using a non-confusing naming scheme for your new functions and variable 2713498266Sopenharmony_cinames. It does not necessarily have to mean that you should use the same as in 2813498266Sopenharmony_ciother places of the code, just that the names should be logical, 2913498266Sopenharmony_ciunderstandable and be named according to what they are used for. File-local 3013498266Sopenharmony_cifunctions should be made static. We like lower case names. 3113498266Sopenharmony_ci 3213498266Sopenharmony_ciSee the [INTERNALS](https://curl.se/dev/internals.html#symbols) document on 3313498266Sopenharmony_cihow we name non-exported library-global symbols. 3413498266Sopenharmony_ci 3513498266Sopenharmony_ci## Indenting 3613498266Sopenharmony_ci 3713498266Sopenharmony_ciWe use only spaces for indentation, never TABs. We use two spaces for each new 3813498266Sopenharmony_ciopen brace. 3913498266Sopenharmony_ci 4013498266Sopenharmony_ci```c 4113498266Sopenharmony_ciif(something_is_true) { 4213498266Sopenharmony_ci while(second_statement == fine) { 4313498266Sopenharmony_ci moo(); 4413498266Sopenharmony_ci } 4513498266Sopenharmony_ci} 4613498266Sopenharmony_ci``` 4713498266Sopenharmony_ci 4813498266Sopenharmony_ci## Comments 4913498266Sopenharmony_ci 5013498266Sopenharmony_ciSince we write C89 code, **//** comments are not allowed. They were not 5113498266Sopenharmony_ciintroduced in the C standard until C99. We use only __/* comments */__. 5213498266Sopenharmony_ci 5313498266Sopenharmony_ci```c 5413498266Sopenharmony_ci/* this is a comment */ 5513498266Sopenharmony_ci``` 5613498266Sopenharmony_ci 5713498266Sopenharmony_ci## Long lines 5813498266Sopenharmony_ci 5913498266Sopenharmony_ciSource code in curl may never be wider than 79 columns and there are two 6013498266Sopenharmony_cireasons for maintaining this even in the modern era of large and high 6113498266Sopenharmony_ciresolution screens: 6213498266Sopenharmony_ci 6313498266Sopenharmony_ci1. Narrower columns are easier to read than wide ones. There is a reason 6413498266Sopenharmony_ci newspapers have used columns for decades or centuries. 6513498266Sopenharmony_ci 6613498266Sopenharmony_ci2. Narrower columns allow developers to easier show multiple pieces of code 6713498266Sopenharmony_ci next to each other in different windows. It allows two or three source 6813498266Sopenharmony_ci code windows next to each other on the same screen - as well as multiple 6913498266Sopenharmony_ci terminal and debugging windows. 7013498266Sopenharmony_ci 7113498266Sopenharmony_ci## Braces 7213498266Sopenharmony_ci 7313498266Sopenharmony_ciIn if/while/do/for expressions, we write the open brace on the same line as 7413498266Sopenharmony_cithe keyword and we then set the closing brace on the same indentation level as 7513498266Sopenharmony_cithe initial keyword. Like this: 7613498266Sopenharmony_ci 7713498266Sopenharmony_ci```c 7813498266Sopenharmony_ciif(age < 40) { 7913498266Sopenharmony_ci /* clearly a youngster */ 8013498266Sopenharmony_ci} 8113498266Sopenharmony_ci``` 8213498266Sopenharmony_ci 8313498266Sopenharmony_ciYou may omit the braces if they would contain only a one-line statement: 8413498266Sopenharmony_ci 8513498266Sopenharmony_ci```c 8613498266Sopenharmony_ciif(!x) 8713498266Sopenharmony_ci continue; 8813498266Sopenharmony_ci``` 8913498266Sopenharmony_ci 9013498266Sopenharmony_ciFor functions the opening brace should be on a separate line: 9113498266Sopenharmony_ci 9213498266Sopenharmony_ci```c 9313498266Sopenharmony_ciint main(int argc, char **argv) 9413498266Sopenharmony_ci{ 9513498266Sopenharmony_ci return 1; 9613498266Sopenharmony_ci} 9713498266Sopenharmony_ci``` 9813498266Sopenharmony_ci 9913498266Sopenharmony_ci## 'else' on the following line 10013498266Sopenharmony_ci 10113498266Sopenharmony_ciWhen adding an **else** clause to a conditional expression using braces, we 10213498266Sopenharmony_ciadd it on a new line after the closing brace. Like this: 10313498266Sopenharmony_ci 10413498266Sopenharmony_ci```c 10513498266Sopenharmony_ciif(age < 40) { 10613498266Sopenharmony_ci /* clearly a youngster */ 10713498266Sopenharmony_ci} 10813498266Sopenharmony_cielse { 10913498266Sopenharmony_ci /* probably grumpy */ 11013498266Sopenharmony_ci} 11113498266Sopenharmony_ci``` 11213498266Sopenharmony_ci 11313498266Sopenharmony_ci## No space before parentheses 11413498266Sopenharmony_ci 11513498266Sopenharmony_ciWhen writing expressions using if/while/do/for, there shall be no space 11613498266Sopenharmony_cibetween the keyword and the open parenthesis. Like this: 11713498266Sopenharmony_ci 11813498266Sopenharmony_ci```c 11913498266Sopenharmony_ciwhile(1) { 12013498266Sopenharmony_ci /* loop forever */ 12113498266Sopenharmony_ci} 12213498266Sopenharmony_ci``` 12313498266Sopenharmony_ci 12413498266Sopenharmony_ci## Use boolean conditions 12513498266Sopenharmony_ci 12613498266Sopenharmony_ciRather than test a conditional value such as a bool against TRUE or FALSE, a 12713498266Sopenharmony_cipointer against NULL or != NULL and an int against zero or not zero in 12813498266Sopenharmony_ciif/while conditions we prefer: 12913498266Sopenharmony_ci 13013498266Sopenharmony_ci```c 13113498266Sopenharmony_ciresult = do_something(); 13213498266Sopenharmony_ciif(!result) { 13313498266Sopenharmony_ci /* something went wrong */ 13413498266Sopenharmony_ci return result; 13513498266Sopenharmony_ci} 13613498266Sopenharmony_ci``` 13713498266Sopenharmony_ci 13813498266Sopenharmony_ci## No assignments in conditions 13913498266Sopenharmony_ci 14013498266Sopenharmony_ciTo increase readability and reduce complexity of conditionals, we avoid 14113498266Sopenharmony_ciassigning variables within if/while conditions. We frown upon this style: 14213498266Sopenharmony_ci 14313498266Sopenharmony_ci```c 14413498266Sopenharmony_ciif((ptr = malloc(100)) == NULL) 14513498266Sopenharmony_ci return NULL; 14613498266Sopenharmony_ci``` 14713498266Sopenharmony_ci 14813498266Sopenharmony_ciand instead we encourage the above version to be spelled out more clearly: 14913498266Sopenharmony_ci 15013498266Sopenharmony_ci```c 15113498266Sopenharmony_ciptr = malloc(100); 15213498266Sopenharmony_ciif(!ptr) 15313498266Sopenharmony_ci return NULL; 15413498266Sopenharmony_ci``` 15513498266Sopenharmony_ci 15613498266Sopenharmony_ci## New block on a new line 15713498266Sopenharmony_ci 15813498266Sopenharmony_ciWe never write multiple statements on the same source line, even for short 15913498266Sopenharmony_ciif() conditions. 16013498266Sopenharmony_ci 16113498266Sopenharmony_ci```c 16213498266Sopenharmony_ciif(a) 16313498266Sopenharmony_ci return TRUE; 16413498266Sopenharmony_cielse if(b) 16513498266Sopenharmony_ci return FALSE; 16613498266Sopenharmony_ci``` 16713498266Sopenharmony_ci 16813498266Sopenharmony_ciand NEVER: 16913498266Sopenharmony_ci 17013498266Sopenharmony_ci```c 17113498266Sopenharmony_ciif(a) return TRUE; 17213498266Sopenharmony_cielse if(b) return FALSE; 17313498266Sopenharmony_ci``` 17413498266Sopenharmony_ci 17513498266Sopenharmony_ci## Space around operators 17613498266Sopenharmony_ci 17713498266Sopenharmony_ciPlease use spaces on both sides of operators in C expressions. Postfix **(), 17813498266Sopenharmony_ci[], ->, ., ++, --** and Unary **+, -, !, ~, &** operators excluded they should 17913498266Sopenharmony_cihave no space. 18013498266Sopenharmony_ci 18113498266Sopenharmony_ciExamples: 18213498266Sopenharmony_ci 18313498266Sopenharmony_ci```c 18413498266Sopenharmony_cibla = func(); 18513498266Sopenharmony_ciwho = name[0]; 18613498266Sopenharmony_ciage += 1; 18713498266Sopenharmony_citrue = !false; 18813498266Sopenharmony_cisize += -2 + 3 * (a + b); 18913498266Sopenharmony_ciptr->member = a++; 19013498266Sopenharmony_cistruct.field = b--; 19113498266Sopenharmony_ciptr = &address; 19213498266Sopenharmony_cicontents = *pointer; 19313498266Sopenharmony_cicomplement = ~bits; 19413498266Sopenharmony_ciempty = (!*string) ? TRUE : FALSE; 19513498266Sopenharmony_ci``` 19613498266Sopenharmony_ci 19713498266Sopenharmony_ci## No parentheses for return values 19813498266Sopenharmony_ci 19913498266Sopenharmony_ciWe use the 'return' statement without extra parentheses around the value: 20013498266Sopenharmony_ci 20113498266Sopenharmony_ci```c 20213498266Sopenharmony_ciint works(void) 20313498266Sopenharmony_ci{ 20413498266Sopenharmony_ci return TRUE; 20513498266Sopenharmony_ci} 20613498266Sopenharmony_ci``` 20713498266Sopenharmony_ci 20813498266Sopenharmony_ci## Parentheses for sizeof arguments 20913498266Sopenharmony_ci 21013498266Sopenharmony_ciWhen using the sizeof operator in code, we prefer it to be written with 21113498266Sopenharmony_ciparentheses around its argument: 21213498266Sopenharmony_ci 21313498266Sopenharmony_ci```c 21413498266Sopenharmony_ciint size = sizeof(int); 21513498266Sopenharmony_ci``` 21613498266Sopenharmony_ci 21713498266Sopenharmony_ci## Column alignment 21813498266Sopenharmony_ci 21913498266Sopenharmony_ciSome statements cannot be completed on a single line because the line would be 22013498266Sopenharmony_citoo long, the statement too hard to read, or due to other style guidelines 22113498266Sopenharmony_ciabove. In such a case the statement will span multiple lines. 22213498266Sopenharmony_ci 22313498266Sopenharmony_ciIf a continuation line is part of an expression or sub-expression then you 22413498266Sopenharmony_cishould align on the appropriate column so that it is easy to tell what part of 22513498266Sopenharmony_cithe statement it is. Operators should not start continuation lines. In other 22613498266Sopenharmony_cicases follow the 2-space indent guideline. Here are some examples from 22713498266Sopenharmony_cilibcurl: 22813498266Sopenharmony_ci 22913498266Sopenharmony_ci```c 23013498266Sopenharmony_ciif(Curl_pipeline_wanted(handle->multi, CURLPIPE_HTTP1) && 23113498266Sopenharmony_ci (handle->set.httpversion != CURL_HTTP_VERSION_1_0) && 23213498266Sopenharmony_ci (handle->set.httpreq == HTTPREQ_GET || 23313498266Sopenharmony_ci handle->set.httpreq == HTTPREQ_HEAD)) 23413498266Sopenharmony_ci /* did not ask for HTTP/1.0 and a GET or HEAD */ 23513498266Sopenharmony_ci return TRUE; 23613498266Sopenharmony_ci``` 23713498266Sopenharmony_ci 23813498266Sopenharmony_ciIf no parenthesis, use the default indent: 23913498266Sopenharmony_ci 24013498266Sopenharmony_ci```c 24113498266Sopenharmony_cidata->set.http_disable_hostname_check_before_authentication = 24213498266Sopenharmony_ci (0 != va_arg(param, long)) ? TRUE : FALSE; 24313498266Sopenharmony_ci``` 24413498266Sopenharmony_ci 24513498266Sopenharmony_ciFunction invoke with an open parenthesis: 24613498266Sopenharmony_ci 24713498266Sopenharmony_ci```c 24813498266Sopenharmony_ciif(option) { 24913498266Sopenharmony_ci result = parse_login_details(option, strlen(option), 25013498266Sopenharmony_ci (userp ? &user : NULL), 25113498266Sopenharmony_ci (passwdp ? &passwd : NULL), 25213498266Sopenharmony_ci NULL); 25313498266Sopenharmony_ci} 25413498266Sopenharmony_ci``` 25513498266Sopenharmony_ci 25613498266Sopenharmony_ciAlign with the "current open" parenthesis: 25713498266Sopenharmony_ci 25813498266Sopenharmony_ci```c 25913498266Sopenharmony_ciDEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing " 26013498266Sopenharmony_ci "server response left\n", 26113498266Sopenharmony_ci (int)clipamount)); 26213498266Sopenharmony_ci``` 26313498266Sopenharmony_ci 26413498266Sopenharmony_ci## Platform dependent code 26513498266Sopenharmony_ci 26613498266Sopenharmony_ciUse **#ifdef HAVE_FEATURE** to do conditional code. We avoid checking for 26713498266Sopenharmony_ciparticular operating systems or hardware in the #ifdef lines. The HAVE_FEATURE 26813498266Sopenharmony_cishall be generated by the configure script for unix-like systems and they are 26913498266Sopenharmony_cihard-coded in the `config-[system].h` files for the others. 27013498266Sopenharmony_ci 27113498266Sopenharmony_ciWe also encourage use of macros/functions that possibly are empty or defined 27213498266Sopenharmony_cito constants when libcurl is built without that feature, to make the code 27313498266Sopenharmony_ciseamless. Like this example where the **magic()** function works differently 27413498266Sopenharmony_cidepending on a build-time conditional: 27513498266Sopenharmony_ci 27613498266Sopenharmony_ci```c 27713498266Sopenharmony_ci#ifdef HAVE_MAGIC 27813498266Sopenharmony_civoid magic(int a) 27913498266Sopenharmony_ci{ 28013498266Sopenharmony_ci return a + 2; 28113498266Sopenharmony_ci} 28213498266Sopenharmony_ci#else 28313498266Sopenharmony_ci#define magic(x) 1 28413498266Sopenharmony_ci#endif 28513498266Sopenharmony_ci 28613498266Sopenharmony_ciint content = magic(3); 28713498266Sopenharmony_ci``` 28813498266Sopenharmony_ci 28913498266Sopenharmony_ci## No typedefed structs 29013498266Sopenharmony_ci 29113498266Sopenharmony_ciUse structs by all means, but do not typedef them. Use the `struct name` way 29213498266Sopenharmony_ciof identifying them: 29313498266Sopenharmony_ci 29413498266Sopenharmony_ci```c 29513498266Sopenharmony_cistruct something { 29613498266Sopenharmony_ci void *valid; 29713498266Sopenharmony_ci size_t way_to_write; 29813498266Sopenharmony_ci}; 29913498266Sopenharmony_cistruct something instance; 30013498266Sopenharmony_ci``` 30113498266Sopenharmony_ci 30213498266Sopenharmony_ci**Not okay**: 30313498266Sopenharmony_ci 30413498266Sopenharmony_ci```c 30513498266Sopenharmony_citypedef struct { 30613498266Sopenharmony_ci void *wrong; 30713498266Sopenharmony_ci size_t way_to_write; 30813498266Sopenharmony_ci} something; 30913498266Sopenharmony_cisomething instance; 31013498266Sopenharmony_ci``` 311