home *** CD-ROM | disk | FTP | other *** search
- /* Splitf.c */
- /* Part of splitf and joinf distribution */
- /* version 1.12, © 1993,1994 Adam Hamilton */
- /* See the README file for copyright information */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "config.h"
-
- #ifdef ACORN
- #define FILETYPE "B8C"
- #include "os.h"
- #endif
-
- #define Bool char
- #define False 0
- #define True 1
- #define MIN(a, b) (a < b ? a : b)
- #define MAX(a, b) (a > b ? a : b)
-
- #ifndef FILENAME_MAX
- #define FILENAME_MAX 8
- #endif
-
- void usage (char *progname)
- {
- fprintf (stderr,
- "File splitter. Version 1.12c - 29 September 1994 by A.Hamilton\n\n");
- fprintf (stderr, "Usage : %s [options] <filename>\n\n", progname);
- fprintf (stderr, "Options (can be abbreviated) :\n");
- fprintf (stderr,
- " -filesize [filesize in K] default = 1420\n");
- #ifndef PC
- fprintf (stderr, " -buffersize [buffersize in K] default = 32\n");
- #endif
- fprintf (stderr, " -path [new path]\n");
- fprintf (stderr, " -interactive\n");
- exit (1);
- }
-
- char *examine_filename (char original_name[], char *name, char *ext)
- {
- char main_name[256], *original, *pointer;
- register int i = -1, n;
-
- if (COLON)
- pointer = strrchr (original_name, ':');
- else
- pointer = NULL;
-
- original = strrchr (original_name, SEPARATOR_SEARCH);
- if ((original = MAX (original, pointer)) == NULL)
- original = original_name;
- else {
- original++;
- }
-
- do {
- i++;
- main_name[i] = original[i]; /* get name */
- } while (main_name[i] != '.' && main_name[i] != '\0');
-
- for (n = 0; (ext[n] = original[i]) != '\0'; n++, i++) ;
- if (main_name[i-n] == '\0') {
- strcpy (name, main_name);
- return ("");
- }
- main_name[i-n] = '\0';
- strcpy (name, main_name);
- return (".spt");
- }
-
- void numtostr (short number, char *name)
- {
- name[0] = (short) (number / 10) + '0';
- name[1] = (number % 10) + '0';
- name[2] = '\0';
- }
-
- int main (int argc, char **argv)
- {
- char source_filename[256], out_filename[256], out_path[256], file_ext[20];
- char out_name[256], header[50], fnum[3], *progname, command[256];
- char type[5], interactive = 0, orig_ext[20];
-
- short file_number = 0;
- long disk_size=1420*1024, read_size=32*1024, data_size;
- long out_position, in_position, bytes_read, bytes_written, file_length;
- long *buffer;
- int args, i;
- FILE *source_file, *out_file;
-
- #ifdef ACORN
- os_filestr filedata;
- #endif
-
- progname = *argv;
- strcpy (type, "0");
- out_path[0] = '\0'; /* set to default path */
- source_filename[0] = '\0';
- args = argc - 1 ;
- if (!args) usage (progname);
- while (args--) {
- argv++;
- if (!strncmp ("-filesize", *argv, MAX (2, strlen (*argv)))) {
- if (!args) {
- fprintf (stderr, "File size required\n\n");
- usage (progname);
- }
- disk_size = (long) atoi (*++argv) * 1024; /* file size */
- args--;
- }
- #ifndef PC
- else if (!strncmp ("-buffersize", *argv, MAX (2, strlen (*argv)))) {
- if (!args) {
- fprintf (stderr, "Buffer size required\n\n");
- usage (progname);
- }
- read_size = (long) atoi (*++argv) * 1024; /* buffer size */
- args--;
- }
- #endif
- else if (!strncmp ("-path", *argv, MAX (2, strlen (*argv)))) {
- if (!args) {
- fprintf (stderr, "Path required\n\n");
- usage (progname);
- }
- sprintf (out_path, "%s%c", *++argv, FILE_SEPARATOR); /* output path */
- args--;
- }
- else if (!strncmp ("-interactive", *argv, MAX (2, strlen (*argv))))
- interactive = 1;
- else {
- strcpy (source_filename, *argv); /* source file */
- if (args) usage (progname);
- }
- }
- if (source_filename[0] == NULL) {
- fprintf (stderr, "Source filename required\n\n");
- usage (progname);
- }
-
- strcpy (file_ext, examine_filename (source_filename, out_filename, orig_ext));
- out_filename[MIN(FILENAME_MAX, 256) - 2]='\0'; /* reduce if necessary */
-
- source_file = fopen (source_filename, "rb"); /* open read binary file */
- if (source_file == NULL) { /* report if error, and stop. */
- fprintf (stderr, "Fatal error opening %s for input.\n", source_filename);
- exit (1);
- }
- printf ("Using file size of %ld bytes.\n", disk_size);
- fseek (source_file, 0, SEEK_END); /* set file pointer to end of */
- file_length = ftell (source_file); /* file, and get file length. */
- fseek (source_file, 0, SEEK_SET); /* reset pointer to start. */
- if (file_length <= disk_size) {
- fprintf (stderr, "No need to split file.\n");
- fclose (source_file);
- exit (0);
- }
-
- buffer = (long *) malloc ((size_t) read_size); /* allocate memory for buffer*/
- if (buffer == NULL) {
- fprintf (stderr,
- "Fatal error, unable to allocate memory block of %ld bytes\n",
- read_size);
- exit (1);
- }
- printf ("Using buffer size of %ld bytes.\n", read_size);
-
- #ifdef ACORN
- filedata.action = 5;
- filedata.name = source_filename;
- os_file (&filedata);
- if (filedata.action != 1) {
- fprintf (stderr, "Fatal error, %s is not a file\n", source_filename);
- exit (1);
- }
- sprintf (type, "t%x", (0xFFF & filedata.loadaddr>>8));
- #endif
-
- sprintf (header, "Split:%s%s=%d=%s|", out_filename, orig_ext,
- (int) (file_length / (disk_size - 7 - strlen (out_filename) -
- strlen (orig_ext)) + 1), type);
- file_number = 0;
-
- in_position = 0;
- while (file_length - in_position > 0) { /* if any data left */
- file_number++;
- numtostr (file_number, fnum);
- while (interactive == 1) {
- printf ("Enter path for %s%s%s (Return for %s) :\n", out_filename,
- fnum, file_ext, out_path[0] == '\0' ? "current directory" : out_path);
- gets (command);
- if (strchr (command, ' ') != NULL) {
- printf ("Invalid path name.\n");
- continue;
- }
- if (command[0] != '\0') {
- strcpy (out_path, command);
- i = strlen (out_path);
- if (out_path[i - 1] != FILE_SEPARATOR)
- if (!COLON || (COLON && out_path[i - 1] != ':')) {
- out_path[i] = FILE_SEPARATOR;
- out_path[i + 1] = '\0';
- }
-
- }
- interactive = interactive | 2;
- }
- interactive = interactive & 1;
- sprintf (out_name, "%s%s%s%s", out_path, out_filename, fnum, file_ext);
- out_file = fopen (out_name, "wb"); /* open output file */
- if (out_file == NULL) { /* report if error, and stop */
- fprintf (stderr, "Fatal error opening %s for output.\n", out_name);
- exit (1);
- }
- out_position = 0;
- printf ("Writing data to %s\n", out_name);
- if (file_number == 1) {
- fprintf (out_file, header);
- out_position = (long) strlen (header);
- }
- else {
- numtostr (file_number, fnum);
- sprintf (header, "Sp:%s%s=%s|", out_filename, orig_ext, fnum);
- fprintf (out_file, header);
- out_position = (long) strlen (header);
- }
- if (disk_size > file_length - in_position + out_position)
- disk_size = file_length - in_position + out_position;
- while (disk_size - out_position > 0) {
- if (disk_size - out_position < read_size)
- data_size = disk_size - out_position;
- else
- data_size = read_size;
-
- bytes_read = fread (buffer, 1, (size_t) data_size, source_file);
- bytes_written = fwrite (buffer, 1, (size_t) bytes_read, out_file);
-
- if (bytes_written < data_size &&
- bytes_written < file_length - out_position) {
- fprintf (stderr, "Fatal error while writing %s\n", out_name);
- exit (1); /* if unsucessfull, stop */
- }
- in_position += bytes_read;
- out_position += bytes_written;
- }
- fclose (out_file);
-
- #ifdef ACORN
- sprintf (command, "SetType %s %s", out_name, FILETYPE);
- system (command);
- #endif
-
- }
- fclose (source_file); /* tidy up*/
- free (buffer);
- fprintf (stderr, "%ld bytes written to %d %s.\n", in_position +
- (file_number * (7 + strlen(source_filename))) + 4 + strlen(type),
- file_number, (file_number == 1) ? "file" : "files"); /* report status */
- fprintf (stderr, "Use : joinf %s01%s \nto restore file.\n",
- out_filename, (file_ext[0] == '\0') ? "" : ".spt");
- exit (1); /* and finish */
- }
-