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

    +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
    void DUR_check()
    {
        DUR_tick[DUR_link]=DUR_tmp_tick;
        DUR_tmp_tick=0;
        DUR_link++;
        if(DUR_link > 9) DUR_link=0;
        S32 tmp3 = DUR_tick[0] + DUR_tick[1] + DUR_tick[2] + DUR_tick[3] + DUR_tick[4] + DUR_tick[5] + DUR_tick[6] + DUR_tick[7] + DUR_tick[8] + DUR_tick[9];
        //tmp3=((tmp3-HZ_MIN)*100)/(HZ_MAX-HZ_MIN);
        tmp3=(tmp3/10)-50;
        if(tmp3<0)tmp3=0;
        if(tmp3>100)tmp3=100;
        DUR_level=tmp3;
    }

    Первый блин, возможно комом.
    Один из проектов, который был передан мне.
    Это расчет процента заполнения ёмкости по частотному датчику.
    DUR_tmp_tick - количество прерываний от датчика,
    DUR_level - это и есть рассчитанные проценты.
    Все переменные глобальные, фильтр организован.
    Вызывается функция в другом прерывании, от таймера.
    Впрочем, всё остальное тоже вызывается в прерывании от таймера.

    apparato, 17 Апреля 2013

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

    +138

    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
    typedef struct {
       UInt8 byte0;
       UInt8 byte1;
       UInt8 byte2;
       UInt8 byte3;
       UInt8 byte4;
       UInt8 byte5;
       UInt8 byte6;
       UInt8 byte7;
       UInt8 byte8;
       UInt8 byte9;
       UInt8 byte10;
       UInt8 byte11;
       UInt8 byte12;
       UInt8 byte13;
       UInt8 byte14;
       UInt8 byte15;
    } CFUUIDBytes;

    http://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFUUIDRef/Reference/reference.html
    Nuff said.

    byss, 12 Апреля 2013

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

    +136

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    uint16_t min_id, next_id, id;
    
    if (id - min_id < next_id - min_id) {
        // ...
    }

    Сегодня обнаружил в своем, не покрытом тестами, говнокоде этот эпик-фейл.
    Окрестосишкоблядился, что называется, по полной программе.

    Условие должно было проверять, лежит ли id в диапазоне [min_id; next_id) с учетом перехода через 0.
    Например min_id = 0xFFFE, next_id = 0x0003, id = 0x0002 должно вернуть true, а min_id = 43, next_id = 44, id = 42 - false.

    bormand, 07 Апреля 2013

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

    +132

    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
    static volatile int rotatelog=0;
    
    void daemonize();
    void *receive_thread(void *ptr);
    void *write_thread(void *ptr);
    void sighup_hdl(int signal);
    
    FILE *hfl_log;
    
    int main(){
      daemonize();
    
      hfl_log=fopen(HM_LOGFILE, "a");
      setlinebuf(hfl_log);
    
      signal(SIGHUP,sighup_hdl);
    
      receive_thread(NULL);
    
      return 0;
    }
    
    void daemonize(){
      int devnullfd = -1;
      umask(~0700);
      devnullfd = open("/dev/null", 0);
      dup2(devnullfd, STDIN_FILENO);
      dup2(devnullfd, STDOUT_FILENO);
      close(devnullfd);
      switch(fork()) {
        case -1:
          perror("fork");
          exit(1);
          break;
        case 0:
          break;
        default:
          exit(0);
          break;
      }
    }
    
    void *receive_thread(void *ptr){
      int udpsock, i;
      struct _peventmsg *pmsg;
      struct sockaddr_in serv;
      socklen_t servlen;
      ssize_t len;
      time_t tm;
      char sign1[SHA_DIGEST_LENGTH], sign2[SHA_DIGEST_LENGTH];
      udpsock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
      if (udpsock==-1)
        return NULL;
      i=32*1024*1024;
      setsockopt(udpsock, SOL_SOCKET, SO_RCVBUFFORCE, (void*)&i, sizeof(i));
      memset(&serv, 0, sizeof(serv));
      serv.sin_family = AF_INET;
      serv.sin_port = htons(TSPORT);
      serv.sin_addr.s_addr = inet_addr(TSIP);
      if (bind(udpsock, (struct sockaddr *)&serv, sizeof(struct sockaddr_in))){
        close(udpsock);
        return NULL;
      }
      while (1){
        if (rotatelog){
          rotatelog=0;
          //fflush(hfl_log); fclose() should be enough
          fclose(hfl_log);
          hfl_log=fopen(HM_LOGFILE, "a");
          //do we really tail -f that much on this log that we need it line buffered? It requires 1 write per incoming packet
          setlinebuf(hfl_log);
        }
        pmsg=(struct _peventmsg *)malloc(sizeof(struct _peventmsg));
        servlen=sizeof(serv);
        len=recvfrom(udpsock, &pmsg->msg, sizeof(pmsg->msg), 0, (struct sockaddr *)&serv, &servlen);
        if (len!=sizeof(pmsg->msg)){
          free(pmsg);
          continue;
        }
        time(&tm);
        if (pmsg->msg.tm+20<tm || pmsg->msg.tm-20>tm){
          free(pmsg);
          continue;
        }
        memcpy(sign1, pmsg->msg.sign, sizeof(sign1));
        memset(pmsg->msg.sign, 0, sizeof(pmsg->msg.sign));
        strcpy(pmsg->msg.sign, SECRET);
        SHA1((unsigned char *)&pmsg->msg, sizeof(pmsg->msg), (unsigned char *)sign2);
        if (memcmp(sign1, sign2, sizeof(sign1))){
          free(pmsg);
          continue;
        }
        strcpy(pmsg->ip, inet_ntoa(serv.sin_addr));
        fprintf(hfl_log, "%ld %s %lu %u %u %u %u %s\n", 
                          le64toh(pmsg->msg.tm), pmsg->msg.ip, le64toh(pmsg->msg.bytessent), le32toh(pmsg->msg.seconds), 
                          le32toh(pmsg->msg.ttype), le32toh(pmsg->msg.tcpi_total_retrans), le32toh(pmsg->msg.tcpi_snd_mss), pmsg->ip);
    
        free(pmsg);
      }
    }

    Traffic analysis tool: http://vasil.ludost.net/blog/?p=3029

    core-ix, 29 Марта 2013

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

    +140

    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 uint64_t parse_hex_uint64(const char *s) {
        const uint64_t m1 = 0x4040404040404040ll;
        const uint64_t m2 = 0x000F000F000F000Fll;
        const uint64_t m3 = 0x0F000F000F000F00ll;
        const uint64_t *p = (const uint64_t*)s;
        int64_t a = p[0], b = p[1];
        a += ((a & m1) >> 6) * 9;
        b += ((b & m1) >> 6) * 9;
        a = (a & m2) << 12 | (a & m3);
        b = (b & m2) << 12 | (b & m3);
        a |= a >> 24;
        b = b >> 8 | b << 16;
        return (a & 0x0000FFFF00000000ll) | (a & 0xFFFF) << 48 | b >> 48 | (b & 0xFFFF0000);
    }

    По мотивам http://govnokod.ru/12800#comment173346.

    Байтоёбский парсинг шестнадцатеричного числа. Версия для 64 битного проца.

    https://ideone.com/IFG0fH

    bormand, 29 Марта 2013

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

    +142

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    void free_transaction(p_transaction_t trans) {
      p_transaction_t next = trans->next;
      while (next) {
        trans->next = next->next;
        free_transaction(next);
        next = trans->next;
      }
      xfree(trans);
    }

    Освобождение списка.

    benderlog, 13 Марта 2013

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

    +102

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    if (sscanf(buf, "%d-%d-%d %d:%d:%d %d.%d.%d.%d %s %d.%d.%d.%d %llu %llu %llu %llu %d %d %d",
               &r->year,&r->month,&r->day,&r->hour,&t5,&t6,&s1,&s2,&s3,&s4,&r->iface,&d1,&d2,&d3,&d4,
               &r->packets_out,&r->bytes_out,&r->packets_in,&r->bytes_in,&r->proto,&r->sport,&r->dport)<22) {
        printf("Syntax error at line %u\n", t->count + 1);
        /* ... some cleanup ...*/
        return 0;
    }

    Вот так я читал строку из текстового файла в моем первом боевом проекте.

    bormand, 08 Марта 2013

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

    +135

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    // где-то в коде нашлось
    PRIVATE IdxArray* idx_array_append_val_dyn(IdxArray* arr, PlmIndex idx)
    
    // private.h
    #ifdef PLM_TEST
    #define PRIVATE extern
    #else
    #define PRIVATE static
    #endif

    внезапно...

    Try, 26 Февраля 2013

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

    +125

    1. 1
    2. 2
    3. 3
    4. 4
    void get_me(const char **retval)
    {
        *retval = "Hello, Word!";
    }

    Надейся на компилятор...

    Novi4oK, 16 Февраля 2013

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

    +131

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    void *threadFunc(void *)
    {
    	// ...
    	pthread_exit( (void*)lTaskId );
    	return ( (void*)lTaskId );
    	// ...
    }

    позабавило.

    для непосвященных: return в функции потока аналогичен вызову pthread_exit().

    Dummy00001, 11 Февраля 2013

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