pastebin - collaborative debugging tool
513.kpaste.net RSS

Difference between
modified post c02016aa by Anonymous on Fri 14th Oct 2011 09:28
original post 0d3fd506 by Anonymous on Fri 14th Oct 2011 09:14
Download diff
Show old version | new version | both versions

    
11
/* CODE WILL TAKE A COMMAND, PASS IT TO CREATEPROCESS(),
22
 * FORK A CHILD AND EXECUTE COMMAND
33
 * By John
44
 *
55
 */
66
77
#include <unistd.h>
88
#include <sys/types.h>
99
#include <stdio.h>
1010
#include <stdlib.h>
1111
1212
char* buffer;
1313
pid_t pid;
1414
1515
void startProcess( char* );
1616
1717
int main ()
1818
{
1919
	char* input = (char*)malloc( 128 );
2020
	buffer = NULL;
2121
2222
	while( 1 )
2323
	{
2424
		printf( "EECE315_Shell - %s$ ", getcwd(NULL, 0) );
2525
		gets( input );
2626
		if( strcmp(input, "exit") == 0 )
2727
		{
2828
			printf( "Terminating shell...\n" );
2929
			return 0;
3030
		}
3131
		else if( strcmp(input, "") != 0 )
3232
		{
3333
			startProcess( input );
3434
		}
3535
	}
3636
}
3737
3838
void startProcess( char* input )
3939
{
4040
	pid = fork();
4141
4242
	if( pid == 0 )
4343
	{
4444
		execvp( input, NULL );
4545
	}
4646
	else if( pid > 0 )
4747
	{
4848
		//parent wait  <---- [vin: wtf where is the wait code?]
4949
	}
5050
	else
5151
	{
5252
		printf( "Fork returned error code, no child" );
5353
	}
5454
}
5555
5656
=========================================================================================== BY VIN
5757
/* try running this on it's own to see whats going on. i used a bunch of print statements to show whats going on
5858
5959
This will be part of the main and it take in the COMMAND NAME from the argument passed from the PARSER and it will search through PATH to find if that command exists, if it exists it will RUN that command with EXECV else it fails and prints a message.
6060
6161
temppath serves two purposes 1) it allocates area for the tokens to be stored in and is passed down to the pathtoken which holds the tokens and each token is passed to the basepath[basepathindex] where each adress of the path is stored. 2) the temppath acts as the full path containing the basepath from PATH and the COMMAND */
6262
6363
#include <stdio.h>
6464
#include <stdlib.h>
6565
#include <unistd.h>
6666
#include <string.h>
6767
6868
int main ()
6969
{
7070
  char * basepath[100];
7171
  char * pathtoken;
7272
  char * pPath;
7373
  char * temppath;
7474
  int basepathindex;
7575
  int pathfound = 0;
7676
  pPath = getenv ("PATH");
7777
  temppath = (char *)malloc(strlen(pPath)+1); //allocate memory for the path tokens
7878
  if (temppath!=NULL)
7979
    printf ("The current path is: %s\n",pPath);
8080
    printf ("Splitting string \"%s\" into tokens:\n",pPath);
8181
    strcpy(temppath, pPath);
8282
    pathtoken = strtok (temppath,":"); // string token the PATH by ':'
8383
  while (pathtoken != NULL) // this loops through all the path tokens
8484
  {
8585
    printf ("storing %s in basepath\n",pathtoken);
8686
    basepath[basepathindex] = pathtoken;
8787
    printf ("the value added to the basepath is %s\n", basepath[basepathindex]);
8888
    pathtoken = strtok (NULL, ":");
8989
    strcpy (temppath, basepath[basepathindex]);
9090
    strcat (temppath, "/");
9191
    strcat (temppath, "ls"); // the "ls" is just a test, this will be replaced with the arguement later on
9292
    printf("the full path of the command is %s \n",temppath);
9393
   if (pathfound == access(temppath, X_OK))  //check if the fullpath if the command can be found
9494
	{printf("!!YAY!! command found in this path\n\n");} //fork, execv, wait function will be placed in here as the command is found (john's "start process" most-likely goes in this block"
9595
	else
9696
	{printf("notfound\n\n");}
9797
    basepathindex++;
9898
  }
9999
  return 0;
100100
}
101-
==============================================================================================
101+
==============================================================================================BY VIN
102-
102+
//This is a basic shell wrapper, run it and check out what it does.
103103
104-
104+
#include <stdio.h>
105-
105+
#include <stdlib.h>
106-
106+
#include <unistd.h>
107107
108-
108+
int main(int argc, char *argv[], char *envp[])
109-
109+
{
110-
110+
	char c = '\0';
111-
111+
	printf("\n[315 NINJA TURTLE SHELLS] ");
112-
112+
	while(c != EOF) {
113-
113+
		c = getchar();
114-
114+
		if(c == '\n')
115-
115+
			printf("[315 NINJA TURTLE SHELLS ] ");
116-
116+
	}
117-
117+
	printf("\n");
118-
118+
	return 0;
119-
119+
}

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at