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

    +113

    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
    #include <stdio.h>
     
    int main()
    {
            void say_hello()
            {
                    puts("Hello govnokod!");
            }
     
            say_hello();
    
    	return 0;
    }

    http://ideone.com/QZipp

    rat4, 11 Июля 2012

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

    +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
    int main()
      {
        int i = 0;
        goto A;
        for (;i<3; ++i)
        L:{
            {
              int x;
              x += i;
              if (0) { A: x = 0; goto B; }
              printf("X: %d ",x);
            }
            {
              int y;
              y -= i;
              if (0) { B: y = 10; goto L; }
              printf("Y: %d\n",y);
            }
          }
      }

    Отсюда: http://rsdn.ru/forum/cpp/4812647.flat.aspx Автор походу уверен в гениальности кода.

    slavap, 10 Июля 2012

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

    +121

    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
    #include "uni.h"
    
    int getopt_long(int argc, char *const argv[],const char *optstring,const struct option *longopts, int *longindex);
    int getopt__long_only(int argc, char *const argv[],const char *optstring,const struct option *longopts, int *longindex);
    int chown(const char *path, uid_t owner, gid_t group);
    uid_t getuid(void);
    int mkdir(const char *pathname, mode_t mode);
    gid_t getgid(void);
    
    char *dirname;
    mode_t perm;
    uid_t owner;
    gid_t group;
    
    int main(int argc,char **argv){
      owner=getuid();
      group=getgid();
      perm=0600;
    char *pEnd;
    char *dirname=(char*)malloc(16);
      if(dirname==NULL){fprintf(stderr, "out of memory!\n");return -1;}
       memset( dirname,'\0',16); 
       strcat(dirname,argv[argc-1]);
    struct option longopts[] = {
      { "mode", required_argument, NULL, 'm' },
      { "parents", NULL, NULL, 'p'},
      { "verbose", required_argument, NULL, 'v'},
      { "context", required_argument, NULL, 'c'},
      { "help", NULL, NULL, 'h'},
      { "version", NULL, NULL, 'r'},
    { 0, 0, 0, 0 } 
    };
    char c;
    while((c=getopt_long(argc,argv,"m:p:v:c:r:h",longopts,NULL)) != -1) {
      switch (c) {
        case 'm':
          perm=strtol(optarg,&pEnd,8);
          printf("%s\n",pEnd);
          break;
        case 'p':
          if(rmdir(dirname)<0){
            fprintf(stderr, "don't manage to delete folder!\n");return -1;
          }
          break;
        case 'v':
          if(mkdir(dirname,perm)<0){
            fprintf(stderr, "mkdir: cannot create directory `%s`: file exists\n",dirname);return -1;
          } else {
            printf("mkdir: created directory `%s`\n",dirname);
            return 0;
          }
          break;
        case 'c':
          break;
        case 'h':
          printf("Usage: mkdir [OPTION]... DIRECTORY...\n");
          printf("Create the DIRECTORY(ies), if they do not already exist.\n");
          printf("\n");
          printf("Mandatory arguments to long options are mandatory for short options too.\n");
          printf("  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask\n");
          printf("  -p, --parents     no error if existing, make parent directories as needed\n");
          printf("  -v, --verbose     print a message for each created directory\n");
          printf("  -Z, --context=CTX  set the SELinux security context of each created\n");
          printf("                      directory to CTX\n");
          printf("      --help     display this help and exit\n");
          printf("      --version  output version information and exit\n");
          printf("\tAuthor AliceGoth\n");
          return 0;
          break;
        case 'r':
          printf("\tMy own mkdir\n");
          printf("\tVersion 1.0\n");
          printf("\tAuthor AliceGoth\n");
          printf("\tEmail [email protected]\n");
          break;
        case 1:

    Мой mkdir

    AliceGoth, 07 Июля 2012

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

    +126

    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
    95. 95
    96. 96
    97. 97
    #include "uni.h"
    #include "xreadlink.h"
    
    int process(char *dir);
    char *cd;
    char *scd;
    struct stat st1;
    
    ino_t cino;
    dev_t cdev;
    int main(){
    cd = (char *)malloc(160+1);
    if(cd==NULL){
     perror("Not enough memory");
     return -1;
    }
    scd = (char *)malloc(1600+1);
    if(scd==NULL){
     perror("Not enough memory");
     return -1;
    }
    memset(cd,'\0',1601);
    memset(scd,'\0',1601);
    strcat(scd,".");
    if(lstat(scd,&st1)==-1){
       perror("stat");
                   exit(EXIT_FAILURE);
    };
    cino = st1.st_ino;
    cdev = st1.st_rdev;
    //printf("inode %lu cdev %llu\n",cino,cdev);
      process(scd);
      return 0;
    };
    
    int process(char *dir){
      DIR *dp;
      struct dirent *ent;
      ino_t lino;
    dev_t ldev;
      ino_t lino2;
    dev_t ldev2;
    int cnt=0;
    char *mydir=(char*)malloc(1600);
    char *mydir2=(char*)malloc(1600);
    char **fpwd=(char**)malloc(160);
    for(int i=0;i<=160;i++){
      *(fpwd+i)=(char*)malloc(1600);
     if(*(fpwd+i)==NULL){perror("Not sufficient memory");
     return -1;
     }
    }
    if(mydir==NULL)return -1;
    if(*fpwd==NULL)return -1;
    strcpy(mydir,dir);
    strcpy(mydir2,dir);
    struct stat lstt;
    struct stat lstt2;
    struct stat stt;
      while(lino!=lino2 || ldev!=ldev2){
        if(lstat(mydir,&lstt)==-1){
            perror("not sufficient memory");
            return -1;
        }
        strcat(mydir2,"/..");
         if(lstat(mydir2,&lstt2)==-1){
            perror("not sufficient memory");
            return -1;
        }
        //printf("yes\n");
        lino=lstt.st_ino;
        ldev=lstt.st_rdev;
        lino2=lstt2.st_ino;
        ldev2=lstt2.st_rdev;
        if ((dp = opendir(mydir2)) == NULL) {
                    fprintf(stderr, "%s: cannot open for reading: %s\n", dir, strerror(errno));
                    return 1;
            }
          if(lstat(mydir2,&stt)==-1){
            perror("not sufficient memory");
            return -1;
          }
          //if(chdir("/")<0){perror("chdir");return -1;};
            while ((ent = readdir(dp)) != NULL){
             if(ent->d_ino==lino && strcmp(ent->d_name,".")!=0 && strcmp(ent->d_name,"..")!=0 && stt.st_rdev==ldev){
                    //printf("%8ld %s\n", ent->d_ino, ent->d_name);
                    strcat(fpwd[cnt++],ent->d_name);
             }
            }
            if (closedir(dp) != 0) {
                    fprintf(stderr, "%s: closedir: %s\n", dir, strerror(errno));
                    return 1;
            }
        //printf("mydir %s inode %lu cdev %llu inode %lu cdev %llu\n",mydir,lino,ldev,lino2,ldev2);
            strcat(mydir,"/..");
        strcpy(mydir2,mydir);
      }

    Поиск текущего каталога в дереве каталогов.

    AliceGoth, 07 Июля 2012

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

    +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
    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
    int * lPtr=item;
    int * lPtrEnd = item + count;
    
    for (lPtr = item ; lPtr < lPtrEnd && *lPtr<fid ; lPtr+=4);
    if( lPtr >= lPtrEnd ) lPtr=lPtrEnd-1; // last element
    if(*lPtr != fid)
    {
            if(--lPtr >=item)
            {   
                    if(*lPtr!= fid)
                    {   
                            if(--lPtr >=item)
                            {   
                                    if(*lPtr!= fid)
                                    {   
                                            if(--lPtr >=item)
                                            {   
                                                    if(*lPtr!= fid)
                                                    {   
                                                            break; // value not found
                                                    }   
                                            } else break;
                                    }   
                            }  else break;
                    }   
            } else break;
    }
    
    
    if( lPtr > item)
    {
            while ( *(lPtr-1) == fid && lPtr > item) lPtr--; // first ocurence of fid
    }

    Быстрый алгоритм поиска

    wtfcode, 03 Июля 2012

    Комментарии (43)
  6. Си / Говнокод #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)
  7. Си / Говнокод #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)
  8. Си / Говнокод #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)
  9. Си / Говнокод #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)
  10. Си / Говнокод #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)