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

    +100

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    f = fopen(file_name, "w");
    if (!f) {
      f = fopen(file_name, "w+");
      if (!f)
        logprintfl(EUCAWARN, "Cannot create %s!\n", file_name);
      else {
        len = fileno(f);
        if (len > 0)
          fchmod(len, S_IRUSR|S_IWUSR);
      }
    }

    Суровые калифорнийские парни сурово создают суровые файлы...

    raorn, 29 Июля 2010

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

    +139

    1. 1
    2. 2
    3. 3
    4. 4
    inline Gdiplus::Color colorrefToGdiColor(COLORREF col, char alpha)
    {
    	return (static_cast<unsigned long>(static_cast<unsigned char>((col & Gdiplus::Color::RedMask) >> Gdiplus::Color::RedShift)) <<   Gdiplus::Color::BlueShift) | (static_cast<unsigned long>(static_cast<unsigned char>((col & Gdiplus::Color::GreenMask) >> Gdiplus::Color::GreenShift)) << Gdiplus::Color::GreenShift) | (static_cast<unsigned long>(static_cast<unsigned char>((col & Gdiplus::Color::BlueMask) >> Gdiplus::Color::BlueShift)) << Gdiplus::Color::RedShift) | (static_cast<unsigned long>(alpha) << Gdiplus::Color::AlphaShift);
    }

    Тихо себя ненавижу. Слава Б-г'у это всё выкидывается.

    Altravert, 23 Июля 2010

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

    +136

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    recordSize=logicalRecordLength;
    BytesToRead=cnt*recordSize;
    records=cnt;
    while ( ((pos + BytesToRead)>dataSize)&&records )
    {
        BytesToRead = --records * recordSize;
    }

    Не думал, что у нас в коде встречу нечто явно похожее на пример №2 из статьи "Индусский код" на Луркморе.
    Ну и стоит добавить, что реально logicalRecordLength всегда равен 1.

    vovochka, 21 Июля 2010

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

    +141

    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
    int AnalizingHeaderLine(char* HeadLine)
    {
    	if(HeadLine==NULL) return -1;
    	if(strlen(HeadLine)==0) return 0;
    
    	if(!strncmp(HeadLine,"HTTP/",5))
    		ProcessStatusHL(HeadLine);
    	else if(!strncmp(strlwr(HeadLine), "date:",5))
    		ProcessDateHL(HeadLine);
    	else if(!strncmp(strlwr(HeadLine), "server:",7))
    		ProcessServerHL(HeadLine);
    	else if(!strncmp(strlwr(HeadLine), "last-modified:",14))
    		ProcessLastModHL(HeadLine);
    	else if(!strncmp(strlwr(HeadLine), "content-type:",13))
    		ProcessContTypeHL(HeadLine);
    	else if(!strncmp(strlwr(HeadLine), "content-length:",15))
    		ProcessContLenHL(HeadLine);
    	else if(!strncmp(strlwr(HeadLine), "pragma:",7))
    		ProcessPragmaHL(HeadLine);
    	else if(!strncmp(strlwr(HeadLine), "Connection",10))
    		ProcessConnectHL(HeadLine);
    	else printf("Unknown header line: %s\n", HeadLine);
    	return strlen(HeadLine);
    }
    
    int ProcessStatusHL(char* HeadLine)
    {
    	short MinVer, MajVer;
    	char ResultStr[32];
    	char Num;
    	short Code;
    	Num = sscanf(HeadLine, "HTTP/%d.%d %d %s", &MinVer, &MajVer, &Code, ResultStr);
    	if(Num!=3 && Num!=4) 
    	{	printf("Error status string\n");
    		return -1;
    	}
    	return Code;
    }
    
    int ProcessDateHL(char* HeadLine)
    {
    	printf("%s\n",HeadLine);
    	return 0;
    }
    
    int ProcessServerHL(char* HeadLine)
    {
    	printf("%s\n",HeadLine);
    	return 0;
    }
    
    int ProcessLastModHL(char* HeadLine)
    {
    	printf("%s\n",HeadLine);
    	return 0;
    }
    
    int ProcessContTypeHL(char* HeadLine)
    {
    	printf("%s\n",HeadLine);
    	return 0;
    }
    
    int ProcessContLenHL(char* HeadLine)
    {
    	printf("%s\n",HeadLine);
    	return 0;
    }
    
    int ProcessPragmaHL(char* HeadLine)
    {
    	printf("%s\n",HeadLine);
    	return 0;
    }
    
    int ProcessConnectHL(char* HeadLine)
    {
    	printf("%s\n",HeadLine);
    	return 0;
    }

    CGI. Обработка HTTP-заголовков. Rev. 1.0

    absolut, 20 Июля 2010

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

    +131

    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
    #include <stdio.h>
    
    void factorization(int num, int show) {
        int num1 = num;
        int n = 2;
        while ( n*n <= num1 ) {
            if ( num%n == 0 ) {
                num = num / n;
                if ( show )
                    printf( "%d\n", n );
            } else {
                n ++;
            }
        }
    }
    
    int main() {
        int i = 0;
        while ( i < 1000 ) {
            factorization(999999, 0);
            i ++;
        }
        return 0;
    }

    Опубликовано в одной из ссылок с http://habrahabr.ru/blogs/ruby/48952/ (если надо, точную ссылку найду позже).
    Код раскладывает число на простые множители тупым перебором делителей. Мало того, что этот код медленный, так он иногда последний множитель пропускает. Одновременно и ошибка, и скорость исправляются так:
    - while ( n*n <= num1 ) {
    + while ( n <= num ) {
    Неожиданно, правда?

    inkanus-gray, 13 Июля 2010

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

    +138

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    /usr/include/sys/seg.h:
    #define shm_ptr u_ptrs.shmptr
    
    myfile.c:
    static SHRMEM_INFO_PTR shm_ptr = NULL;

    Сегодня для разнообразия системный хедер от AIX.

    Повбывав бы производителей, которые ограничивают полет моей фантазии (и так весьма приземленный) в именованиях моих личных переменных!

    nil, 09 Июля 2010

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

    +135

    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
    int Xor4Bit_2 (unsigned char data)
    {
         unsigned char result = data;
          while (data != 0)
         {
             result ^= data & 1;
             data >>= 1;
         }
          result &= 1;
         return result;
    }
    
    вот как студенты получают xor битов числа
    это же нужно так извратить простой рабочий алгоритм
         
    int Xor4Bit_2 (unsigned char data)
    {
         int result = 0       
         while (data != 0)
         {
             result ^= data & 1;
             data >>= 1;
         }
         return result;
    }

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

    ageron, 04 Июля 2010

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

    +111

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    double f_x (double x, double y, int nom)
    {
      double f_x[]={x+y, x*y, x*y, sqrt(x*x + y*y), cos(x)/sin(y)};
      return f_x[nom];
    }

    Нужна одна из функций :-)

    Goga, 01 Июля 2010

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

    +142

    1. 1
    enum {size = 10, timeout = 50};

    vovochka, 30 Июня 2010

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

    +140

    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
    int 
    grub_auth_strcmp (const char *user_input, const char *template) 
    { 
      int ok = 1; 
      const char *ptr1, *ptr2; 
      for (ptr1 = user_input, ptr2 = template; *ptr1; ptr1++) 
        if (*ptr1 == (ptr2 ? *ptr2 : ptr1[1]) && ok && ptr2 != NULL) 
          ptr2++; 
        else 
          ok = 0; 
     
      return !ok; 
    }

    Несвежий говнокод (давно пропатчено) и, возможно, кто-то скажет "баян", однако оставлю это здесь.
    Код из загрузчика grub 1.97, проверка пароля. Принимает за верный пароль любую подстроку пароля.

    cfdev, 27 Июня 2010

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