btwotch    

, Deutschland · männlich · registriert seit 2008 · heute zuletzt online


mailto, against spam!

Nazis on the moon or Nazis with Dinosaurs?

Umfrage · · 2 Kommentare

Trailer:
Iron Sky:

Iron Sky Teaser 3 - We Come In Peace!
Youtube - Video

Danger 5:

DANGER 5 TRAILER
Youtube - Video

Nazis on the moon or Nazis with Dinosaurs?
Diese Umfrage wurde am 19. Nov 2011 um 00:00 beendet.

75,0% (3)
Iron Sky (Nazis on the moon)
25,0% (1)
Danger 5 (Nazis with Dinosaurs)
12. November 2011 00:16

SSL-Client with gnutls

Computer ·

See also:

  • http://www.gnu.org/software/gnutls/manual/html_node/Simple-client-example-with-anonymous-authentication.html
  • http://codesearch.google.com/#58vgXOqJphs/src/gnutls.c&q=gnutls_transport_set_ptr%20lang:c&type=cs
    Here's the code:

        1 
        2 #include <stdio.h>
        3 #include <string.h>
        4 #include <stdlib.h>
        5 #include <sys/socket.h>
        6 #include <netdb.h>
        7 #include <gnutls/gnutls.h>
        8 
        9 #define EXIT_ON_ERROR(x) {fprintf(stderr, "critical: %s\n", x); exit(-1);}
       10 
       11 #define GET_MSG "GET / HTTP/1.0\r\n\r\n"
       12 int main(int argc, char **argv)
       13 {
       14   int fd, ret;
       15   char buf[16384];
       16   struct addrinfo *res;
       17   gnutls_session_t session;
       18   gnutls_anon_client_credentials_t anoncred;
       19 
       20   gnutls_global_init();
       21   gnutls_anon_allocate_client_credentials (&anoncred);
       22   gnutls_init (&session, GNUTLS_CLIENT);
       23   gnutls_priority_set_direct (session, "PERFORMANCE:+ANON-ECDH:+ANON-DH",
       24                               NULL);
       25   gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, anoncred);
       26 
       27   // connect
       28   if (getaddrinfo(argv[1], "443", NULL, &res) != 0)
       29     EXIT_ON_ERROR("getaddrinfo");
       30   fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
       31   if (fd == -1)
       32     EXIT_ON_ERROR("socket");
       33   if (connect(fd, res->ai_addr, res->ai_addrlen) == -1)
       34     EXIT_ON_ERROR("connect");
       35 
       36 
       37 //   send(fd, GET_MSG, strlen(GET_MSG), 0);
       38 //   recv(fd, buf, sizeof(buf), 0);
       39 
       40   gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) fd);
       41   ret = gnutls_handshake (session);
       42   if (ret < 0 )
       43     {
       44       gnutls_strerror (ret);
       45       EXIT_ON_ERROR("gnutls_handshake_session");
       46     }
       47 
       48   gnutls_record_send (session, GET_MSG, strlen (GET_MSG));
       49   if (gnutls_record_recv (session, buf, sizeof(buf)) < 0)
       50     EXIT_ON_ERROR("gnutls_record_recv");
       51 
       52   freeaddrinfo(res);
       53   gnutls_deinit(session);
       54 
       55   printf("%s\n", buf);
       56   close(fd);
       57 
       58   return 0;
       59 }

Compile: gcc tls_client.c `pkg-config --libs --cflags gnutls`
Usage: ./a.out www.heise.de

Memleaks:

==1676== LEAK SUMMARY:
==1676== definitely lost: 0 bytes in 0 blocks
==1676== indirectly lost: 0 bytes in 0 blocks
==1676== possibly lost: 0 bytes in 0 blocks

Try this with openssl :popcorn:

11. November 2011 12:00

Tags:  ·  ·  ·  ·  ·

13. September 2011

Sonstiges ·

#osc11 8bit music on gameboy

13. September 2011 21:10

Tags:

12. September 2011

Sonstiges ·

#osc11

12. September 2011 18:06

Tags:

Simple Backup Tool

Sonstiges ·

I had the choice:

  1. setup git (finally)
  2. create a backup program with (simple) versioning support

