Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************
- * MicroDD v. 1.0 *
- * Author: Phracker *
- * Date: 2018-05-20 *
- * Offered as-is *
- * No warranty *
- *********************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <limits.h>
- int main( int argc, char **argv ){
- char infile [FILENAME_MAX];
- char outfile[FILENAME_MAX];
- size_t bc = 0; // Number of bytes to copy (if zero, copy entire file)
- off_t istart = 0; // starting position in input file
- off_t ostart = 0; // starting position in output file
- /*** BEGIN PARSE COMMAND LINE PARAMETERS ***/
- for( int i = 1; i < argc; i++ ){
- char *name = strtok( argv[i], "=" );
- char *value = strtok( NULL, "\0" );
- if( !strcmp( name, "if" ) ) strncpy( infile, value, strlen( value ) );
- else if( !strcmp( name, "of" ) ) strncpy( outfile, value, strlen( value ) );
- else if( !strcmp( name, "bc" ) ) bc = atoi( value );
- else if( !strcmp( name, "istart" ) ) istart = atoi( value );
- else if( !strcmp( name, "ostart" ) ) ostart = atoi( value );
- else fprintf( stderr, "%s: %s: switch not recognized.\n", argv[0], name );
- }
- /*** END PARSE COMMAND LINE PARAMETERS ***/
- // This section is for determining when the end of a regular file has been reached.
- // Processing for a given file will be skipped if the file was not specified.
- struct stat istat, ostat;
- if( infile[0] ) stat( infile, &istat );
- if( errno == ENOENT ) fprintf( stderr, "%s: %s: File not found.\n", argv[0], infile );
- errno = 0;
- if( outfile[0] ) stat( outfile, &ostat );
- int savederrno = errno;
- if( !bc ){
- if( infile[0] && S_ISREG( istat.st_mode ) ) bc = istat.st_size - istart;
- else if( outfile[0] && savederrno != ENOENT /* Check to see if outfile exists */ && S_ISREG( ostat.st_mode ) ) bc = ostat.st_size - ostart;
- }
- // And now for the actual copying...
- int ip, op;
- if( infile[0] ){
- ip = open( infile, O_RDONLY );
- lseek( ip, istart, SEEK_SET );
- }
- else ip = 0;
- if( outfile[0] ){
- op = open( outfile, O_WRONLY | O_CREAT, 0644 );
- lseek( op, ostart, SEEK_SET );
- }
- else op = 1;
- char c;
- for( int i = 0; i < (bc?bc:INT_MAX); i++ ){
- read( ip, &c, 1 );
- write( op, &c, 1 );
- }
- close( ip );
- close( op );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement