Hi, I'm trying to write a program to archive, basicaly concatenate some files from a folder. The names of the files are obtained from the file_ls file which contains the result you get when you run ls -la in linux. It's lines of text like this:

drwxr----- 13 slobo slobo 2048 2009-01-16 22:44 ..
-rw-r--r-- 1 slobo slobo 1646 2008-12-15 09:42 usermap.txt

The program should concatenate the files upon inputing the following command:

load file_ls usermap.txt archivename

So it looks into file_ls, finds all the files, then concatenates their content and writes it in the file archivename. The program should also write a header for each of the files, before the content of the file itself, like the tar specifications, that's where I'm going to use usermap.txt. But I'm not worrying about that now, first I'm trying to get it to just concatenate the files ie add their content to the archivename.

My code looks like this :

#include<stdio.h>
#include<string.h>
#define MAX 1000
FILE *s, *d;
void copy( FILE *d, FILE *s)
{
	int c;
	char zone[MAX];
	while((c=fread(zone, 1, MAX, s))>0)
		fwrite(zone, 1, c, d);
	
}
	

void load(char *archivename)
{
	char *x;
	long len;
	FILE* pf = fopen("file_ls", "rb");
	fseek(pf, 0, SEEK_END);
	len=ftell(pf);
	fseek(pf, 0, SEEK_SET);
	x=(char*)malloc(len);
	fread(x, 1, len, pf);
	char *t, *u;
	t=strtok(x, "\n");
	while(t!=NULL){
		if(t[0]=='-')
		{
			u=strchr(t, ':');
			u+=4;
			printf("This is the name of a file from file_ls \n");
			puts(u);
			puts(t);
			s = fopen(u, "r");
			d = fopen(archivename, "a+");
			copy(d,s);
		}
	t=strtok(NULL, "\n");
	}
	
}

int main(){
	char *command, *archivename, *filename, *p;
	int i, j, n, ok=0;
	fgets(command, 100, stdin);
	
	p="load file_ls usermap.txt";
	n=strlen(p);
	if(strncmp(command, p, n )==0)
	ok=1;
	if(ok==1)
	{
	archivename=command+n+1;
	printf("The name of the archive is: \n");
	puts(archivename);
	printf("  Launching load!!!");
	load(archivename);
	}
                fclose(s);
	fclose(d);
	return 0;
}

I need some help with this. It compiles ok, but it doesn't work, doesn't copy anything. I think the copy function is alright, because it worked in other, simpler programs. Any help would be appreciated.

If you can change the content of the input file, change the ls command to report file names only. That would greatly simplify your program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.