2017-04-30 56 views
-1

因此,我在终端窗口中运行c程序时遇到问题。我正在Linux Mint上运行,并尝试在终端窗口中编译和运行程序。我有3个文件:主文件(cShell.c),命令文件(commands.c)和命令文件(commands.h)的头文件。试图在终端中运行c程序时出现问题

他们列在下面。所有这三个文件都位于同一个文件夹中,这是我尝试运行时终端中的工作目录。我可以使用下面的语法成功编译程序 -

gcc *.c -o a.out 

然而,当我尝试使用“./cShell”我收到以下错误运行实际文件:

[email protected] ~/Desktop/cShell $ ./cShell.c 
./cShell.c: line 8: syntax error near unexpected token `(' 
./cShell.c: line 8: `void print_user(void)' 
[email protected] ~/Desktop/cShell $ 

cShell:

#include <stdlib.h> 
#include <stdio.h> 
#include "commands.h" 



void print_user(void) 
{ 
    char user[1024]; 
    if (getlogin_r(user, sizeof(user)) != 0) { 
     fprintf(stdout, "User not found"); 
    } 
    else { 
    strcpy(user, getlogin()); 
    fprintf(stdout, "%s", user); 
    } 
} 


void print_cwd(void) 
{ 
    char cwd[1024]; 
    if (getcwd(cwd, sizeof(cwd)) == NULL) { 
     fprintf(stdout, "CWD not found"); 
    } 
    fprintf(stdout, "%s", cwd); 
} 

#define RL_BUFSIZE 1024 
char *read_line(void) 
{ 
    char *line = NULL; 
    ssize_t bufsize = 0; // have getline allocate a buffer for us 
    getline(&line, &bufsize, stdin); 
    return line; 
} 


#define TOK_BUFSIZE 64 
#define TOK_DELIM " \t\r\n\a" 
char **split_line(char *line) 
{ 
    int bufsize = TOK_BUFSIZE, position = 0; 
    char **tokens = malloc(bufsize * sizeof(char*)); 
    char *token; 

    if (!tokens) { 
    fprintf(stderr, "lsh: allocation error\n"); 
    exit(EXIT_FAILURE); 
    } 

    token = strtok(line, TOK_DELIM); 
    while (token != NULL) { 
    tokens[position] = token; 
    position++; 

    if (position >= bufsize) { 
     bufsize += TOK_BUFSIZE; 
     tokens = realloc(tokens, bufsize * sizeof(char*)); 
     if (!tokens) { 
     fprintf(stderr, "lsh: allocation error\n"); 
     exit(EXIT_FAILURE); 
     } 
    } 

    token = strtok(NULL, TOK_DELIM); 
    } 
    tokens[position] = NULL; 
    return tokens; 
} 

int launch(char **args) 
{ 
    pid_t pid; 
    int status; 


// if (execvp(args[0], args) == -1) { 
// perror("lsh"); 
// } 
// return 1; 

    pid = fork(); 
    if (pid == 0) { 
    // Child process 
    if (execvp(args[0], args) == -1) { 
     perror("lsh"); 
    } 
    exit(EXIT_FAILURE); 
    } else if (pid < 0) { 
    // Error forking 
    perror("lsh"); 
    } else { 
    // Parent process 
    do { 
     waitpid(pid, &status, WUNTRACED); 
    } while (!WIFEXITED(status) && !WIFSIGNALED(status)); 
    } 

    return 1; 
} 

int execute(char **args) 
{ 
    int i; 

    if (args[0] == NULL) { 
    // An empty command was entered. 
    return 1; 
    } 

    for (i = 0; i < num_builtins(); i++) { 
    if (strcmp(args[0], builtin_str[i]) == 0) { 
     return (*builtin_func[i])(args); 
    } 
    } 

    return launch(args); 
} 

void loop(void) 
{ 
    char *line; 
    char **args; 
    int status; 

    do { 
    print_user(); 
    printf("@"); 
    print_cwd(); 
    printf("> "); 
    line = read_line(); 
    args = split_line(line); 
    status = execute(args); 

    free(line); 
    free(args); 
    } while (status); 
} 

int main(int argc, char **argv) 
{ 
    // Load config files, if any. 

    // Run command loop. 
    loop(); 

    // Perform any shutdown/cleanup. 

    return EXIT_SUCCESS; 
} 

commands.c:

#include <stdlib.h> 
#include <stdio.h> 

int (*builtin_func[]) (char **) ; 

/* 
    get current user????!?!? 
    o   ls 
    o   stat 
    o   timeout 
    ·   grep 
    ·   diff 

x   clear 
x   cat 
x   cp 
x   cd 
x   mkdir 
x   rmdir 
x   sleep 
x   kill 
x   env 
x   wait 
*/ 

