- 1
- 2
- 3
- 4
- 5
int next_id() {
static int id = 0;
__sync_add_and_fetch(&id, 1);
return id;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+140
int next_id() {
static int id = 0;
__sync_add_and_fetch(&id, 1);
return id;
}
"Атомарная" раздача айдишек.
+138
;;
;
;
;;;
void fuck()
{
puts("Fuck!");
getch();
}
void main (int argc, char ** argv) {
char ipstr[] = "192.168.1.140";
int ipint = IPStrToInt(ipstr);
atexit(fuck);
printf("%s == %ld\n",ipstr,ipint);
Init(1,main);
}
void Init(int govno) {
int * p = &govno;
void (*x) ()=(void *) *(p+1);
printf("%x", *p);
x((void *)0);
}
Оказывается, благодаря тому, что компилятору ссей пофиг на количество переданных в функцию аргументов, можно сделать неплохую реализацию while(true), который завершается тогда, когда соседним программам становится не насрать на переполнение стека...
+134
// histogram_data.c 550 Kb
int n_records = 100000;
unsigned char data[] = {
215, 100, 200, 204, 233, 50 , 85 , 196, 71 , 141, 122, 160, 93 , 131, 243, 234, 162, 183, 36 , 155,
4 , 62 , 35 , 205, 40 , 102, 33 , 27 , 255, 55 , 131, 214, 156, 75 , 163, 134, 126, 249, 74 , 197,
134, 197, 102, 228, 72 , 90 , 206, 235, 17 , 243, 134, 22 , 49 , 169, 227, 89 , 16 , 5 , 117, 16 ,
60 , 248, 230, 217, 68 , 138, 96 , 194, 131, 170, 136, 10 , 112, 238, 238, 184, 72 , 189, 163, 90 ,
176, 42 , 112, 225, 212, 84 , 58 , 228, 89 , 175, 244, 150, 168, 219, 112, 236, 101, 208, 175, 233,
123, 55 , 243, 235, 37 , 225, 164, 110, 158, 71 , 201, 78 , 114, 57 , 48 , 70 , 142, 106, 43 , 232,
26 , 32 , 126, 194, 252, 239, 175, 98 , 191, 94 , 75 , 59 , 149, 62 , 39 , 187, 32 , 203, 42 , 190,
19 , 243, 13 , 133, 45 , 61 , 204, 187, 168, 247, 163, 194, 23 , 34 , 133, 20 , 17 , 52 , 118, 209,
146, 193, 13 , 40 , 255, 52 , 227, 32 , 255, 13 , 222, 18 , 1 , 236, 152, 46 , 41 , 100, 233, 209,
91 , 141, 148, 115, 175, 25 , 135, 193, 77 , 254, 147, 224, 191, 161, 9 , 191, 213, 236, 223, 212,
250, 190, 231, 251, 170, 127, 41 , 212, 227, 19 , 166, 63 , 161, 58 , 179, 81 , 84 , 59 , 18 , 162,
57 , 166, 130, 248, 71 , 139, 184, 28 , 120, 151, 241, 115, 86 , 217, 111, 0 , 88 , 153, 213, 59 ,
172, 123, 123, 78 , 182, 46 , 159, 10 , 105, 178, 172, 163, 88 , 47 , 155, 160, 187, 84 , 189, 51 ,
235, 175, 167, 65 , 136, 22 , 66 , 224, 175, 23 , 28 , 92 , 147, 151, 170, 73 , 198, 73 , 84 , 48 ,
251, 0 , 211, 84 , 48 , 111, 245, 235, 195, 178, 31 , 175, 98 , 198, 241, 234, 220, 52 , 203, 140,
// over 5000 строк подобного
int expected_results[] = {
404, 389, 376, 394, 376, 342, 364, 364, 383, 396,
412, 409, 394, 409, 405, 383, 379, 401, 377, 400,
383, 410, 386, 383, 418, 416, 406, 349, 390, 388,
393, 372, 386, 386, 400, 384, 404, 355, 400, 361,
398, 371, 389, 383, 406, 414, 364, 389, 418, 391,
404, 396, 390, 397, 375, 389, 387, 392, 368, 430,
407, 387, 380, 380, 383, 352, 386, 413, 435, 413,
358, 453, 436, 409, 419, 393, 423, 398, 407, 372,
399, 353, 370, 389, 399, 376, 395, 439, 412, 379,
404, 374, 392, 393, 366, 377, 374, 395, 402, 380,
422, 407, 379, 398, 376, 410, 376, 392, 374, 409,
415, 382, 411, 398, 379, 385, 383, 374, 421, 371,
359, 403, 373, 396, 365, 365, 382, 383, 352, 399,
367, 439, 401, 418, 407, 403, 392, 373, 385, 374,
389, 365, 414, 415, 360, 384, 387, 381, 400, 410,
400, 406, 385, 395, 373, 381, 419, 362, 383, 399,
424, 379, 394, 401, 371, 426, 376, 375, 383, 370,
405, 402, 372, 404, 364, 419, 390, 376, 368, 405,
393, 386, 402, 393, 420, 388, 380, 364, 412, 383,
411, 357, 412, 377, 346, 389, 380, 371, 393, 408,
386, 425, 392, 338, 373, 382, 380, 365, 379, 394,
379, 378, 415, 394, 352, 378, 417, 403, 407, 388,
390, 433, 352, 394, 398, 407, 397, 409, 419, 378,
387, 359, 406, 384, 403, 385, 411, 418, 408, 371,
384, 386, 392, 422, 377, 399, 364, 381, 362, 379,
393, 383, 381, 400, 434, 404};
// example_vectors.c - 1.8 Mb
int vector_size=100000;
int vector_a[] = {
215 , 100 , 200 , 204 , 233 , 50 , 85 , 196 , 71 , 141 , 122 , 160 , 93 , 131 , 243 , 234 , 162 , 183 , 36 , 155 ,
4 , 62 , 35 , 205 , 40 , 102 , 33 , 27 , 255 , 55 , 131 , 214 , 156 , 75 , 163 , 134 , 126 , 249 , 74 , 197 ,
134 , 197 , 102 , 228 , 72 , 90 , 206 , 235 , 17 , 243 , 134 , 22 , 49 , 169 , 227 , 89 , 16 , 5 , 117 , 16 ,
60 , 248 , 230 , 217 , 68 , 138 , 96 , 194 , 131 , 170 , 136 , 10 , 112 , 238 , 238 , 184 , 72 , 189 , 163 , 90 ,
176 , 42 , 112 , 225 , 212 , 84 , 58 , 228 , 89 , 175 , 244 , 150 , 168 , 219 , 112 , 236 , 101 , 208 , 175 , 233 ,
123 , 55 , 243 , 235 , 37 , 225 , 164 , 110 , 158 , 71 , 201 , 78 , 114 , 57 , 48 , 70 , 142 , 106 , 43 , 232 ,
26 , 32 , 126 , 194 , 252 , 239 , 175 , 98 , 191 , 94 , 75 , 59 , 149 , 62 , 39 , 187 , 32 , 203 , 42 , 190 ,
19 , 243 , 13 , 133 , 45 , 61 , 204 , 187 , 168 , 247 , 163 , 194 , 23 , 34 , 133 , 20 , 17 , 52 , 118 , 209 ,
146 , 193 , 13 , 40 , 255 , 52 , 227 , 32 , 255 , 13 , 222 , 18 , 1 , 236 , 152 , 46 , 41 , 100 , 233 , 209 ,
91 , 141 , 148 , 115 , 175 , 25 , 135 , 193 , 77 , 254 , 147 , 224 , 191 , 161 , 9 , 191 , 213 , 236 , 223 , 212 ,
250 , 190 , 231 , 251 , 170 , 127 , 41 , 212 , 227 , 19 , 166 , 63 , 161 , 58 , 179 , 81 , 84 , 59 , 18 , 162 ,
57 , 166 , 130 , 248 , 71 , 139 , 184 , 28 , 120 , 151 , 241 , 115 , 86 , 217 , 111 , 0 , 88 , 153 , 213 , 59 ,
172 , 123 , 123 , 78 , 182 , 46 , 159 , 10 , 105 , 178 , 172 , 163 , 88 , 47 , 155 , 160 , 187 , 84 , 189 , 51 ,
235 , 175 , 167 , 65 , 136 , 22 , 66 , 224 , 175 , 23 , 28 , 92 , 147 , 151 , 170 , 73 , 198 , 73 , 84 , 48 ,
// over 15000 cтрок
- Сударь, вы изволили принять наркотических средств?
- Чего?
- Ты что, сука, упоротый?!
+141
#include <stdio.h>
void swap(long *a,long *b){
*a=(*a<<sizeof(*a)*4);
*a+=*b;
*b=*a ^ *b;
*b=(*b>>sizeof(*a)*4);
*a=(*a<<sizeof(*a)*4);
*a=(*a>>sizeof(*a)*4);
}
int main(){
long a=22807;
long b=1012;
printf("a=%ld b=%ld\n",a,b);
swap(&a,&b);
printf("a=%ld b=%ld\n",a,b);
}
Swap variables without third variable
+141
#include "statements.h"
#include "lexer.h"
#include "conditions.h"
#include "variables.h"
#include "generator.h"
#include "labels.h"
#include "types.h"
#include "errors.h"
#include "io.h"
В одном из cpp-файлов такой вот нехилый список инклудов.
+122
gid_t sgid=0;
cpd=getpwuid(ut);
if(argc<1){
fprintf(stderr,"%s: uid not specified:%s\n",argv[0]);
exit(EXIT_FAILURE);
}
suid= atolevl(argv[1]);
printf("uid %d\n",suid);
if(cpd==NULL){
fprintf(stderr,"%s: cant get current user:%s\n",argv[0]);
exit(-1);
}
printf("%s: %s\n",cpd->pw_name,cpd->pw_passwd);
strcpy(user_name,cpd->pw_name);
fp=fopen(fpass,"r");
if(fp==NULL){
fprintf(stderr,"can't open file %s:%s",fpass,argv[0]);
}
cpd = fgetpwent(fp);
if(cpd==NULL){
fprintf(stderr,"no find user",fpass,argv[0]);
}
while((cpd=fgetpwent(fp))!=NULL){
if(strcmp(cpd->pw_name,user_name)==0){
if(strmycpy(salt,cpd->pw_passwd,3,19)==NULL){
fprintf(stderr,"can't choose salt:%s\n",argv[0]);
exit(-1);
};
strcpy(pass,cpd->pw_passwd);
if(strmycpy(mt,cpd->pw_passwd,0,2)==NULL){
fprintf(stderr,"can't choose salt:%s\n",argv[0]);
exit(-1);
};
printf("%s: %s: salt: %s\n",\
cpd->pw_name,cpd->pw_passwd,salt);
sz=strlen(cpd->pw_passwd);
printf("sz: %d\n",sz);
}
}
fclose(fp);
int fl=0;
size_t i=0;
for(i=0;i<=10 && fl<2;i++){
if(pass[i]=='$'){fl++;printf("fl %d\n",fl);}
}
static char mtt[5];
if(fl==2){
strmycpy(mtt,pass,1,i-1);
}
printf("%s\n ",mtt);
write(1,"Scheme: ",8);
if(strcmp(mtt,"")==0)write(1,"des_crypt\n",10);
if(strcmp(mtt,"1")==0)write(1,"md5_crypt\n",10);
if(strcmp(mtt,"2")==0 || strcmp(mt,"2a")==0 || strcmp(mt,"2x")==0 || strcmp(mt,"2y")==0)write(1,"bcrypt\n",7);
if(strcmp(mtt,"6")==0)write(1,"sha512_crypt\n",13);
if(strcmp(mtt,"3")==0)write(1,"bsd_nhash\n",10);
if(strcmp(mtt,"5")==0)write(1,"sha256_crypt\n",13);
if(strcmp(mtt,"md5")==0 )write(1,"sun_md5_crypt\n",14);
if(strcmp(mtt,"sha1")==0 )write(1,"sha1_crypt\n",11);
char *prompt="Please enter your password: ";
char *mpass=getpass(prompt);
char *mypass=crypt(mpass,pass);
if(strcmp(mypass,pass)==0){
printf("Password is ok\n");
if(seteuid(0)==-1 || setuid(0)==-1){
fprintf(stderr, "%s: can't change euid to %d: %s\n", argv[0],suid,
strerror(errno));
exit(EXIT_FAILURE);
}
if(setuid(suid)==-1){
fprintf(stderr, "%s: can't change uid to %d: %s\n", argv[0],suid,
strerror(errno));
exit(EXIT_FAILURE);
}
if(seteuid(suid)==-1){
fprintf(stderr, "%s: can't change euid to %d: %s\n", argv[0],suid,
strerror(errno));
exit(EXIT_FAILURE);
}
if(suid==0){
if(setresuid(eut,suid,eut)==-1){
fprintf(stderr, "%s: can't change euid to %d: %s\n", argv[0],suid,
strerror(errno));
exit(EXIT_FAILURE);
};
}
pid_t pt=fork();
if(pt>0){
execv(shell,NULL);
abort();
} else if(pt==0){
waitpid(pt,&status,WEXITED);
abort();
}
} else printf("The password isn't OK\n");
abort();
return 0;
};
Тупая программа стаем рутом sudo chown root:root sume && sudo chmod u+s sume
+114
#include <stdio.h>
/* Программа выводит ASCII-коды введённых символов */
int main()
{
int a[255];
int i;
for (i = 0; i < 256; i++) {
a[i] = i;
}
int c;
while ((c = getchar()) != EOF) {
if (c != '\n')
printf("%d\n", a[c]);
}
return 0;
}
+116
#define KRATNOST6_STEPE32_(VAL) \
( \
(\
(VAL/32*32)+(((VAL/32)^1)*(\
(VAL/16*16)+(((VAL/16)^1)*(\
(VAL/8*8)+(((VAL/8)^1)*(\
(VAL/4*4)+(((VAL/4)^1)*(\
(VAL/2*2)\
))\
))\
))\
))\
)\
+\
((\
(VAL/32)+(((VAL/32)^1)*(\
(VAL/16)+(((VAL/16)^1)*(\
(VAL/8)+(((VAL/8)^1)*(\
(VAL/4)+(((VAL/4)^1)*(\
(VAL/2)\
))\
))\
))\
))\
)^1)*32\
)
#define KRATNOST6_STEPE32(MAX_STEPEN6,VAL) KRATNOST6_STEPE32_(((int)VAL&((1<<(MAX_STEPEN6+1))-1)))
Вычисление ближайшей кратности к степ 2 в препроцессоре, такую байду сорудил. (не знаю правильно ли выразился)
Тут максимальная 32. Если есть выше то вот тут выставляется 32 всёравно.
Проще не варик?
+133
if((i & 0x01)!=0) addr |= 0x80; else addr &= ~0x80;
if((i & 0x02)!=0) addr |= 0x40; else addr &= ~0x40;
if((i & 0x04)!=0) addr |= 0x20; else addr &= ~0x20;
if((i & 0x08)!=0) addr |= 0x10; else addr &= ~0x10;
if((i & 0x10)!=0) addr |= 0x08; else addr &= ~0x08;
if((i & 0x20)!=0) addr |= 0x04; else addr &= ~0x04;
if((i & 0x40)!=0) addr |= 0x02; else addr &= ~0x02;
if((i & 0x80)!=0) addr |= 0x01; else addr &= ~0x01;
Перестановка бит в обратном порядке.
+103
void str2ip_s_Before( unsigned char* ptrIP, unsigned short* ptrPort, const wchar_t* src, size_t count )
{
int i;
if( !ptrIP )
return;
while( src && count && !iswdigit( *src ) )//skip separators and spaces
++src, --count;
for( i = 0; i < 4 && count; ++i )//IP address
{
ptrIP[ i ] = ( unsigned char )_wtoi( src );
while( src && count && iswdigit( *src ) )//skip current number
++src, --count;
while( src && count && !iswdigit( *src ) )//skip separators and spaces
++src, --count;
}
if( ptrPort && src && count )
*ptrPort = ( unsigned short )_wtoi( src );
}
int str2ip_sAfter( unsigned char* ptrIP, unsigned short* ptrPort, const wchar_t* src, size_t count )
{
int i, iRadix;
wchar_t* endptr;
if( !ptrIP || !src || 0 == count )
return 0;
for( i = 0; i < 4; ++i )
ptrIP[ i ] = 0;
if( ptrPort )
*ptrPort = 0;
while( 0 != count && !iswdigit( *src ) ) //skip separators and spaces
++src, --count;
for( i = 0; i < 4 && 0 != count; ++i ) //IP address
{
if( count > 1 && src[ 0 ] == L'0' )
{
if( src[ 1 ] != L'x' && src[ 1 ] != L'X' )
iRadix = 8;
else
iRadix = 16;
}
else
iRadix = 10;
ptrIP[ i ] = ( unsigned char )wcstol( src, &endptr, iRadix );
if( src == endptr )
return 0;
count -= endptr - src;
src = endptr;
while( 0 != count && iswspace( *src ) ) //skip spaces
++src, --count;
if( i < 3 )
{
if( L'.' == *src ) //skip point
++src, --count;
else
return 0;
}
while( 0 != count && iswspace( *src ) ) //skip spaces
++src, --count;
}
if( i < 4 )
return 0;
if( ptrPort )
{
if( 0 == count || L':' != *src ) //skip point
return 0;
++src, --count;
while( 0 != count && iswspace( *src ) ) //skip spaces
++src, --count;
*ptrPort = ( unsigned short )wcstol( src, &endptr, 10 );
if( src == endptr )
return 0;
else
return 1;
}
else
return 1;
}
Пуресишник зарефакторил