Posts Tagged ‘c’

scratching my head over a c problem…

Thursday, May 17th, 2007

today, i wanted to try out my test arabic gtk program to see if behdad's new changes to pango magically fixed the renown arabic shaping issue [in short, it had nothing to do with it]. anyway, i discovered that i needed to install libquran, and to make a long story short, my test program, which used to work before, segfaulted. i ran gdb and valgrind only to find the segfault happening within libquran at the closing of the configuration file (noting this libquran code hasn't been changed in 3 years now).

i looked at the source, and discovered that the file pointer was becoming null after a call to getline. i tried to see if i could reproduce this in a smaller test program, and i discovered that i indeed could -

please click here to see the program code, i am tired of trying to get the formatting right with igSyntaxHiliter and/or with the wp code editor, and i am also tired of dealing with dreamhost not letting me post an fopen function call because mod_security flags it as an emergency error and returns a 503...

the program displayed the first line from testfile, but unexpectedly displayed that fp is null and segfaulted at the fclose. checking the return from getline, i see that it returns successfully (the number of characters it read).

while i got around this problem by modifying the library to do a malloc followed by an fgets, i am just confused -this library code hasn't been touched in 3 years, it used to work before, and i just repulled it from cvs when i discovered this. so why is it broken now? the only thing that i can think of being different is that my box now runs a 64 bit version of linux, but would that break it?

any ideas?

come again?

Sunday, November 27th, 2005

a long time ago, when i still used to use xmms, there was this nice plugin called Repeat It! that let you say, "repeat this song between this and this times forever."

i found myself missing that feature today... unfortunately, i don't have xmms installed and don't want it installed because i now like the gtk2 based mp3 players, like beep for example.

but because beep is being rewritten as bmpx, i figure rather than porting the plugin, i can just write a much simpler command line version based on the existing plugin that works with beep.

note however that this is really bad... it does very little error checking, and the sleep solution isn't really the best solution... but its a pretty good solution for my limited uses :)

and in 25 lines, here it is:

 
#include <stdio.h>
#include <signal.h>
#include <bmp/beepctrl.h>
 
void got_signal(int x){
	printf("Exiting!\n"); exit(1);
}
 
int main(int argc, char** argv){
 
   int startpos, endpos;
   if (!xmms_remote_is_running(0))
      return (0*printf("\033[1mbmp is not running.\033[0m\n"))-1;
 
   if (argc != 3)
      return (0*printf("usage: %s start end\n", argv[0]));
 
   startpos = atoi(argv[1]);
 
   endpos = atoi(argv[2]);
 
   signal(SIGINT, got_signal);
 
   if (!xmms_remote_is_playing(0)){ xmms_remote_play(0); }
 
   while (1){
      xmms_remote_jump_to_time(0, startpos * 1000);
      sleep(endpos - startpos);
   }
}

to compile it, just do:

 
gcc `beep-config --cflags --libs` loopsegment.c -o loop

then just run beep, load up the file you want to play, and run the script passing in start and end seconds.