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

    −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
    #ifdef DEBUG
    static int (*printfn)(FILE *stream, const char *format, ...) = fprintf;
    #else
    static void (*printfn)(int priority, const char *format, ...) = syslog;
    #endif
    
    /* somewhere in getopt() options */
    #ifdef DEBUG
        printfn = noprintf;
    #else
        printfn = nosyslog;
    #endif
    
    /* elsewhere in a common header file */
    #ifdef DEBUG
    #undef LOG_ERR
    #undef LOG_WARNING
    #undef LOG_INFO
    #define LOG_ERR stderr
    #define LOG_WARNING stderr
    #define LOG_INFO stderr
    #endif
    
    void nosyslog(int priority, const char *format, ...)
    {
        (void)priority;
        (void)format;
    }
    
    int noprintf(FILE *stream, const char *format, ...)
    {
        (void)stream;
        (void)format;
        return 0;
    }

    А попроще способа для --quiet / -DDEBUG нету?

    codemonkey, 29 Июля 2018

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

    +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
    copy(v) char *v; {	/* copy ctokn to v */
    	char *p;
    
    	p=ctokn;
    	while( *v++ = *p++ );
    	}
    
    compare(v) char *v; {	/* compare ctokn with v */
    	char *p;
    
    	for( p=ctokn; ; ++p ){
    		if( *p != *v++ ) return( 0 );
    		if( *p == 0 ) return(1);
    		}
    	}
    
    int *yalloc(n){ /* allocate n+1 words from vector mem */
    	int *omem;
    	omem = mem;
    	mem =+ n+1;
    	if(mem-mem0 >= memsiz) error("memory overflow");
    	return(omem);
    	}
    
    aryfil( v, n, c ) int *v,n,c; { /* set elements 0 through n-1 to c */
      int i;
      for( i=0; i<n; ++i ) v[i] = c;
      }
    
    union( a, b, c ) int *a, *b, *c; {
      /* set a to the union of b and c */
      /* a may equal b */
      /* return 1 if c is not a subset of b, 0 otherwise */
    
      _REGISTER int i, x, sub;
    
      sub = 0;
      for( i=0; i<tbitset; ++i ){
        x = b[i] | c[i];
        if( x != b[i] ) sub=1;
        a[i] = x;
        }
      return( sub );
      }
    
    prlook( pp ) int *pp;{
    	int j;
    	pp = pp->lset;
    	if( pp == 0 ) printf("\tNULL");
    	else {
    		printf(" { " );
    		for( j=1; j<=nterms; ++j ){
    			if( (pp[j>>4]>>(j&017) )&01 != 0 ) printf( "%s ", symnam(j) );
    			}
    		printf( "}" );
    		}
    	}

    https://github.com/eunuchs/unix-archive/blob/master/PDP-11/Trees/V6/usr/source/yacc/source/y1.c

    https://www.tuhs.org//Archive/Distributions/Research/Dennis_v6/v6src.tar.gz

    Для любителей обмазываться несвежим сишкокодом. Research Unix

    j123123, 28 Июля 2018

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

    −2

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    #include <stdio.h>
    
    int main(void) {
        int a, b;
        int *p = &a;
        int *q = &b + 1;
        printf("%p %p %d\n", (void *)p, (void *)q, p == q);
        return 0;
    }

    https://habr.com/company/pvs-studio/blog/418023/

    0x7fff4a35b19c 0x7fff4a35b19c 0

    кто понимает почему?

    guestinxo, 25 Июля 2018

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

    −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
    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
    // https://github.com/Qqwy/raii_with/blob/74e4c66a821fba6a483d62a8c583b3fab06e3443/raii/raii.h#L60
    
    /**
     * Custom Control Structure Macro to provide Resource Acquisition Is Initialization (and Resource Relinquishment is Destruction).
     *
     * Use this to run a block of code with `var_decl` initialized to `init`, where at the end of the block (or at an earlier `safe_return`),
     * the passed `destr`-function will automatically be called with the given resource.
     *
     * Gotcha's:
     * 1. Do not use `return` from within `raii_with`, but only `safe_return`, because otherwise the destructors will not be run.
     * 2. Do not perform pointer-swaps with `var_decl`; the destructor will still be run on the original structure, because `raii` keeps its own reference to the resource.
     */
    #define raii_with(var_decl, init, destr)                                \
      while(1) /* i.c.m. break on l.4, so we can jump past the user-supplied block */ \
        if(0)                                                               \
        raii_glue(__raii_with_finished, __LINE__):                              \
          break;                                                            \
        else                                                                \
          /* initialize _tmp lifetime list elem so replacement `raii_lifetime_list` can have previous one as tail. */ \
          for(struct raii_lifetime_list_t _tmp = {.elem.resource = init, .elem.destructor = destr, .next = raii_lifetime_list};;) \
            /* initialize user-supplied variable name */                    \
            for(var_decl = _tmp.elem.resource;;)                            \
              if (1) {                                                      \
                /* Fill `_tmp`'s tail before `raii_lifetime_list` is shadowed */ \
                _tmp.next = raii_lifetime_list;                             \
                goto raii_glue(__raii_with_setup, __LINE__);                    \
              } else                                                        \
              raii_glue(__raii_with_setup, __LINE__):                           \
                /* Shadow `raii_lifetime_list` with inner version */        \
                for(struct raii_lifetime_list_t *raii_lifetime_list = &_tmp;;) \
                  if(1){                                                    \
                    goto raii_glue(__raii_with_body, __LINE__);                 \
                  } else                                                    \
                    while (1) /* so break works as expected */              \
                      while (1) /*so continue works as expected */          \
                        if (1){                                             \
                          /*after the else-block (or break or continue), destruct and finish */ \
                          destruct_raii_lifetime(raii_lifetime_list->elem); \
                          goto raii_glue(__raii_with_finished, __LINE__);       \
                        } else                                              \
                        raii_glue(__raii_with_body, __LINE__):
    
    
    #endif // RAII_WITH_H

    raii

    A simple library to provide RAII in standard-compliant C99, using raii_with(resource, initializer, destructor) { ... }-syntax:

    j123123, 19 Июля 2018

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

    −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
    #include <stdio.h>
    #include <inttypes.h>
         
    int main(void) {
      
    	uint64_t a = 1<<31;
    	uint64_t b = 1<<32;
    	uint64_t c = (uint64_t)1<<32;
         
    	printf("a:%llx\n", a);
    	printf("b:%llx\n", b);
    	printf("c:%llx\n", c);
         
    	return 0;
    }

    a:ffffffff80000000
    b:0
    c:100000000

    Занимался битоёбством и не сразу понял откуда в алгоритме мусор.

    govnokod3r, 16 Июля 2018

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

    +5

    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
    void sort3(uint32_t a[static 3])
    {
      //                   0     1     2     3     4     5     6     7     8
      uint32_t tmp[9] = {a[0], a[1], a[2], a[0], a[1], a[0], a[2], a[1], a[0]};
      uint8_t bits = (a[0] <= a[1]) | ((a[1] <= a[2]) << 1) | ((a[0] <= a[2]) << 2);
      static const uint8_t b[] =
      {
        [0b000] = 6,
        [0b001] = 2,
        [0b010] = 1,
        [0b101] = 5,
        [0b110] = 4,
        [0b111] = 0,
      };
      memcpy(a, tmp+b[bits], 3*sizeof(uint32_t));
    }

    Новая инновационная сортировка на 3 элемента без if-ов
    https://wandbox.org/permlink/pTLXgxKKQuaiVCxb

    j123123, 15 Июля 2018

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

    −2

    1. 1
    argc

    Зачем нужен argc? Нельзя просто смотреть NULL-terminated argv?

    3_dar, 14 Июля 2018

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

    0

    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 check_manifest_line (int p) {
      static char c[MAX_LINE_LEN + 1];
      int l = p - START_MANIFEST_POSITION;
      if (l <= MAX_LINE_LEN && l > 4) {
        get_binlog_data (c, START_MANIFEST_POSITION, l);
        c[l] = 0;
        char *pp = c;
        int op = -1;
        if (l >= 6 && !memcmp (c, "start", 5)) {
          op = 1; 
          pp += 5;
        } else if (l >= 5 && !memcmp (c, "stop", 4)) {
          op = 2;
          pp += 4;
        } else if (l >= 8 && !memcmp (c, "version", 7)) {
          op = 3;
          pp += 7;
        }
        if (is_whitespace (*pp) && op > 0) {      
          pp ++;
          pp = eat_whitespace (pp);
          if (!*pp) {
            START_MANIFEST_POSITION = p + 1;
            return;
          }
          if (op == 1 || op == 2) {
            char *rr = pp;
            pp = eat_not_whitespace (pp);
            char *zz = pp;
            pp = eat_whitespace (pp);
            *zz = 0;
            if (pp == c + l && zz - rr > 0) {
              struct cluster *C = CC;
              int x = BINLOG_NAME_LEN - 1;
              while (x >= 0 && BINLOG_NAME[x] != '/') {
                x--;
              }
              add_cluster (BINLOG_NAME, x + 1, rr, (MAIN_REPLICA ? 2 : 0) + (op == 2 ? 4 : 0) + (1 << 30));
              CC = C;
            }
          } else {
            assert (op == 3);
            char *rr = pp;
            pp = eat_not_whitespace (pp);
            char *rr_end = pp;
            pp = eat_whitespace (pp);
            *rr_end = 0;
            if (!*pp) {
              START_MANIFEST_POSITION = p + 1;
              return;
            }
            int version = atoi (pp);
            pp = eat_not_whitespace (pp);
            pp = eat_whitespace (pp);
            if (!*pp) {
              START_MANIFEST_POSITION = p + 1;
              return;
            }
            long long size = atoll (pp);
            pp = eat_not_whitespace (pp);
            pp = eat_whitespace (pp);
            if (pp == c + l && rr_end > rr && version > 0 && size >= 0) {
              struct cluster *C = CC;
              int x = BINLOG_NAME_LEN - 1;
              while (x >= 0 && BINLOG_NAME[x] != '/') {
                x --;
              }
              int rrlen = rr_end - rr;
              int i;
              for (i = 0; i < max_cluster; i++) if (Clusters[i]) {
                //fprintf (stderr, "i = %d, binlog_name = %s, rr = %s, rrlen = %d\n", i, Clusters[i]->binlog_name, rr, rrlen);
                const char *s = Clusters[i]->binlog_name;
                int l = strlen (Clusters[i]->binlog_name) - 1;
                while (l >= 0 && s[l] != '/') { l --; }
                s = s + l + 1;
                if (!memcmp (s, rr, rrlen) && s[rrlen] == '.') {
                  int old = atoi (s + rrlen + 1);
                  if (old >= version) {
                    vkprintf (0, "New version %d, old %d. Skipping\n", version, old);
                    START_MANIFEST_POSITION = p + 1;
                    return;
                  }
                  if (unlink (Clusters[i]->binlog_name) < 0) {
                    fprintf (stderr, "Can not delete old file %s: %m\n", Clusters[i]->binlog_name);
                  }
                  delete_cluster (i);
                }
              }
              static char name[1024];
              memcpy (name, rr, rrlen);
              name[rrlen] = '.';
              sprintf (name + rrlen + 1, "%06d", version);
              add_cluster (BINLOG_NAME, x + 1, name, (MAIN_REPLICA ? 2 : 0) + (1 << 30));
              LAST_SIZE = size;
              CC = C;
            }
          }
        }
      }
      START_MANIFEST_POSITION = p + 1;

    https://github.com/vk-com/kphp-kdb/blob/ce6dead5b3345f4b38487cc9e45d55ced3dd7139/copyfast/copyfast-engine.c#L1081-L1181


    Очередная порция вконтактового говнокода.

    j123123, 05 Июля 2018

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

    −1

    1. 1
    main(){int i=0;char*c = "(5((((`+((((f(H+(P7(d(([)(/(`G(X)7d+(.X+d)X+d(_(d.GXA`gg/d(dggI7(dgg`+(dg7^)(dgKE((dGD/((d/K)(((((((((0X)(((7F(<(XK/H7(d`)`)(.D(F(H(G(/(D`7X(HOg+6(`YgX;(fd7d7//d+7X[+GHKgdX7gg77fcggYKgfg/";while(i++<1122)printf(i%34?(c[i/6]-40&1<<i%6)?"XX":" ":"\n");}

    Ёбаные обфускаторы.

    codemonkey, 02 Июля 2018

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

    0

    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
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    int  removing_mask (int *x , int *y, int *lng  , char board[][Y], 
    					char *content[PL] , int select, void   (*write  )( int x , int y  ,char board[][Y],
    					char *content)  , int search_cross_pnt(char cross_point[LN] , int *point   ,char *content[PL] , int in_x , int in_y) , 
    					int (*chk_mask )(char board[][Y] , int mem ,int sh, int br )
    					   ){
    	char mask_word[LN] = {0}  ;
    	int sm = 1;
    	int lvl   =  0;
    	int shift =  0;
    	int brd = 0;
    	int mem = 0;
    	int stp = 0;
    	int mn = 0;
    	int bi = 0;
    	int rd = 0;
    	int gor = 0 ,vert = 0;
    	int pnt = 0;
    	int hg  = 15;
    	int	sr = 0;
    	int *one_value = 0  , *second_value = 0;
    	int ctr = 0;
        
    	if(select == 0 ){
    	  mem = *y;
    	  hg  = 	  *y < LN	 ?  *y : hg    ;
    	}
    	else   {
    		mem = *x;
    		hg  = 	  *x < LN	 ?  *x : hg    ;
    	
    	}
    	
    	for(   lvl = 0     ,rd = 0; lvl <= *lng  - 1    ;      lvl++  ,mem++	  ){
    		 
    			for(brd = 0  ;    brd  <= hg  ; brd++           ){
    			 	  	
    			 select == 0 ? ( shift = *x ,    one_value = &sr ,   	second_value  =  &mem) :  (shift = *y       ,  one_value = &mem ,   	second_value  = &sr  ) ;
    	
    	if(	 (select == 0 && board[ shift - 1][ *second_value ]  == 0  )  ||  (select == 1 && board[*one_value  ][shift  - 1 ]   == 0    )){
    		
    		for(    pnt = 0, stp = 0;   stp  <= LN  ;stp++     ){
    				   	  
    			if ( 	 chk_mask  ( board  , mem    ,   shift ,  brd  )   == 1   ){
    						 pnt = 1;
    						
    						break;
    				}	
    					sr = shift++ - brd ;
    					mask_word[stp] 	 = 	board[    	*one_value      ][ 	*second_value   ]  ;
    				}
    		     
    	     	}   	 
    				if(pnt == 1 ){
    				     	break;
    					 }
    
    				sm =  search_cross_pnt( mask_word   ,&mn , content  ,*x, *y  ) ; 
    
    			 	if(  sm > bi   ){
    					
    				
    				    select == 0 ? (  gor   =	*x - brd, vert   = 	mem)	: (gor   =	    mem  ,vert   =  *y - brd) ; 
    			        
    					bi = 	sm   ;
    				    rd = mn; 
    			     }
    				}
    			}		 
    			if(  (select == 1 &&  isalpha(board[gor][vert -  1]  ) ||
    			    
    				 (select == 0 &&  isalpha(board[gor - 1 ][vert] )) ||
    				
    				 ( gor && vert  )   ==  0 
    							) ) { 
    			ctr = 1;
    		}
    		if(  ( (    content[rd]   != " " ) )  && 
    			
    			( board[gor ][vert ]  ==  content[rd][0] || !isalpha (board[gor ][vert ]  )  ) 
    		
    			&& !ctr 
    		
    		) {
    			
    			*x = gor  ;   
    			*y = vert ;
    			*lng =   strlen(content[rd ]); 
    			write( gor   , vert    ,  board ,     content[rd]     );
    					content[rd] = " ";
    					return 1 ;
    			}
    	
    			return 0;
    	}

    полный аут кроссворду 3 ч.

    gne4do, 22 Июня 2018

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