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

    +261

    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
    /* load config */
            for (i = 0; ; i++) {
                    conf[0] = '\0';
                    switch (i) {
                    case 0:
                            /* ~ */
                            snprintf(conf, sizeof conf, "%s/.%s",
                                pwd->pw_dir, SWM_CONF_FILE);
                            break;
                    case 1:
                            /* global */
                            snprintf(conf, sizeof conf, "/etc/%s",
                                SWM_CONF_FILE);
                            break;
                    case 2:
                            /* ~ compat */
                            snprintf(conf, sizeof conf, "%s/.%s",
                                pwd->pw_dir, SWM_CONF_FILE_OLD);
                            break;
                    case 3:
                            /* global compat */
                            snprintf(conf, sizeof conf, "/etc/%s",
                                SWM_CONF_FILE_OLD);
                            break;
                    default:
                            goto noconfig;
                    }
    
                    if (strlen(conf) && stat(conf, &sb) != -1)
                            if (S_ISREG(sb.st_mode)) {
                                    cfile = conf;
                                    break;
                            }
            }

    Вот так spectrwm грузит конфиги.

    codemonkey, 15 Августа 2016

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

    +273

    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
    #include <math.h>
    #include <stdio.h>
    
    double  DoubleToTheInt(double base, int power) {
        return pow(base, power);
    }
    
    int main() {
        // приводим к указателю на функуцию с обратным порядком аргументов
        double (*IntPowerOfDouble)(int, double) =
            (double (*)(int, double))&DoubleToTheInt;
    
        printf("(0.99)^100: %lf \n", DoubleToTheInt(0.99, 100));
        printf("(0.99)^100: %lf \n", IntPowerOfDouble(100, 0.99));
    }

    "Изящный способ отстрелить себе ногу по самую голову."
    Утащено с Хабры.

    Vindicar, 15 Августа 2016

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

    −1

    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
    // Add a UPnP port
    bool Win32UPnPAddPort(UINT outside_port, UINT inside_port, bool udp, char *local_ip, wchar_t *description, bool remove_before_add)
    {
    	bool ret = false;
    	HRESULT hr;
    	IUPnPNAT *nat = NULL;
    	wchar_t ip_str[MAX_SIZE];
    	BSTR bstr_ip, bstr_description, bstr_protocol;
    	wchar_t *protocol_str = (udp ? L"UDP" : L"TCP");
    	// Validate arguments
    	if (outside_port == 0 || outside_port >= 65536 || inside_port == 0 || inside_port >= 65536 ||
    		IsEmptyStr(local_ip) || UniIsEmptyStr(description))
    	{
    		return false;
    	}
    
    	StrToUni(ip_str, sizeof(ip_str), local_ip);
    	bstr_ip = SysAllocString(ip_str);
    	bstr_description = SysAllocString(description);
    	bstr_protocol = SysAllocString(protocol_str);
    
    	hr = CoCreateInstance(CLSID_UPnPNAT, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPNAT, (void **)&nat);
    
    	if (SUCCEEDED(hr))
    	{
    		if (nat != NULL)
    		{
    			IStaticPortMappingCollection *collection = NULL;
    			hr = nat->get_StaticPortMappingCollection(&collection);
    
    			if (SUCCEEDED(hr))
    			{
    				if (collection != NULL)
    				{
    					IStaticPortMapping *mapping = NULL;
    
    					if (remove_before_add)
    					{
    						hr = collection->Remove((long)outside_port, bstr_protocol);
    					}
    
    					hr = collection->Add((long)outside_port, bstr_protocol, (long)inside_port,
    						bstr_ip, VARIANT_TRUE, bstr_description, &mapping);
    
    					if (SUCCEEDED(hr))
    					{
    						ret = true;
    
    						if (mapping != NULL)
    						{
    							mapping->Release();
    						}
    					}
    
    					collection->Release();
    				}
    				else
    				{
    					WHERE;
    				}
    			}
    			else
    			{
    				WHERE;
    			}
    
    			nat->Release();
    		}
    		else
    		{
    			WHERE;
    		}
    	}
    	else
    	{
    		WHERE;
    	}
    
    	SysFreeString(bstr_ip);
    	SysFreeString(bstr_description);
    	SysFreeString(bstr_protocol);
    
    	return ret;
    }

    Отсюда https://github.com/SoftEtherVPN/SoftEtherVPN/blob/master/src/Cedar/Win32Com.cpp#L157
    Там еще много такого. https://github.com/SoftEtherVPN/SoftEtherVPN/blob/master/src/Cedar/Win32Com.cpp#L963 вот например тоже забавная хрень. Нашел эту штуку по ссылке с говнохабра http://habrahabr.ru/post/208782/

    j123123, 04 Января 2016

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

    +14

    1. 1
    integerValue *= 0;

    не обнулил, а на ноль умножил

    besprincypniycentner, 09 Декабря 2015

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

    −4

    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
    #include <stdio.h>
    #include <string.h>
    
    static struct foo {
    
    } fooo;
    
    int main(void)
    {
        int peace_of_shit = 0xFF;
        memcpy(&fooo, &dream, sizeof(peace_of_shit));
        **/Хочу увидеть говно**/
        return 0;
    }

    Как я могу увидеть говно, что лежит в fooo?)

    MiD, 09 Октября 2015

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

    −3

    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
    enum test_result {
        TEST_FAILED = -1,
        TEST_SKIPPED,
        TEST_OK
    }
    
    struct test_node {
        const char *name;
        const char *descr;
        enum test_result (*measurement_test_handler)(void* params);
        void *params;
        enum test_result meas_result;
        enum test_result init_result;
    }

    О духи говнакода наставьте агнца вашего на путь истинный. Написал сие я, запустил и всё пошло по п*изде, по причине невыравненых данных. #pragma pack не помог. Помогло лишь изменение типа enum test_result на uint8_t. Подскажите, дабы не наступать на подобные грабли болей, как кто себя проверяет при работе со структурами?

    MiD, 02 Октября 2015

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

    −1

    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
    #include<stdio.h>
    	main(void){
    	#define   SIZE 10
    	int workers[SIZE] = {0,0,0,0,0,0,0,0,0};
           int frequency[SIZE] = {0,0,0,0,0,0,0,0,0};          		
    	int freq[SIZE]  = {0,0,0,0,0,0,0,0,0};
    	int salary = 200;	
    	int  m = 0, k = 0 ,n = 0, x = 0;
    
    for( k = 1 ; 	k <= SIZE - 1  ; ++k){
       printf("inter summary cash = ");
           scanf("\n%d", &m);
    	  workers[k] = m;
    	  freq[k ]	= (int) salary +  ( workers[k] * 0.01 ) * 9;
    		printf("number index%4d  up salary $%d\n", k  , freq[k]);
    		puts("");
    	}
    
    for(n = 1;n <= SIZE  - 1; n++){
      freq[x]	=  salary +  (workers[n] * 0.01 ) * 9   ;
        if(freq[n] >= 200 && freq[n] < 1000 ){
           ++frequency[(int)  freq[n] / 100  ]  ;
    	 }
    else if ( freq[n] > 1000 ){
    	++frequency[10];
    		 }
    	}		
    		
          for(x = 2;  x <=  SIZE ; x++ ){							
    	printf("$%4d = $%4d 	%d\n",salary, salary + 99,	frequency[x]  );						
                  salary += 100;		
    		}	
    return 0;
    	}

    Задачу по Дейтелам решал.

    tyrin, 27 Сентября 2015

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

    +14

    1. 1
    2. 2
    3. 3
    /*
    удалено по просьбе правообладателя
    */

    Думал я, что меня уже так просто не удивишь, пока не увидел ЭТО!

    gorthauer87, 24 Сентября 2015

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

    +1006

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    if(cond) {
          #define INIT_COUNT 1
    } else {
          #define INIT_COUNT 2
    }

    Тот, кто показал мне этот кусочек кода, был очень удивлем тем , что все время выполняется только ветка else.

    refactor, 05 Августа 2015

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

    +1000

    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
    int cutNCountLt3(char *in, char *out, long min) {
        int lt3 = 0;
        char *i = in, *j = in, *k = out;
    
        while (*j && *i) {
            j = i;
            while (*j != ' ' && *j)
                *k++ = *j++;
    
            *k++ = *j;
    
            int len = j - i;
            lt3 += len < 3;
    
            if (len < min)
                *(k -= len + 1) = '\0';
    
            i = j + 1;
        }
    
        return lt3;
    }

    К #9911
    А конкретнее http://govnokod.ru/9911#comment295215

    После нескольких ревизий в комментах, подумал, что наложить здесь будет лучше... Прошу любить, но не жаловать

    Elvenfighter, 03 Августа 2015

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