c - Commands like 1; and 2c1; on shell -
i developing simple "tar-like" archiving c program. works writing files, directories , file contents in text file, , can extract them in same way (it creates empty files, , fill them archive content).
when have archive text files or pdfs, works well.
with audio files such mp3s, @ end of extraction, have these lines on shell:
1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1; 2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1; 2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c;1;1;112;112; 1;0x1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c;1;1;112;112;1;0x1;2c1;2c1;2c1; 2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1; 2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c1;2c i don't know why program writes on shell, , why audio tracks. executing them, noticed 1;commands executed, , 2c1;commands unknown.
can me find out why?
this code wrote extracting part:
void crea_file(file *f) // crea file estratti dall'archivio { int contatore = 0; char x[4096]; bool listtrovata = false; while (fscanf(f, " %s", x) == 1) { if(strcmp(x, "%list%")==0 && listtrovata==false) { listtrovata= true; puts("trovato il primo \n"); continue; } else if (strcmp(x, "%list%")==0 && listtrovata) { long position; position = ftell(f); printf("trovato il secondo \n"); printf("list trovato alla posizione %ld", position); fseek(f, 0, seek_end); break; } else if (listtrovata) { contatore++; char* file; // stringa contenente il percorso da aprire (verrà creato in seguito) file = collega(getcwd(null, 0), x); creat(file, perms); printf("sto cercando l'inizio e la fine di content, passando un contatore %d \n", contatore); trovainiziofine(contatore); printf("ora scrivo il file trovato"); scrivifile(file); //inserisco funzione che parte da inizio e scrive carattere per carattere nel file destinazione. se la posizione di ftell è uguale fine, allora esci. } } printf("esco \n"); } void trovainiziofine(int cont) // trova il carattere di inizio e di fine del file nella sezione %content% { file* contenuto; char * path; char x[4096]; int =1; path = collegaslash(getcwd(null, 0), nome); contenuto = fopen(path, "r"); while (fscanf(contenuto, "%s", x) == 1) { if(strcmp(x, "%content%")==0 && == ((cont*2)-1) ) { inizio = ftell(contenuto); puts("trovato il primo \n"); //puts(x); printf("content trovato alla posizione %d \n", inizio); i++; } else if(strcmp(x, "%content%")==0 && == ((cont*2)) ) { fine = ftell(contenuto); daleggere = fine-9; puts("trovato il primo \n"); printf("content trovato alla posizione %d \n", fine); break; i++; } else if(strcmp(x, "%content%")==0) { i++; } printf("giro numero %d \n", i); } printf("esco da trova inizio file \n"); fclose(contenuto); } void scrivifile(const char * arrivo) //scrive file creati in precedenza { file * partenza; file * target; int c; int spazio = 'a'; int = 0; int pos; char * path; path = collegaslash(getcwd(null, 0), nome); partenza = fopen(path, "r"); fseek(partenza, inizio, seek_set); target = fopen(arrivo, "w"); //apro il file if (target) { //se è aperto while ((c = fgetc(partenza)) != eof && ftell(partenza)<=fine-10) { //e il carattere preso non eccede la fine del file fputc(c, target); fputc(c, stdout); pos = ftell(partenza); if(pos==fine) { break; } //scrivo lo stesso carattere in out (file in uscita) } // fclose(target); //chiudo il file fclose(partenza); } else { printf("errore di scrittura del file \n"); } } my archive has 3 section: %list%, %dirs% , %content%.
crea_file reads %list% section , creates empty files in current directory.
trovainiziofine reads %content% section , saves 2 indexes: 1 @ beginning of file content, , 1 @ end.
scrivifile fills empty file archive content.
without code at, quite hard.
still, 1 thing makes me suspect problems:
it works writing files, directories , file contents in text file
you can't treat arbitrary binary data text, in cases. c libraries might end-of-line translation, destroy binary data. must use binary files arbitrary data, or take care "protect" data survives being treated text.
go binary.
Comments
Post a Comment