struct stat 
{ 
    dev_t st_dev;   /* inode's device */ 
    ino_t st_ino;   /* inode's number */ 
    mode_t st_mode;  /* inode protection mode */ 
    nlink_t st_nlink;  /* number of hard links */ 
    uid_t st_uid;   /* user ID of the file's owner */ 
    gid_t st_gid;   /* group ID of the file's group */ 
    dev_t st_rdev;  /* device type */ 
    off_t  st_size;  /* file size, in bytes */ 
    time_t st_atime;  /* time of last access */ 
    long  st_spare1; 
    time_t st_mtime;  /* time of last data modification */ 
    long  st_spare2; 
    time_t st_ctime;  /* time of last file status change */ 
    long  st_spare3; 
    long  st_blksize;  /* optimal blocksize for I/O */ 
    long  st_blocks;  /* blocks allocated for file */ 
    u_long st_flags;  /* user defined flags for file */ 
    u_long st_gen;   /* file generation number */ 
}; 

struct dirent { 
    unsigned long d_fileno; /* file number of entry */ 
    unsigned short d_reclen; /* length of this record */ 
    unsigned char d_type;  /* file type, see below */ 
    unsigned char d_namlen; /* length of string in d_name */ 
    char      d_name[255 + 1]; /* name must be no longer than this */ 
}; 

typedef struct DIR { 
    char   *dirname;     /* directory being scanned */ 
    struct dirent  current;      /* current entry */ 
    int   dirent_filled;    /* is current un-processed? */ 
} DIR; 

char *builtin_str[] = { 
     "cd", 
     "ls", 
     "help", 
     "exit", 
     "mkdir", 
     "rmdir", 
     "stat", 
     "cp", 
     "cat", 
     "clear", 
     "sleep", 
     "kill", 
     "wait", 
     "timeout", 
     "env" 

}; 

int num_builtins(void) { 
    return sizeof(builtin_str)/sizeof(char *); 
} 

int _diff(char **args) 
{ 
    if (args[1] == NULL || args[2] == NULL) { 
     fprintf(stderr, "Expected 2 arguments to \"diff,\"\n"); 
    } else { 
     wait(1); 
    } 
    return 1; 
} 

extern char **environ; 
int _env(char **args) 
{ 
    for (char **env = environ; *env; ++env) 
     printf("%s\n", *env); 
    return 1; 
} 

#define ESRCH 3 
//ESRCH process does not exist 
int _timeout(char **args) 
{ 
    int exitstatus; 
    if (args[1] == NULL || args[2] == NULL) { 
     fprintf(stderr, "Expected 2+ arguments to \"timeout,\" duration, command, args[]\n"); 
    } else { 
     int numArgs; 
     numArgs = sizeof(args)/sizeof(char*); 
     char* newArgs; 
     newArgs = malloc(sizeof(char*)*(numArgs-2)); 

     for (int i = 2; i < numArgs; i++){ 
      newArgs[i-2] = args[i]; 
     } 

     pid_t pid; 
     pid = fork(); 
     if (pid == 0) { 
      // Child process 
      for (int i = 0; i < num_builtins(); i++) { 
      if (strcmp(newArgs[0], builtin_str[i]) == 0) { 
       return (*builtin_func[i])(newArgs); 
      } 
      } 

      if (execvp(newArgs[0], newArgs) == -1) { 
       perror("lsh"); 
      } 
      exit(EXIT_FAILURE); 
     } else if (pid < 0) { 
      // Error forking 
      perror("lsh"); 
     } else { 
      // Parent process 
      int duration; 
      duration = atoi(args[1]); 
      do { 
       wait(1); 
       duration -= 1; 
      } while (duration > 0 && kill(pid, 0) != ESRCH); 
      if (duration == 0 && kill(pid, 0) == ESRCH){ 
       kill(pid, 7); 
       fprintf(stdout, "Timeout exceeded; Child pid %d killed.\n", pid); 
      } 
      else{ 
       fprintf(stdout, "Child pid %d completed in %d seconds..\n", pid, atoi(args[1])-duration); 
      } 
     } 
     free(newArgs); 
    } 
    return 1; 
} 

int _wait(char **args) 
{ 
    int exitstatus; 
    if (args[1] == NULL) { 
     fprintf(stderr, "Expected argument to \"wait\"\n"); 
    } else { 
     exitstatus = wait(atoi(args[1])); 
     fprintf(stdout, "Child process exit status: %d\n", exitstatus); 
    } 
    return 1; 
} 

int _kill(char **args) 
{ 
    if (args[1] == NULL || args[2] == NULL) { 
     fprintf(stderr, "Expected 2 arguments to \"kill\" - pid, signum\n"); 
    } else { 
     if (kill(atoi(args[1]),atoi(args[2])) != 0) { 
      perror("Error"); 
     } 
    } 
    return 1; 
} 

int _sleep(char **args) 
{ 
    if (args[1] == NULL) { 
     fprintf(stderr, "Expected argument to \"sleep\"\n"); 
    } else { 
     if (sleep(atoi(args[1])) != 0) { 
      perror("Error"); 
     } 
    } 
    return 1; 
} 

//doesn't work in debug; works in real term 
int _clear(char **args) 
{ 
    const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J"; 
    write(fileno(stdout), CLEAR_SCREEN_ANSI, 12); 
    return 1; 
} 

int _cat(char **args) 
{ 
    FILE *read_this; 
    if (args[1] == NULL) { 
     fprintf(stderr, "Expected argument to \"cat\"\n"); 
    } else { 
     read_this = fopen(args[1], "r"); 
     if (read_this == NULL) { 
      fprintf(stderr, "Can't open input file\n"); 
      return 1; 
     } 
     char buf[BUFSIZ]; 
     while(NULL != fgets(buf, sizeof buf, read_this)) { 
      printf("%s", buf); 
     } 
    } 
    return 1; 
} 


#define O_RDONLY 0x0000  /* open for reading only */ 
#define O_WRONLY 0x0001  /* open for writing only */ 
#define O_RDWR  0x0002  /* open for reading and writing */ 
#define O_ACCMODE 0x0003  /* mask for above modes */ 
#define O_CREAT  0x0200  /* create if nonexistant */ 
#define O_TRUNC  0x0400  /* truncate to zero length */ 
#define O_EXCL  0x0800  /* error if already exists */ 
#define EINTR  4  /* Interrupted system call */ 

char *strcpy(char *d, const char *s) 
{ 
    char *saved = d; 
    while (*s) 
    { 
     *d++ = *s++; 
    } 
    *d = 0; 
    return saved; 
} 

int _cp(char **args) 
{ 
    if (args[1] == NULL || args[2] == NULL) { 
     fprintf(stderr, "Expected 2 arguments to \"cp\"\n"); 
     return 1; 
    } 
    char* to; 
    char* from; 
    to = malloc(sizeof(args[2])); 
    from = malloc(sizeof(args[1])); 
    strcpy(to, args[2]); 
    strcpy(from, args[1]); 

    int errno; 
    int fd_to, fd_from; 
    char buf[4096]; 
    ssize_t nread; 
    int saved_errno; 

    fd_from = open(from, O_RDONLY); 
    if (fd_from < 0) 
     return -1; 

    fd_to = fopen(to, "rb+"); 
    if(fd_to == NULL) { 
     fd_to = fopen(to, "wb"); 
    } 

    fd_to = open(to, O_RDWR | O_CREAT | O_TRUNC, 0777); 
    if (fd_to < 0) 
     goto out_error; 

    while (nread = read(fd_from, buf, sizeof buf), nread > 0) 
    { 
     char *out_ptr = buf; 
     ssize_t nwritten; 

     do { 
      nwritten = write(fd_to, out_ptr, nread); 
      if (nwritten >= 0) 
      { 
       nread -= nwritten; 
       out_ptr += nwritten; 
      } 
      else if (errno != EINTR) 
      { 
       goto out_error; 
      } 
     } while (nread > 0); 
    } 

    if (nread == 0) 
    { 
     if (close(fd_to) < 0) 
     { 
      fd_to = -1; 
      goto out_error; 
     } 
     close(fd_from); 
     free(to); 
     free(from); 
     /* Success! */ 
     return 1; 
    } 
    out_error: 
    saved_errno = errno; 
    close(fd_from); 
    if (fd_to >= 0) 
     close(fd_to); 
    free(to); 
    free(from); 
    errno = saved_errno; 
    perror("Error: "); 
    return 1; 
} 

//fixme 
int _stat(char **args) 
{ 
    struct stat *buf; 
    buf = malloc(sizeof(struct stat)); 
    if (args[1] == NULL) { 
     fprintf(stderr, "Expected argument to \"stat\"\n"); 
    } 
    else { 
     if (stat(args[1], buf) != 0) { 
      perror("Error: "); 
     } 
     else { 
      long size = buf->st_blocks; 
      fprintf(stdout, "%lu", size); 
     } 
    } 
    //free(buf); 
    return 1; 
} 

//fixme 
int _ls(char **args) 
{ 
    DIR *dp; 
    struct dirent *ep; 
    char cwd[1024]; 
    getcwd(cwd, sizeof(cwd)); 
    if (args[1] == NULL) { 
     dp = opendir (cwd); 
     if (dp != NULL) 
     { 
      while (ep = readdir (dp)) 
       fprintf(stdout, "%s\n", ep->d_name); 
      (void) closedir (dp); 
     } 
     else 
      perror ("Couldn't open the directory"); 
    } 
    else { 
     dp = opendir (args[1]); 
     if (dp != NULL) 
     { 
      while (ep = readdir (dp)) 
       fprintf(stdout, "%s\n", ep->d_name); 
      (void) closedir (dp); 
     } 
     else 
      perror ("Couldn't open the directory"); 
    } 
    fprintf(stdout, "%s\n"); 
    return 1; 
} 

int _rmdir(char **args) 
{ 
    if (args[1] == NULL) { 
     fprintf(stderr, "Expected argument to \"rmdir\"\n"); 
    } else { 
     if (rmdir(args[1]) != 0) { 
      perror("Error: "); 
     } 
    } 
    return 1; 
} 

int _mkdir(char **args) 
{ 
    if (args[1] == NULL) { 
     fprintf(stderr, "Expected argument to \"mkdir\"\n"); 
    } else { 
     if (mkdir(args[1]) != 0) { 
      perror("Error"); 
     } 
    } 
    return 1; 
} 

int _cd(char **args) 
{ 
    if (args[1] == NULL) { 
     fprintf(stderr, "lsh: expected argument to \"cd\"\n"); 
    } else { 
     if (chdir(args[1]) != 0) { 
      perror("Error"); 
     } 
    } 
    return 1; 
} 

int _help(char **args) 
{ 
    int i; 
    for (i = 0; i < num_builtins(); i++) { 
     printf(" %s\n", builtin_str[i]); 
    } 
    printf("Use the man command for information on other programs.\n"); 
    return 1; 
} 

int _exit(char **args) 
{ 
    return 0; 
} 

int (*builtin_func[]) (char **) = { 
     &_cd, 
     &_ls, 
     &_help, 
     &_exit, 
     &_mkdir, 
     &_rmdir, 
     &_stat, 
     &_cp, 
     &_cat, 
     &_clear, 
     &_sleep, 
     &_kill, 
     &_wait, 
     &_timeout, 
     &_env, 
}; 

commands.h:

#ifndef COMMANDS_H_ 
#define COMMANDS_H_ 

int cd(char **args); 
int ls(char **args); 
int help(char **args); 
int shell_exit(char **args); 
int num_builtins(void); 

char *builtin_str[]; 
int (*builtin_func[]) (char **); 

#endif /* COMMANDS_H_ */ 

上午我编译或运行程序错了吗?有什么我失踪?提前致谢。

+1

你看到'-o a.out'?尝试运行'.a.out' – InternetAussie

+3

cShell.c是一个源文件。你不执行它,你执行编译输出。 – SHG

+0

至于你在尝试运行'./cShell'时出错:我怀疑它试图将它作为脚本文件执行,而事实并非如此。 – InternetAussie

回答

0

"gcc *.c -o a.out". However when I attempt to run the actual file using "./cShell"

-o a.out说写编译器的输出到一个名为a.out文件(这是默认的,所以你可以离开它)。

但是,然后你尝试运行./cShell.c(或./cShell - 你的问题说,在不同的部分)?你刚刚编译了cShell.c - 所以现在你想执行它(而不是编译版本)?这不是它的工作原理。

尝试gcc *.c -o cShell然后./cShell(注意,不是cShell.c)

+0

这是它的谢谢你的答案!来自Python背景的这些东西让我感到困惑,现在我明白了。 –

0

编译

gcc MyProgram.c -o MyProgram 

然后

./MyProgram 

确保您有足够的权限在源文件和out文件。检查与

ls -la 

你想看到这样的源文件:

-rw-rw-r-- 1 user user < stuff you dont need now > MyProgram.c 

和这样的事情对你的编译器的输出文件:

-rwxrwxr-x 1 user user < stuff you dont need now > MyProgram* 
                this star means 
               this file is executable 

如果您不要,使用chmod来更改权限。chmod的语法如下(当然有一个男人页)

chmod 0775 MyProgram 

其中0定义一个八进制represantation输入,其余的数字是指usergroupother。鉴于你是当前用户试图运行MyProgram的事实,你可以简单地依靠chmod 0744 MyProgram来执行它('./MyProgram',因为你在它存在的文件中)。

你可以在线和手册页找到更多的chmod

+2

“*'chmod 0777 programmname.c ....'*”no,no,just no。不要将源文件标记为可执行文件,根本不是这样做的,这会导致OP所面临的这种混淆: - ( – alk

+0

OP没有说明他的问题的确切性质,而我ofc给了他一个通用的蓝图来寻找他的方式,因为有没有精确的程序名称和东西 从来没有那么少,我会更新我的帖子,以适应这些需要,因为你是正确的+ x在chmod!谢谢指出! – Diaman

相关问题