increased filename length from 32 to 256, we need this.
[my-code/mp3db.git] / mp3read.c
index dd61d86..af6dfdd 100644 (file)
--- a/mp3read.c
+++ b/mp3read.c
@@ -1,40 +1,85 @@
-/* read mp3 info, hackbard */
+/* read mp3 header & tag, author: hackbard */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
+#include <errno.h>
+
+#define ID_TAG_SIZE 128
+#define MAX_BUF_SIZE 32
+#define MAX_TITLE 30
+#define MAX_ARTIST 30
+#define MAX_ALBUM 30
+#define MAX_YEAR 4
+#define MAX_COMMENT 30
+#define MAX_GENRE 1
 
-#define MAX_READ 1024
 #define MAX_FILENAME 256
 
+
+/*
+info:
+http://www.dv.co.yu/mpgscript/mpeghdr.htm
+*/
+
 int main (int argc,char **argv)
 {
- int file_fd; /* desc fir file */
- char buf[MAX_READ]; /* buffer for the actual info */
+ int file_fd;
+ int file_size;
+ unsigned char buf[MAX_BUF_SIZE];
  char filename[MAX_FILENAME];
 
- strcpy(argv[1],filename);
+ memset(buf,0,sizeof(buf));
+
+ strcpy(filename,argv[1]);
+ file_size=atoi(argv[2]);
 
  if((file_fd=open(filename,O_RDONLY))<=0) {
   puts("open failed");
   return -23;
  }
 
- if((read(file_fd,&buf,MAX_READ))<=MAX_READ) {
+ if((lseek(file_fd,file_size-ID_TAG_SIZE,SEEK_SET))<0) {
+  puts("cannot seek to id tag");
+  return -23;
+ }
+
+ /* verify TAG now */
+ if((read(file_fd,&buf,3))<3) {
   puts("read failed");
   return -23;
  }
+ if(strncmp(buf,"TAG",3)) {
+  puts("TAG not found");
+  return -23;
+ }
 
- printf(" -> %s",buf);
- puts("");
- puts("done");
- return 23;
-}
+ read(file_fd,&buf,MAX_TITLE);
+ buf[MAX_TITLE-1]=0;
+ printf("title: %s<br>\n",buf);
+
+ read(file_fd,&buf,MAX_ARTIST);
+ buf[MAX_ARTIST-1]=0;
+ printf("artist: %s<br>\n",buf);
+
+ read(file_fd,&buf,MAX_ALBUM);
+ buf[MAX_ALBUM-1]=0;
+ printf("album: %s<br>\n",buf);
 
+ // read(file_fd,&buf,MAX_YEAR);
+ // printf("year: %s<br>\n",buf);
 
+ // read(file_fd,&buf,MAX_COMMENT);
+ // printf("comment: %s<br>\n",buf);
 
+ // read(file_fd,&buf,MAX_GENRE);
+ // printf("genre: %d<br>\n",(int)*buf);
+
+ close(file_fd);
+ return 23;
+}