Question

my code is pasted below.

I'm trying to use dup2 to redirect my output to file.

if I use it to redirect it works fine (if I remove the comments), output in file and not on stdout. ex: ls > test , results in ls outputting to test.

the problem is that ls, without the > doesn't output anything. If I leave the comments ls outputs just as it should, albeit with no ability to redirect.

redirect[0] is either < or > or nothing redirect[1] is the path for the file to redirect to

command is is an array of cstrings with the pices of the command commands is as well

example output with code commented

xxxxx@myshell:/home/majors/kingacev/ubuntumap/cop4610/proj1> ls
a.out  myshell.c  myshell.c~
xxxxx@myshell:/home/majors/kingacev/ubuntumap/cop4610/proj1>

with code uncommented

xxxxx@myshell:/home/majors/kingacev/ubuntumap/cop4610/proj1> ls
xxxxx@myshell:/home/majors/kingacev/ubuntumap/cop4610/proj1>


  /*
  if (!strcmp(redirect[0],">")){
    if ((fd = open(redirect[1], O_RDWR | O_CREAT)) != -1)
      dup2(fd, STDOUT_FILENO);
    close(fd);
  }
  */

  if (command[0][0] == '/'){
    int c = execv(command[0], commands);
    if (c != 0){
      printf("ERROR: command does not exist at path specified\n");
      exit(0);
    }
  }
  else if (!execv(path, commands)){
    exit(0);
  }

No correct solution

OTHER TIPS

This code works, redirecting to file.out:

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

int main(void)
{
    int fd;
    char *redirect[] = { ">", "file.out" };
    char *command[] = { "/bin/ls", "-l", 0 };

    if (!strcmp(redirect[0], ">"))
    {
        if ((fd = open(redirect[1], O_WRONLY | O_CREAT, 0644)) != -1)
        {
            fprintf(stderr, "Dupping stdout to %s\n", redirect[1]);
            dup2(fd, STDOUT_FILENO);
            close(fd);
        }
    }

    if (command[0][0] == '/')
    {
        execv(command[0], command);
        fprintf(stderr, "ERROR: command %s does not exist at path specified\n", command[0]);
        return(1);
    }
    else
    {
        fprintf(stderr, "ERROR: not handling relative names like %s\n", command[0]);
        return(1);
    }
    return 0;
}

This code works too, not redirecting to file:

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

int main(void)
{
    int fd;
    char *redirect[] = { "<", "file.in" };
    char *command[] = { "/bin/ls", "-l", 0 };

    if (!strcmp(redirect[0], ">"))
    {
        if ((fd = open(redirect[1], O_WRONLY | O_CREAT, 0644)) != -1)
        {
            fprintf(stderr, "Dupping stdout to %s\n", redirect[1]);
            dup2(fd, STDOUT_FILENO);
            close(fd);
        }
    }

    if (command[0][0] == '/')
    {
        execv(command[0], command);
        fprintf(stderr, "ERROR: command %s does not exist at path specified\n", command[0]);
        return(1);
    }
    else
    {
        fprintf(stderr, "ERROR: not handling relative names like %s\n", command[0]);
        return(1);
    }
    return 0;
}

Note that it sets up the command array and uses execv(command[0], command); — this is the recommended way of doing business. Your code appears to have a variable commands with presumably the arguments to the program; you also appear to have a variable path with presumably the path name of the program. Since we can't see what's in those, it is hard to know what they contain and where there might be problems. Note the explicit null pointer (0) at the end of the command array. That is crucial. Note too that the error messages identify what was failing. There are few things more frustrating than a program that says "it went wrong" without identifying what 'it' is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top