Library to split and join files

Some problems with your code:

1. To get file size, use stat() function
2. You should check if variables are NULL (libSPLT.c:35) before you use them (libSPLT.c:33-34)
3. What if there will be more than 999 chunks?
4. What if chunk size is larger than the amount of memory+swap the computer has?
5. You're leaking memory in split() function - you never free out (also in join() function with chunk variable)
6. What is "%.3d" in the format string?
7. You have possible buffer overflow by 1 byte on libSPLT.c:39 (or more if we consider also point nr. 3)
8. memset on libSPLT.c:33-34 is unneccesary
9. make install will install that library to base system. That's heresy!
 
1. To get file size, use stat() function
I 'll make update soon in library.
2. You should check if variables are NULL (libSPLT.c:35) before you use them (libSPLT.c:33-34)
I fixed now.
3. What if there will be more than 999 chunks?
Try it and you will see.
4. What if chunk size is larger than the amount of memory+swap the computer has?
As I said, I 'll make updates.
5. You're leaking memory in split() function - you never free out (also in join() function with chunk variable)
They are string's not some thing really will abuse memory in all :D.
6. What is "%.3d" in the format string?
This is a formatting style in printf family functions, it will hold 3 number's, ex:
Code:
int a=7;
printf("%.3d", a);
output will be: 007
I used this trick to avoid using string functions to generate part extensions.
7. You have possible buffer overflow by 1 byte on libSPLT.c:39 (or more if we consider also point nr. 3)
I think you are right, I just forget to fix it.
8. memset on libSPLT.c:33-34 is unneccesary
It is best practice to initial memory before use it.
9. make install will install that library to base system. That's heresy!
I will fix it this too.
This a patch to libSPLT.c, save it as libsplt.patch:
Code:
--- libSPLT.c	2010-10-31 21:36:06.000000000 +0100
+++ libSPLT2.c	2010-10-31 21:40:02.000000000 +0100
@@ -29,13 +29,13 @@
      nbytes=lbytes;
     /* memory allocation for buffer of chunks and chunks name */	
     buffer = (char *)malloc(sizeof(char)*nbytes);
-    out = (char*)malloc(sizeof(char)*strlen(fname)+3);
-    memset(buffer, '\0', nbytes);
-    memset(out, '\0', strlen(fname)+3);
+    out = (char*)malloc(sizeof(char)*strlen(fname)+4);
     if(buffer == NULL || out == NULL)
      return -2;
+    memset(buffer, '\0', nbytes);
+    memset(out, '\0', strlen(fname)+4);
     fread(buffer, 1, nbytes ,src);
-    /* generate chunk part number*/
+    /* generate chunk part number */
     sprintf(out, "%s.%.3d", fname, i);
     dest = fopen(out , "wb");
     if(dest == NULL)
@@ -43,13 +43,14 @@
     fwrite(buffer , 1, nbytes ,dest);
     fclose(dest);
     free(buffer);
+    free(out);
     i++;
    }
   fclose(src);
  return nchunks;
  }
 
-/*Function to join files*/
+/* Function to join files */
 int join(char *fname, int nchunks)
  {
   int i;
@@ -58,16 +59,16 @@
   dest=fopen(fname, "wb");
   if(dest == NULL)
    return -1;
-  /* loop until we ritch last chunk*/
+  /* loop until we ritch last chunk */
   for(i=1;i<=nchunks;i++)
    {
-    /*allocate memory for chunk*/	
+    /* allocate memory for chunk */	
     chunk=(char*)malloc(sizeof(char)*strlen(fname)+4);
     if(chunk == NULL)
      return -2;
-    /*generate chunk number ti open later*/
+    /* generate chunk number ti open later */
     sprintf(chunk, "%s.%.3d", fname, i);
-    /*allocate buffer in size of chunk each time*/
+    /* allocate buffer in size of chunk each time */
     buffer=(char*)malloc(sizeof(char)*file_size(chunk));
     if(buffer == NULL)
      return -3;
@@ -77,13 +78,14 @@
     fread(buffer, 1, file_size(chunk), src);
     fwrite(buffer, 1, file_size(chunk), dest);
     free(buffer);
+    free(chunk);
     fclose(src);
    }
   fclose(dest);
   return 0;
  }
 
-/**get size of file*/
+/* get size of file */
 long int file_size(char *file)
  {
   int fsize;
@@ -91,10 +93,10 @@
   fname = fopen(file ,"rb");
   if(fname == NULL)
    return -1;
-  /*move the pointer to end of file*/ 
+  /* move the pointer to end of file */ 
   if(fseek(fname ,0 , SEEK_END) !=0)
    return -1;
-  /*get the position of current pointer*/ 
+  /* get the position of current pointer */ 
   fsize = ftell(fname);
   if( fsize !=-1)
    {
Then:
Code:
cd libSPLT
patch < libSPLT.patch
 
Back
Top