Advertisement
Guest User

MicroDD

a guest
May 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. /*********************
  2.  * MicroDD v. 1.0    *
  3.  * Author: Phracker  *
  4.  * Date: 2018-05-20  *
  5.  * Offered as-is     *
  6.  * No warranty       *
  7.  *********************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/stat.h>
  15. #include <errno.h>
  16. #include <limits.h>
  17.  
  18. int main( int argc, char **argv ){
  19.     char infile [FILENAME_MAX];
  20.     char outfile[FILENAME_MAX];
  21.     size_t bc = 0;    // Number of bytes to copy (if zero, copy entire file)
  22.     off_t istart = 0; // starting position in input file
  23.     off_t ostart = 0; // starting position in output file
  24.  
  25.     /*** BEGIN PARSE COMMAND LINE PARAMETERS ***/
  26.     for( int i = 1; i < argc; i++ ){
  27.         char *name = strtok( argv[i], "=" );
  28.         char *value = strtok( NULL, "\0" );
  29.         if( !strcmp( name, "if" ) ) strncpy( infile, value, strlen( value ) );
  30.         else if( !strcmp( name, "of" ) ) strncpy( outfile, value, strlen( value ) );
  31.         else if( !strcmp( name, "bc" ) ) bc = atoi( value );
  32.         else if( !strcmp( name, "istart" ) ) istart = atoi( value );
  33.         else if( !strcmp( name, "ostart" ) ) ostart = atoi( value );
  34.         else fprintf( stderr, "%s: %s: switch not recognized.\n", argv[0], name );
  35.     }
  36.     /*** END PARSE COMMAND LINE PARAMETERS ***/
  37.  
  38.     // This section is for determining when the end of a regular file has been reached.
  39.     // Processing for a given file will be skipped if the file was not specified.
  40.     struct stat istat, ostat;
  41.     if( infile[0] ) stat( infile, &istat );
  42.     if( errno == ENOENT ) fprintf( stderr, "%s: %s: File not found.\n", argv[0], infile );
  43.     errno = 0;
  44.     if( outfile[0] ) stat( outfile, &ostat );
  45.     int savederrno = errno;
  46.     if( !bc ){
  47.         if( infile[0] && S_ISREG( istat.st_mode ) ) bc = istat.st_size - istart;
  48.         else if( outfile[0] && savederrno != ENOENT /* Check to see if outfile exists */ && S_ISREG( ostat.st_mode ) ) bc = ostat.st_size - ostart;
  49.     }
  50.  
  51.     // And now for the actual copying...
  52.     int ip, op;
  53.     if( infile[0] ){
  54.         ip = open( infile, O_RDONLY );
  55.         lseek( ip, istart, SEEK_SET );
  56.     }
  57.     else ip = 0;
  58.     if( outfile[0] ){
  59.         op = open( outfile, O_WRONLY | O_CREAT, 0644 );
  60.         lseek( op, ostart, SEEK_SET );
  61.     }
  62.     else op = 1;
  63.     char c;
  64.     for( int i = 0; i < (bc?bc:INT_MAX); i++ ){
  65.         read( ip, &c, 1 );
  66.         write( op, &c, 1 );
  67.     }
  68.     close( ip );
  69.     close( op );
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement