Exemplo n.º 1
0
/* Load code and prototypes from file */
static void load(char *_filename) {
    FILE *fp;
    char *cp;
    word n, left;
    char filename[100]; /* should suffice on all systems */

    strcpy(filename, _filename);
    M = mallocate (memorysize + 1); /* allocate main memory array */
    if (M == NULL) abend("Memory size too large (use /m option)\n");

    addext(filename, ".ccd");
    if ((fp = fopen(filename, BINARYREAD)) == NULL) {
        fprintf(stderr, "Cannot open .ccd file\n");
        endrun(10);
    };

    ic = 0; /* read static data and code */
    left = memorysize + 1; /* from .ccd file */
    do {
        if (left == 0) abend("Memory size too small (use /m option)\n");
        n = min (IOBLOCK / sizeof(word), left);
        n = fread((char *) &M[ic], sizeof(word), (int) n, fp);
        ic += n;
        left -= n;
    } while (n != 0); /* now ic = number of words read */

    fclose(fp);
    /* Get various addresses passed by GENERATOR */
    ipradr = M[ic - 5]; /* primitive type desctriptions */
    temporary = M[ic - 4]; /* global temporary variables */
    strings = M[ic - 3]; /* string constants */
    lastprot = M[ic - 2]; /* last prototype number */
    freem = M[ic - 1]; /* first free word in memory */

    /* Read prototypes from .pcd file */
    addext(filename, ".pcd");
    if ((fp = fopen(filename, BINARYREAD)) == NULL) {
        fprintf(stderr, "Cannot open .pcd file\n");
        endrun(10);
    }
    for (n = MAINBLOCK; n <= lastprot; n++) {
        cp = ballocate (sizeof(protdescr));
        if (cp == NULL) abend("Memory size too large (use /m option)\n");
        prototype[n] = (protdescr *) cp;
        if (fread(cp, sizeof(protdescr), 1, fp) != 1)
            abend("Cannot read .pcd file\n");
    }
    fclose(fp);

    /* Open trace file */
    if (debug) {
        addext(filename, ".trd");
        if ((tracefile = fopen(filename, "w")) == NULL)
            abend("Cannot open .trd file\n");
    }
} /* end load */
Exemplo n.º 2
0
char *
find_backup_file_name(char *file)
{
  char *dir;
  char *base_versions;
  int highest_backup;

  if (backup_type == simple)
    {
      char *s = malloc (strlen (file) + strlen (simple_backup_suffix) + 1);
      strcpy (s, file);
      addext (s, simple_backup_suffix, '~');
      return s;
    }
  base_versions = concat (basename (file), ".~");
  if (base_versions == 0)
    return 0;
  dir = dirname (file);
  if (dir == 0)
    {
      free (base_versions);
      return 0;
    }
  highest_backup = max_backup_version (base_versions, dir);
  free (base_versions);
  free (dir);
  if (backup_type == numbered_existing && highest_backup == 0)
    return concat (file, simple_backup_suffix);
  return make_version_name (file, highest_backup + 1);
}
char *
find_backup_file_name (const char *file, enum backup_type backup_type)
{
  size_t backup_suffix_size_max;
  size_t file_len = strlen (file);
  size_t numbered_suffix_size_max = INT_STRLEN_BOUND (int) + 4;
  char *s;
  const char *suffix = simple_backup_suffix;

  /* Allow room for simple or `.~N~' backups.  */
  backup_suffix_size_max = strlen (simple_backup_suffix) + 1;
  if (HAVE_DIR && backup_suffix_size_max < numbered_suffix_size_max)
    backup_suffix_size_max = numbered_suffix_size_max;

  s = malloc (file_len + backup_suffix_size_max + numbered_suffix_size_max);
  if (s)
    {
      strcpy (s, file);

#if HAVE_DIR
      if (backup_type != simple)
	{
	  int highest_backup;
	  size_t dir_len = basename (s) - s;

	  strcpy (s + dir_len, ".");
	  highest_backup = max_backup_version (file + dir_len, s);
	  if (! (backup_type == numbered_existing && highest_backup == 0))
	    {
	      char *numbered_suffix = s + (file_len + backup_suffix_size_max);
	      sprintf (numbered_suffix, ".~%d~", highest_backup + 1);
	      suffix = numbered_suffix;
	    }
	  strcpy (s, file);
	}
#endif /* HAVE_DIR */

      addext (s, suffix, '~');
    }
  return s;
}
Exemplo n.º 4
0
/*
  This function loads a data file to the disk.
*/
int loadDataToDisk(char *name)
{
	FILE *fileToBeLoaded;
	int freeBlock[MAX_DATAFILE_SIZE_BASIC];
	int i,j,k,num_of_chars=0,num_of_blocks_reqd=0,file_size=0,num_of_words=0;
	for(i=0;i<MAX_DATAFILE_SIZE_BASIC;i++)
		freeBlock[i]=-1;
	char c='\0',*s;
	char filename[50],buf[16];
	s = strrchr(name,'\\');
	if(s!=NULL)
		strcpy(filename,s+1);
	else
		strcpy(filename,name);

	filename[15]='\0';
	addext(filename,".dat");

	expandpath(name);
	fileToBeLoaded = fopen(name, "r");
	if(fileToBeLoaded == NULL)
	{
		printf("File \'%s\' not found.!\n", name);
		return -1;
	}
	if(fileToBeLoaded == NULL)
	{
		printf("The file could not be opened!");
		return -1;
	}

	fseek(fileToBeLoaded, 0L, SEEK_END);

	num_of_chars = ftell(fileToBeLoaded);

	fseek(fileToBeLoaded,0,SEEK_SET);
	while(1)
	{
		fgets(buf,16,fileToBeLoaded);
		num_of_words++;
		if(feof(fileToBeLoaded))
			break;
	}
	num_of_blocks_reqd = (num_of_words/512) + 1;
	//printf("\n Chars = %d, Words = %d, Blocks(chars) = %d, Blocks(words) = %d",num_of_chars,num_of_words,num_of_blocks_reqd,(num_of_words/512));
	if(num_of_blocks_reqd > MAX_DATAFILE_SIZE)
	{
		printf("The size of file exceeds %d blocks",MAX_DATAFILE_SIZE);
		return -1;
	}

	fseek(fileToBeLoaded,0,SEEK_SET);

	for(i = 0; i < num_of_blocks_reqd + 1; i++)
	{
		if((freeBlock[i] = FindFreeBlock()) == -1){
				printf("not sufficient space in disk to hold a new file.\n");
				FreeUnusedBlock(freeBlock, MAX_DATAFILE_SIZE_BASIC);
				return -1;
			}
	}
	i = CheckRepeatedName(filename);
	if( i < FAT_SIZE )
	{
		printf("Disk already contains the file with this name. Try again with a different name.\n");
		FreeUnusedBlock(freeBlock, MAX_DATAFILE_SIZE_BASIC);
		return -1;
	}

	k = FindEmptyFatEntry();
	if( k == -1 )
	{
		FreeUnusedBlock(freeBlock, MAX_DATAFILE_SIZE_BASIC);
		printf("No free FAT entry found.\n");
		return -1;
	}


	for(i = DISK_FREE_LIST ;i < DISK_FREE_LIST + NO_OF_FREE_LIST_BLOCKS; i++)		//updating disk free list in disk
		writeToDisk(i, i);
	emptyBlock(TEMP_BLOCK);				//note:need to modify this

	for( i = 1 ; i < MAX_DATAFILE_SIZE_BASIC ; i++ )
	{
		storeValue(disk[TEMP_BLOCK].word[i-1],freeBlock[i]);
	}
	writeToDisk(TEMP_BLOCK,freeBlock[0]);

	for(i=0;i<num_of_blocks_reqd;i++)
	{
		j = writeFileToDisk(fileToBeLoaded, freeBlock[i+1], DATA_FILE);
		file_size++;
	}


	AddEntryToMemFat(k, filename, file_size * BLOCK_SIZE, freeBlock[0]);
	for(i = FAT; i < FAT + NO_OF_FAT_BLOCKS ; i++){
		writeToDisk(i,i);				//updating disk fat entry note:check for correctness
	}

      close(fileToBeLoaded);
      return 0;

}
Exemplo n.º 5
0
/*
  This function loads the executable file corresponding to the first arguement to an appropriate location on the disk.
  This function systematically uses the above functions to do this action.
*/
int loadExecutableToDisk(char *name)
{
	FILE *fileToBeLoaded;
	int freeBlock[SIZE_EXEFILE_BASIC];
	int i,j,k,l,file_size=0,num_of_lines=0,num_of_blocks_reqd=0;
	for(i=0;i<SIZE_EXEFILE_BASIC;i++)
		freeBlock[i]=-1;
	char c='\0',*s;
	char filename[50];
	s = strrchr(name,'\\');
	if(s!=NULL)
		strcpy(filename,s+1);
	else
		strcpy(filename,name);

	filename[15]='\0';

	addext(filename,".xsm");

	expandpath(name);
	fileToBeLoaded = fopen(name, "r");
	if(fileToBeLoaded == NULL){
	    printf("File %s not found.\n", name);
	    return -1;
	  }
	if(fileToBeLoaded == NULL){
		printf("The file could not be opened");
		return -1;
	}

	while(c!=EOF)
	{
		c=fgetc(fileToBeLoaded);
		if(c=='\n')
			num_of_lines++;
	}

	num_of_blocks_reqd = (num_of_lines / (BLOCK_SIZE/2)) + 1;

	if(num_of_blocks_reqd > SIZE_EXEFILE)
	{
		printf("The size of file exceeds %d blocks",SIZE_EXEFILE);
		return -1;
	}

	fseek(fileToBeLoaded,0,SEEK_SET);

	for(i = 0; i < num_of_blocks_reqd + 1; i++)
	{
		if((freeBlock[i] = FindFreeBlock()) == -1){
				printf("Insufficient disk space!\n");
				FreeUnusedBlock(freeBlock, SIZE_EXEFILE_BASIC);
				return -1;
			}
	}
	i = CheckRepeatedName(filename);
	if( i < FAT_SIZE ){
		printf("Disk already contains the file with this name. Try again with a different name.\n");
		FreeUnusedBlock(freeBlock, SIZE_EXEFILE_BASIC);
		return -1;
	}

	k = FindEmptyFatEntry();
	if( k == -1 ){
		FreeUnusedBlock(freeBlock, SIZE_EXEFILE_BASIC);
		printf("No free FAT entry found.\n");
		return -1;
	}


	for(i = DISK_FREE_LIST ;i < DISK_FREE_LIST + NO_OF_FREE_LIST_BLOCKS; i++)		//updating disk free list in disk
		writeToDisk(i, i);
	emptyBlock(TEMP_BLOCK);				//note:need to modify this

	for( i = 1 ; i < SIZE_EXEFILE_BASIC ; i++ )
	{
		storeValue(disk[TEMP_BLOCK].word[i-1],freeBlock[i]);
	}
	writeToDisk(TEMP_BLOCK,freeBlock[0]);

	for(i=0;i<num_of_blocks_reqd;i++)
	{
		j = writeFileToDisk(fileToBeLoaded, freeBlock[i+1], ASSEMBLY_CODE);
		file_size++;
	}



	AddEntryToMemFat(k, filename, file_size * BLOCK_SIZE, freeBlock[0]);
	for(i = FAT; i < FAT + NO_OF_FAT_BLOCKS ; i++){
		writeToDisk(i,i);
	}

      close(fileToBeLoaded);
      return 0;
}
Exemplo n.º 6
0
void
read_config_file (const char *cf) {
     char *bp;
     char *p;
     char buf[BUFSIZE];
     FILE *config = NULL;

     if (cf) {
	  /* User explicitly specified a config file */
	  if ((config = fopen (cf, "r")) == NULL) {
	       perror (cf);
	       gripe (CONFIG_OPEN_ERROR, cf);
	       return;
	  }
     } else {
	  /* Try some things - unfortunately we cannot lookup
	     the config file to use in the config file :-). */
	  int i;

	  for(i=0; i < SIZE(default_config_files); i++) {
	       cf = default_config_files[i];
	       if ((config = fopen (cf, "r")) != NULL)
		    break;
	  }

	  if (config == NULL) {
	       gripe (CONFIG_OPEN_ERROR, CONFIG_FILE);
	       return;
	  }
     }

     if (debug)
	  fprintf(stderr, "Reading config file %s\n", cf);
     configuration_file = cf;

     while ((bp = fgets (buf, BUFSIZE, config)) != NULL) {
	  while (whitespace(*bp))
	       bp++;

	  for (p = bp; *p && *p != '#' && *p != '\n'; p++) ;
	  if (!*p) {
	       gripe (LINE_TOO_LONG);
	       gripe (BAD_CONFIG_FILE, cf);
	       return;
	  }
	  while (p > bp && whitespace(p[-1]))
	       p--;
	  *p = 0;
      
	  if (*bp == 0)
	       continue;

	  if (!strncmp ("MANPATH_MAP", bp, 11))
	       adddir (bp+11, 0);
	  else if (!strncmp ("MANPATH", bp, 7))
	       addglobdir (bp+7, 1);
	  else if(!strncmp ("MANDATORY_MANPATH", bp, 17))/* backwards compatible */
	       adddir (bp+17, 1);
	  else if (!strncmp ("FHS", bp, 3))
	       fhs = 1;
	  else if (!strncmp ("FSSTND", bp, 6))
	       fsstnd = 1;
	  else if (!strncmp ("NOAUTOPATH", bp, 10))
		  noautopath = 1;
	  else if (!strncmp ("NOCACHE", bp, 7))
		  nocache = 1;
	  else if (*bp == '.')
	       addext (bp);
	  else
	       addval (bp);
     }
}