Is there a way to make the following code portable?

This is a discussion on Is there a way to make the following code portable? within the Linux Programming forums, part of the Platform Specific Boards category; ....It is quite portable inside the unix land....but I'm looking for a way to make it more standard. The two ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Is there a way to make the following code portable?

    ....It is quite portable inside the unix land....but I'm looking for a way to make it more standard.
    The two following functions are 1.constructor for a disk class (for a virtual machine) and a way to execute shell directory commands of the host system.

    Code:
    disk::disk(string name,vm* owner)
    {
        /// Check if name is already in the global list
    
    
        ofstream new_disk("disks/"+name+"_prop");
         
        new_disk<<"Name: "<<name<<endl;
        system(("mkdir disks/"+name).c_str());
        
        pwd="disks/"+name+"/";
        disk_of = owner;
        //(*disk_of).shell(); //Works
    }
    Code:
    void disk::command(std::string com)
    {
        istringstream in(com);
        string test;
        in>>test;
        if(test=="cd")
            {
                in>>test;
                pwd+=test; //pwd is a string containing the present working dir to carry over from calls to system();
                system(("cd "+test).c_str());
            }
        else
        {
            system(("cd "+pwd+" && "+com).c_str());
        }
    }
    Last edited by manasij7479; 07-03-2011 at 01:27 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    This perhaps ?
    Filesystem Home
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    That page , at a point says that
    A proposal, N1975, to include Boost.Filesystem in Technical Report 2 has been accepted by the C++ Standards Committee. The Boost.Filesystem library will stay in alignment with the TR2 Filesystem proposal as it works its way through the TR2 process.
    Does that mean, C++11 would contain something like the Boost Filesystem library ?.. I can't find anything to verify it ..(open-std.org only mentions that it'd been proposed....)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Depends on how much portability you wish. If you're willing to get ahead of the curve, with functions that might find their way into the C++ standard, look up boost filesystem. Otherwise, it is not hard to roll your own, depending on what you want to achieve.

    Using system calls inherently limits you to your original target system (or systems compatible with it). More importantly, however, such things can be broken depending on how your system is configured, because system calls inherently rely on whatever command interpreter is invoked by the system() call - such things are inherently implementation defined. Furthermore, relying on command interpreters also makes your program vulnerable to system configuration (what version of a shell is configured) and user configuration (what startup scripts the user executes).

    If you are happy being limited to posix systems, you might want to look up functions named mkdir() and chdir(). Information on those may be found via "man 2 mkdir" and "man 2 chdir" respectively (on most unix systems, assuming man pages are installed).

    More generally, however, you will need to write code that detects the target system (or maybe, compiler) and responds accordingly. For example, for a function that creates a directory ....
    Code:
    //   untested code, written to demonstrate the basic approach, follows.
    
    #if defined (_WIN32)      //   _WIN32 is defined by many compilers that target windows
    #include <winbase.h>
    #else                            //   assume POSIX (or, at least, some common unix setup)
    #include <sys/stat.h>
    #include <sys/types.h>
    #endif
    
    bool create_directory(const char *path)   // return false if we succeed
    {
    #ifdef _WIN32
          return bool(CreateDirectory(path, LPSECURITY_ATTRIBUTES(NULL)));
    #else
          return bool(mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR));    //   grant owner access only
    #endif     
    }
    Depending on what systems you wish to port to, you will need to identify particular macros and header files for each one. Since these capabilities are outside scope of the C or C++ standards, you are left with having to use techniques that vary with target system.

    In the above, I have assumed you want to use win32 API functions under windows, and otherwise will only build on a posix system. Those assumptions are not true in general (libraries with compilers targeting windows often support posix functions, and "not windows" is also not the same as "posix").
    Last edited by grumpy; 07-03-2011 at 02:11 AM.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-29-2011, 08:27 PM
  2. How do I make an EXE out of a C code?
    By NA84 in forum C Programming
    Replies: 4
    Last Post: 11-07-2010, 12:40 AM
  3. How to Make This code more efficient
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 04-12-2010, 01:29 AM
  4. Replies: 6
    Last Post: 01-03-2003, 04:40 PM
  5. Make node code - right?
    By MethodMan in forum C Programming
    Replies: 1
    Last Post: 03-24-2002, 01:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21