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

    +140

    1. 1
    2. 2
    3. 3
    4. 4
    if ((u[i]!=100500)&&(v[j]==100500))
        v[j]=matr[i+1][j+1].cost-u[i];
    if ((u[i]==100500)&&(v[j]!=100500))
        u[i]=matr[i+1][j+1].cost-v[j];

    пересчет платежей в ТЗЛП

    resettik, 04 Мая 2011

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

    +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
    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
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    #include <sys/wait.h>
    void sighndlr_1(int signo) { //obrabot4ik dlya 1go processa
        printf("I'm child process #1. I've got SIGINT! :) So, I won't do anything...\n"); //prosto vyvod na ekran soobscheniya o polu4enii signala
    }
    
    void sighndlr_2(int signo) { //dlhya 2go
        printf("I'm child process #2. I've got SIGINT :)\n");
    }
    
    int main(int argc, char **argv) {
        pid_t pid1,pid2,pid3,pid4; //4 do4ernih processa
        static struct sigaction act1,act2,act3; //3 struktury dlya 3h processov
        act1.sa_handler = sighndlr_1; //ustanovka obrabot4ika dlya 1 struktory
        act2.sa_handler = sighndlr_2; //dlya 2i
        act3.sa_handler = SIG_IGN; //ignoriruem signal v 3 strukture
        switch(pid1 = fork()) { //sozdaem 1 do4erniy process
        case -1: //vihodim pri oshibke sozdaniya
    	printf("Error fork\n");
    	exit(1);
    	break;
        case 0:
    	printf("Child process #1 started (pid = %d)\n", getpid()); //soobshenie ob uspeshnom sozdanii i vyvod pid
    	sigaction(SIGINT,&act1,NULL); //ustanovka struktury obrabot4ika dlya SIGINT
    	for(;;) pause(); //beskone4nyi cikl
    	break;
        default:
        switch(pid2 = fork()) { //sozdaem 2 process
        case -1:
    	printf("Error fork\n");
    	exit(1);
    	break;
        case 0:
    	printf("Child process #2 started (pid = %d)\n", getpid());
    	sigaction(SIGINT,&act2,NULL); //dlya SIGINT
    	sigaction(SIGQUIT,&act3,NULL); //ignorim SIGQUIT
    	for(;;) pause();
    	break;
        default:
        switch(pid3 = fork()) { //3 process
        case -1:
    	printf("Error fork\n");
    	exit(1);
    	break;
        case 0:
    	printf("Child process #3 started (pid = %d)\n", getpid());
    	sigaction(SIGINT,&act3,NULL); //ignorim SIGINT
    	for(;;) pause();
    	break;
        default:
        switch(pid4 = fork()) { //4 process
        case -1:
    	printf("Error fork\n");
    	exit(1);
    	break;
        case 0:
    	printf("Child process #4 started (pid = %d)\n", getpid());
    	setsid(); //menyaem identifikator seansa dlya processa
    	printf("Process #4 changed sid\n");
    	for(;;) pause();
    	break;
        default:
        printf("Finishing parent process... (pid = %d)\n", getpid());
        exit(0); //zavershaetsya roditelsky process
        break;
        }        
        break;
        }
        break;
        }
        break;
        }
    }

    Лаба по курсу операционных систем. Нужно было создать 4 дочерних процесса, и для каждого процесса создать свои обработчики для сигнала SIGINT или SIGQUIT. Полученный говнокод полон повторяющихся конструкций, и слишком сильно запутан операторами switch-case.

    Boten, 30 Апреля 2011

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

    +142

    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
    char version[80] = "       POSIX compliant Communications test program version 1.00 4-25-1999\r\n";
       char version1[80] = "          Copyright(C) Mark Zehner/Peter Baumann 1999\r\n";
      
    fputs(version,output);               //display the program introduction
       fputs(version1,output);
       fputs(version2,output);
       fputs(version3,output);
       fputs(version4,output);
       fputs(version5,output);
       fputs(version6,output);
       fputs(version7,output);
       fputs(version8,output);
       fputs(version9,output);
       fputs(version10,output);
       fputs(version11,output); 
       fputs(version12,output);
       fputs(version13,output);

    http://www.comptechdoc.org/os/linux/programming/c/linux_pgcserial.html
    кришну вроде не вызывают. код странным образом напоминает индусский...

    ursus, 26 Апреля 2011

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

    +142

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    for(int i=0; i<16; i++) servoPos[i] = 64;
    	for(int i=0; i<16; i++) servoOnOff[i] = SV_OFF;
    	for(int i=0; i<16; i++) servoDirection[i] = SV_FOR;
    	for(int i=0; i<16; i++) servoRange[i] = 15;
    	for(int i=0; i<16; i++) servoCenter[i] = 3000;
    	for(int i=0; i<16; i++) servoSpeed[i] = 127;

    кусочек кетайского дзена.
    Программа управления серво-приводами (хоть бы не аэс, хоть бы не аэс)

    ursus, 26 Апреля 2011

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

    +142

    1. 1
    2. 2
    3. 3
    4. 4
    if(n==0) m[n][i][j][k+1]=1.5;
    if(n==1) m[n][i][j][k+1]=1.0;
    if(n==2) m[n][i][j][k+1]=0.0;
    if(n==3) m[n][i][j][k+1]=0.4;

    долго не мог заметить

    ragim, 23 Апреля 2011

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

    +143

    1. 1
    2. 2
    3. 3
    4. 4
    for (int i = 0; i < ptr->len; i++)
    {
    	*(((unsigned char *) ptr->payload) + i) = (unsigned char) toupper(*(((unsigned char *) ptr->payload) + i));
    }

    Перевод символов, находящихся в строке по адресу ptr->payload (типа void *) в верхний регистр.

    Fellrond, 21 Апреля 2011

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

    +145

    1. 1
    2. 2
    3. 3
    //Determine padding for use in allocating new memory
    int padding = 0;
    while ( (width * 3 + padding) % 4 != 0) padding++;

    http://www.cplusplus.com/forum/windows/6353/
    Ещё одна история о том, как людям не дано запомнить двоичную природу данных.
    Если что, вот корректный шаблон на D:

    int packed(int alignment)(int location) {
    static assert(alignment == 2 | alignment == 4 | alignment == 8);
    const badEnd = alignment - 1; //0b11, 0b111, 0b1111
    return location & badEnd ? (location & ~badEnd) + alignment : location;
    }

    denis-sh, 16 Апреля 2011

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

    +132

    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
    int cont[15];
    bool stop_access = false;
    struct cel container[MAX_SIZE];
    struct termios savetty;
    struct termios tty;
    pthread_t thread[100];
    pthread_mutexattr_t muattr; 
    pthread_mutex_t count_mutex; 
    int icn=0;//Number elements in container
    static int x;
    static int y;
    void* stack;
      pthread_mutex_unlock(&count_mutex); 
      sleep(1);
    };
    return (void*)1;
    };
    
    void* main_thread(void *arg){
       while(true){
       sleep(1);
      char ic=getchar();
        if(ic=='\n'){
          printf("Thread\tnumber elements\n");
          for(int i=0;i<=x;i++)
          pthread_cancel(thread[i]);
          for(int i=0;i<=x;i++){
    	printf("%d ",i);
    	int cn=0;
    	for(int j=0;j<=icn;j++){
    	  if(container[j].thrnm==i){ cn++; };
    	  };
    	  printf("\t%d\n",cn);
    	};
    	printf("container number element %d\n",icn);
    	exit(0);
          };
     };
      
    };
    
    int main(int argc, char * argv[])
    {
      icn=0;
      
    if(argc<2){printf("1 arg n number of threads, 2 argument maximum number element in container");exit(0);};
    x=atoi(argv[1]);
    y=atoi(argv[2]);
    if(x>64){printf("Число потоков должно быть меньше 64\n");exit(0);}
    printf("x %d y %d\n",x,y);
    //mutex initialization
    int ret;
    ret = pthread_mutexattr_init(&muattr);
    
      //switch the keyboard to noncanonical mode
    pid_t pt=tcgetsid(0);
     // char *var=ctermid (NULL);
      printf("pid %d\n",pt);
    tcgetattr (0, &tty);
    savetty = tty;
    tty.c_lflag &= ~(ICANON);
    tty.c_cc[VMIN] = 1;
    tcsetattr (0, TCSAFLUSH, &tty);
    pthread_t mthr;
     pthread_attr_t mattr;
      pthread_attr_init(&mattr);
      pthread_attr_setdetachstate(&mattr,PTHREAD_CREATE_DETACHED);
    int mres = pthread_create(&mthr, &mattr, main_thread, NULL);
      if (mres == 0) {
        printf("Creating main thread\n");
        sleep(0.7);
      } else {
        perror("Creating the main first thread");
      return EXIT_FAILURE;
      }
      
      
    for(int i=0;i<=x;i++){
      int id1, id2, result;
      id1 = 1;
      pthread_attr_t attr;
      pthread_attr_init(&attr);
      // отсоединенный поток - не ждем его возврата
      pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
      result = pthread_create(&thread[i], &attr, thread_func, &i);
      if (result == 0) {
        printf("Creating thread %d\n",i);
        sleep(1);
      } else {
        perror("Creating the first thread");

    AliceGoth, 14 Апреля 2011

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

    +147

    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
    void
    syslog(int pri, const char *fmt, ...)
    {
    	va_list ap;
    
    	va_start(ap, fmt);
    	vsyslog(pri, fmt, ap);
    	va_end(ap);
    }
    ......................................
    void
    vsyslog(int pri, const char *fmt, va_list ap)
    {
    	vsyslog_r(pri, &sdata, fmt, ap);
    }
    ....................................
    void
    vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
    {
    	int cnt;
    	char ch, *p, *t;
    	time_t now;
    	int fd, saved_errno, error;
    #define	TBUF_LEN	2048
    #define	FMT_LEN		1024
    	char *stdp = NULL, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
    	int tbuf_left, fmt_left, prlen;
    
    #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
    	/* Check for invalid bits. */
    	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
    		if (data == &sdata) {
    			syslog(INTERNALLOG,
    			    "syslog: unknown facility/priority: %x", pri);
    		} else {
    			syslog_r(INTERNALLOG, data,
    			    "syslog_r: unknown facility/priority: %x", pri);
    		}
    		pri &= LOG_PRIMASK|LOG_FACMASK;
    	}
    .......................
    }
    ......................

    Из исходников Bioninc - Android libc.
    В реализации сислога, в случае ошибки, вызывается syslog

    elenbert, 14 Апреля 2011

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

    +146

    1. 1
    #define TRUE (rand() > 0.1 ? TRUE : FALSE) // happy debugging losers

    понедельничный неговнокод "Где здесь Си" = )

    приятного дебага на неделе, коллеги ))

    Lure Of Chaos, 11 Апреля 2011

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