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

    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.

    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());
        disk_of = owner;
        //(*disk_of).shell(); //Works
    void disk::command(std::string com)
        istringstream in(com);
        string test;
                pwd+=test; //pwd is a string containing the present working dir to carry over from calls to system();
                system(("cd "+test).c_str());
            system(("cd "+pwd+" && "+com).c_str());
    Last edited by manasij7479; 07-03-2011 at 02:27 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    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 ..( only mentions that it'd been proposed....)

  4. #4
    Registered User
    Join Date
    Jun 2005
    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 ....
    //   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>
    bool create_directory(const char *path)   // return false if we succeed
    #ifdef _WIN32
          return bool(CreateDirectory(path, LPSECURITY_ATTRIBUTES(NULL)));
          return bool(mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR));    //   grant owner access only
    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 03:11 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-29-2011, 09: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, 01:40 AM
  3. How to Make This code more efficient
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 04-12-2010, 02:29 AM
  4. Replies: 6
    Last Post: 01-03-2003, 05:40 PM
  5. Make node code - right?
    By MethodMan in forum C Programming
    Replies: 1
    Last Post: 03-24-2002, 02:31 PM

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