For those who said it was working, I mainly looked at the code for the last part.
Here are some general comments:
char buf[60];
sprintf(buf,"%d\n",num);
better:
#define BUFSIZE 60
char buf[BUFSIZE];
sprintf(buf,"%d\n",num);
still better (avoids Y2K-like problem):
#define BUFSIZE 60
char buf[BUFSIZE];
if (snprintf(buf,BUFSIZE,"%d\n",num) >= BUFSIZE)
return -1;