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

    +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
    float Q_rsqrt( float number )
    {
            long i;
            float x2, y;
            const float threehalfs = 1.5F;
     
            x2 = number * 0.5F;
            y  = number;
            i  = * ( long * ) &y;                       // evil floating point bit level hacking
            i  = 0x5f3759df - ( i >> 1 );               // what the fuck?
            y  = * ( float * ) &i;
            y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
    //      y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
    
            return y;
    }

    The following code is the fast inverse square root implementation from Quake III Arena, stripped of C preprocessor directives, but including the exact original comment text.

    Вот что такое настоящие магические числа.

    bormand, 30 Июня 2012

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

    +137

    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
    void
    meta_window_constrain (MetaWindow          *window,
                           MetaFrameBorders    *orig_borders,
                           MetaMoveResizeFlags  flags,
                           int                  resize_gravity,
                           const MetaRectangle *orig,
                           MetaRectangle       *new)
    {
      ConstraintInfo info;
    
      /* ... */
    
      setup_constraint_info (&info, window,  orig_borders, flags, resize_gravity, orig, new);
    
      /* ... */
    
      /* Ew, what an ugly way to do things.  Destructors (in a real OOP language,
       * not gobject-style--gobject would be more pain than it's worth) or
       * smart pointers would be so much nicer here.  *shrug*
       */
      if (!orig_borders)
        g_free (info.borders);
    }
    
    static void
    setup_constraint_info (/* ... */)
    {
      /* ... */
    
      /* Create a fake frame geometry if none really exists */
      if (orig_borders && !window->fullscreen)
        info->borders = orig_borders;
      else
        info->borders = g_new0 (MetaFrameBorders, 1);
    
      /* ... */
    }

    http://git.gnome.org/browse/mutter/tree/src/core/constraints.c

    rat4, 29 Июня 2012

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

    +130

    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
    int print_entry(const char* name, const char* dir,const struct stat* st)
    {
      if(!S_ISDIR(st->st_mode)){
          if(S_ISREG(st->st_mode)){
            printf("<file type=\"regular file\" owner=\"%d\" group=\"%d\">%s/%s</file>\n",st->st_uid,st->st_gid,dir,name);
          };
          if(S_ISCHR(st->st_mode)){
            printf("<file type=\"charcter device\" owner=\"%d\" group=\"%d\">%s/%s</file>\n",st->st_uid,st->st_gid,dir,name);
          }
          if(S_ISBLK(st->st_mode)){
            printf("<file type=\"block device\" owner=\"%d\" group=\"%d\">%s/%s</file>\n",st->st_uid,st->st_gid,dir,name);
          }
          if(S_ISFIFO(st->st_mode)){
            printf("<file type=\"FIFO(named pipe)\" owner=\"%d\" group=\"%d\">%s/%s</file>\n",st->st_uid,st->st_gid,dir,name);
          }
          if(S_ISLNK(st->st_mode)){
             char *linkname;
             ssize_t r;
             char *lname=strcat(dir,"");
             linkname =  new char[st->st_size + 1];
             if (linkname == NULL) {
                fprintf(stderr, "insufficient memory\n");
                exit(EXIT_FAILURE);
             }
            r=readlink(lname, linkname, st->st_size + 1);
            if (r < 0) {
              return 0;
            }
               if (r > st->st_size) {
            fprintf(stderr, "symlink increased in size "
                            "between lstat() and readlink()\n");
            exit(EXIT_FAILURE);
        }
    
            linkname[st->st_size] = '\0';
            printf("<file type=\"symbolic link\" owner=\"%d\" group=\"%d\" linkname=\"%s\">%s/%s</file>\n",st->st_uid,st->st_gid,linkname,dir,name);
          }
          if(S_ISSOCK(st->st_mode)){
            printf("<file type=\"socket\" owner=\"%d\" group=\"%d\">%s/%s</file>\n",st->st_uid,st->st_gid,dir,name);
          }
        }
        return 0;
    }
     
    int main(int argc, char* argv[])
    {
    if(argc != 2)
        {
        fprintf(stderr, "Usage: %s DIR\n", argv[0]);
        exit(1);
        }
        printf("<dir name=\"%s\">\n",argv[1]);
        walk(argv[1], print_entry, 1,0);
        printf("</dir>",argv[1]);
    }

    Создание xml файла всех директорий, поддиректорий и их файлов

    AliceGoth, 22 Июня 2012

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

    +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
    26. 26
    27. 27
    28. 28
    #include <stdio.h>
    #include <unistd.h> 
    #include <stdlib.h> 
    #define MAX_STEP 6
    static int num;
    int seq_gen(int step){
      if(step<MAX_STEP){
    for(int idx=1;idx<=MAX_STEP;idx++){
    for(int i=0;i<=step;i++)printf(" ");
       printf("<id%d step=\"%d\">\n",idx,step);
       seq_gen(++step);
       --step;
       printf("</id%d>\n",idx);
      };
    };
    if(step==MAX_STEP){
     for(int i=1;i<=MAX_STEP;i++){
     for(int si=0;si<=step;si++)printf(" ");
      printf("<id%d>%d</id%d>\n",i,num++,i); 
     }
    }
    };
    int main(){
    printf("<root>\n");
    seq_gen(1); 
    printf("</root>");
    return 0;
    };

    Создает xml файл с 6 элементов с 6 вложенными элементами пока уровень вложенности достигнет 6.

    AliceGoth, 21 Июня 2012

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

    +137

    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
    #define FMT_2_LEN  16
    #define FMT_4_LEN  24
    // и т.д. всего около десятка форматов
    
    void calc_check_code(const unsigned char * from, unsigned fmt, unsigned * code)
    {
        switch (fmt) {
        case 2:
            //...
            memset(data, 0, sizeof(FMT_2_LEN));
            // выборочное наполнение data из from
            make_code(data, FMT_2_LEN, code);
            break;
        case 4:
            //...
            memset(data, 0, sizeof(FMT_4_LEN));
            // выборочное наполнение data из from
            make_code(data, FMT_2_LEN, code);
            break;
        // для всех остальных аналогично
    }

    странно, и почему контрольный код не совпадает с эталонными примерами...

    defecate-plusplus, 20 Июня 2012

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

    +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
    ...
        ASSERT( sizeof(L"SystemPartition") <= sizeof(nameBuffer) );
    /* Китайский код? Или по другому нельзя было запихнуть строку в ентот массив? */
        nameBuffer[0]  = L'S';
        nameBuffer[1]  = L'y';
        nameBuffer[2]  = L's';
        nameBuffer[3]  = L't';
        nameBuffer[4]  = L'e';
        nameBuffer[5]  = L'm';
        nameBuffer[6]  = L'P';
        nameBuffer[7]  = L'a';
        nameBuffer[8]  = L'r';
        nameBuffer[9]  = L't';
        nameBuffer[10] = L'i';
        nameBuffer[11] = L't';
        nameBuffer[12] = L'i';
        nameBuffer[13] = L'o';
        nameBuffer[14] = L'n';
        nameBuffer[15] = L'\0';
    
        nameString.MaximumLength = sizeof(L"SystemPartition");
        nameString.Length        = sizeof(L"SystemPartition") - sizeof(WCHAR);
    
    
    
        status = NtSetValueKey(setupHandle,
                                &nameString,
                                TITLE_INDEX_VALUE,
                                REG_SZ,
                                volumeNameString.Buffer,
                                volumeNameString.Length + sizeof(WCHAR)
                               );
    ...

    В Мелкософт китайцев пригласили?

    Кусок кода ядра Шindoшs ИТ
    файл ioinit.c, строка 3312

    Destinat1on, 16 Июня 2012

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

    +133

    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
    static inline int getKey(short int high, short int low)
    {
        return (((int)high)<<16)+(int)low;
    }
    
    static inline short getHigh(int pKey)
    {
        return (short)(pKey>>16);
    }
    
    static inline short getLow(int pKey)
    {
        return (short)(pKey&0x00000000FFFFFFFF);
    }

    вот так. getHigh() оставил для полноты.

    Dummy00001, 14 Июня 2012

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

    +137

    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
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
    	long i, copy, n, temp;
    	int *ar, j, k, flag, d1, d2;
    	for(i=987654321; i>=123456789; i--)
    	{		
    		copy=i;
    		ar=(int *)calloc(10, sizeof(int));
    		while(copy>0)
    		{
    			if(copy%10==0)
    				break;
    			if(ar[copy%10]!=0)
    				break;
    			ar[copy%10]=1;
    			copy/=10;
    		}
    		free(ar);
    		if(copy==0)
    		{				
    			for(j=8; j>=5; j--)
    			{
    				flag=1;
    				n=i/(long)pow(10, j);
    				copy=i-n*(long)pow(10, j);
    				for(k=2; k<=8 && flag; k++)
    				{
    					temp=n*k;
    					if(temp>copy)
    					{
    						flag=0;
    						break;
    					}
    					while(temp>0)
    					{
    						d1=temp/(long)pow(10, (int)log10(temp));
    						d2=copy/(long)pow(10, (int)log10(copy));
    						if(d1!=d2)
    						{
    							flag=0;
    							break;
    						}
    						temp=temp-d1*(long)pow(10, (int)log10(temp));
    						copy=copy-d2*(long)pow(10, (int)log10(copy));
    					}
    					if(copy==0)
    					{
    						printf("The pandigital number is %ld and the integer is %ld\n", i, n);
    						return 0;
    					}
    				}
    			}
    		}
    	}
    	return 0;
    }

    Очередной гений с пр. Ейлер:
    http://projecteuler.net/problem=38
    http://projecteuler.net/thread=38&page=8


    >Runtime: 14.86s on 2.67Ghz machine

    TheHamstertamer, 08 Июня 2012

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

    +123

    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
    __flash unsigned char strings[] =
    {
      20,     '*','*','Р','е','ж','и','м',' ','О','т','ч','ё','т','о','в',' ','Ф','Р','*','*',
      20,     'П','р','о','м','.',':','1',' ','С','м','е','н','.',':','2',' ','Ф','O',':','3',
      20,     ' ','З','а','к','р','ы','т','ь',' ','с','м','е','н','у',' ','Ф','Р','?',' ',' ',
      20,     ' ','П','е','ч','а','т','а','т','ь',' ','X','-','О','т','ч','ё','т','?',' ',' ',
      20,     '*','Р','е','ж','и','м',' ','К','о','р','р','е','к','ц','и','и',' ','Ф','Р','*',
      20,     'В','Н','Е','С',':','1',' ','В','Ы','П','Л',':','2',' ','К','О','Р','Р',':','3',
      20,     '*','К','Ф','Р','*',' ','О','п','е','р','.','В','н','е','с','е','н','и','е',' ',
      20,     '*','К','Ф','Р','*',' ','О','п','е','р','.',' ','В','ы','п','л','а','т','а',' ',
      20,     '*','К','Ф','Р','*',' ','К','о','р','р','е','к','т','-','й',' ','ч','е','к',' ',
      20,     '*','Р','е','ж','и','м',' ','П','р','о','г','р','а','м','-','я',' ','Ф','Р','*',
      20,     'В','Р','Е','М','Я',':','1',' ','П','Р','Л',':','2',' ','Т','И','П',':','3',' ',
      20,     '*','П','Ф','Р','*',' ',' ','Д','а','т','а','-','В','р','е','м','я',' ',' ',' ',
      12,     'Т','И','П',' ','Т','О','П','Л','И','В','А','?',
      4,      'д','о','з','а',
      5,      'C','У','М','М','А',
      5,      'C','Д','А','Ч','А',
      4,      'р','у','б','.',
      9,      'О','Ш','И','Б','К','А',' ','Ф','Р',
      9,      'П','О','Л','Н','.',' ','Б','А','К',
      7,      'П','а','р','о','л','ь',':',
      20,     ' ',' ','П','е','ч','а','т','ь',' ','X','-','О','т','ч','ё','т','а',' ',' ',' ',
      20,     ' ',' ','З','а','к','р','ы','т','и','е',' ','с','м','е','н','ы',' ','Ф','Р',' ',
      20,     '*','Р','е','ж','и','м',' ','П','р','о','г','.',' ','П','а','р','а','м','.','*',
      4,      'Т','Р','К',':',
      3,      'Р','К',':',
      5,      '*','П','П','*',' ',
      20,     '*','И','Н','Ф','*',' ',' ','Ц','Е','Н','А',' ','З','А',' ','Л','И','Т','Р',' ',
      20,     ' ','Г','р','а','н','и','т','-','2','м','к',' ','V','e','r',' ','1','.','1',' ',
      20,     ' ',' ','П','о','д','ч','и','н','ё','н','н','ы','й',' ','р','е','ж','и','м',' ',
      20,     ' ',' ',' ','А','к','т','и','в','н','ы','й',' ','р','е','ж','и','м',' ',' ',' ',
      20,     ' ','Ф','а','т','а','л','ь','н','а','я',' ','о','ш','и','б','к','а','!','!','!',
      20,     '*','C','у','м','м','а','р','н','ы','е',' ','с','ч','ё','т','ч','и','к','и','*',
      20,     'C','М','Е','Н','Н','Ы','Е',':','1',' ','Ф','И','С','К','А','Л','-','Е',':','2',
      20,     '*','C','М','.','С','Ч','*',' ','Т','Р','К',':',' ',' ',' ',' ',' ',' ',' ',' ',
      20,     'С','б','р','о','с','и','т','ь',' ','С','м','е','н','.','С','Ч','.','?',' ',' ',
      6,      'Н','Л','.','С','Ч','=',
      6,      'Ф','С','.','С','Ч','=',
      20,     ' ',' ',' ','П','е','ч','а','т','ь',' ','ч','е','к','а',' ',' ',' ',' ',' ',' ',
      20,     '*','П','Ф','Р','*',' ','П','а','р','о','л','ь',' ','К','а','с','с','и','р','а',
      20,     '*','П','Ф','Р','*',' ','Т','и','п',' ','Ф','Р',' ',' ',' ',' ',' ',' ',' ',' ',
      20,     ' ','В','ы','к','л','ю','ч','е','н','и','е',' ','п','и','т','а','н','и','я',' ',
      20,     'Н','Л',':','1',' ','Б','Н',':','2',' ','П','Р',':','3',' ',' ',' ',' ',' ',' ',//'C','Б',':','4',' ',
      20,     ' ','П','о','т','е','р','я',' ','с','в','я','з','и',' ','с',' ','Т','Р','К',' ',
      20,     ' ','О','ш','и','б','к','а',' ','Ф','Р',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
      20,     ' ',' ',' ',' ','П','е','р','е','з','а','п','у','с','к','!','!','!',' ',' ',' ',
      20,     ' ','З','а','п','р','о','с',' ','с','о','с','т','о','я','н','и','я',' ','Ф','Р',
      20,     ' ','У','с','т','а','н','о','в','к','а',' ','в','р','е','м','е','н','и',' ',' ',
      20,     '*','C','М','.','С','Ч','*',' ',' ',' ','С','М','Е','Н','Н','Ы','Е',' ',' ',' ',
      20,     '*','О','Ф','Р','*',' ','Ф','И','С','К','.',' ','О','Т','Ч','Ё','Т','Ы',' ',' ',
      20,     '№',' ','С','М','Е','Н',':','1',' ','П','О',' ','Д','А','Т','А','М',':','2',' ',
      20,     '*','Р','О','Ф','Р','*',' ',' ','Ф','О',' ','П','О',' ','Д','А','Т','А','М',' ',
      20,     'c',' ',' ','-',' ',' ','-',' ',' ',' ','п','о',' ',' ','-',' ',' ','-',' ',' ',
      20,     '*','О','Ф','Р','*',' ',' ','Ф','О',' ','П','О',' ','С','М','Е','Н','А','М',' ',
      20,     '*','О','б','щ','а','я',' ','к','о','н','ф','и','г','у','р','а','ц','и','я','*',
      20,     'У','с','т','-','к','и',':','1',' ','З','а','м','е','н','а',' ','П','O',':','2',
      20,     'П','р','о','и','з','в','е','с','т','и',' ','з','а','м','е','н','у',' ','П','О',
      20,     '*','К','о','н','ф','*',' ',' ','П','а','р','а','м','е','т','р',':',' ',' ',' ',
      20,     'П','а','р','о','л','ь',' ','о','п','е','р','-','р','а',':',' ',' ',' ',' ',' ',
      20,     'П','а','р','о','л','ь',' ','а','д','м','и','н','.',':',' ',' ',' ',' ',' ',' ',
      20,     'М','и','н','.',' ','д','о','з','а',':',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
      20,     'Т','е','х','н','о','л','о','г','и','ч','.',' ','о','т','п','у','с','к',':',' ',
      11,     'О','Ш','И','Б','К','А',' ','Т','Р','К',':',
      20,     ' ','В','В','Е','Д','И','Т','Е',' ','С','Е','К','Ц','И','Ю',':',' ',' ',' ',' ',
      8,      'В','о','з','в','р','а','т',':',
      8,      'п','е','ч','а','т','а','т','ь',
      11,     'н','е',' ','п','е','ч','а','т','а','т','ь',
      10,     'c','п','р','а','ш','и','в','а','т','ь',
      20,     'В','о','з','в','р','а','т','?',' ','Д','а',':','1',' ','О','т','м',':','3',' ',
      10,     'C','б','о','р','к','а',' ','о','т',' ',
      11,     'З','а','г','р','у','з','к','а',' ','П','О',
      9,      'Н','е','т',' ','с','в','я','з','и',
      10,     'Н','е','т',' ','б','у','м','а','г','и',
      7,      '2','4',' ','ч','а','с','а',
      11,     'Н','е','п','р','.',' ','р','е','ж','и','м',
      11,     'П','О','В',':','1',' ','О','Т','М',':','3',
      16,     'Н','е','в','е','р','н','ы','й',' ','т','и','п',' ','Т','Р','К',
      11,     'C','к','и','д','к','а',' ','№',' ',':',' ',

    Всё, к сожалению, не влезло - 117 строк!

    boolivar, 07 Июня 2012

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

    +135

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    // заполнить кадр на пересылку
    for (i = 1; i<frame[0]; i++) 
    {	sciOutFrame[i] = frame[i];}
    
    // Установим длину кадра ( в первый байт кадра )
    sciOutFrame[0] = i;

    boolivar, 07 Июня 2012

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