of course I chose option 2 :grins:

it walks a directory tree and saves files (backup filename: sha1sum) as a lzma-archive; moreover it saves
some information in a sqlite3-database (id, user, hostname, filetype, hash, filepath ...)

Here's the code:

    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <stdlib.h>
    4 #include <sys/types.h>
    5 #include <sys/stat.h>
    6 #include <dirent.h>
    7 #include <sys/stat.h>
    8 #include <fcntl.h>
    9 #include <unistd.h>
   10 #include <libgen.h>
   11 #include <time.h>
   12 #include <magic.h>
   13 #include <archive.h>
   14 #include <archive_entry.h>
   15 #include <sqlite3.h>
   16 
   17 #include <openssl/sha.h>
   18 #include <openssl/md5.h>
   19 
   20 #error "complete BACKUP_BASE define"
   21 #define BACKUP_BASE "/somewhere"
   22 #define BACKUP_DIR BACKUP_BASE"/"
   23 #define BACKUPDB_FILE BACKUP_BASE"/db"
   24 
   25 #define USE_SHA1
   26 #ifdef USE_SHA1
   27 #define DIGEST_LEN SHA_DIGEST_LENGTH
   28 #else
   29 #define DIGEST_LEN MD5_DIGEST_LENGTH
   30 #endif
   31 
   32 
   33 #define CREATE_FILETABLE "CREATE TABLE IF NOT EXISTS files (id TEXT NOT
   34                                                             NULL PRIMARY
   35                                                             KEY, user
   36                                                             VARCHAR(20),
   37                                                             filepath TEXT
   38                                                             NOT NULL, type
   39                                                             TEXT NOT NULL,
   40                                                             hashsum TEXT
   41                                                             NOT NULL,
   42                                                             hostname
   43                                                             VARCHAR(256),
   44                                                             comment VARCHAR(
   45                                                             256), version
   46                                                             VARCHAR(256),
   47                                                             time BIGINT);"
   48 #define INSERT_FILE "INSERT INTO files (id, user, filepath, type, hashsum,
   49                                         hostname, comment, version, time)
   50                                         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
   51                                         "
   52 
   53 void save_file(int fd, char *filename, char *targetfilepath, struct stat
   54                *st)
   55 {
   56   int len;
   57   char buf[8192];
   58   struct archive *a;
   59   struct archive_entry *entry;
   60   struct stat tmp_st;
   61 
   62   if (stat(targetfilepath, &tmp_st) == 0)
   63     return;
   64 
   65 
   66   a = archive_write_new();
   67   archive_write_set_compression_xz(a);
   68   archive_write_set_format_pax_restricted(a);
   69   archive_write_open_filename(a, targetfilepath);
   70   entry = archive_entry_new();
   71   archive_entry_set_mtime(entry, time(NULL), 0);
   72   archive_entry_set_pathname(entry, filename);
   73   archive_entry_set_size(entry, st->st_size);
   74   archive_entry_set_filetype(entry, AE_IFREG);
   75   archive_entry_set_perm(entry, 0644);
   76   archive_write_header(a, entry);
   77   lseek(fd, 0, SEEK_SET);
   78   while ((len = read(fd, buf, sizeof(buf))) > 0)
   79     archive_write_data(a, buf, len);
   80 
   81   archive_entry_free(entry);
   82   archive_write_close(a);
   83   archive_write_finish(a);
   84 }
   85 
   86 void save_fileinfo(sqlite3 *db, unsigned char *digest, char *filepath, char
   87                    *type, char *hostname, char *user, char *comment, char
   88                    *version, unsigned int backup_time)
   89 {
   90   int i, j;
   91   char digest_id_char[SHA_DIGEST_LENGTH+SHA_DIGEST_LENGTH+1], digest_char[
   92   DIGEST_LEN+DIGEST_LEN+1];
   93   unsigned char digest_id[SHA_DIGEST_LENGTH];
   94   sqlite3_stmt *stmt;
   95   SHA_CTX ctx;
   96 
   97 
   98   SHA1_Init(&ctx);
   99 
  100   SHA1_Update(&ctx, digest, DIGEST_LEN);
  101   SHA1_Update(&ctx, hostname, strlen(hostname));
  102   SHA1_Update(&ctx, &backup_time, sizeof(backup_time));
  103   SHA1_Update(&ctx, user, strlen(user));
  104   SHA1_Update(&ctx, filepath, strlen(filepath));
  105   SHA1_Final(digest_id, &ctx);
  106 
  107   for (i = 0, j = 0; i < SHA_DIGEST_LENGTH; i++, j+=2)
  108     sprintf(digest_id_char+j, "%02x", digest_id[i]);
  109   for (i = 0, j = 0; i < DIGEST_LEN; i++, j+=2)
  110     sprintf(digest_char+j, "%02x", digest[i]);
  111 
  112   sqlite3_prepare_v2(db, INSERT_FILE, -1, &stmt, 0);
  113   sqlite3_bind_text(stmt, 1, digest_id_char, -1, SQLITE_STATIC);
  114   sqlite3_bind_text(stmt, 2, user, -1, SQLITE_STATIC);
  115   sqlite3_bind_text(stmt, 3, filepath, -1, SQLITE_STATIC);
  116   sqlite3_bind_text(stmt, 4, type, -1, SQLITE_STATIC);
  117   sqlite3_bind_text(stmt, 5, digest_char, -1, SQLITE_STATIC);
  118   sqlite3_bind_text(stmt, 6, hostname, -1, SQLITE_STATIC);
  119   sqlite3_bind_text(stmt, 7, comment, -1, SQLITE_STATIC);
  120   sqlite3_bind_text(stmt, 8, version, -1, SQLITE_STATIC);
  121   sqlite3_bind_int(stmt, 9, backup_time);
  122   if (sqlite3_step(stmt) != SQLITE_DONE)
  123     {
  124       fprintf(stderr, "Can't insert: %s\n", sqlite3_errmsg(db));
  125       exit(1);
  126     }
  127   sqlite3_finalize(stmt);
  128 
  129 }
  130 
  131 void process_file(char *filepath, struct stat *st, sqlite3 *db, unsigned
  132                   int backup_time)
  133 {
  134   int i, j, len, fd;
  135   char buf[8192];
  136   char type[20];
  137   magic_t m;
  138   unsigned char digest[DIGEST_LEN];
  139 #ifdef USE_SHA1
  140   SHA_CTX ctx;
  141   SHA1_Init(&ctx);
  142 #else
  143   MD5_CTX ctx;
  144   MD5_Init(&ctx);
  145 #endif
  146   char backup_file[strlen(BACKUP_DIR)+1+(2*sizeof(digest))+7+1];
  147 
  148   if (!strcmp(filepath, BACKUPDB_FILE))
  149     return;
  150   fd = open(filepath, 0, O_RDONLY);
  151   if (fd == -1)
  152     return;
  153 
  154   m = magic_open(MAGIC_MIME_TYPE);
  155   magic_load(m, NULL);
  156   magic_compile(m, NULL);
  157 
  158   len=read(fd, buf, sizeof(buf));
  159 
  160 #ifdef USE_SHA1
  161   SHA1_Update(&ctx, buf, len);
  162 #else
  163   MD5_Update(&ctx, buf, len);
  164 #endif
  165   snprintf(type, 19, "%s", magic_buffer(m, buf, len));
  166   printf("type: %s ", type);
  167 
  168   while ((len=read(fd, buf, sizeof(buf))) > 0)
  169 #ifdef USE_SHA1
  170     SHA1_Update(&ctx, buf, len);
  171 #else
  172     MD5_Update(&ctx, buf, len);
  173 #endif
  174 
  175   magic_close(m);
  176 
  177 #ifdef USE_SHA1
  178   SHA1_Final(digest, &ctx);
  179 #else
  180   MD5_Final(digest, &ctx);
  181 #endif
  182   for (i = 0; i < sizeof(digest); i++)
  183     printf("%02x", (unsigned char)digest[i]);
  184 
  185 
  186   printf(" %s ", filepath);
  187 
  188   if (S_ISREG(st->st_mode))
  189     printf("normal file\n");
  190 
  191   snprintf(backup_file, strlen(BACKUP_DIR)+2, "%s/", BACKUP_DIR);
  192   for (i = 0, j = 0; i < sizeof(digest); i++, j+=2)
  193     sprintf(backup_file+strlen(BACKUP_DIR)+1+j, "%02x", (unsigned char)
  194             digest[i]);
  195 
  196   sprintf(backup_file+strlen(BACKUP_DIR)+1+(2*sizeof(digest)), ".tar.xz");
  197   save_file(fd, basename(filepath), backup_file, st);
  198   save_fileinfo(db, digest, filepath, type, "erebos", getenv("USER"),
  199                 "comment", "version", backup_time);
  200 
  201   close(fd);
  202 }
  203 
  204 void walktree(char *base_dir, sqlite3 *db, unsigned int backup_time)
  205 {
  206   struct stat st;
  207   struct dirent *entry;
  208   DIR *d;
  209   char *filepath = realpath(base_dir, NULL);
  210   char *real_backuppath = realpath(BACKUP_DIR, NULL);
  211   int filepath_len = strlen(filepath);
  212 
  213   if (!strncmp(filepath, real_backuppath, strlen(real_backuppath)))
  214     goto frees;
  215   if (basename(filepath)[0] == '.')
  216     goto frees;
  217   d = opendir(filepath);
  218   lstat(filepath, &st);
  219   if (S_ISREG(st.st_mode))
  220     {
  221       process_file(filepath, &st, db, backup_time);
  222       goto ret;
  223     }
  224   else if (!S_ISDIR(st.st_mode))
  225     goto ret;
  226 
  227   filepath = realloc(filepath, PATH_MAX);
  228 
  229   while ((entry = readdir(d)) != NULL)
  230     {
  231       if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
  232         continue;
  233       if (filepath_len + strlen(entry->d_name) >= PATH_MAX)
  234         {
  235           fprintf(stderr, "wrong filepath :(\n");
  236           exit(-1);
  237         }
  238 
  239       sprintf(filepath+filepath_len, "/%s", entry->d_name);
  240 
  241       lstat(filepath, &st);
  242       if (S_ISREG(st.st_mode))
  243         process_file(filepath, &st, db, backup_time);
  244       else if (S_ISDIR(st.st_mode))
  245         walktree(filepath, db, backup_time);
  246 
  247     }
  248 
  249 
  250 ret:
  251   closedir(d);
  252 frees:
  253   free(filepath);
  254   free(real_backuppath);
  255 }
  256 
  257 sqlite3* init_db()
  258 {
  259   sqlite3 *db;
  260   char *errmsg = 0;
  261 
  262   sqlite3_open(BACKUPDB_FILE, &db);
  263 
  264   if (sqlite3_exec(db, CREATE_FILETABLE, NULL, 0, &errmsg) != SQLITE_OK)
  265     {
  266       fprintf(stderr, "Can't create table: %s\n%s\n", sqlite3_errmsg(db),
  267               CREATE_FILETABLE);
  268       return NULL;
  269     }
  270 
  271   return db;
  272 }
  273 
  274 int main(int argc, char **argv)
  275 {
  276   sqlite3 *db;
  277 
  278   if (argc != 2)
  279     {
  280       fprintf(stderr, "%s <dirpath>\n", argv[0]);
  281       exit(-1);
  282     }
  283 
  284   db = init_db();
  285   if (db == NULL)
  286     exit(-1);
  287   walktree(argv[1], db, time(NULL));
  288 
  289   sqlite3_close(db);
  290   return 0;
  291 }

