- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
#include <stdio.h>
int main()
{
void say_hello()
{
puts("Hello govnokod!");
}
say_hello();
return 0;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+113
#include <stdio.h>
int main()
{
void say_hello()
{
puts("Hello govnokod!");
}
say_hello();
return 0;
}
http://ideone.com/QZipp
+141
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 Автор походу уверен в гениальности кода.
+121
#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
+126
#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);
}
Поиск текущего каталога в дереве каталогов.
+133
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
}
Быстрый алгоритм поиска
+135
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.
Вот что такое настоящие магические числа.
+137
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
+130
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 файла всех директорий, поддиректорий и их файлов
+135
#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.
+137
#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;
// для всех остальных аналогично
}
странно, и почему контрольный код не совпадает с эталонными примерами...