My C program store a shell command output in a FILE, pass it to a char array and print it.
You don't need to look at the function in the program since the problem is on main.
The output of the command in my shell is:
$ cmus-remote -Q | grep 'tag title' | sed s/'tag title'/''/g
The View From The Afternoon
The char value in gdb is:
34 fclose(tmp2);
(gdb) print title
$4 = 0x603010 " The View From The Afternoon\n"
(gdb) n
51 char *lyric = lyrics_get(artist, title);
(gdb) print title
$5 = 0x603010 " The View From The After\001\002"
This is the code
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
#include "/usr/include/glyr/glyr.h"
#include <unistd.h>
#include <string.h>
char *lyrics_get(char *a, char *b);
int main(void)
{
FILE *tmp1;
FILE *tmp2;
char *artist;
char *title;
tmp1 = popen("cmus-remote -Q | grep 'tag artist' | sed s/'tag artist'/''/g | sed '1s/^.//'", "r");
tmp2 = popen("cmus-remote -Q | grep 'tag title' | sed s/'tag title'/''/g", "r");
fseek(tmp1, 0, SEEK_END);
size_t size1 = ftell(tmp1);
rewind(tmp1);
artist = malloc((size1 +1) * (sizeof(char)));
fread(artist, sizeof(char), size1, tmp1);
fclose(tmp1);
fseek(tmp2, 0, SEEK_END);
size_t size2 = ftell(tmp2);
rewind(tmp2);
title = malloc((size2 +1) * (sizeof(char)));
fread(title, sizeof(char), size2, tmp2)
fclose(tmp2);
char *lyric = lyrics_get(artist, title);
printf("%s", title);
printf("%s", artist);
printf("%s", lyric);
return 0;
}
char *lyrics_get(char *a, char *b)
{
glyr_init();
atexit (glyr_cleanup);
GlyrQuery q;
glyr_query_init (&q);
glyr_opt_type (&q,GLYR_GET_LYRICS);
glyr_opt_artist (&q, (a));
glyr_opt_title (&q, (b));
glyr_opt_force_utf8 (&q, true);
GLYR_ERROR err;
GlyrMemCache * head = glyr_get (&q,&err,NULL);
return (head->data);
glyr_free_list(head);
glyr_query_destroy(&q);
}
Why it's different?