小编Zac*_*ico的帖子

如何使用S_ISREG()和S_ISDIR()POSIX宏?

这是我写的一个C程序,用于递归导航和输出目录和常规文件.它在我的Linux机器上编译并运行良好.但是在Solaris上,dit->d_type == 8检查和其他类似检查不起作用,因为没有d_type字段.我已经阅读过这个问题的答案是使用S_ISREG()S_ISDIR()宏,但它们并不能完全按照我目前的代码中的方式工作.我注释掉了在我的Linux机器上运行的行.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

void helper(DIR *, struct dirent *, struct stat, char *, int, char **);
void dircheck(DIR *, struct dirent *, struct stat, char *, int, char **);

int main(int argc, char *argv[]){

  DIR *dip;
  struct dirent *dit;
  struct stat statbuf;
  char currentPath[FILENAME_MAX];
  int depth = 0; /*Used to correctly space output*/

  /*Open Current Directory*/ …
Run Code Online (Sandbox Code Playgroud)

c solaris stat readdir

11
推荐指数
2
解决办法
9万
查看次数

在C中制作自己的shell

我正在尝试用C创建自己的shell,但是我遇到了strtok的问题.我使用它来正确地解析输入中的命令和参数,但我无法解析路径(它目前是段错误).一旦我正确解析了路径,我应该能够在每个部分上调用execlp并相应地进行fork处理.任何见解将不胜感激,代码如下.如果您认为我可以做得更好,也可以随意评论风格选择.

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

void parse(char *, char *);
void process(char *, char *, int);

int main(int argc, char *argv[]) {
  char *command;
  char *path;
  char buffer[1024];
  command = (char *)malloc(sizeof(char));
  path = (char *)malloc(sizeof(char));
  int loop = 1;
  while(loop == 1){
    path = getenv("MYPATH");
    if(path == NULL)
      path = "/bin#.";
    printf("($MYPATH is %s)\n", path);

    printf("myshell$ ");
    command = fgets(buffer, 1024, stdin);
    printf("Buffer: %s", buffer);
    printf("Command: %s", command);

    if(strcmp(command,"exit\n") == 0 || strcmp(command, "quit\n") == …
Run Code Online (Sandbox Code Playgroud)

c shell operating-system posix

1
推荐指数
1
解决办法
4974
查看次数

标签 统计

c ×2

operating-system ×1

posix ×1

readdir ×1

shell ×1

solaris ×1

stat ×1