main 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// Copyright 1999 Element 14 Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/************************************************************************/
/* File:    main.c                                                      */
/* Purpose: The main program.                                           */
/*                                                                      */
/* Author:  David Cotton <mailto:dcotton@e-14.com>                      */
/* History: 0.01  1999-01-26                                            */
/*                Created.                                              */
/************************************************************************/


// Include Standard C headers
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>

// Include Application headers
#include "types.h"
#include "TList.h"
#include "recurse.h"
#include "RMalloc.h"
#ifdef MemCheck_MEMCHECK
36
  #include "MemCheck/MemCheck.h"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#endif /* MemCheck_MEMCHECK */

// Define pre-processor symbols.
// #define USE_TEST_HARNESS



/* ----------------------------------------- FUNCTIONS ------------------------------------------ */



#ifdef USE_TEST_HARNESS
/************************************************************************/
/* main                                                                 */
/*                                                                      */
/* Implements a test harness to stress the TList funcationality. Run    */
/*  this harness and ensure that all lsist appear correct, no nodes     */
/*  have been lost where they shouldn't be, and the lists are sorted    */
/*  correctly.                                                          */
/*                                                                      */
/* Parameters: none.                                                    */
/*                                                                      */
/* Assumes:    USE_TEST_HARNESS is defined.                             */
/*                                                                      */
/* Returns:    void.                                                    */
/*                                                                      */
/************************************************************************/
void main(void)
{
/*
#ifdef MemCheck_MEMCHECK
  MemCheck_InitNoDebug();
  MemCheck_RedirectToFilename("$.MemCheck");
  MemCheck_SetStoreMallocFunctions(1);
//  MemCheck_InterceptSCLStringFunctions();
  atexit(MemCheck_OutputBlocksInfo);
  MemCheck_SetQuitting(1, 1);
  MemCheck_SetAccessQuitting(1, 1);
#endif
*/
  for (int j=1; j<=1; j++)
  {
    list<int> single;
    for (int i=1; i<=10; i++)
    {
      single.append_node(new node<int> (rand()%10));
    }
    printf("The unsorted list...\n");
    single.show_list();
    single.sort();
    printf("\nThe sorted list...\n");
    single.show_list();
  }


  // Now do a list of filepaths.
  printf("Now displaying a list of CFilePath objects.");
  list<CFilePath> paths_list;

  paths_list.append_node(new node<CFilePath> ("First"));
  paths_list.append_node(new node<CFilePath> ("Second"));
  paths_list.append_node(new node<CFilePath> ("Third"));
  paths_list.append_node(new node<CFilePath> ("Fourth"));
  paths_list.append_node(new node<CFilePath> ("rfdfrsdg"));
  cout << "The unsorted list..." << endl;
  paths_list.show_list();
  cout << endl << "The sorted list..." << endl;
  paths_list.sort();
  paths_list.show_list();
  cout << endl << endl;
  cout << "Now delete the second node." << endl;
  paths_list.delete_node("Second");
  cout << "Now delete the last node." << endl;
  paths_list.delete_node("rfdfrsdg");
  cout << "Now delete the first node." << endl;
  paths_list.delete_node("First");
  paths_list.show_list();
  cout << "End of the program." << endl;
}
#endif // USE_TEST_HARNESS





#ifndef USE_TEST_HARNESS


/* -------------------------------------- Local variables --------------------------------------- */
/* This stores the maximum length allowable before a warning is produced. The default value is 100. */
unsigned int max_filename_length = 100;

/* This is the path seperator character */
char path_seperator = '.';

/* If this is 0, only errors are produced, otherwise verbose output */
unsigned int verbose_text = 0;

/* This stores the root filename. */
char* root_filename = NULL;

/* This stores the name the archiver is to use. */
char* archive_name = NULL;

/* This stores the total number of files that have been scanned. */
unsigned long num_files_scanned = 0;


// This routine tidies up the memory allocated in main() for things like filenames.
void tidy_memory(void)
{
  DBUG_PRINTF("tidy_memory()\n");

  if (root_filename)
  {
    rfree(root_filename);
  }

  if (archive_name)
  {
    rfree(archive_name);
  }
}


162 163 164 165 166 167 168 169 170 171 172 173 174 175
void display_help_message(void)
{
  printf("Usage: DirScan   [[-r <Root Path>] [-a <archive name>] [-v] [-maxlen <Maximum Length>]..]\n");
  printf("\n");
  printf("  -r is the path of the directory to be archived.\n");
  printf("  -a is the name of the archive to be created. If a CD is to be blown, then this should be under 8 characters in length.\n");
  printf("  -maxlen sets the maximum length of permissible filename. It has a default value of 100 and a minimum of 50.");
  printf("  -v switches on verbose output. this displays all the files to be placed inside the archive and their path lengths.");
  printf("\n");
  printf("  TarExtend can be used to overcome a limitation in the RiscOS implementation of Tar which only allows path lengths of 100 characters or less. TarExtend improves this situation by renaming any directory structures that pass this limit to less than it by renaming directories inside the structure. It also performs CRC checks on the finished archive and all the individual files, allowing verification of the archive.\n");
  printf("  It outputs three files; arc, dearc and manual. Arc can be run to archive the required files; drarc can be run to dearchive them, and manual describes the renames performed (if any).\n");
  printf("\n");
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

int main( int argc, char **argv )
{
#ifdef MemCheck_MEMCHECK
  MemCheck_InitNoDebug();
  MemCheck_RedirectToFilename("<obey$dir>.MemCheck");
  MemCheck_SetStoreMallocFunctions(1);
  MemCheck_SetStoreMiscFunctions( 1);	// So that diagnostics tell us which functions malloced each malloc block.
//  MemCheck_InterceptSCLStringFunctions();
  atexit(MemCheck_OutputBlocksInfo);
  MemCheck_SetQuitting(1, 1);
  MemCheck_SetAccessQuitting(1, 1);
  MemCheck_RegisterArgs( argc, argv);	// Tell MemCheck that strings in argv[0...argc-1] are ok to read.
#endif // MemCheck_MEMCHECK

191
  printf("Tar Extension Utility Version 0.03 (%s)\n", __DATE__ );
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  atexit(tidy_memory);

  gstrans(argv);
  ++argv;
  while (argc > 1 && **argv == '-')
  {
    if (strcmp(argv[0],"-r") == 0)                 /* directory name */
    {
      rfree (*argv); // Free the gstrans'ed string for the -r flag
      // Allocate memory for root filename
      unsigned int length = strlen(*++argv);
      root_filename = rmalloc(length+1);
      if (!root_filename)
      {
        printf("Error allocating memory for the root filename.\n");
        exit(EXIT_FAILURE);
      }
      else
      {
        strcpy(root_filename, *argv);
        printf("Root filename is %s\n", root_filename);
        rfree (*argv); // Free the gstrans'ed string for the root filename
      }
      --argc;
    }

    if (strcmp(argv[0],"-v") == 0)                 /* verbose text */
    {
      verbose_text=1;
      rfree (*argv);
    }

    if (strcmp(argv[0],"-a") == 0)                 /* archive filename */
    {
      rfree (*argv); // Free the gstrans'ed string for the -a flag
      // Allocate memory for archive name
      unsigned int length = strlen(*++argv);
      archive_name = rmalloc(length+1);
      if (!archive_name)
      {
        printf("Error allocating memory for the archive name.\n");
        exit(EXIT_FAILURE);
      }
      else
      {
        strcpy(archive_name, *argv);
        printf("Archive name is %s\n", archive_name);
        rfree (*argv);// Free the gstrans'ed string for the archive filename
      }
      --argc;
    }

    if (strcmp(argv[0],"-maxlen") == 0)            /* max length allowed before complaining */
    {
      max_filename_length = atoi(*++argv);
      printf("Setting maximum allowable filename length before warning to %d\n", max_filename_length);
      rfree (*argv);// Free the gstrans'ed string for the -maxlen flag
    }

    ++argv;
    --argc;
  }

255
  printf("Tar Extension Utility Version 0.03 (%s)\n", __DATE__ );
256 257 258 259

  if (max_filename_length < 50)
  {
    printf("The maximum length given must have a value greater than 50.\n");
260
    display_help_message();
261 262 263 264 265
  }

  if (!archive_name) /* no archive name has been given. */
  {
    printf("No name given for archive. Use the -a flag to set it.\n");
266
    display_help_message();
267 268 269 270 271 272
    exit(EXIT_FAILURE);
  }

  if (!root_filename) /* no root filename has been given. */
  {
    printf("No name given for root filename. Use the -r flag to set it.\n");
273
    display_help_message();
274 275 276 277 278 279 280
    exit(EXIT_FAILURE);
  }

  // Ensure root filename exists
  if (!file_exists(root_filename))
  {
    printf("File %s does not exist.\n", root_filename);
281
    display_help_message();
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    exit(EXIT_FAILURE);
  }

  // Scan the directories...
  char namebuf[1024];
  sprintf( namebuf, "%s", root_filename);
  reccheckdir( namebuf, namebuf+strlen(namebuf), namebuf+strlen(namebuf)+1);


  printf("Number of files scanned: %d\n", num_files_scanned);

  display_paths();

  DBUG_PRINTF("Now output all the files.\n");
  // Output the (de)archiver file(s)
  char* filename_store = rmalloc(strlen(root_filename)+15); // Where 15 is enough to append the leafnames below... Nasty but a quick hack
  if (!filename_store)
  {
    cout << "Error allocating memory for filenames." << endl;
  }
  DBUG_PRINTF("Output the archiver file.\n");
  strcpy(filename_store, root_filename);
  strcat(filename_store, ".^.arc");
  output_archiver_file  (filename_store);
  DBUG_PRINTF("Output the dearchiver file.\n");
  strcpy(filename_store, root_filename);
  strcat(filename_store, ".^.dearc");
  output_dearchiver_file(filename_store);
  DBUG_PRINTF("Output the manual file.\n");
  strcpy(filename_store, root_filename);
  strcat(filename_store, ".^.manual");
  output_manual_file(filename_store);
  rfree(filename_store);

  return (EXIT_SUCCESS);
}
#endif // USE_TEST_HARNESS