热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

execvp()无法在我的shell中工作-execvp()notworkinginmyshell

Iamtryingtomakeatinyshell.MyproblemisthatwhenIcallexecvp()-Igeterrors.Forexampl

I am trying to make a tiny shell. My problem is that when I call execvp() - I get errors. For example, when I type in ls -l it returns ls: invalid option -- '

我想做一个小壳。我的问题是,当我调用execvp()时 - 我得到错误。例如,当我输入ls -l时,它返回ls:invalid选项 - '

Can someone, please, help me understand why I am getting this error? For my code, the function command split gets the user input, and splits them up into separate commands. Separate commands are seperated by ; character.

请有人帮助我理解为什么我会收到此错误?对于我的代码,函数命令split获取用户输入,并将它们拆分为单独的命令。单独的命令分开;字符。

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAX_CHARACTERS 512
#define HISTORY_SIZE 10

int commandSplit(char *c, char *a[], int t[]) {

int count = 0;
int total = 0;
char *temp[MAX_CHARACTERS];
char *readCommands = strtok(c, ";");
while(readCommands != NULL) {
    printf("Reading full command: %s\n", readCommands);
    temp[count] = readCommands;
    count++;
    readCommands = strtok(NULL, ";");
}
printf("Done reading full commands\n");
for(int i = 0; i   ");
fgets(commands, MAX_CHARACTERS, stdin);

if(strlen(commands) == 0) continue;

numOfCommands = commandSplit(commands, args, tracker);
printf("There are %i commands!\n", numOfCommands);

if(strcmp(args[0], "exit") == 0) {
    printf("Exiting\n");
    exitProgram = 1;
    continue;
}

int l = 0;
for(int i = 0; i 

1 个解决方案

#1


2  

Your problem is that fgets() also reads the newline character. As a result, the last argument of execvp() arguments array contains a newline, causing ls complain about an unrecognized argument: what you acctually pass to ls is -l\n; what you need to pass is just -l without the newline.

您的问题是fgets()也会读取换行符。结果,execvp()arguments数组的最后一个参数包含一个换行符,导致ls抱怨一个无法识别的参数:你实际传递给ls的是-l \ n;你需要传递的只是-l而没有换行符。

Try adding this code after the fgets call to trim the input buffer:

尝试在fgets调用输入缓冲区后添加此代码:

int len;
len = strlen(commands);
if (len > 0 && commands[len-1] == '\n') {
    commands[len-1] = '\0';
}

推荐阅读
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • C++: 实现基于类的四面体体积计算
    本文介绍如何使用C++编程语言,通过定义类和方法来计算由四个三维坐标点构成的四面体体积。文中详细解释了四面体体积的数学公式,并提供了两种不同的实现方式。 ... [详细]
author-avatar
sweet佳楠名人博客
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有