Esempio n. 1
0
File: n1.c Progetto: 99years/plan9
void casecf(void)
{	/* copy file without change */
	FILE *fd;
	char *eof, *p;
	extern int hpos, esc, po;

	/* this may not make much sense in nroff... */

	lgf++;
	nextf[0] = 0;
	if (!skip() && getname()) {
		if (strncmp("<<", nextf, 2) != 0) {
			if ((fd = fopen(nextf, "r")) == NULL) {
				ERROR "can't open file %s", nextf WARN;
				done(02);
			}
			eof = (char *) NULL;
		} else {	/* current file */
			if (pbp > lastpbp || ip) {
				ERROR "casecf: not reading from file" WARN;
				done(02);
			}
			eof = &nextf[2];
			if (!*eof)  {
				ERROR "casecf: missing end of input token" WARN;
				done(02);
			}
			p = eof;
			while(*++p)
				;
			*p++ = '\n';
			*p = 0;
			fd = ifile;
		}
	} else {
		ERROR "casecf: no argument" WARN;
		lgf--;
		return;
	}
	lgf--;

	/* make it into a clean state, be sure that everything is out */
	tbreak();
	hpos = po;
	esc = 0;
	ptesc();	/* to left margin */
	esc = un;
	ptesc();
	ptlead();
	ptps();
	ptfont();
	flusho();
	cpout(fd, eof);
	ptps();
	ptfont();
}
int main()
{
int fsinit = 0;
char input[256];
char *parser;
unsigned short n = 0;
char dirbuf[BLOCK_SIZE];
int i =0;
unsigned short bytes_written;
unsigned int number_of_blocks =0, number_of_inodes=0;

printf("Enter command:\n");
while(1)
{

scanf(" %[^\n]s", input);
parser = strtok(input," ");

if(strcmp(parser, "initfs")==0)
{
char *filepath;
char *num1, *num2;
filepath = strtok(NULL, " ");
num1 = strtok(NULL, " ");
num2 = strtok(NULL, " ");
	if(access(filepath, F_OK) != -1)
	{
	if((fd = open(filepath,O_RDWR,0600))== -1)
    {
        printf("\n filesystem already exists but open() failed with error [%s]\n",strerror(errno));
         return 1;
    }
	printf("filesystem already exists and the same will be used.\n");
	fsinit=1;
	}
	else
	{
	if (!num1 || !num2)
	  printf(" All arguments(path, number of inodes and total number of blocks) have not been entered\n");
		else
		{
		number_of_blocks = atoi(num1);
		number_of_inodes = atoi(num2);
		
		if(initialize_fs(filepath,number_of_blocks, number_of_inodes))
		{
		printf("The file system is initialized\n");
		fsinit = 1;
		}
		else
		{
		printf("Error initializing file system. Exiting... \n");
		return 1;
		}
		}
	}
 parser = NULL;
}
	else if(strcmp(parser, "mkdir")==0)
	{
		char *dirname;
	
	 if(fsinit == 0)
	 printf("The file system is not initialized. Please retry after initializing file system\n");
	 else
	 {
	  dirname = strtok(NULL, " ");
	  if(!dirname)
	    printf("No directory name entered. Please retry\n");
	  else
	  {
	  unsigned int dirinum = allocateinode();
	  if(dirinum < 0)
		{
		 printf("Error : ran out of inodes \n");
		 return 1;
		}
	  mkdirectory(dirname,dirinum);
	  }
	  }
	   parser = NULL;
	}
	else if(strcmp(parser, "cpin")==0)
	{
		char *targname;
		char *srcname;
	  if(fsinit == 0)
	 printf("The file system is not initialized. Please retry after initializing file system\n");
	  else
	  {

	  srcname = strtok(NULL, " ");
	  targname = strtok(NULL, " ");
	 
	  if(!srcname || !targname )
	    printf("Required file names(source and target file names) have not been entered. Please retry\n");
	  else
	  {
	  	
	   cpin(srcname,targname);
	  }
	  }
	   parser = NULL;
	}
	else if(strcmp(parser, "cpout")==0)
	{
		char *targnameout;
		char *srcnameout;
	 if(fsinit == 0)
	  printf("The file system is not initialized. Please retry after initializing file system\n");
	 else
	  {
	  
	  srcnameout = strtok(NULL, " ");
	  targnameout = strtok(NULL, " ");
	 
	  if(!srcnameout || !srcnameout )
	    printf("Required file names(source and target file names) have not been entered. Please retry\n");
	  else
	  {
	  	
	   cpout(srcnameout,targnameout);
	  }
	  }
	   parser = NULL;
	}
	else if(strcmp(parser, "q")==0)
	{
	
	lseek(fd,BLOCK_SIZE,0);

	 if((bytes_written =write(fd,&super,BLOCK_SIZE)) < BLOCK_SIZE)
	 {
	 printf("\nERROR : error in writing the super block");
	 return 1;
	 } 
	
	return 0;
	}
	else
	{
	printf("\nInvalid command\n ");
	}
	}
}