| 1 | <?xml version="1.0" encoding="iso-8859-1"?>
|
|---|
| 2 | <!DOCTYPE chapter PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc">
|
|---|
| 3 | <chapter id="CodingSuggestions">
|
|---|
| 4 | <chapterinfo>
|
|---|
| 5 | <author>
|
|---|
| 6 | <firstname>Steve</firstname><surname>French</surname>
|
|---|
| 7 | </author>
|
|---|
| 8 | <author>
|
|---|
| 9 | <firstname>Simo</firstname><surname>Sorce</surname>
|
|---|
| 10 | </author>
|
|---|
| 11 | <author>
|
|---|
| 12 | <firstname>Andrew</firstname><surname>Bartlett</surname>
|
|---|
| 13 | </author>
|
|---|
| 14 | <author>
|
|---|
| 15 | <firstname>Tim</firstname><surname>Potter</surname>
|
|---|
| 16 | </author>
|
|---|
| 17 | <author>
|
|---|
| 18 | <firstname>Martin</firstname><surname>Pool</surname>
|
|---|
| 19 | </author>
|
|---|
| 20 | </chapterinfo>
|
|---|
| 21 |
|
|---|
| 22 | <title>Coding Suggestions</title>
|
|---|
| 23 |
|
|---|
| 24 | <para>
|
|---|
| 25 | So you want to add code to Samba ...
|
|---|
| 26 | </para>
|
|---|
| 27 |
|
|---|
| 28 | <para>
|
|---|
| 29 | One of the daunting tasks facing a programmer attempting to write code for
|
|---|
| 30 | Samba is understanding the various coding conventions used by those most
|
|---|
| 31 | active in the project. These conventions were mostly unwritten and helped
|
|---|
| 32 | improve either the portability, stability or consistency of the code. This
|
|---|
| 33 | document will attempt to document a few of the more important coding
|
|---|
| 34 | practices used at this time on the Samba project. The coding practices are
|
|---|
| 35 | expected to change slightly over time, and even to grow as more is learned
|
|---|
| 36 | about obscure portability considerations. Two existing documents
|
|---|
| 37 | <filename>samba/source/internals.doc</filename> and
|
|---|
| 38 | <filename>samba/source/architecture.doc</filename> provide
|
|---|
| 39 | additional information.
|
|---|
| 40 | </para>
|
|---|
| 41 |
|
|---|
| 42 | <para>
|
|---|
| 43 | The loosely related question of coding style is very personal and this
|
|---|
| 44 | document does not attempt to address that subject, except to say that I
|
|---|
| 45 | have observed that eight character tabs seem to be preferred in Samba
|
|---|
| 46 | source. If you are interested in the topic of coding style, two oft-quoted
|
|---|
| 47 | documents are:
|
|---|
| 48 | </para>
|
|---|
| 49 |
|
|---|
| 50 | <para>
|
|---|
| 51 | <ulink url="http://lxr.linux.no/source/Documentation/CodingStyle">http://lxr.linux.no/source/Documentation/CodingStyle</ulink>
|
|---|
| 52 | </para>
|
|---|
| 53 |
|
|---|
| 54 | <para>
|
|---|
| 55 | <ulink url="http://www.fsf.org/prep/standards_toc.html">http://www.fsf.org/prep/standards_toc.html</ulink>
|
|---|
| 56 | </para>
|
|---|
| 57 |
|
|---|
| 58 | <para>
|
|---|
| 59 | But note that coding style in Samba varies due to the many different
|
|---|
| 60 | programmers who have contributed.
|
|---|
| 61 | </para>
|
|---|
| 62 |
|
|---|
| 63 | <para>
|
|---|
| 64 | Following are some considerations you should use when adding new code to
|
|---|
| 65 | Samba. First and foremost remember that:
|
|---|
| 66 | </para>
|
|---|
| 67 |
|
|---|
| 68 | <para>
|
|---|
| 69 | Portability is a primary consideration in adding function, as is network
|
|---|
| 70 | compatability with de facto, existing, real world CIFS/SMB implementations.
|
|---|
| 71 | There are lots of platforms that Samba builds on so use caution when adding
|
|---|
| 72 | a call to a library function that is not invoked in existing Samba code.
|
|---|
| 73 | Also note that there are many quite different SMB/CIFS clients that Samba
|
|---|
| 74 | tries to support, not all of which follow the SNIA CIFS Technical Reference
|
|---|
| 75 | (or the earlier Microsoft reference documents or the X/Open book on the SMB
|
|---|
| 76 | Standard) perfectly.
|
|---|
| 77 | </para>
|
|---|
| 78 |
|
|---|
| 79 | <para>
|
|---|
| 80 | Here are some other suggestions:
|
|---|
| 81 | </para>
|
|---|
| 82 |
|
|---|
| 83 | <orderedlist>
|
|---|
| 84 |
|
|---|
| 85 | <listitem><para>
|
|---|
| 86 | use d_printf instead of printf for display text
|
|---|
| 87 | reason: enable auto-substitution of translated language text
|
|---|
| 88 | </para></listitem>
|
|---|
| 89 |
|
|---|
| 90 | <listitem><para>
|
|---|
| 91 | use SAFE_FREE instead of free
|
|---|
| 92 | reason: reduce traps due to null pointers
|
|---|
| 93 | </para></listitem>
|
|---|
| 94 |
|
|---|
| 95 | <listitem><para>
|
|---|
| 96 | don't use bzero use memset, or ZERO_STRUCT and ZERO_STRUCTP macros
|
|---|
| 97 | reason: not POSIX
|
|---|
| 98 | </para></listitem>
|
|---|
| 99 |
|
|---|
| 100 | <listitem><para>
|
|---|
| 101 | don't use strcpy and strlen (use safe_* equivalents)
|
|---|
| 102 | reason: to avoid traps due to buffer overruns
|
|---|
| 103 | </para></listitem>
|
|---|
| 104 |
|
|---|
| 105 | <listitem><para>
|
|---|
| 106 | don't use getopt_long, use popt functions instead
|
|---|
| 107 | reason: portability
|
|---|
| 108 | </para></listitem>
|
|---|
| 109 |
|
|---|
| 110 | <listitem><para>
|
|---|
| 111 | explicitly add const qualifiers on parm passing in functions where parm
|
|---|
| 112 | is input only (somewhat controversial but const can be #defined away)
|
|---|
| 113 | </para></listitem>
|
|---|
| 114 |
|
|---|
| 115 | <listitem><para>
|
|---|
| 116 | when passing a va_list as an arg, or assigning one to another
|
|---|
| 117 | please use the VA_COPY() macro
|
|---|
| 118 | reason: on some platforms, va_list is a struct that must be
|
|---|
| 119 | initialized in each function...can SEGV if you don't.
|
|---|
| 120 | </para></listitem>
|
|---|
| 121 |
|
|---|
| 122 | <listitem><para>
|
|---|
| 123 | discourage use of threads
|
|---|
| 124 | reason: portability (also see architecture.doc)
|
|---|
| 125 | </para></listitem>
|
|---|
| 126 |
|
|---|
| 127 | <listitem><para>
|
|---|
| 128 | don't explicitly include new header files in C files - new h files
|
|---|
| 129 | should be included by adding them once to includes.h
|
|---|
| 130 | reason: consistency
|
|---|
| 131 | </para></listitem>
|
|---|
| 132 |
|
|---|
| 133 | <listitem><para>
|
|---|
| 134 | don't explicitly extern functions (they are autogenerated by
|
|---|
| 135 | "make proto" into proto.h)
|
|---|
| 136 | reason: consistency
|
|---|
| 137 | </para></listitem>
|
|---|
| 138 |
|
|---|
| 139 | <listitem><para>
|
|---|
| 140 | use endian safe macros when unpacking SMBs (see byteorder.h and
|
|---|
| 141 | internals.doc)
|
|---|
|
|---|