Advertisement
puppet106

slice.c

Feb 24th, 2020
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.68 KB | None | 0 0
  1. // slice file to pieces of selected size
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10. #define SLICESIZE 5000
  11. #define MAXNAME 50
  12. #define MAXEXTENSION 10
  13.  
  14. // routines
  15. char *itoa(long int val, int base);
  16. void removeaddextension(char *filename, int flag);
  17. int fetchsizetype(char *argument);
  18. void arrangeslicefilename(int chapter);
  19. void showusage();
  20. void diskwritefailure();
  21.  
  22. // constants & variables
  23. char *myname;
  24. const char *sizetypes[]={ "BY", "KB", "MB" };
  25. char sourcefilename[MAXNAME+MAXEXTENSION], filenameextension[MAXEXTENSION];
  26. char slicefilename[MAXNAME+MAXEXTENSION+24];
  27. int disregardextention=1, sizetype=0; // 0 bytes, 1 kbytes, 2 mbytes
  28. int sizetypesmultiplication[3]={ 1, 1024, 1024*1024 };
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32.   int i, cc, charcount=0, currentchapter=1;
  33.   unsigned long slicesize=SLICESIZE; // 5000 bytes is default size
  34.   char buf[1], ttype[MAXNAME];
  35.   size_t nread, nwrite;
  36.   int infile, outfile;
  37.   strcpy((myname=malloc(sizeof(argv[0]))), argv[0]);
  38.  
  39.     // parse command line options
  40.     while ((cc = getopt(argc, argv, ":ef:")) != -1)
  41.      switch (cc) {
  42.       case 'e':
  43.        disregardextention=0;
  44.       break;
  45.       case 'f':
  46.        strcpy(ttype, optarg);
  47.        if ((i=fetchsizetype(ttype)))
  48.         slicesize=i;
  49.       break;
  50.       case '?':
  51.        showusage();
  52.       break;
  53.       default:
  54.        abort();
  55.     break; }
  56.     if (optind==argc || argc<2)
  57.      showusage();
  58.     slicesize*=sizetypesmultiplication[sizetype];
  59.  
  60.    // slice files
  61.    for (i=optind;i<argc;i++, currentchapter=1) {
  62.     // source filename and extension
  63.     strcpy(sourcefilename, argv[i]);
  64.     if ((infile=open(sourcefilename, O_RDONLY))==-1)
  65.      showusage();
  66.     if (disregardextention)
  67.      removeaddextension(sourcefilename, 2);
  68.     // open outfile and break into pieces
  69.     arrangeslicefilename(currentchapter);
  70.     if ((outfile=open(slicefilename, O_WRONLY| O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))==-1)
  71.      diskwritefailure();
  72.     while (1) {
  73.      if (!(nread=read(infile, buf, sizeof(buf))))
  74.       break;
  75.      if ((nwrite=write(outfile, buf, nread))==-1)
  76.       diskwritefailure();
  77.      ++charcount;
  78.      if (charcount==slicesize) {
  79.       charcount=0;
  80.       ++currentchapter;
  81.       close(outfile);
  82.       arrangeslicefilename(currentchapter);
  83.     outfile=open(slicefilename, O_WRONLY| O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); } }
  84.     close(infile);
  85.    close(outfile); }
  86.    
  87.  return 0;
  88. }
  89.  
  90. // filename extension add/remove
  91. void removeaddextension(char *filename, int flag) // 0 remove, 1 add extension, 2 read extension
  92. {
  93.   int i, n=0;
  94.  
  95.   switch (flag) {
  96.    case 0:
  97.     i=strlen(filename);
  98.     while (i)
  99.      if (filename[i--]=='.')
  100.     filename[i+1]='\0';
  101.    break;
  102.    case 1:
  103.     removeaddextension(filename, 0);
  104.     strcat(filename, filenameextension);
  105.    break;
  106.    case 2:
  107.     i=strlen(filename);
  108.     while (filename[i]!='.' && i)
  109.      i--;
  110.     if (i)
  111.      for (;i<strlen(filename);i++)
  112.       filenameextension[n++]=filename[i];
  113.     filenameextension[n]='\0';
  114.   break; }
  115. }
  116.  
  117. // fetch sizetype
  118. int fetchsizetype(char *argument)
  119. {
  120.   int i=strlen(argument), endpoint, n=0;
  121.   char tsizetype[3];
  122.    
  123.    while (!isdigit(argument[i]) && i>-1)
  124.     --i;
  125.    if (i==-1)
  126.     return 0;
  127.    endpoint=++i;
  128.    for (;i<strlen(argument);i++)
  129.     tsizetype[n++]=toupper(argument[i]);
  130.    tsizetype[n]='\0';
  131.    argument[endpoint]='\0'; // remove sizetype letters
  132.    for (i=0;i<3;i++)
  133.     if (!strcmp(tsizetype, sizetypes[i]))
  134.      break;
  135.    if (i==3)
  136.     return 0;
  137.    sizetype=i;
  138.    
  139.  return atoi(argument);
  140. }
  141.  
  142. // arrange slicefilename
  143. void arrangeslicefilename(int chapter)
  144. {
  145.   strcpy(slicefilename, sourcefilename);
  146.   if (disregardextention)
  147.    removeaddextension(slicefilename, 0);
  148.   strcat(slicefilename, "_");
  149.   strcat(slicefilename, itoa(chapter, 10));
  150.   if (disregardextention)
  151.    removeaddextension(slicefilename, 1);
  152. }
  153.  
  154. // integer to string
  155. char *itoa(long int val, int base)
  156. {
  157.     static char buf[32] = {0};
  158.     int sign_flag=0, v = 30;
  159.     buf[31]='\0';
  160.    
  161.     if (!val) {
  162.      buf[v]='0';
  163.     return &buf[v]; }
  164.     if (val<0) {
  165.      val*=-1;
  166.     sign_flag=1; }
  167.    
  168.     for(; val && v ; --v, val /= base)
  169.      buf[v] = "0123456789abcdef"[val % base];
  170.     if (sign_flag) {
  171.      buf[v]='-';
  172.     return &buf[v]; }
  173.     else
  174.      return &buf[v+1];
  175. }
  176.  
  177. // show usage
  178. void showusage()
  179. {
  180.   printf("%s [-e disregard extension] [-f <slice size (BY, KB, MB)>] [files ...]\n", myname);
  181.    
  182.  exit(EXIT_FAILURE);
  183. }
  184.  
  185. // error writing to disk
  186. void diskwritefailure()
  187. {
  188.   printf("error writing to disk!\n");  
  189.    
  190.  exit(EXIT_FAILURE);
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement