L
lnatz
Hi, I am writing a shell for a class and I have to write some builtins
such as alias and cd. I am having some trouble with alias. I anyone
could give me some ideas about how to do it I would greatly appreciate
it. I have some code that I began but I am having some segmentation
fault. My code is:
/* the parser function parses a single pointer based on the second
argument. It returns a double pointer*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "parser.h"
struct aliases
{
char *original;
char *replacement;
};
struct aliases *list=NULL;
int alias(char *command)
{
printf("in alias\n");
char **temp;
char **temp2;
char *s = strstr(command," = ");
if (s)
{
printf("alias: =:not found\n");
return 0;
}
temp = parser(command,'=');
temp2 = parser(temp[0],' ');
printf("about to set original\n");
char *ori;
ori = (char *)malloc(sizeof(char)*256);
char *rep;
rep = (char *)malloc(sizeof(char)*256);
strcpy(ori,temp2[1]);
strcpy(rep,temp[1]);
printf("ori:%s\n",ori);
printf("rep:%s\n",rep);
(*list).original=ori;
printf("original:%s\n",(*list).original);
(*list).replacement = rep;
printf("replacement:%s\n",(*list).replacement);
return 0;
}
the segmentation fault occurs at "(*list).original=ori". Basically so
far if I enter "alias ls="ls -l" " it should print "original:ls" and
"replacement:"ls -l" ".
Natalie
such as alias and cd. I am having some trouble with alias. I anyone
could give me some ideas about how to do it I would greatly appreciate
it. I have some code that I began but I am having some segmentation
fault. My code is:
/* the parser function parses a single pointer based on the second
argument. It returns a double pointer*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "parser.h"
struct aliases
{
char *original;
char *replacement;
};
struct aliases *list=NULL;
int alias(char *command)
{
printf("in alias\n");
char **temp;
char **temp2;
char *s = strstr(command," = ");
if (s)
{
printf("alias: =:not found\n");
return 0;
}
temp = parser(command,'=');
temp2 = parser(temp[0],' ');
printf("about to set original\n");
char *ori;
ori = (char *)malloc(sizeof(char)*256);
char *rep;
rep = (char *)malloc(sizeof(char)*256);
strcpy(ori,temp2[1]);
strcpy(rep,temp[1]);
printf("ori:%s\n",ori);
printf("rep:%s\n",rep);
(*list).original=ori;
printf("original:%s\n",(*list).original);
(*list).replacement = rep;
printf("replacement:%s\n",(*list).replacement);
return 0;
}
the segmentation fault occurs at "(*list).original=ori". Basically so
far if I enter "alias ls="ls -l" " it should print "original:ls" and
"replacement:"ls -l" ".
Natalie