Esempio n. 1
0
void* nshCalloc(int number, int size) {
	int num_of_bytes = number*size; //we want specific number of bytes
	node *temp;
	temp = (node*)nshMalloc(num_of_bytes);
	if(temp == NULL) {//could not allocate space
		printf("Could not allocate that many items, out of memory.\n");
		return NULL;
	}//else we found that much memory
	return temp;
}
Esempio n. 2
0
//Insert function
void nshInsert(EnvP *list, char *name, char *value){

	EnvP temp = (EnvP) nshMalloc(sizeof(struct env));
	if (temp == NULL)
	{
		return;
	}
	EnvP left,right;
	right = *list;
	int found = 0;
	int i;
	left = NULL;
	strcpy(temp->name,name);
	strcpy(temp->value,value);

	//Checks to see if list is empty
	if (right == NULL)
	{
		*list=temp;
		(*list)->next = NULL;
		return;
	}

	//If list is not empty, insert new item alphabetically
	while(right != NULL && found == 0)
	{
		for (i = 0; i < sizeof(right->name);i++)
		{
			//If letter matches,keep checking current var
			if (right->name[i] == name[i])
			{
				found = 1;
				continue;
			}
			//if new var is after current var, keep searching
			else if (right->name[i] < name[i])
			{
				left=right;
				nshNext(&right);
				break;
			}
			//if new var is before current var
			else if (right->name[i] > name[i])
			{
				found = 1;
				break;
			}

		}


	}

	//Store new value
	//Checks if new value will be first in list
	if (left == NULL)
	{
		temp->next = *list;
		*list = temp;
	}
	else
	{
		left->next = temp;
		temp->next = right;
	}
}