home *** CD-ROM | disk | FTP | other *** search
- /* Joinf.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 <ctype.h>
-
- #ifdef ACORN
- #include "os.h"
- #endif
-
- #define TITLE "File joiner. Version 1.12 - 30 March 1994 by A.Hamilton\n\n"
- #define Bool char
- #define False 0
- #define True 1
- #define MAX(a, b) (a > b ? a : b)
-
- void usage (char *progname)
- {
- fprintf (stderr, TITLE);
- fprintf (stderr, "Usage : %s [options] <filename>\n\n", progname);
- fprintf (stderr, "Options (can be abbreviated) :\n");
- #ifndef PC
- fprintf (stderr, " -buffersize [buffersize in K] default = 32\n");
- #endif
- fprintf (stderr, " -filename [new filename]\n");
- fprintf (stderr, " -interactive\n");
- fprintf (stderr, " -info\n");
- exit (1);
- }
-
- char *examine_filename (char original_name[], char *name, char *path)
- {
- char main_name[256], *original, ext[20], *pointer;
- 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++;
- strncpy (path, original_name, original - original_name);
- path[original - original_name] = '\0';
- }
-
- do {
- i++;
- main_name[i] = original[i]; /* get name */
- } while (main_name[i] != '.' && main_name[i] != '\0');
- if (main_name[i] == '\0') {
- main_name[i-2] = '\0';
- strcpy (name, main_name);
- return ("");
- }
- main_name[i-2] = '\0';
- strcpy (name, main_name);
-
- for (n = 0; (ext[n] = original[i]) != '\0'; n++, i++) ;
- return (ext);
- }
-
- 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], file_ext[20], type[5];
- char out_name[256], source_name[256], header[50], fnum[3], in_path[256];
- char check_name[256], *progname, interactive = 0, command[256];
- static char splith1[]="Split:", splith2[]="Sp:";
- short file_number = 0, check_number;
- long read_size=32*1024, out_position, in_position, bytes_read, bytes_written;
- long file_length, *buffer;
- int total_number, i, n, args;
- Bool info = False;
- FILE *source_file, *out_file;
-
- #ifdef ACORN
- os_filestr filedata;
- #endif
-
- progname = *argv;
- in_path[0] = '\0';
- out_filename[0] = '\0';
- source_filename[0] = '\0';
- args = argc - 1 ;
- if (!args) usage (progname);
- while (args--) {
- argv++;
- if (!strncmp ("-filename", *argv, MAX (2, strlen (*argv)))) {
- if (!args) {
- fprintf (stderr, "Filename required\n\n");
- usage (progname);
- }
- strcpy (out_filename, *++argv); /* output filename */
- 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 ("-interactive", *argv, MAX (2, strlen (*argv))))
- interactive = 1;
- else if (!strncmp ("-info", *argv, MAX (3, strlen (*argv))))
- info = True;
- 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, source_name, in_path));
-
- 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);
- }
- i=0;
- while ((header[i++] = fgetc (source_file)) != '|' && i<300) ;
- for (i = 0; i < 6; i++) if (header[i] != splith1[i]) {
- fprintf (stderr,"Fatal error, no split header in file %s\n",
- source_filename);
- fclose (source_file);
- exit (1);
- }
-
- n=0;
- #ifdef ACORN
- while (header[i] != '=') {
- out_name[n++] = (header[i] == '.' ? '/' : header[i]);
- i++;
- }
- n++;
- i++;
- #else
- while ((out_name[n++] = header[i++]) != '=') ;
- #endif
-
- out_name[--n] = '\0';
- if (out_filename[0] == '\0') strcpy (out_filename, out_name);
- n = 0;
- while ((fnum[n++] = header[i++]) != '=') ;
- fnum[--n] = '\0';
- total_number = atoi (fnum);
- n = 0;
- while ((type[n++] = header[i++]) != '|') ;
- type[--n] = '\0';
-
- if (info) {
- printf (TITLE);
- printf ("Information :\n\n");
- printf (" Output filename is %s", out_filename);
-
- #ifdef ACORN
- if (type[0] == 't') {
- printf (", filetype ");
- for (n = 1; type[n] != '\0'; n++)
- printf ("%c", toupper(type[n]));
- }
- #endif
-
- printf ("\n");
- bytes_read = 0;
- }
- else {
-
- buffer = (long *) malloc ((size_t) read_size); /* allocate memory for */
- if (buffer == NULL) { /* a buffer.*/
- 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);
-
- out_file = fopen (out_filename, "wb"); /* open output file */
- if (out_file == NULL) { /* report if error, and stop */
- fprintf (stderr, "Fatal error opening %s for output.\n", out_filename);
- exit (1);
- }
- }
-
- out_position = 0;
- for (file_number = 1; file_number <= total_number; file_number++) {
- numtostr (file_number, fnum);
- while (interactive == 1 && file_number > 1) {
- printf ("Enter path for %s%s%s (Return for %s) :\n", source_name,
- fnum, file_ext, in_path[0] == '\0' ? "current directory" : in_path);
- gets (command);
- if (strchr (command, ' ') != NULL) {
- printf ("Invalid path name.\n");
- continue;
- }
- if (command[0] != '\0') {
- strcpy (in_path, command);
- i = strlen (in_path);
- if (in_path[i - 1] != FILE_SEPARATOR)
- if (!COLON || (COLON && in_path[i - 1] != ':')) {
- in_path[i] = FILE_SEPARATOR;
- in_path[i + 1] = '\0';
- }
-
- }
- interactive = interactive | 2;
- }
- interactive = interactive & 1;
- sprintf (source_filename, "%s%s%s%s", in_path, source_name, fnum, file_ext);
-
- if (file_number != 1) {
- 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);
- }
- i = 0;
- while ((header[i++] = fgetc (source_file)) != '|' && i<300) ;
- for (i = 0; i < 3; i++) if (header[i] != splith2[i]) {
- fprintf (stderr,"Fatal error, bad header in file %s\n",
- source_filename);
- fclose (source_file);
- exit (1);
- }
-
- n = 0;
- #ifdef ACORN
- while (header[i] != '=') {
- check_name[n++] = (header[i]=='.' ? '/' : header[i]);
- i++;
- }
- n++;
- i++;
- #else
- while ((check_name[n++] = header[i++]) != '=') ;
- #endif
-
- check_name[--n] = '\0';
- if (strcmp (out_name, check_name)) {
- fprintf (stderr,"Fatal error, split file %s does not match.\n",
- source_filename);
- fclose (source_file);
- exit (1);
- }
- n = 0;
- while ((fnum[n++] = header[i++]) != '|') ;
- fnum[--n] = '\0';
- check_number = atoi (fnum);
- if (check_number != file_number) {
- fprintf (stderr,"Fatal error, incorrect part.\n");
- fclose (source_file);
- exit (1);
- }
- }
- in_position = (long) i;
-
- fseek (source_file, 0L, SEEK_END); /* set file pointer to end of */
- file_length = ftell (source_file); /* file, and get file length. */
- fseek (source_file, in_position, SEEK_SET); /* reset pointer to start. */
-
- if (info) {
- printf (" %s...%ld bytes\n", source_filename,
- file_length - in_position);
- bytes_read += file_length - in_position;
- }
- else {
- printf ("Reading data from %s.....%ld bytes\n", source_filename,
- file_length - in_position);
- while (file_length - in_position > 0) {
- bytes_read = fread (buffer, 1, (size_t) read_size, source_file);
- bytes_written = fwrite (buffer, 1, (size_t) bytes_read, out_file);
-
- if (bytes_written < read_size &&
- bytes_written < file_length-in_position) {
- fprintf (stderr, "Fatal error while writing %s\n", out_filename);
- exit (1); /* if unsucessfull, stop */
- }
- in_position += bytes_read;
- out_position += bytes_written;
- }
- }
- fclose (source_file);
- if (!info && (bytes_written < bytes_read)) {
- fprintf (stderr, "Fatal error while writing %s\n", out_filename);
- exit (1); /* if unsucessfull, stop */
- }
- }
- if (info)
- printf ("\nTotal of %ld bytes contained in %d files.\n", bytes_read,
- total_number);
- else {
- fclose (out_file); /* tidy up*/
- free (buffer);
-
- #ifdef ACORN
- if (type[0] == 't') {
- filedata.action = 18;
- filedata.name = out_filename;
- sscanf (type, "t%x", &filedata.loadaddr);
- os_file (&filedata);
- }
- #endif
-
- fprintf (stderr, "%ld bytes written to file %s\n", out_position,
- out_filename);
- }
- exit (0); /* and finish */
- }
-