Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:02:23+00:00 2026-06-11T12:02:23+00:00

int bytes_read; int rv; int nchars = 200; /*max possible number for the input

  • 0
       int bytes_read;
       int rv;
       int nchars = 200;  /*max possible number for the input of the user*/
       size_t nbytes = nchars;  /*size of chars in bytes*/
       char *commands[2];
       char *line = malloc(nbytes + 1);
       bytes_read = getline(&line, &nbytes, stdin);  /*read line from stdin*/
       if (bytes_read == -1) {
           printf("Read line error");
           exit(-1);
       } else {
           if (line[strlen(line-1)] == '\n') {
               line[strlen(line-1)] = '\0';  /*change new line character in the end of the line of stdin*/
           }
       }
       if (strcmp(line,"exit") == 0) {
            rv = 3;
            exit(rv);
       }
       commands[0] = line;
       commands[1] = NULL;
       execvp(commands[0], commands);
       perror("Execution error");
       exit(-1);

I have a problem in the code above. If i use getline or even fgets to get input from the user from the terminal, and type "ls" for example execvp prints that there is “no such file or directory”. But If I put commands[0]="ls" it runs correctly. What could be the reason?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:02:25+00:00Added an answer on June 11, 2026 at 12:02 pm
    if (line[strlen(line-1)] == '\n') {
        line[strlen(line-1)] = '\0';  /*change new line character in the end of the line of stdin*/
    

    That logic to remove the ‘\n’ looks incorrect. I think it should be:

    if (line [ strlen(line) - 1 ] == '\n' )
        line [ strlen(line) - 1 ] = '\0';  /*change new line character in the end of the line of stdin*/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that a char and an int are calculated as being 8 bytes
I have this code: while (1) { char buffer[4096]; memset(buffer,0,4096); int bytes_read = recv(client_fd,
What 0 (number of bytes read) returned by InputStream.read(byte[]) and InputStream.read(byte[], int, int) means?
int a[2]; This in memory actually looks like: //Assuming int is 2 bytes add=2000,
An int ( Int32 ) has a memory footprint of 4 bytes. But what
class a { int variable;//4 bytes } class a { static int variable;//? bytes
How can I convert from bytes to float in php? Like in Java int
I want to know how to announce int to make sure it's 4 bytes
#include <stdio.h> #include <unistd.h> #include <sys/time.h> #include <sys/types.h> int main () { char name[20];
This is the code: unsigned int number; FILE* urandom = fopen(/dev/urandom, r); if (urandom)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.