Makefile:

all: backup.c
        gcc -std=gnu99 -lssl backup.c -lmagic -Wall -g -larchive -lsqlite3 -o backup

Restore tool will be written as soon as I need it :poptart:

27. August 2011 18:11

Tags:  ·  ·

Very simple 2D Game (SDL)

Computer ·

In case you wanna create a 2D SDL game:

    1 #include <SDL/SDL.h>
    2 #include <SDL/SDL_image.h>
    3 #include <SDL/SDL_gfxPrimitives.h>
    4 #include <stdio.h>
    5 #include <stdlib.h>
    6 #include <unistd.h>
    7 
    8 
    9 #define GAME_X 640
   10 #define GAME_Y 400
   11 
   12 #define MAP_MAX_X 10000
   13 #define MAP_MAX_Y 10000
   14 enum object_type
   15 {
   16   BACKGROUND,
   17   PLAYER
   18 };
   19 
   20 struct objects
   21 {
   22   enum object_type type;
   23   uint16_t id;
   24 
   25   uint32_t map_x;
   26   uint32_t map_y;
   27 
   28   int16_t speed_x;
   29   int16_t speed_y;
   30 };
   31 
   32 struct video_objects // 1st is background
   33 {
   34   struct objects o;
   35   SDL_Surface *surface;
   36   struct video_objects *next;
   37 
   38   uint16_t x;
   39   uint16_t y;
   40   uint8_t w;
   41   uint8_t h;
   42 };
   43 
   44 struct
   45 {
   46   uint16_t x;
   47   uint16_t y;
   48 } map_quadrant;
   49 
   50 SDL_Surface *background = NULL;
   51 SDL_Surface *screen = NULL;
   52 
   53 uint32_t now_ticks;
   54 
   55 struct objects *player;
   56 
   57 
   58 //------------------------------------------
   59 
   60 void motion(struct objects *o)
   61 {
   62 
   63   if ((int32_t)(o->map_x - o->speed_x) < 0)
   64     {
   65       o->map_x = 0;
   66       o->speed_x = 1;
   67     }
   68 
   69   if ((int32_t)(o->map_y - o->speed_y) < 0)
   70     {
   71       o->map_y = 0;
   72       o->speed_y = 1;
   73     }
   74 
   75   if ((int32_t)(o->map_y + o->speed_y) > MAP_MAX_Y)
   76     {
   77       o->map_y = MAP_MAX_Y;
   78       o->speed_y = -1;
   79     }
   80 
   81   if ((int32_t)(o->map_x + o->speed_x) > MAP_MAX_X)
   82     {
   83       o->map_x = MAP_MAX_X;
   84       o->speed_x = -1;
   85     }
   86 
   87   o->map_x += o->speed_x;
   88   o->map_y += o->speed_y;
   89 
   90 }
   91 
   92 void player_position(int *x, int *y) // depends on map quadrant
   93 {
   94   *x = player->map_x % (GAME_X-20);
   95   *y = player->map_y % (GAME_Y-20);
   96 }
   97 
   98 void draw_info()
   99 {
  100   char pos[50];
  101 
  102   boxRGBA(screen, GAME_X+2, GAME_Y-10, 1024, GAME_Y, 0, 40, 0, 255);
  103   snprintf(pos, 50, "pos: %d/%d quadrant: %d/%d speed: %d/%d", player->
  104            map_x, player->map_y, map_quadrant.x, map_quadrant.y, player->
  105            speed_x, player->speed_y);
  106   stringRGBA(screen, GAME_X+2, GAME_Y-9, pos, 255, 0, 0, 255);
  107 }
  108 
  109 void draw(struct video_objects *vo)
  110 {
  111   SDL_Rect r;
  112 
  113   map_quadrant.x = player->map_x/GAME_X;
  114   map_quadrant.y = player->map_y/GAME_Y;
  115   do
  116     {
  117       switch (vo->o.type)
  118         {
  119         case BACKGROUND:
  120           SDL_BlitSurface(vo->surface, NULL, screen, NULL);
  121           break;
  122         case PLAYER:
  123           //r.x = vo->x;
  124           //r.y = vo->y;
  125           player_position(&r.x, &r.y);
  126           r.w = 20;
  127           r.h = 20;
  128           SDL_BlitSurface(vo->surface, NULL, screen, &r);
  129           draw_info();
  130           motion(&vo->o);
  131           break;
  132         }
  133 
  134       vo = vo->next;
  135     }
  136   while (vo != NULL);
  137 
  138 
  139   SDL_Flip(screen);
  140 
  141 }
  142 
  143 int events()
  144 {
  145   SDL_Event ev;
  146   SDL_keysym keysym;
  147 
  148 
  149   while (SDL_PollEvent(&ev) != 0)
  150     {
  151       keysym = ev.key.keysym;
  152       switch (ev.type)
  153         {
  154         case SDL_KEYDOWN:
  155           switch (keysym.sym)
  156             {
  157             case SDLK_q:
  158               return -1;
  159 
  160             case SDLK_UP:
  161             case SDLK_k:
  162               player->speed_y--;
  163               break;
  164             case SDLK_DOWN:
  165             case SDLK_j:
  166               player->speed_y++;
  167               break;
  168             case SDLK_RIGHT:
  169             case SDLK_l:
  170               player->speed_x++;
  171               break;
  172             case SDLK_LEFT:
  173             case SDLK_h:
  174               player->speed_x--;
  175               break;
  176             case SDLK_s:
  177               player->speed_x = 0;
  178               player->speed_y = 0;
  179               break;
  180             default:
  181               break;
  182             }
  183           break;
  184         }
  185     }
  186 
  187   return 0;
  188 }
  189 
  190 void wait()
  191 {
  192   uint32_t diff = SDL_GetTicks() - now_ticks;
  193   uint32_t delay = 30-diff;
  194 
  195   //printf("ticks: %u\n", diff);
  196 
  197   if (delay < 1000)
  198     SDL_Delay(delay);
  199   now_ticks = SDL_GetTicks();
  200 }
  201 
  202 struct video_objects *init_vos()
  203 {
  204   struct video_objects *vo = calloc(1, sizeof(struct video_objects));
  205 
  206   vo->next = NULL;
  207   //vo->surface = IMG_Load("pics/galaxy.png");
  208   vo->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, GAME_X, GAME_Y, 32, 0,0,
  209                 0,0);
  210   boxRGBA(vo->surface, 0, 0, GAME_X, GAME_Y, 0, 0, 0, 255);
  211   vo->o.type = BACKGROUND;
  212 
  213   return vo;
  214 }
  215 
  216 void add_player(struct video_objects *vo, uint16_t id)
  217 {
  218   while (vo->next != NULL)
  219     vo = vo->next;
  220 
  221   vo->next = calloc(1, sizeof(struct video_objects));
  222   vo = vo->next;
  223   vo->next = NULL;
  224   vo->o.type = PLAYER;
  225   vo->o.id = id;
  226   vo->surface = IMG_Load("pics/player1.png");
  227 
  228   vo->y = GAME_Y/2;
  229   vo->x = GAME_X/2;
  230   vo->w = vo->surface->w;
  231   vo->h = vo->surface->h;
  232 
  233   vo->o.map_x = 10;
  234   vo->o.map_y = 10;
  235   vo->o.speed_y = 0;
  236   vo->o.speed_x = 0;
  237 
  238   player = &vo->o;
  239 }
  240 
  241 
  242 struct video_objects *init_objects()
  243 {
  244   struct video_objects *vo;
  245 
  246   vo = init_vos();
  247   add_player(vo, 1);
  248   vlineRGBA(screen, GAME_X+1, 0, GAME_Y, 255, 0, 0, 255);
  249 
  250   return vo;
  251 }
  252 
  253 int main()
  254 {
  255   struct video_objects *vo;
  256 
  257   SDL_Init(SDL_INIT_VIDEO);
  258 
  259   atexit(SDL_Quit);
  260 
  261   screen = SDL_SetVideoMode(1024, GAME_Y, 16, SDL_HWSURFACE | SDL_DOUBLEBUF)
  262            ;
  263   vo = init_objects();
  264 
  265   if (screen == NULL)
  266     {
  267       printf("%s\n", SDL_GetError());
  268       exit(-1);
  269     }
  270 
  271   while (1)
  272     {
  273       draw(vo);
  274       if (events() == -1)
  275         break;
  276       wait();
  277     }
  278 
  279   printf("bye\n");
  280   return 0;
  281 }

compile:

gcc `sdl-config --libs --cflags` file.c -o file -lSDL_image -lSDL_gfx -g -Wall
06. August 2011 00:21

Tags:  ·  ·  ·

Give gif,png,jpg files right extension

Computer · · 2 Kommentare

  • just save as filetype.pl
  • ./filetype.pl <path to images>


    1 #!/usr/bin/perl -w
    2 
    3 use strict;
    4 use File::Type;
    5 use File::Find();
    6 
    7 my $ft = File::Type->new();
    8 
    9 sub analyze {
   10         my $file = $File::Find::name;
   11         my $newname = $file;
   12         my $type = $ft->checktype_filename($file);
   13         my $ext = ""; #extension
   14 
   15         $ext="jpg" if ($type eq "image/jpeg");
   16         $ext="png" if ($type eq "image/x-png");
   17         $ext="gif" if ($type eq "image/gif");
   18 
   19         if (not "$ext" eq "") {
   20                 $newname = $newname.".".$ext;
   21                 print "file: $file type: $type new name: $newname\n";
   22                 rename "$file", "$newname";
   23         }
   24 }
   25 
   26 #analyze $ARGV[0];
   27 
   28 File::Find::find({wanted => \&analyze}, "$ARGV[0]");

Update:

  • renames files to their md5sums
  • deletes pics if size is less than 53 x 53

        1 #!/usr/bin/perl -w
        2 
        3 use strict;
        4 use File::Type;
        5 use File::Find();
        6 use Image::Size;
        7 use Digest::MD5;
        8 
        9 my $ft = File::Type->new();
       10 my $md5 = Digest::MD5->new;
       11 
       12 sub analyze {
       13         my $file = $File::Find::name;
       14         return if (-d $file);
       15         my $newname = $file;
       16         my $type = $ft->checktype_filename($file);
       17         my $ext = "";
       18         my $x; my $y;
       19         my $sum;
       20 
       21         #print "dir: $file\n" if (-d $file);
       22         print "file: $file type: $type ";
       23 
       24         open my $F, "<" , $file;
       25         $md5->addfile($F);
       26         close($F);
       27         $sum = $md5->hexdigest;
       28 
       29 
       30         $ext="jpg" if ("$type" eq "image/jpeg");
       31         $ext="png" if ($type eq "image/x-png");
       32         $ext="gif" if ($type eq "image/gif");
       33 
       34         print "ext: $ext ";
       35         if (not "$ext" eq "") {
       36                 $newname = $sum.".".$ext;
       37                 ($x, $y) = imgsize("$file");
       38                 print "new name: $newname size: $x $y";
       39                 if ($x < 53 or $y < 53) {
       40                         unlink $file;
       41                         print " UNLINKED";
       42                 } else {
       43                         rename "$file", "$newname";
       44                 }
       45         }
       46         print "\n";
       47 }
       48 
       49 File::Find::find({wanted => \&analyze, no_chdir => 1}, "$ARGV[0]");
23. Juli 2011 23:47

Tags:  ·  ·  ·  ·  ·

Calltrace

Technologie / Wissenschaft ·

In your favourite debugger you often get only a stacktrace, but I often just wanna have a calltrace;
howto go get it:

  • valgrind:   valgrind --tool=callgrind ./proxy
  • callgrind_annotate:  callgrind_annotate --tree=calling callgrind.out.<pid>

Example:

238 * proxy.c:handle_server [/home/btwotch/git/proxy/proxy]
16,645 > proxy.c:receive_header (1x) [/home/btwotch/git/proxy/proxy]
7,086 > proxy.c:send_data (3x) [/home/btwotch/git/proxy/proxy]
17,913 > proxy.c:send_header (2x) [/home/btwotch/git/proxy/proxy]
1,317 > proxy.c:close_connection (1x) [/home/btwotch/git/proxy/proxy]

So handle_server called several other functions:

  • receive_header for 1 time
  • send_data for 3 times
  • send_header for 2 times
  • close_connection for 1 time
18. Juli 2011 16:47

Tags:  ·  ·  ·

Seite 2 von 29
« Zurück ·  1 2 3 4 5..29 · 
Blog durchsuchen
(nur öffentliche Einträge)

Willst du auch bloggen?
Kostenlos bloggen bei Spin.de
Diese Seite ist eine auf spin.de gelagerte persönliche Homepage, deren Verantwortlichkeit beim Nutzer liegt.
spin.de ist eine große Online-Community mit Chat, Blogs, Foren, Online-Spielen und vielem mehr.
Deine eigene Homepage mit Blog und Gästebuch

Impressum · Datenschutz · Sitemap