Questions tagged [dup]

dup() is a c system call that duplicates a file descriptor

Usage: int dup(int oldfd); (#include <unistd.h>)

The C system call dup duplicates a file descriptor. It returns a second file descriptor that points to the same file table entry as precious file descriptor does, so that we can treat the two file descriptors as identical.

The dup system call uses the lowest-numbered unused file descriptor as its new file descriptor. Compare which has two arguments, and that you specify the new destination file descriptor number in the second argument.

See http://man7.org/linux/man-pages/man2/dup.2.html for more details.

176 questions
1
vote
1 answer

execve and pipe issues - how to recover original pipe?

I have been making simple shell which performs pipe. Here is some code for operating pipe syntax. int fd[2]; int stdin_copy; int stdout_copy; int status; char * msg; if (pipe(fd) == -1) { perror("pipe"); exit(1); } // fd[0] : process read…
구마왕
  • 488
  • 6
  • 20
1
vote
1 answer

pipe & dup functions in UNIX

I have a suspicious point from the code written below. #include #include #include #include int main(void){ int pid; int i,j; char c; int pfd[2]; if(pipe(pfd) ==…
구마왕
  • 488
  • 6
  • 20
1
vote
1 answer

Copy of object with attribute set in ruby

Is it possible to return create copy of object with an attribute set, in Ruby? Of course, a method can be defined to do this - class URI::Generic def with_query(new_query) ret = self.dup ret.query = new_query ret end end But this…
George Simms
  • 3,930
  • 4
  • 21
  • 35
1
vote
1 answer

Redirecting stdin with FIFO (names pipe)

I'm creating a C program with a server-client bases. I've been trying to redirect the stdin to a named pipe I created and I've managed to put a client writing to the pipe. On the server side I opened the same pipe, closed stdin and redirected the…
1
vote
1 answer

Simple http generic server using fork and dup

I'm trying to write a very basic HTTP server. I want to separate the server and the service in two separate executable (similar to inetd). So I have a generic server, that forks a service and execute its code using exec. The main loop is as…
Nemo
  • 461
  • 4
  • 15
1
vote
2 answers

Why does a << b act differently than a = a + b (with copied array of hashes with strings)

I'm trying to modify a copy of an array without changing the original array. It's an array of hashes, so to to make an "all new" copy of the array I use: foo = [ { :a => "aaaaaa" } ] foocopy = foo.map { |h| h.dup } I want to append some data to a…
jpw
  • 18,697
  • 25
  • 111
  • 187
1
vote
0 answers

Creating complex variant records with descendants in rails

I have an interesting puzzle I've been trying to find a more elegant solution. I have 2 models. Surveys and Questions which are connect in a has_many through relation by QuestionLogs. has_many :questions, :through => :question_logs This works fine,…
ere
  • 1,739
  • 3
  • 19
  • 41
1
vote
1 answer

Executing redirection ( ">" and "<" ) in custom shell in C

I'm supposed to write a simple custom shell in C that can handle redirection with just the "<" and ">" commands. To do this I'm parsing each command (in an array of strings), checking for the characters '<' and '>' and then opening a file name with…
Alex
  • 2,145
  • 6
  • 36
  • 72
1
vote
1 answer

Counting the number of duplicates for a file descriptor in Linux

In Linux, one can duplicate a file descriptor by using the dup command family. Is there any way to get the count of the number of the duplicates for a file descriptor by way of a system call?
sdeber
  • 21
  • 1
1
vote
3 answers

C dup2 overwrite file bug when line > 1

I have the following simple program that catenates infile to outfile char *execArgs[] = { "cat", NULL}; int outfile = open("outfile", O_WRONLY | O_CREAT, 0644); int infile = open("infile", O_RDONLY, 0); dup2(outfile, STDOUT_FILENO); …
Mariska
  • 1,913
  • 5
  • 26
  • 43
1
vote
1 answer

Why fork() close one of the file descriptor by dup2()

I wrote code to get sense of dup2(). int main(int argc, char* argv[]) { FILE *fp = fopen(argv[1],"r"); int fdold,fdnew; fdold = fileno(fp); fdnew = dup2(fdold,fdnew); while (1) { sleep(1000); } } the lsof shows 2…
user1744585
  • 147
  • 1
  • 1
  • 10
1
vote
1 answer

dup does not redirect

I am writing code to redirect stdout to a file(such as the result of an ls being returned to a file) and the dup2() function does not redirect my output. Here is the code that I have: void testDup() { int newft; if(newfd =…
Owl_Prophet
  • 362
  • 5
  • 15
1
vote
2 answers

using dup2 and execv to get folder content in C

I wrote a program to use ls command in Linux terminal to read the content of the folder and write the text from ls to the screen with my C program. Here's the code I wrote: #include #include #include int main() { …
HZero
  • 69
  • 1
  • 1
  • 7
1
vote
3 answers

String copy using pipes

i have written the following code to copy a string "hello world" to another char array using fork and pipes instead of using standard library functions or standard i/o streams. The program is compiling successfully but i am not getting any output.…
manav m-n
  • 11,136
  • 23
  • 74
  • 97
1
vote
1 answer

dup method not working after migrating Rails 3.2 → 4.0

After following the Rails 4.0 supplement, I got to the Some specific issues part, where Michael mentions One tiny change in the Micropost spec (spec/models/micropost_spec.rb) is a change from the dup method (to duplicate the user’s…
gregoltsov
  • 2,269
  • 1
  • 22
  • 37