Esempio n. 1
0
int main(int agrc,char const *agrv[])
{
//	char s[80]={};
	char s1[]="Hello ";
//	printf("%d",strlen(s1));
	const	char *s2="World";
	mycat(s1,s2);
//	cat(s,s2); 
	printf("%s",s1);
	printf(" %d",strlen(s1));
	printf(" %d",sizeof(s1));
}
Esempio n. 2
0
main(int argc, char *argv[ ])
{
       //*Lab 5 CODE
  char *tok;
  char cmdArgs[2][MAX] = {'\0'};
  char cwd[128];
  int cmdCount = 0;
       //LAB 5 CODE*
  char temp[MAX];

  int n;
  char line[MAX], ans[MAX];

       //*Lab 5 CODE
  getcwd(cwd, 128);   // get CWD pathname
       // Lab 5 CODE*
  printf("cwd: %s\n", cwd);

  if (argc < 3){
     printf("Usage : client ServerName ServerPort\n");
     exit(1);
  }

  client_init(argv);
  // sock <---> server
  printf("********  processing loop  *********\n");
  while (1){
    //printf("input two numbers to add : ");
    //bzero(line, MAX);                // zero out line[ ]

          // *LAB 5 CODE
    cmdCount = 0;
    strcpy(cmdArgs[0],"");
    strcpy(cmdArgs[1],"");
    
    printf("Input a command: ");    
          // LAB 5 CODE*

    fgets(line, MAX, stdin);         // get a line (end with \n) from stdin
    //printf("line: %s\n", line);
    line[strlen(line)-1] = 0;        // kill \n at end
    //printf("line[0]: %c\n", line[0]);
    //if(line[0]==0)                  // exit if NULL line
       //{exit(0);}

         // *Lab 5 CODE
    strcpy(temp, line);
    //printf("temp: %s\n", temp);
    
    tok = strtok(line, " ");
    cmdCount++;
    //printf("temp: %s\n", temp);
    //printf("cmdArgs[0] = %s\n", cmdArgs[0]);
    //printf("cmdCount: %d\n", cmdCount);
    while(tok != NULL)
    {
      strcpy(cmdArgs[cmdCount-1], tok);
      tok = strtok(NULL, " ");
      cmdCount++;
      //printf("cmdCount: %d\n", cmdCount);
    }
    //printf("cmdCount: %d\n", cmdCount);
    //printf("line: %s\n", line);
    //printf("cmdArgs[0] = %s\n", cmdArgs[0]);
    if(strcmp(cmdArgs[0], "lcat") == 0) 
    {
      if(strcmp(cmdArgs[1],"") != 0) {mycat(cmdArgs[1]);}
      else {printf("ERROR: pathname not given");}
    }
    else if (strcmp(cmdArgs[0], "lpwd") == 0) 
    {
      mypwd(cwd);
    }
    else if (strcmp(cmdArgs[0], "lls") == 0) 
    {
      printf("args: %s\n", cmdArgs[1]);
      if(strcmp(cmdArgs[1],"") != 0) {myls(cmdArgs[1]);}
      else 
      {
        printf("cwd: %s\n", cwd);
        myls(cwd);
      }
    }
    else if (strcmp(cmdArgs[0], "lcd") == 0) 
    {
      if(strcmp(cmdArgs[1],"") != 0) {mycd(cmdArgs[1]);}
      else {mycd(cwd);}
    }
    else if (strcmp(cmdArgs[0], "lmkdir") == 0) 
    {
      if(strcmp(cmdArgs[1],"") != 0) {mymkdir(cmdArgs[1]);}
      else {printf("ERROR: pathname not given");}
    }
    else if (strcmp(cmdArgs[0], "lrmdir") == 0) 
    {
      if(strcmp(cmdArgs[1],"") != 0) {myrmdir(cmdArgs[1]);}
      else {printf("ERROR: pathname not given");}
    }
    else if (strcmp(cmdArgs[0], "lrm") == 0) 
    {
      if(strcmp(cmdArgs[1],"") != 0) {myrm(cmdArgs[1]);}
      else {printf("ERROR: pathname not given");}
    }
    else if (strcmp(cmdArgs[0], "get") == 0) 
    {
      if(strcmp(cmdArgs[1],"") != 0) {mycp(cmdArgs[1], cwd);}
      else {printf("ERROR: pathname not given");}
    }
    else if (strcmp(cmdArgs[0], "quit") == 0) 
    {
      printf("Goodbye!\n");
      return 0;
    }
    else // Not a local command, work with the sever
    {
      // Send command to server
      n = write(server_sock, temp, MAX);
      printf("client: wrote n=%d bytes; line=(%s)\n", n, temp);
      // Send command pathname
      //n = write(server_sock, cmdArgs[0], MAX);
      //printf("client: wrote n=%d bytes; line=(%s)\n", n, line);

      // Now read the Reply and Results in from the Server...

      //REPLY
      //n = read(server_sock, ans, MAX);
      //printf("client: read  n=%d bytes; echo=(%s)\n",n, ans);

      //RESULTS
      //n = read(server_sock, ans, MAX);
      //printf("client: read  n=%d bytes; echo=(%s)\n",n, ans);

      readData(ans);
    }
         // Lab 5 CODE*

    //
    // Send ENTIRE line to server
    //n = write(server_sock, line, MAX);
    //printf("client: wrote n=%d bytes; line=(%s)\n", n, line);

    // Read a line from sock and show it
    //n = read(server_sock, ans, MAX);
    //printf("client: read  n=%d bytes; echo=(%s)\n",n, ans);
  }
}
Esempio n. 3
0
int main() {
  char a[128] = "These";
  char b[128] = "are";
  char c[128] = "the";
  char d[128] = "test";
  char e[128] = "cases";
  char f[128] = "car";
  char g[128] = "aren";
  char h[128] = "abcdefghijklmnopqrstuvwxyzzzzzzzzzzzz";
  char i[128] = "cases";
  char j[128] = "casesss";
  char k[128] = "arent";
  char l[128] = "longwords";
  char m[128] = "neededlongwords";
  char n[128] = "lastlongwordormaybenot";

  char s[128];
  char t[128];
  char u[128];

  char v[128];
  char w[128];

  char x[128];
  char y[128];
  char z[128];
  
  printf("---mylen Testing---\n");
  printf("Test One:\n These - - - Length: 5\n Run: %d\n", mylen(a));
  printf("Test Two:\n are - - - Length: 3\n Run: %d\n", mylen(b));
  printf("Test Three:\n abcdefghijklmnopqrstuvwxyz - - - Length: 26\n Run: %d\n", mylen(h));
  printf("\n");

  printf("---mycmp Testing---\n");
  printf("Test One:\n Word 1: cases - Word 2: car\n Run: %d\n", mycmp(e, f));
  printf("Test Two:\n Word 1: cases - Word 2: cases\n Run: %d\n", mycmp(e, i));
  printf("Test Three:\n Word 1: are - Word 2: the\n Run: %d\n", mycmp(b, c));
  printf("Test Four:\n Word 1: casesss - Word 2: cases\n Run: %d\n", mycmp(j, e));
  printf("Test Five:\n Word 1: aren - Word 2: arent\n Run: %d\n", mycmp(g, k));
  printf("\n");

  printf("---mycpy Testing---\n");
  printf("Before Copying:\n");
  printf(" x: %s\n y: %s\n z: %s\n", x, y, z);
  printf("Copying:\n %s --> x\n %s --> y\n %s --> z\n", h, i, j);
  mycpy(x, h);
  mycpy(y, i);
  mycpy(z, j);
  printf("After Copying:\n");
  printf(" x: %s\n y: %s\n z: %s\n", x, y, z);
  printf("\n");

  printf("---mycpyn Testing---\n");
  printf("Before Copying:\n");
  printf(" s: %s\n t: %s\n u: %s\n", s, t, u);
  printf("Copying:\n %s - 6 --> s\n %s - 7 --> t\n %s - 10 --> u\n", l, m, n);
  mycpyn(s, l, 6);
  mycpyn(t, m, 7);
  mycpyn(u, n, 10);
  printf("After Copying:\n");
  printf(" s: %s\n t: %s\n u: %s\n", s, t, u);
  printf("\n");

  printf("---mycat Testing---\n"); 
  printf("Before Catting:\n");
  printf(" s: %s\n m: %s\n h: %s\n", s, m, h);
  printf("Catting:\n %s --> s\n %s --> m\n %s --> h\n", h, c, d);
  mycat(s, h);
  mycat(m, c);
  mycat(h, d);
  printf("After Catting:\n");
  printf(" s: %s\n m: %s\n h: %s\n", s, m, h);
  printf("\n");

  printf("---mycatn Testing---\n"); 
  printf("Before Catting:\n");
  printf(" s: %s\n m: %s\n h: %s\n", s, m, h);
  printf("Catting:\n %s - 8 --> s\n %s - 3 --> m\n %s - 2 --> h\n", h, c, d);
  mycatn(s, h, 8);
  mycatn(m, c, 3);
  mycatn(h, d, 2);
  printf("After Catting:\n");
  printf(" s: %s\n m: %s\n h: %s\n", s, m, h);
  printf("\n");
  
  printf("---mychr Testing---\n");
  printf("Test One:\n Searching in: %s\n Searching for: %c\n", e, 'e');
  printf(" Pointer: %p - Points at: %c\n", mychr(e,'e'), *mychr(e,'e'));
  printf("Test Two:\n Searching in: %s\n Searching for: %c\n", h, 't');
  printf(" Pointer: %p - Points at: %c\n", mychr(h,'t'), *mychr(h,'t'));
  printf("\n");

  printf("---mystr Testing---\n");
  printf("Test One:\n Searching in: %s\n Searching for: %s\n", n, "long");
  printf(" Pointer: %p - Points at: %s\n", mystr(n,"long"), *mystr(n,"long"));
  
  return 0;
}
int callInternal(command* C, char* homeDir)
{
	if (C->argc == 0)
		return 0;
	else if (strcmp(C->argv[0],"exit") == 0)
		return 1;
	else if (strcmp(C->argv[0],"mycat") == 0)
	{
		if (C->argc == 2)
			mycat(C->argv[1]);
		else if (C->argc == 1)
			printf("Argument missing\n");
		else if (C->argc > 2)
			printf("Incorrect usage of mycat\n");
	}
	else if (strcmp(C->argv[0],"mycp") == 0)
	{
		if (C->argc == 3)
			mycp(C->argv[1],C->argv[2]);
		else if (C->argc == 2)
			printf("Destination needed\n");
		else if (C->argc == 1)
			printf("Source & destination needed\n");
		else
			printf("Incorrect usage of mycp\n");
	}
	else if (strcmp(C->argv[0],"mypwd") == 0)
	{
		if (C->argc > 1)
			printf("Incorrect usage of mypwd\n");
		else
		{		
			char* pwd = mypwd();
			printf("%s\n",pwd);
			free(pwd);
		}
	}
	else if (strcmp(C->argv[0],"mymkdir") == 0)
	{
		if (C->argc < 2)
			printf("missing argument\n");
		else if (C->argc > 2)
			printf("Incorrect usage of mymkdir\n");
		else
			mymkdir(C->argv[1]);
	}
	else if (strcmp(C->argv[0],"myrmdir") == 0)
	{
		if (C->argc < 2)
			printf("missing argument\n");
		else if (C->argc > 2)
			printf("Incorrect usage of mymkdir\n");
		else
			myrmdir(C->argv[1]);	
	}
	else if (strcmp(C->argv[0],"mycd") == 0)
	{
		if (C->argc >2)
			printf("Incorrect usage of mycd\n");
		else if (C->argc == 2)
			mycd(C->argv[1]);
		else
			mycd(homeDir);
	}
	else if (strcmp(C->argv[0],"myls") == 0)
	{
		if (C->argc == 1)
			myls(NULL,0);
		else if (C->argc == 2)
			if (strcmp(C->argv[1],"-l") == 0)
				myls(NULL,1);
			else
				myls(C->argv[1],0);
		else if (C->argc == 3)
			if (strcmp(C->argv[1],"-l") == 0)
				myls(C->argv[2],1);
			else
			printf("Incorrect usage of myls\n");
		else
			printf("Incorrect usage of myls\n");
	}
	else if (strcmp(C->argv[0],"myrm") == 0)
	{
		if (C->argc == 2)
			myrm(C->argv[1]);
		else if (C->argc == 1)
			printf("missing argument\n");
		else
			printf("Incorrect usage of myrm\n");
	}
	else
		printf("command not found: %s\n",C->argv[0]);
	return 0;
}
Esempio n. 5
0
int main(int argc, char* argv[])
{
	char tmp[1000];
	int fd, nread,i;
	char buf[BUF_SIZE];
	struct linux_dirent *d;
	int bpos;
	char d_type;
	int argLen=0;
	char* args[1024];
	localtime();
	for(i=1;i<argc;i++)
		if(argv[i][0]=='-')
		{
			int k=1;
			while(argv[i][k])
			{
				if(argv[i][k]=='a')
					fa=1;
				else if(argv[i][k]=='l')
					fl=1;
				else if(argv[i][k]=='h')
					fh=1;
				k++;
			}
		}
		else
			args[argLen++]=argv[i];
	if(argLen==0)
		args[argLen++]=".";
	for(i=0;i<argLen;i++)
	{
		write(1,args[i],getLen(args[i]));
		write(1,": \n",3);
		fd = open(args[i], O_RDONLY | O_DIRECTORY);
		if (fd == -1)
			handle_error("open");

		for ( ; ; ) {
			nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
			if (nread == -1)
				handle_error("getdents");
			if (nread == 0)
				break;
			for (bpos = 0; bpos < nread;) {
				struct stat sb;
				d = (struct linux_dirent *) (buf + bpos);
				d_type = *(buf + bpos + d->d_reclen - 1);
				char jj[10000],slash[2];
				slash[0]='/';
				slash[1]='\0';
				jj[0]='\0';
				mycat(jj,args[i]); if(jj[getLen(jj)-1]!='/') mycat(jj,slash); mycat(jj,d->d_name);
				if(!fa && !fl)
				{
					if(d->d_name[0]!='.')
					{
						lstat(jj,&sb);
						color = d_type == DT_LNK ? 3 : d_type == DT_DIR ? 2 : sb.st_mode & S_IXUSR ? 1 : 0;
						pc(color);
						write(1,("%s",d->d_name),getLen(d->d_name));
						pc(0);
						write(1,"\n",1);
					}
				}
				else if(fa && !fl)
				{
					lstat(jj,&sb);
					color = d_type == DT_LNK ? 3 : d_type == DT_DIR ? 2 : sb.st_mode & S_IXUSR ? 1 : 0;
					pc(color);
					write(1,("%s",d->d_name),getLen(d->d_name));
					pc(0);
					write(1,"\n",1);
				}
				else if(fa || fl)
				{
					if(!fa && d->d_name[0]=='.')
					{
						bpos += d->d_reclen;
						continue;
					}
					lstat(jj,&sb);

					if(DT_LNK == d_type) write(1,"l",1);
					else if(DT_DIR == d_type) write(1,"d",1);
					else if(DT_BLK == d_type) write(1,"b",1);
					else if(DT_SOCK == d_type) write(1,"s",1);
					else if(DT_CHR == d_type) write(1,"?",1);
					else write(1,"-",1);
					write( 1,("%s",(sb.st_mode & S_IRUSR) ? "r" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IWUSR) ? "w" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IXUSR) ? "x" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IRGRP) ? "r" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IWGRP) ? "w" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IXGRP) ? "x" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IROTH) ? "r" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IWOTH) ? "w" : "-") ,1);
					write( 1,("%s",(sb.st_mode & S_IXOTH) ? "x" : "-") ,1);
					itos(sb.st_nlink,tmp);
					print(tmp,0);
					getId(sb.st_uid, 0);
					print(rStr,10);
					getId(sb.st_gid, 1);
					print(rStr,10);
					if(fh) 
					{
						int cnt=0;
						char kk[1000];
						kk[0]='\0';
						long long int t = sb.st_size,p=0;
						while(t/1024)
						{
							p=t%1024;
							t/=1024;
							cnt++;
						}
						itos(t,tmp);
						mycat(kk,tmp);
						tmp[0]='.';
						tmp[1]='\0';
						mycat(kk,tmp);
						itos(p,tmp);
						tmp[2]='\0';
						mycat(kk,tmp);
						if(cnt==1)
						{
							tmp[0]='K';
							tmp[1]='\0';
							mycat(kk,tmp);
						}
						else if(cnt==2)
						{
							tmp[0]='M';
							tmp[1]='b';
							tmp[2]='\0';
							mycat(kk,tmp);
						}
						else if(cnt>=3)
						{
							tmp[0]='G';
							tmp[1]='b';
							tmp[2]='\0';
							mycat(kk,tmp);
						}
						print(kk,11);
					}
					else
					{
						itos(sb.st_size,tmp);
						print(tmp,8);
					}
					convert(sb.st_mtime);
					print(mon[mnth],0);
					itos(day,tmp);
					print(tmp,0);
					itos(hh,tmp);
					print(tmp,2);
					write(1,":",1);
					itos(min,tmp);
					print(tmp,3);
					color = d_type == DT_LNK ? 3 : d_type == DT_DIR ? 2 : sb.st_mode & S_IXUSR ? 1 : 0;
					pc(color);
					print(d->d_name,0);
					pc(0);
					if(d_type == DT_LNK)
					{
						char slink[1000];
						readlink(jj,slink,sb.st_size+1);
						write(1," ->",3);
						print(slink,0);
					}
					write(1,"\n",1);
					day=1; y=1970; mnth=1;yy=0;mm=0;dd=0;color=0;
				}
				bpos += d->d_reclen;
			}
		}
	}
	return 0;
}
Esempio n. 6
0
int main(int argc, char** argv)
{
	int count=0, i=0, cmp, j, valid=0;
	char key[MAX];
	
	//run the shell till user inputs 'quit' or Ctrl-C
	while(strcmp(cmdline, "quit") != 0)
	{
		count = 0;
		
		//our shell's prompt
		printf("afreen@myShell $ ");
		//get command line input
		gets(cmdline);
		
		//tokenize the CL input delimited by " " using strtok() into args
		args[count++] = strtok(cmdline, " ");
		
		//tokenize the entire string till the next token encountered
		//does not have a NULL pointer
		while((args[count++] = strtok(NULL, " ")));
		count--;		//no of arguments (argc)
		
		//if user wants to clear screen
		if(strcmp(args[0], "clear") == 0)
		{
			myclear();
		}
		
		//checking for the input command
		strcpy(key, args[0]);
		for(i=0; i<18; i++)	//look through our check array for command
		{
		  if(strcmp(check[i], key) == 0)
		  {
		      valid = -1;
		      switch(i)
		      {
		        case 0:   mycat(count, args);
		                  break;
		        
		        case 1:   mycd(count, args);
		                  break;
          
		        case 2:   mycp(count, args);
		                  break;
		         
		        case 3:   mydate(count, args);
		                  break;
		          
		        case 4:   myhead(count, args);
		                  break;
		                  
		        case 5:   myls(count, args);
		                  break;
		                  
		        case 6:   myll(count, args);
		                  break;
		                  
		        case 7:   myln(count, args);
		                  break;
		                  
		        case 8:   myld(count, args);
		                  break;
		                  
		        case 9:   mymkdir(count, args);
		                  break;
		                  
		        case 10:  mymv(count, args);
		                  break;
		                  
		        case 11:  mypwd(count, args);
		                  break;
		                  
		        case 12:  myrm(count, args);
		                  break;
		                  
		        case 13:  myrmdir(count, args);
		                  break;
		                  
		        case 14:  mytail(count, args);
		                  break;
		                  
		        case 15:  mytime(count, args);
		                  break;
		                  
		        case 16:  mytouch(count, args);
		                  break;
		                  
		        case 17:  myuname(count, args);
		                  break;
		                  		        
		        default: printf("%s: Command not found\n", key);
		    		  break;
		      }	
		      break;  
		  }
		}
		
		
  	}
  	return 0;
}