home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / utilities / split / Support / Source / Splitf_c < prev    next >
Encoding:
Text File  |  1994-09-29  |  8.2 KB  |  266 lines

  1. /* Splitf.c                                      */
  2. /* Part of splitf and joinf distribution         */
  3. /* version 1.12, © 1993,1994 Adam Hamilton       */
  4. /* See the README file for copyright information */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "config.h"
  10.  
  11. #ifdef ACORN
  12. #define  FILETYPE       "B8C"
  13. #include "os.h"
  14. #endif
  15.  
  16. #define Bool char
  17. #define False 0
  18. #define True 1
  19. #define MIN(a, b) (a < b ? a : b)
  20. #define MAX(a, b) (a > b ? a : b)
  21.  
  22. #ifndef FILENAME_MAX
  23. #define FILENAME_MAX 8
  24. #endif
  25.  
  26. void usage (char *progname)
  27. {
  28.   fprintf (stderr,
  29.       "File splitter. Version 1.12c - 29 September 1994 by A.Hamilton\n\n");
  30.   fprintf (stderr, "Usage : %s [options] <filename>\n\n", progname);
  31.   fprintf (stderr, "Options (can be abbreviated) :\n");
  32.   fprintf (stderr,
  33.       "    -filesize     [filesize in K]          default = 1420\n");
  34. #ifndef PC
  35.   fprintf (stderr, "    -buffersize   [buffersize in K]        default = 32\n");
  36. #endif
  37.   fprintf (stderr, "    -path         [new path]\n");
  38.   fprintf (stderr, "    -interactive\n");
  39.   exit (1);
  40. }
  41.  
  42. char *examine_filename (char original_name[], char *name, char *ext)
  43. {
  44.   char main_name[256], *original, *pointer;
  45.   register int i = -1, n;
  46.  
  47.   if (COLON)
  48.     pointer = strrchr (original_name, ':');
  49.   else
  50.     pointer = NULL;
  51.  
  52.   original = strrchr (original_name, SEPARATOR_SEARCH);
  53.   if ((original = MAX (original, pointer)) == NULL)
  54.     original = original_name;
  55.   else {
  56.     original++;
  57.   }
  58.  
  59.   do {
  60.     i++;
  61.     main_name[i] = original[i];                      /* get name */
  62.   } while (main_name[i] != '.' && main_name[i] != '\0');
  63.  
  64.   for (n = 0; (ext[n] = original[i]) != '\0'; n++, i++) ;
  65.   if (main_name[i-n] == '\0') {
  66.     strcpy (name, main_name);
  67.     return ("");
  68.   }
  69.   main_name[i-n] = '\0';
  70.   strcpy (name, main_name);
  71.   return (".spt");
  72. }
  73.  
  74. void numtostr (short number, char *name)
  75. {
  76.   name[0] = (short) (number / 10) + '0';
  77.   name[1] = (number % 10) + '0';
  78.   name[2] = '\0';
  79. }
  80.  
  81. int main (int argc, char **argv)
  82. {
  83.   char source_filename[256], out_filename[256], out_path[256], file_ext[20];
  84.   char out_name[256], header[50], fnum[3], *progname, command[256];
  85.   char type[5], interactive = 0, orig_ext[20];
  86.  
  87.   short file_number = 0;
  88.   long disk_size=1420*1024, read_size=32*1024, data_size;
  89.   long out_position, in_position, bytes_read, bytes_written, file_length;
  90.   long *buffer;
  91.   int args, i;
  92.   FILE *source_file, *out_file;
  93.  
  94. #ifdef ACORN
  95.   os_filestr filedata;
  96. #endif
  97.  
  98.   progname = *argv;
  99.   strcpy (type, "0");
  100.   out_path[0] = '\0';                           /* set to default path */
  101.   source_filename[0] = '\0';
  102.   args = argc - 1 ;
  103.   if (!args) usage (progname);
  104.   while (args--) {
  105.     argv++;
  106.     if (!strncmp ("-filesize", *argv, MAX (2, strlen (*argv)))) {
  107.       if (!args) {
  108.         fprintf (stderr, "File size required\n\n");
  109.         usage (progname);
  110.       }
  111.       disk_size = (long) atoi (*++argv) * 1024;  /* file size */
  112.       args--;
  113.     }
  114. #ifndef PC
  115.     else if (!strncmp ("-buffersize", *argv, MAX (2, strlen (*argv)))) {
  116.       if (!args) {
  117.         fprintf (stderr, "Buffer size required\n\n");
  118.         usage (progname);
  119.       }
  120.       read_size = (long) atoi (*++argv) * 1024;  /* buffer size */
  121.       args--;
  122.     }
  123. #endif
  124.     else if (!strncmp ("-path", *argv, MAX (2, strlen (*argv)))) {
  125.       if (!args) {
  126.         fprintf (stderr, "Path required\n\n");
  127.         usage (progname);
  128.       }
  129.       sprintf (out_path, "%s%c", *++argv, FILE_SEPARATOR);  /* output path */
  130.       args--;
  131.     }
  132.     else if (!strncmp ("-interactive", *argv, MAX (2, strlen (*argv))))
  133.       interactive = 1;
  134.     else {
  135.       strcpy (source_filename, *argv);              /* source file */
  136.       if (args) usage (progname);
  137.     }
  138.   }
  139.   if (source_filename[0] == NULL) {
  140.     fprintf (stderr, "Source filename required\n\n");
  141.     usage (progname);
  142.   }
  143.  
  144.   strcpy (file_ext, examine_filename (source_filename, out_filename, orig_ext));
  145.   out_filename[MIN(FILENAME_MAX, 256) - 2]='\0';     /* reduce if necessary */
  146.  
  147.   source_file = fopen (source_filename, "rb");       /* open read binary file */
  148.   if (source_file == NULL) {                    /* report if error, and stop. */
  149.     fprintf (stderr, "Fatal error opening %s for input.\n", source_filename);
  150.     exit (1);
  151.   }
  152.   printf ("Using file size of %ld bytes.\n", disk_size);
  153.   fseek (source_file, 0, SEEK_END);             /* set file pointer to end of */
  154.   file_length = ftell (source_file);            /* file, and get file length. */
  155.   fseek (source_file, 0, SEEK_SET);             /* reset pointer to start. */
  156.   if (file_length <= disk_size) {
  157.     fprintf (stderr, "No need to split file.\n");
  158.     fclose (source_file);
  159.     exit (0);
  160.   }
  161.  
  162.   buffer = (long *) malloc ((size_t) read_size); /* allocate memory for buffer*/
  163.   if (buffer == NULL) {
  164.     fprintf (stderr,
  165.         "Fatal error, unable to allocate memory block of %ld bytes\n",
  166.         read_size);
  167.     exit (1);
  168.   }
  169.   printf ("Using buffer size of %ld bytes.\n", read_size);
  170.  
  171. #ifdef ACORN
  172.   filedata.action = 5;
  173.   filedata.name = source_filename;
  174.   os_file (&filedata);
  175.   if (filedata.action != 1) {
  176.     fprintf (stderr, "Fatal error, %s is not a file\n", source_filename);
  177.     exit (1);
  178.   }
  179.   sprintf (type, "t%x", (0xFFF & filedata.loadaddr>>8));
  180. #endif
  181.  
  182.   sprintf (header, "Split:%s%s=%d=%s|", out_filename, orig_ext,
  183.       (int) (file_length / (disk_size - 7 - strlen (out_filename) -
  184.       strlen (orig_ext)) + 1), type);
  185.   file_number = 0;
  186.  
  187.   in_position = 0;
  188.   while (file_length - in_position > 0) {        /* if any data left */
  189.     file_number++;
  190.     numtostr (file_number, fnum);
  191.     while (interactive == 1) {
  192.       printf ("Enter path for %s%s%s (Return for %s) :\n", out_filename,
  193.           fnum, file_ext, out_path[0] == '\0' ? "current directory" : out_path);
  194.       gets (command);
  195.       if (strchr (command, ' ') != NULL) {
  196.         printf ("Invalid path name.\n");
  197.         continue;
  198.       }
  199.       if (command[0] != '\0') {
  200.         strcpy (out_path, command);
  201.         i = strlen (out_path);
  202.         if (out_path[i - 1] != FILE_SEPARATOR)
  203.           if (!COLON || (COLON && out_path[i - 1] != ':')) {
  204.             out_path[i] = FILE_SEPARATOR;
  205.             out_path[i + 1] = '\0';
  206.           }
  207.  
  208.       }
  209.       interactive = interactive | 2;
  210.     }
  211.     interactive = interactive & 1;
  212.     sprintf (out_name, "%s%s%s%s", out_path, out_filename, fnum, file_ext);
  213.     out_file = fopen (out_name, "wb");           /* open output file */
  214.     if (out_file == NULL) {                      /* report if error, and stop */
  215.       fprintf (stderr, "Fatal error opening %s for output.\n", out_name);
  216.       exit (1);
  217.     }
  218.     out_position = 0;
  219.     printf ("Writing data to %s\n", out_name);
  220.     if (file_number == 1) {
  221.       fprintf (out_file, header);
  222.       out_position = (long) strlen (header);
  223.     }
  224.     else {
  225.       numtostr (file_number, fnum);
  226.       sprintf (header, "Sp:%s%s=%s|", out_filename, orig_ext, fnum);
  227.       fprintf (out_file, header);
  228.       out_position = (long) strlen (header);
  229.     }
  230.     if (disk_size > file_length - in_position + out_position)
  231.       disk_size = file_length - in_position + out_position;
  232.     while (disk_size - out_position > 0) {
  233.       if (disk_size - out_position < read_size)
  234.         data_size = disk_size - out_position;
  235.       else
  236.         data_size = read_size;
  237.  
  238.       bytes_read = fread (buffer, 1, (size_t) data_size, source_file);
  239.       bytes_written = fwrite (buffer, 1, (size_t) bytes_read, out_file);
  240.  
  241.       if (bytes_written < data_size &&
  242.           bytes_written < file_length - out_position) {
  243.         fprintf (stderr, "Fatal error while writing %s\n", out_name);
  244.         exit (1);                                  /* if unsucessfull, stop */
  245.       }
  246.       in_position += bytes_read;
  247.       out_position += bytes_written;
  248.     }
  249.     fclose (out_file);
  250.  
  251. #ifdef ACORN
  252.     sprintf (command, "SetType %s %s", out_name, FILETYPE);
  253.     system (command);
  254. #endif
  255.  
  256.   }
  257.   fclose (source_file);                          /* tidy up*/
  258.   free (buffer);
  259.   fprintf (stderr, "%ld bytes written to %d %s.\n", in_position +
  260.       (file_number * (7 + strlen(source_filename))) + 4 + strlen(type),
  261.       file_number, (file_number == 1) ? "file" : "files");   /* report status */
  262.   fprintf (stderr, "Use : joinf %s01%s \nto restore file.\n",
  263.       out_filename, (file_ext[0] == '\0') ? "" : ".spt");
  264.   exit (1);                                      /* and finish */
  265. }
  266.