Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // slice file to pieces of selected size
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <ctype.h>
- #define SLICESIZE 5000
- #define MAXNAME 50
- #define MAXEXTENSION 10
- // routines
- char *itoa(long int val, int base);
- void removeaddextension(char *filename, int flag);
- int fetchsizetype(char *argument);
- void arrangeslicefilename(int chapter);
- void showusage();
- void diskwritefailure();
- // constants & variables
- char *myname;
- const char *sizetypes[]={ "BY", "KB", "MB" };
- char sourcefilename[MAXNAME+MAXEXTENSION], filenameextension[MAXEXTENSION];
- char slicefilename[MAXNAME+MAXEXTENSION+24];
- int disregardextention=1, sizetype=0; // 0 bytes, 1 kbytes, 2 mbytes
- int sizetypesmultiplication[3]={ 1, 1024, 1024*1024 };
- int main(int argc, char *argv[])
- {
- int i, cc, charcount=0, currentchapter=1;
- unsigned long slicesize=SLICESIZE; // 5000 bytes is default size
- char buf[1], ttype[MAXNAME];
- size_t nread, nwrite;
- int infile, outfile;
- strcpy((myname=malloc(sizeof(argv[0]))), argv[0]);
- // parse command line options
- while ((cc = getopt(argc, argv, ":ef:")) != -1)
- switch (cc) {
- case 'e':
- disregardextention=0;
- break;
- case 'f':
- strcpy(ttype, optarg);
- if ((i=fetchsizetype(ttype)))
- slicesize=i;
- break;
- case '?':
- showusage();
- break;
- default:
- abort();
- break; }
- if (optind==argc || argc<2)
- showusage();
- slicesize*=sizetypesmultiplication[sizetype];
- // slice files
- for (i=optind;i<argc;i++, currentchapter=1) {
- // source filename and extension
- strcpy(sourcefilename, argv[i]);
- if ((infile=open(sourcefilename, O_RDONLY))==-1)
- showusage();
- if (disregardextention)
- removeaddextension(sourcefilename, 2);
- // open outfile and break into pieces
- arrangeslicefilename(currentchapter);
- if ((outfile=open(slicefilename, O_WRONLY| O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))==-1)
- diskwritefailure();
- while (1) {
- if (!(nread=read(infile, buf, sizeof(buf))))
- break;
- if ((nwrite=write(outfile, buf, nread))==-1)
- diskwritefailure();
- ++charcount;
- if (charcount==slicesize) {
- charcount=0;
- ++currentchapter;
- close(outfile);
- arrangeslicefilename(currentchapter);
- outfile=open(slicefilename, O_WRONLY| O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); } }
- close(infile);
- close(outfile); }
- return 0;
- }
- // filename extension add/remove
- void removeaddextension(char *filename, int flag) // 0 remove, 1 add extension, 2 read extension
- {
- int i, n=0;
- switch (flag) {
- case 0:
- i=strlen(filename);
- while (i)
- if (filename[i--]=='.')
- filename[i+1]='\0';
- break;
- case 1:
- removeaddextension(filename, 0);
- strcat(filename, filenameextension);
- break;
- case 2:
- i=strlen(filename);
- while (filename[i]!='.' && i)
- i--;
- if (i)
- for (;i<strlen(filename);i++)
- filenameextension[n++]=filename[i];
- filenameextension[n]='\0';
- break; }
- }
- // fetch sizetype
- int fetchsizetype(char *argument)
- {
- int i=strlen(argument), endpoint, n=0;
- char tsizetype[3];
- while (!isdigit(argument[i]) && i>-1)
- --i;
- if (i==-1)
- return 0;
- endpoint=++i;
- for (;i<strlen(argument);i++)
- tsizetype[n++]=toupper(argument[i]);
- tsizetype[n]='\0';
- argument[endpoint]='\0'; // remove sizetype letters
- for (i=0;i<3;i++)
- if (!strcmp(tsizetype, sizetypes[i]))
- break;
- if (i==3)
- return 0;
- sizetype=i;
- return atoi(argument);
- }
- // arrange slicefilename
- void arrangeslicefilename(int chapter)
- {
- strcpy(slicefilename, sourcefilename);
- if (disregardextention)
- removeaddextension(slicefilename, 0);
- strcat(slicefilename, "_");
- strcat(slicefilename, itoa(chapter, 10));
- if (disregardextention)
- removeaddextension(slicefilename, 1);
- }
- // integer to string
- char *itoa(long int val, int base)
- {
- static char buf[32] = {0};
- int sign_flag=0, v = 30;
- buf[31]='\0';
- if (!val) {
- buf[v]='0';
- return &buf[v]; }
- if (val<0) {
- val*=-1;
- sign_flag=1; }
- for(; val && v ; --v, val /= base)
- buf[v] = "0123456789abcdef"[val % base];
- if (sign_flag) {
- buf[v]='-';
- return &buf[v]; }
- else
- return &buf[v+1];
- }
- // show usage
- void showusage()
- {
- printf("%s [-e disregard extension] [-f <slice size (BY, KB, MB)>] [files ...]\n", myname);
- exit(EXIT_FAILURE);
- }
- // error writing to disk
- void diskwritefailure()
- {
- printf("error writing to disk!\n");
- exit(EXIT_FAILURE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement