1. Си / Говнокод #18091

    +142

    1. 1
    2. 2
    3. 3
    4. 4
    fstat(fd, &s);
    count = s.st_size % sizeof(uint32_t) == 0 ? s.st_size : s.st_size + (sizeof(uint32_t) - s.st_size % sizeof(uint32_t));
    buf = malloc(count);
    /* etc */

    Собственно, round up to nearest 32-bit word. Нет ли метода покрасивше?

    codemonkey, 30 Апреля 2015

    Комментарии (0)
  2. Си / Говнокод #18080

    +147

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    void Write_Char(char ch)
    {
    	switch(ch)
    	{
    	 case 'a':
    	 {
    		 set_mode_data;
    		 set_db4_0;
    		 set_db5_1;
    		 set_db6_1;
    		 set_db7_0;
    		 strobe();
    		 set_mode_data;
    		 set_db4_1;
    		 set_db5_0;
    		 set_db6_0;
    		 set_db7_0;
    		 strobe();
    		 return;
    	 }
    	 case 'b':
    	 	 {
    	 		 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_1;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_1;
    	 		 set_db6_0;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 return;
    	 	 }
    	 case 'c':
    	 	 {
    	 		 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_1;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 set_mode_data;
    	 		 set_db4_1;
    	 		 set_db5_1;
    	 		 set_db6_0;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 return;
    	 	 }
    	 case 'd':
    	 	 {
    	 		 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_1;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_0;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 return;
    	 	 }
    	 case 'e':
    	 	 {
    	 		 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_1;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 set_mode_data;
    	 		 set_db4_1;
    	 		 set_db5_0;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 return;
    	 	 }
    	 case 'f':
    	 	 {	 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_1;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 set_mode_data;
    	 		 set_db4_0;
    	 		 set_db5_1;
    	 		 set_db6_1;
    	 		 set_db7_0;
    	 		 strobe();
    	 		 return;
    	 	 }
    //еще ~800 строк.

    Ну почему тут только 100 строк можно вводить? Я хочу поделиться этим со всем миром, ведь это так легко - наговнокодить на Си.(из программы общения с дисплеем hd44780 для STM32)

    Fantom, 28 Апреля 2015

    Комментарии (11)
  3. Си / Говнокод #18071

    +145

    1. 1
    2. 2
    if (in)
    	goto out;

    Найдено на просторах ядра (fsl_esai.c кому интересно). Не ГК само по себе но читается замечательно.

    lorc, 27 Апреля 2015

    Комментарии (6)
  4. Си / Говнокод #18062

    +142

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    if (false == very_long_function(arg1, arg2, ...)) 
    {
        if (false == very_long_other_function(even_more_args) 
        {
            do_something();
        }
        else 
        {   
            do_something_else();
        }
    }

    И вот такими многоэтажками забит весь проект.

    codemonkey, 26 Апреля 2015

    Комментарии (5)
  5. Си / Говнокод #18046

    +140

    1. 1
    2. 2
    3. 3
    4. 4
    void GetPublicKey(char *destination, int len)
    {
    	GetPrivateKey(destination, len);
    }

    pmiller, 23 Апреля 2015

    Комментарии (1)
  6. Си / Говнокод #18035

    +137

    1. 1
    2. 2
    3. 3
    static const uint32_t frequencies[] = {
    #include "frequencies.h"
    };

    codemonkey, 21 Апреля 2015

    Комментарии (27)
  7. Си / Говнокод #18030

    +142

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    #include <inttypes.h>
    #include <stdio.h>
     
    typedef union
    {
      struct
      {
        #define BITG(n) uint8_t bit##n : 1
        BITG(0);
        BITG(1);
        BITG(2);
        BITG(3);
        BITG(4);
        BITG(5);
        BITG(6);
        BITG(7);
        #undef BITG
      } bits;
      uint8_t value;
    }getbit;
     
    uint8_t bit_sum(uint8_t, uint8_t);
     
     
    uint8_t bit_sum(uint8_t a, uint8_t b)
    {
      getbit op1, op2, opr;
      uint8_t carry;
      op1.value=a; op2.value=b;
      #define OP1(n) op1.bits.bit##n
      #define OP2(n) op2.bits.bit##n
      #define OPR(n) opr.bits.bit##n
      #define XOR(a,b) ((a)^(b))
      #define AND(a,b) ((a)&(b))
      OPR(0) = XOR(OP1(0), OP2(0));
      carry = AND(OP1(0), OP2(0));
      #define SETBIT(n)                \
      OPR(n) = XOR                     \
               (                       \
                 carry,                \
                 XOR(OP1(n), OP2(n))   \
               );
     
      #define CARRYBIT(n)              \
      carry = XOR                      \
              (                        \
                AND(OP1(n), OP2(n)),   \
                AND                    \
                (                      \
                  XOR(OP1(n), OP2(n)), \
                  carry                \
                )                      \
              );
      SETBIT(1);
      CARRYBIT(1);
      SETBIT(2);
      CARRYBIT(2);
      SETBIT(3);
      CARRYBIT(3);
      SETBIT(4);
      CARRYBIT(4);
      SETBIT(5);
      CARRYBIT(5);
      SETBIT(6);
      CARRYBIT(6);
      SETBIT(7);
      return opr.value;
      #undef SETBIT
      #undef CARRYBIT
      #undef OP1
      #undef OP2
      #undef OPR
      #undef XOR
      #undef AND
    }
     
    int main (int argc, char *argv[], char *envp[])
    {
      uint8_t a, b, c;
      scanf ("%"SCNu8"%"SCNu8, &a, &b);
      c = bit_sum(a,b);
      printf("%"PRIu8"\n", c);
      return 0;
    }

    Побитовое сложение двух 8-битных чисел по схеме двоичного сумматора

    j123123, 20 Апреля 2015

    Комментарии (11)
  8. Си / Говнокод #18028

    +105

    1. 1
    x = x + exp(ln(2)*i);

    Встретил в коде опроса датчика, не сразу понял, что это делает.
    Когда понял, понял что лучше бы не понимал.

    Vindicar, 20 Апреля 2015

    Комментарии (43)
  9. Си / Говнокод #17991

    +132

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    struct ass {
            void (*func)(const char *, void *);
            void *data;
        } asses[] = {
            { NULL, NULL },
            /* Тут типа scanf */
            { NULL, NULL },
        };
    
        while (getopt_long_only(argc, argv, "", long_options, &option_index) != -1) {
            if (option_index < 0)
                return 1;
            struct ass *derp = &asses[option_index];
            if (derp->func != NULL)
                derp->func(optarg, derp->data);
        }

    Правим код начальника.

    codemonkey, 13 Апреля 2015

    Комментарии (370)
  10. Си / Говнокод #17978

    +138

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    if(argc > 6){
    	for(arg = argv[6]; strlen((char *)arg) >= 17 &&
    					isdigit(arg[0]) &&
    					isdigit(arg[1]) &&
    					isdigit(arg[3]) &&
    					isdigit(arg[4]) &&
    					isdigit(arg[6]) &&
    					isdigit(arg[7]) &&
    					isdigit(arg[9]) &&
    					isdigit(arg[10]) &&
    					isdigit(arg[12]) &&
    					isdigit(arg[13]) &&
    					isdigit(arg[15]) &&
    					isdigit(arg[16])
    					; arg+=18){
    
    		int t1, t2;
    		struct period *sp;
    
    		t1 = (arg[0] - '0') * 10 + (arg[1] - '0');
    		t1 = (t1 * 60) + (arg[3] - '0') * 10 + (arg[4] - '0');
    		t1 = (t1 * 60) + (arg[6] - '0') * 10 + (arg[7] - '0');
    		t2 = (arg[9] - '0') * 10 + (arg[10] - '0');
    		t2 = (t2 * 60) + (arg[12] - '0') * 10 + (arg[13] - '0');
    		t2 = (t2 * 60) + (arg[15] - '0') * 10 + (arg[16] - '0');
    		if(t2 < t1) break;
    		sp = myalloc(sizeof(struct period));
    		if(sp){
    			sp->fromtime = t1;
    			sp->totime = t2;
    			sp->next = acl->periods;
    			acl->periods = sp;
    		}
    		if(arg[17]!=',') break;
    	}
    }

    Получение временных диапазонов, из https://github.com/z3APA3A/3proxy/blob/devel/src/3proxy.c#L1470
    Основное говно в том, что нифига не проверяются входные данные т.е. секунд и минут может быть больше 59, а часов может быть больше 23. И проверку if(t2 < t1) лучше заменить на if(t2 <= t1)
    А еще там в том исходнике в качестве отступов используются и пробелы и табы. Вообще там очень много говнокода

    j123123, 12 Апреля 2015

    Комментарии (5)