日期:2014-05-16  浏览次数:20811 次

linux c实现shell,但出现编译后,不能链接,望高手指点指点,谢谢啦
程序源码如下:
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>

#define MAX_COMMAND_LEN 250
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"

struct jobSet
{
struct job *head;
struct job *fg;
};

struct childProgram
{
pid_t pid;
char **argv;
};

struct job
{
int jobId;
int numProgs;
int runningProgs;
char *text;
char *cmdBuf;
pid_t pgrp;
struct childProgram *progs;
struct job *next;
};

void freejob(struct job *cmd)
{
int i;

for(i = 0; i < cmd->numProgs; i++)
{
free(cmd->progs[i].argv);
}
free(cmd->progs);
if(cmd->text)
free(cmd->text);
free(cmd->cmdBuf);
}

int getCommand(FILE *source, char *command)
{
if(source == stdin)
{
printf("#");
fflush(stdout);
}

if(!fgets(command, MAX_COMMAND_LEN, source))
{
if(source == stdin)
printf("\n");
return 1;
}

command[strlen(command) - 1] = '\0';
return 0;

}

int praseCommand(char **commandPtr, struct job *job, int *isBg)
{
char *command;
char *returnCommand = NULL;
char *src, *buf;
int argc = 0;
int done = 0;
int argvAlloced;
char quote = '\0';
int count;
struct childProgram *prog;

while(**commandPtr &&isspace(**commandPtr))
(*commandPtr)++;

if(!**commandPtr || (**commandPtr == '#'))
{
job->numProgs = 0;
*commandPtr = NULL;
return 0;
}

*isBg = 0;
job->numProgs = 1;
job->progs = malloc(sizeof(*job->progs));

job->cmdBuf = command = calloc(1, strlen(*commandPtr) + 1);
job->text = NULL;
prog = job->progs;

argvAlloced = 5;
prog->argv = malloc(sizeof(*prog->argv) * argvAlloced);
prog->argv[0] = job->cmdBuf;

buf = command;
src = *commandPtr;
while(*src && !done)
{

if(quote == *src)
quote = '\0';

else if(quote)
{
if(*src == '\\')
{
src++;
if(!*src)
{
fprintf(stderr, "character expected after \\\n");
freeJob(job);
return 1;
}
if(*src != quote)
*buf++ = '\\';
}

*buf++ = *src;
}
else if(isspace(*src))
{
if(*prog->argv[argc])
{
buf++, argc++;
if((argc + 1) == argvAlloced)
{
argvAlloced += 5;
prog->argv = realloc(prog->argv, 
sizeof(*prog->argv) * argvAlloced);
}
prog->argv[argc] = buf;
}
}
else switch(*src)
{
case '"':
case '\'': quote = *src; break;
case '#': done = 1; break;
case '&': *isBg = 1;
case ';': done = 1; returnCommand = *commandPtr + (src - *commandPtr) + 1; break;
case '\\': src++; 
if(!*src)
{
freeJob(job);
fprintf(stderr,"charecter expected after \\\n");
return 1;
}
default: *buf++ = *src;
}
src++;
}

if(*prog->argv[argc])
{
argc++;
}
if(!argc)
{
freeJob(job);
return 0;
}
prog->argv[argc] = NULL;