hey everyone!

I've been facing some problems with this piece of
code.
It extracts and puts ID3v1 Tags into mp3 files.

The problem is that when i use it to get tags from
individual mp3s it works, but when i put it into a
while loop reading directory entries, it exits after
reading just one file's tags.

I am sure that the problem lies here coz when i
comment out the call to this function in the loop, the
loop completes and reads all the entries.


Please help.

====================CODE==========================
#include<fcntl.h>
#include<string.h>
#include<malloc.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdio.h>

typedef struct ID3v1Tag_{
        char Tagmarker[3];
        char Title[31];
        char Artist[31];
        char Album[31];
        char Year[5];
        char Comments[31];
        char Genre;
}ID3v1Tag;
/* Theres an extra byte to store \0 */


int gettags(const char *, ID3v1Tag *); 
/* Pass it the filename of the mp3 with path. 
*  Returns the status of the operation, 0 if
successful, negative if failed.
* See source for actual failure return values.
*/

int puttags(const char *, ID3v1Tag *);
/* Pass it the filename of the mp3 file with path.
* Returns the status of the operation, 0 if
successful, negative if failed.
* See source for actual failure return values.
*/


int gettags(const char *fname, ID3v1Tag *tag) {

        int fd;
        off_t filelen;
        
        
        if(tag==NULL)
                return -1;      

        if((access(fname,R_OK))<0)
                return -2;      /* Permission denied: No read access. */
        
        if((fd=open(fname,O_RDONLY))<0) {
                tag=NULL;
                return -3;      /* Could not open file. */
        }
        
        filelen=lseek(fd,0,SEEK_END);
        lseek(fd,filelen-128,SEEK_SET);

        if((read(fd,tag->Tagmarker,3))<3) {
                close(fd);
                return -4;
        }

        
        if(strncmp(tag->Tagmarker,"TAG",3)!=0) {
                close(fd);
                return -5;      /* No tags were present in the file.*/
        }
        

        if((read(fd,tag->Title,30))<30) {
                close(fd);
                return -4;
        }

        strcpy(&tag->Title[30],"\0");
        
        if((read(fd,tag->Artist,30))<30) {
                close(fd);
                return -4;
        }
        
        strcpy(&tag->Artist[30],"\0");
        
        if((read(fd,tag->Album,30))<30) {
                close(fd);
                return -4;
        }
        
        strcpy(&tag->Album[30],"\0");
        
        if((read(fd,tag->Year,4))<4) {
                close(fd);
                return -4;
        }
        

        strcpy(&tag->Year[4],"\0");
        
        if((read(fd,tag->Comments,30))<30) {
                close(fd);
                return -4;
        }

        strcpy(&tag->Comments[30],"\0");
        
        if((read(fd,&tag->Genre,1))<1) {
                close(fd);
                return -4;
        }
                
        
        if((close(fd))<0)
                fprintf(stderr,"Error closing file.\n");

        return 0;
}


int puttags(const char *fname, ID3v1Tag *tag) {

        int fd;
        off_t filelen;

        if(tag==NULL)
                return -1;

        if((access(fname,W_OK))<0)
                return -2;

        if((fd=open(fname,O_WRONLY | O_SYNC))<0) 
                return -3;
        
        filelen=lseek(fd,0,SEEK_END);
        lseek(fd,filelen-128,SEEK_SET);

        memcpy(tag->Tagmarker,"TAG",3);
        
        if((write(fd,tag->Tagmarker,3))<3) {
                close(fd);
                return -4;
        }

        if((write(fd,tag->Title,30))<30) {
                close(fd);
                return -4;
        }

        if((write(fd,tag->Artist,30))<30) {
                close(fd);
                return -4;
        }

        if((write(fd,tag->Album,30))<30) {
                close(fd);
                return -4;
        }

        if((write(fd,tag->Year,4))<4) {
                close(fd);
                return -4;
        }

        if((write(fd,tag->Comments,30))<30) {
                close(fd);
                return -4;
        }

        if((write(fd,&tag->Genre,1))<1) {
                close(fd);
                return -4;
        }

        close(fd);
        return 0;
}


        
====================CODE======================

This whole thing was in two parts one a header with
the function protos and the definition of the ID3v1TAg
struct, i 've combined it for this list.


Thanks

Shehjar


__________________________________________________
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com

          ================================================
To unsubscribe, send email to [EMAIL PROTECTED] with unsubscribe in subject header. 
Check archives at http://www.mail-archive.com/ilugd%40wpaa.org

Reply via email to