LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2010, 09:04 AM   #1
skiron.liu
Member
 
Registered: Jun 2009
Location: Harbin
Posts: 32

Rep: Reputation: 0
Question How to solve practice item 3.2 in APUE2


As subject:
Write your own dup2 function that performs the same service as the dup2 function described in Section 3.12, without calling the fcntl function. Be sure to handle errors correctly.
I think maybe use open("/dev/fd/n",mode), but I don't know how to do!~~~
PS:
This is dup2 prototype:
int dup2(int filedes, int filedes2);
 
Old 01-28-2010, 03:46 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by skiron.liu View Post
As subject:
Write your own dup2 function that performs the same service as the dup2 function described in Section 3.12, without calling the fcntl function. Be sure to handle errors correctly.
I think maybe use open("/dev/fd/n",mode), but I don't know how to do!~~~
Yes. That is probably possible. But i think it path you need to open is in /proc instread of /dev.

The path of the file to open() is a string so you need to translate the file descriptor (which is an int) to a string so you can append it to the path.

Then you just need to call open("/proc/..", mode) an return the dupped file descriptor returned by open().

You may want to use the same mode as the original file descriptor. Have a look yourself in the /proc doc's to check if that is possible, and if so, how.
 
Old 01-28-2010, 06:20 AM   #3
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
This is what you might want.
Oh, you might need to run it as root though. proc filesystem is owned by root and files are not writable by normal users. I dont know if APUE wanted this to be done as a normal user.

Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include    <stdio.h>
#include <unistd.h>

char arr[] = "my name is...my name is mydup\n";

int main()
{
    int fd1, fd2;

    char pidstr[128] = "\0";
    fd1 = open ("~/test/file_for_dup", O_CREAT|O_RDWR);
    if (fd1 < 0) {
        perror ("open");
        exit(-1);
    }

    sprintf (pidstr, "/proc/self/fd/%d", fd1);
    printf ("pidstr: %d %s\n", fd1, pidstr);
    write (fd1, arr, sizeof(arr));
    fd2 = open (pidstr, O_RDWR);
    if (fd2 < 0) {
        perror ("open");
        exit(-1);
    }
    lseek (fd2, sizeof (arr), SEEK_SET);
    write (fd2, arr, sizeof(arr));
    close (fd2);
    close (fd1);
}

Last edited by shishir; 01-28-2010 at 06:31 AM.
 
Old 01-29-2010, 09:40 AM   #4
skiron.liu
Member
 
Registered: Jun 2009
Location: Harbin
Posts: 32

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by Hko View Post
Yes. That is probably possible. But i think it path you need to open is in /proc instread of /dev.

The path of the file to open() is a string so you need to translate the file descriptor (which is an int) to a string so you can append it to the path.

Then you just need to call open("/proc/..", mode) an return the dupped file descriptor returned by open().

You may want to use the same mode as the original file descriptor. Have a look yourself in the /proc doc's to check if that is possible, and if so, how.
but "dup2" function can specify the value of the new descriptor with the "filedes2" argument. If i only use open() that just return the lowest-numbered available file descriptor.
 
Old 01-29-2010, 12:23 PM   #5
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
I cant think of a clean way, but what I can think of is a somewhat roundabout way of emulating dup2 signature as well.

You can have a static structure that looks like :

struct fdpairs
{
int newfd; //new fd asked for in second param of dup2
int retfd; //fd returned by open
};

Also, you can check if the newfd being requested for is already existing by using fstat with that fd. If it returns an error, then you can go ahead with the following:
You can maintain a table of newfd to retfd and use them as you want. In case the newfd being asked for is already there in the table, remove the entry from the table and create a new entry.

Does this achieve the desired behaviour?
 
Old 05-26-2011, 07:43 AM   #6
TAP3AH
LQ Newbie
 
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
Here is my solution. It doesn't handle EINTR (interrupted by a signal) since I don't know yet how to do it.
Comments, corrections and fresh solutions are appreciated.


Code:
#include <unistd.h>

int mydup_rec(int od,int nd){
  int fd=dup(od);
  if(fd==nd)
    return nd;
  else{
    int fd0=mydup_rec(od,nd);
    close(fd);
    return fd0;
  }
}

int mydup2(int oldd,int newd){
  int fd,fd0,maxd=sysconf(_SC_OPEN_MAX);

  if(oldd<0 || oldd>=maxd || /* check the range of descriptors */
     newd<0 || newd>=maxd || /* and if oldd is open*/
     (lseek(oldd,0,SEEK_CUR)==-1 && errno==EBADF)){
    errno=EBADF;
    return -1;
  }

  if(oldd==newd) /* if they are the same don't do anything */
    return oldd;

  close(newd);   /* close newd if open */

  return mydup_rec(oldd,newd);
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Item not starting up Koop Ubuntu 0 08-26-2008 06:07 PM
menuconfig cannot see my new item... okeyla Linux - Newbie 0 12-28-2006 10:45 AM
AlsaMixer item definitions lothario Linux - Software 2 12-10-2006 11:20 PM
squirrelmail and sent item Ammad Linux - Networking 3 02-01-2006 02:25 AM
mozilla xpm kmenu item desktop item cjae Linux - Newbie 3 04-06-2005 08:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration