Esempio n. 1
0
void
move_file(void)
{
	char dstfile[sizeof(volhdr->voldir[0].name) + 1];
	size_t namelen;
	int i, slot = -1;

	/*
	 * Make sure the name in the volume header is max. 8 chars,
	 * NOT including NUL.
	 */
	namelen = strlen(ufilename);
	if (namelen > sizeof(volhdr->voldir[0].name)) {
		printf("Warning: '%s' is too long for volume header, ",
		       ufilename);
		namelen = sizeof(volhdr->voldir[0].name);
		printf("truncating to '%.8s'\n", ufilename);
	}
	memset(dstfile, 0, sizeof(dstfile));
	memcpy(dstfile, ufilename, namelen);

	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; i++) {
		if (names_match(i, vfilename)) {
			if (slot != -1) {
				printf("Error: Cannot move '%s' to '%s' - "
				    "duplicate source files exist!\n",
				    vfilename, dstfile);
				exit(1);
			}
			slot = i;
		}
		if (names_match(i, dstfile)) {
			printf("Error: Cannot move '%s' to '%s' - "
			    "destination file already exists!\n",
			    vfilename, dstfile);
			exit(1);
		}
	}
	if (slot == -1) {
		printf("File '%s' not found\n", vfilename);
		exit(1);
	}

	/* `dstfile' is already padded with NULs */ 
	memcpy(volhdr->voldir[slot].name, dstfile,
	    sizeof(volhdr->voldir[slot].name));
	
	write_volhdr();
}
Esempio n. 2
0
name_pointer
#line 270 "./cwebdir/comm-w2c.ch"

id_lookup P3C(char*,first,char*,last,char,t)
#line 666 "./cwebdir/common.w"
{
char*i= first;
int h;
int l;
name_pointer p;
if(last==NULL)for(last= first;*last!='\0';last++);
l= last-first;
/*38:*/
#line 683 "./cwebdir/common.w"

h= (unsigned char)*i;
while(++i<last)h= (h+h+(int)((unsigned char)*i))%hash_size;


/*:38*/
#line 673 "./cwebdir/common.w"
;
/*39:*/
#line 691 "./cwebdir/common.w"

p= hash[h];
while(p&&!names_match(p,first,l,t))p= p->link;
if(p==NULL){
p= name_ptr;
p->link= hash[h];hash[h]= p;
}

/*:39*/
#line 674 "./cwebdir/common.w"
;
if(p==name_ptr)/*41:*/
#line 706 "./cwebdir/common.w"
{
if(byte_ptr+l> byte_mem_end)overflow("byte memory");
if(name_ptr>=name_dir_end)overflow("name");
strncpy(byte_ptr,first,l);
(++name_ptr)->byte_start= byte_ptr+= l;
if(program==cweave)init_p(p,t);
}

/*:41*/
#line 675 "./cwebdir/common.w"
;
return(p);
}
Esempio n. 3
0
void
delete_file(void)
{
	int i;

	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i) {
		if (names_match(i, vfilename)) {
			break;
		}
	}
	if (i >= SGI_BOOT_BLOCK_MAXVOLDIRS) {
		printf("File '%s' not found\n", vfilename);
		exit(1);
	}

	/* XXX: we don't compact the file space, so get fragmentation */
	volhdr->voldir[i].name[0] = '\0';
	volhdr->voldir[i].block = volhdr->voldir[i].bytes = 0;
	write_volhdr();
}
Esempio n. 4
0
id_pointer id_lookup (char* first,char* last,int ilk)
  /* look up an identifier */
{ int l,h; /* length and hash code of the given identifier */
  if (last==NULL) last=first+(l=(int)strlen(first)); /* null-terminated string */
  else l=(int)(last-first); /* compute the length */
  
  { char* p=first;
    h=*p; while (++p<last) h=((h<<1)+*p)%hash_size;
  }
  
  { id_pointer p=hash[h]; /* the head of the hash list */
    while (p!=NULL && !names_match(p,first,l,ilk)) p=p->hash_link;
    if (p==NULL) /* we haven't seen this identifier before */
    
    { p=id_ptr; /* this is where the new name entry will be created */
      if (id_ptr++>=id_table_end) overflow ("identifier");
      name_begin(p)=store_string(first,l);
      if (program==cweave) init_id_name(p,ilk);
      p->hash_link=hash[h]; hash[h]=p; /* insert |p| at beginning of hash list */
    }
    return p;
  }
}
Esempio n. 5
0
void
write_file(void)
{
	FILE *fp;
	int slot;
	size_t namelen;
	int block, i;
	struct stat st;
	char fbuf[512];

	if (!opt_q)
		printf("Writing file %s\n", ufilename);
	if (stat(ufilename, &st) < 0) {
		perror("stat");
		exit(1);
	}
	if (!opt_q)
		printf("File %s has %lld bytes\n", ufilename, st.st_size);
	slot = -1;
	for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i) {
		if (volhdr->voldir[i].name[0] == '\0' && slot < 0)
			slot = i;
		if (names_match(i, vfilename)) {
			slot = i;
			break;
		}
	}
	if (slot == -1) {
		printf("No directory space for file %s\n", vfilename);
		exit(1);
	}
	/* -w can overwrite, -a won't overwrite */
	if (be32toh(volhdr->voldir[slot].block) > 0) {
		if (!opt_q)
			printf("File %s exists, removing old file\n", 
				vfilename);
		volhdr->voldir[slot].name[0] = 0;
		volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0;
	}
	if (st.st_size == 0) {
		printf("bad file size\n");
		exit(1);
	}
	/* XXX assumes volume header starts at 0? */
	block = allocate_space((int)st.st_size);
	if (block < 0) {
		printf("No space for file\n");
		exit(1);
	}

	/*
	 * Make sure the name in the volume header is max. 8 chars,
	 * NOT including NUL.
	 */
	namelen = strlen(vfilename);
	if (namelen > sizeof(volhdr->voldir[slot].name)) {
		printf("Warning: '%s' is too long for volume header, ",
		       vfilename);
		namelen = sizeof(volhdr->voldir[slot].name);
		printf("truncating to '%.8s'\n", vfilename);
	}

	/* Populate it w/ NULs */
	memset(volhdr->voldir[slot].name, 0,
	    sizeof(volhdr->voldir[slot].name));
	/* Then copy the name */
	memcpy(volhdr->voldir[slot].name, vfilename, namelen);

	volhdr->voldir[slot].block = htobe32(block);
	volhdr->voldir[slot].bytes = htobe32(st.st_size);

	write_volhdr();

	/* write the file itself */
	i = lseek(fd, block * 512, SEEK_SET);
	if (i < 0) {
		perror("lseek write");
		exit(1);
	}
	i = st.st_size;
	fp = fopen(ufilename, "r");
	while (i > 0) {
		fread(fbuf, 1, i > 512 ? 512 : i, fp);
		if (write(fd, fbuf, 512) != 512) {
			perror("write file");
			exit(1);
		}
		i -= i > 512 ? 512 : i;
	}
	fclose(fp);
}