C help ......please

Sharky Forums


Results 1 to 3 of 3

Thread: C help ......please

  1. #1
    Catfish Howski's Avatar
    Join Date
    Oct 2002
    Location
    Caerphilly
    Posts
    218

    C help ......please

    As I suck at C and linux ...... Can someone help me I've got this code and I need to get the last few errors out before I can start adding stuff..... the first 4 errors are the ones I'm stuck on ...

    My code....


    /*
    A small shell program;
    Coursework for UQC109S2 : Networks and operating systems;
    Written by Howard Webb (student number 00503227);
    This program is free software; you can redistribute it and/or modify
    it (if I ever make it avalable!);
    Written in Emacs;
    Made for, tested and run in Linux
    Code moddified from the tutorial found at http://www.hawklord.uklinux.net/system/shell/cs3.htm (Thanks alot!!!);
    Prinsibles explained by Nigle Gunton;
    Additional code written with the help of (add tut name);
    Books used in writting:
    C Programming by Tony Royce Published by: Macmillan Press LTD ISBN:0-333-63851-4;
    Operating Systems with Linux by John O'Gorman Published by Palgrave Macmillan Ltd ISBN:0333947452 (course text);
    */
    #include "def.h"

    int main (void)
    {
    int i;

    initcold();

    for (;
    {
    initwarm();

    if (getline())
    if (i == parse());
    execute(i);
    }
    }
    void initcold (void)
    {
    /*
    signal(SIGINT, SIG_IGN);
    signal (SIGOUIT, SIG_IGN);
    */
    }



    void initwarm(void)
    {
    int i;

    backgnd = FALSE;
    lineptr = line;
    avptr = avline;
    infile[0] = '\0';
    outfile[0] = '\0';
    append = FALSE;

    for (i = 0; i<PIPELINE; ++i)
    {
    cmdlin[i].infd = 0;
    cmdlin[i].outfd = 1;
    }

    for (i = 3; i<MAXOPEN; ++i)
    close(i);

    printf("Shell: ");
    fflush(stdout);
    }
    int getline (void)
    {
    int i;

    for (i = 0; (line[i] = getchar())!='\n' && i<MAXLINE; ++i);

    if (i==MAXLINE)
    {
    fprintf(stderr, "Command line too long\n");
    return(ERROR);
    }
    line[i+1] = '\0';
    return (OKAY);
    }
    int parse(void)
    {
    int i;

    if (builtin())
    return 0;
    /* 1 */
    command(0);

    /* 2 */
    if (check("<"))
    getname(infile);

    /* 3 */
    for (i = 1; i<PIPELINE; ++i)
    if (check("|"))
    command(i);
    else
    break;

    /* 4 */
    if (check(">"))
    {
    if (check(">"))
    append = TRUE;

    getname(outfile);
    }

    /* 5 */
    if (check("&"))
    backgnd = TRUE;

    /* 6 */
    if (check("\n"))
    return(i);
    else
    {
    fprintf(stderr, "Command line syntax error\n");
    return (ERROR);
    }
    }

    void command(int i)
    {
    int j, flag, inword;

    for (j = 0; j<MAXARG-1; ++j)
    {
    while (*lineptr==' ' || *lineptr=='\t')
    ++lineptr;

    cmdlin[i].av[j] = avptr;
    cmdlin[i].av[j+1] = NULL;

    for (flag = 0; flag==0
    {
    switch (*lineptr)
    {
    case '>':
    case '<':
    case '|':
    case '&':
    case '\n':
    if (inword==FALSE)
    cmdlin[i].av[j] = NULL;

    *avptr++ = '\0';
    return;
    case ' ':
    case '\t':
    inword = FALSE;
    *avptr++ = '\0';
    flag = 1;
    break;
    default:
    inword = TRUE;
    *avptr++ = *lineptr++;
    break;
    }
    }
    }
    }
    void execute(int j)
    {
    int i, fd, fds[2];

    /* 1 */
    if (infile[0] !='\0')
    cmdlin[0].infd = open(infile, O_RDONLY);

    /* 2 */
    if (outfile[0] !='\0'){
    if (append==FALSE)
    cmdlin[j-1].outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    else
    cmdlin[j-1].outfd = open(outfile, O_WRONLY | O_CREAT | O_APPEND, 0666);
    }
    /* 3 */
    if (backgnd==TRUE)
    signal (SIGCHLD, SIG_IGN);
    else
    signal (SIGCHLD, SIG_DFL);

    /* 4 */
    for (i = 0; i <j; ++i)
    {
    /* 5 */
    if (i<j-1)
    {
    pipe (fds);
    cmdlin[i].outfd = fds[1];
    cmdlin[i+1].infd = fds[0];
    }

    /* 6 */
    forkexec(&cmdlin[i]);

    /* 7 */
    if ((fd = cmdlin[i].infd)!=0)
    close(fd);

    if ((fd = cmdlin[i].outfd)!=1)
    close(fd);
    }

    /* 8 */
    if (backgnd==FALSE)
    while (wait (NULL) != lastpid);
    }
    int forkexec(struct cmd *ptr)
    {
    int i, pid;

    /* 1 */
    if (pid = fork())
    {
    /* 2 */
    if (backgnd==TRUE)
    printf("%d\n", pid);
    lastpid = pid;
    }
    else
    {
    /* 3 */
    if (ptr->infd==0 && backgnd==TRUE)
    ptr->infd = open("/dev/null", O_RDONLY);

    /* 4 */
    if (ptr->infd!=0)
    {
    close(0);
    dup(ptr->infd);
    }

    if (ptr->outfd!=1)
    {
    close(1);
    dup(ptr->outfd);
    }

    /* 5 */
    if (backgnd==FALSE)
    {
    signal (SIGINT, SIG_DFL);
    signal (SIGQUIT, SIG_DFL);
    }

    /* 6 */
    for (i = 3; i<MAXOPEN; ++i)
    close(i);

    /* 7 */
    execvp(ptr->av[0] ptr->av);
    exit(1);
    }
    }
    int check(char *ptr)
    {
    char *tptr;

    while (*lineptr== ' ' )
    lineptr++;

    tptr = lineptr;

    while (*ptr!='\0' && *ptr==*tptr)
    {
    ptr++;
    tptr++;
    }
    if (*ptr!='\0')
    return(FALSE);
    else
    {
    lineptr = tptr;
    return(TRUE);
    }
    }
    int getname(char *name)
    {
    int i;

    for (i = 0; i<MAXNAME; ++i)
    {
    switch (*lineptr)
    {
    case '>':
    case '<':
    case '|':
    case '&':
    case ' ':
    case '\n':
    case '\t':
    *name = '\0';
    return(-1);

    default:
    *name++ = *lineptr++;
    break;
    }
    }
    *name = '\0';
    }
    int builtin(void)
    {
    if (check("exit"))
    exit (0);
    else if (check("cd"))
    return do_cd();
    else if (check("pwd"))
    return do_pwd();
    else
    return 0;
    }



    and def.h


    /*
    DEf.H to be included in Coursework for UQC109S2 : Network and Operating Systems, with Cousework.c
    Code modified form that found at
    */
    #include <stdio.h>
    #include <limits.h>
    #include <signal.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <stdlib.h>
    #include <unistd.h>

    #define TRUE 1
    #define FALSE 0
    #define OKAY 1
    #define ERROR 0
    #define MAXOPEN 16 /* maximum open pipes */
    #define MAXLINE 200 /* Maximum length of input line */
    #define MAXARG 20 /* Max number of args for each simple command */
    #define PIPELINE 5 /* Max number of simple commands in a pipeline */
    #define MAXNAME 100 /* Maximum length of i/o redirection filename */

    char line[MAXLINE+1]; /* User typed input line */
    char *lineptr; /* Pointer to current position in line[] */
    char avline[MAXLINE+1]; /* Argv strings taken from line[] */
    char *avptr; /* Pointer to current position in avline[] */
    char infile[MAXNAME+1]; /* Input redirection filename */
    char outfile[MAXNAME+1]; /* Ouput redirection filename */

    int backgnd; /* TRUE if & ends pipeline else FALSE */
    int lastpid; /* PID of last simple command in pipeline */
    int append; /* TRUE for append redirection (») else FALSE */

    struct cmd
    {
    char *av[MAXARG];
    int infd;
    int outfd;
    } cmdlin[PIPELINE]; /* Argvs and fds, one per simple command */
    /*
    added stuff to stop warnings
    */
    void initcold(void);
    void initwarm(void);
    int getline(void);
    int parse(void);
    void execute(int j);
    int close(int);
    int builtin(void);
    void command(int i);
    int getname(char *name);
    int check(char *ptr);
    int forkexec(struct cmd *ptr);
    int do_cd(void);
    void err_sys(const char *, ...);
    char path_alloc(int *);


    and my version of pwd (untested as of yet!!)

    #include "def.h"
    int
    main(void)
    {
    char *ptr;
    int size;
    getcwd(ptr, size);
    printf("cwd = %s\n", ptr);
    exit(0);
    }


    Any help would be brill thanx
    -How.
    "Sir, God hath taken away your eldest son by a cannon-shot. It brake his leg. We were necessitated to have it cut off, whereof he died"
    -Oliver Cromwell ,York, 5 July 1644
    "Of weapons of the staff, the pike is the most plain, most honorable, and most noble. Among renowned knights and great lords this weapon is highly esteemed both for its lack of deceit and for the strength, valor and deep judgement required to handle it. Its use requires a most subtle and delicate knowledge of times, motions, and a ready resolution to strike - qualities not resident in the average man."
    Di Grasse - His true Art of Defense
    The Sealed Knot Bringing the Past to Life...

  2. #2
    Catfish Howski's Avatar
    Join Date
    Oct 2002
    Location
    Caerphilly
    Posts
    218
    here you go errors

    cd /coursework/
    gcc -Wall -pedantic -ansi Coursework.c
    Coursework.c: In function `forkexec':
    Coursework.c:215: warning: suggest parentheses around assignment used as truth value
    Coursework.c:253: parse error before `ptr'
    Coursework.c:256: warning: control reaches end of non-void function
    Coursework.c: In function `getname':
    Coursework.c:303: warning: control reaches end of non-void function
    Coursework.c: In function `builtin':
    Coursework.c:311: warning: implicit declaration of function `do_pwd'
    Coursework.c: At top level:
    Coursework.c:325: conflicting types for `do_cd'
    def.h:55: previous declaration of `do_cd'
    Coursework.c: In function `do_cd':
    Coursework.c:324: parameter name omitted
    Coursework.c:326: warning: control reaches end of non-void function

    it's the no void function ones I don't get and parentheses around assignment used as truth value
    -How.
    "Sir, God hath taken away your eldest son by a cannon-shot. It brake his leg. We were necessitated to have it cut off, whereof he died"
    -Oliver Cromwell ,York, 5 July 1644
    "Of weapons of the staff, the pike is the most plain, most honorable, and most noble. Among renowned knights and great lords this weapon is highly esteemed both for its lack of deceit and for the strength, valor and deep judgement required to handle it. Its use requires a most subtle and delicate knowledge of times, motions, and a ready resolution to strike - qualities not resident in the average man."
    Di Grasse - His true Art of Defense
    The Sealed Knot Bringing the Past to Life...

  3. #3
    Catfish Howski's Avatar
    Join Date
    Oct 2002
    Location
    Caerphilly
    Posts
    218
    it's ok I got it now.
    -How.
    "Sir, God hath taken away your eldest son by a cannon-shot. It brake his leg. We were necessitated to have it cut off, whereof he died"
    -Oliver Cromwell ,York, 5 July 1644
    "Of weapons of the staff, the pike is the most plain, most honorable, and most noble. Among renowned knights and great lords this weapon is highly esteemed both for its lack of deceit and for the strength, valor and deep judgement required to handle it. Its use requires a most subtle and delicate knowledge of times, motions, and a ready resolution to strike - qualities not resident in the average man."
    Di Grasse - His true Art of Defense
    The Sealed Knot Bringing the Past to Life...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •