After one week and not so much time on my hands, I achieved it. I have some questions about the way I did it though.
With the first try, I achieve it like this:
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stddef.h>
void remove_enter( char *s ) /* function to remove enter from stdin */
{
s[strcspn( s, "\n" )] = '\0';
}
int main(int argc, char *argv[])
{
char artist[150];
char album[150];
char genre[40];
char year[8];
char track[50];
/* check for command line arguments and print msg */
if (argc < 2)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(1);
}
printf("Enter artist name:\n"); /* ask for input, read to array, remove enter */
fgets(artist, sizeof(artist), stdin);
remove_enter(artist);
printf("Enter album name:\n");
fgets(album, sizeof(album), stdin);
remove_enter(album);
printf("Enter year:\n");
fgets(year, sizeof(year), stdin);
remove_enter(year);
printf("Enter genre:\n");
fgets(genre, sizeof(genre), stdin);
remove_enter(genre);
int pid, status;
int i;
/* count files *.wav and fork() with counter i<=tracks_number + ask for track name */
for (i = 1; i < argc; i++)
{
printf("Enter track name:\n");
fgets(track, sizeof(track), stdin);
remove_enter(track);
pid=fork();
if (pid==0)
{
char *command[] = {"/usr/local/bin/lame","-V 2","--tt",track,"--ta",artist, "--tl",album, "--ty",year, "--tg",genre,argv[i],NULL};
execvp("lame", command);
sleep(1);
}
else
{
wait(&status);
}
}
return 0;
}
While I find it somehow "dirty", it works. After that, I decided to achieve it with the se of malloc() and a struct specifier. I'm having a problem to understand -how- to use malloc() in an array. I have this modified code from the first try:
Code:
/* same as above to save space */
#define LEN 1000
struct tag_t {
char artist[LEN];
char album[LEN];
char year[LEN];
char genre[LEN];
char track[LEN];
} taglist;
void remove_enter( char *s ) /* function to remove enter from stdin */
{
s[strcspn( s, "\n" )] = '\0';
}
int main(int argc, char *argv[])
{
int max;
/* check for command line arguments and print msg */
if (argc < 2)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(1);
}
printf("Enter artist name:\n"); /* ask for input, read to array, remove enter */
char *artist = (char*) malloc(50 *sizeof(char));
fgets(taglist.artist, LEN, stdin);
remove_enter(taglist.artist);
/* same for album, year and genre to save space in post */
int pid, status;
int i;
/* count files *.wav and fork() with counter i<=tracks_number + ask for track name */
for (i = 1; i < argc; i++)
{
printf("Enter track name:\n");
fgets(taglist.track, LEN, stdin);
remove_enter(taglist.track);
pid=fork();
if (pid==0)
{
char *command[] = {"/usr/local/bin/lame","-V 2","--tt",taglist.track,"--ta",taglist.artist, "--tl",taglist.album, "--ty",taglist.year, "--tg",taglist.genre,argv[i],NULL};
execvp("lame", command);
sleep(1);
}
else
{
wait(&status);
}
}
free(artist);
return 0;
}
By trying a long name (let's say n chars) as the first argument, it fails to pass it to 'char *command[]' and skips (n-30) characters. I -think- I need to use malloc() as well to this array but I cannot understand how.
Sorry for the very long post and a big thank you to anyone who reads it.
Comments about the structure of the code are also welcome.