Current Working Directory Implementation – Initial Steps

It has been almost two weeks, I started working on my pharo GSoC project. The first week was more of reading and understanding stuff. This week I have tried to do a small implementation of the same. In this blog post, I will go through the implementation of ‘Current Working Directory in Linux’, and difficulties I have faced. In the last blog post, I have discussed various ways of doing this and I have decided on using the ‘getcwd()’ method.

Overall, the code is simple and easy to understand. Basically, to get the current working directory, I have to call a basic unix fucntion ‘getcwd’ using the FFI ‘Foreign Function Interface’ in Pharo. Using FFI is simple, you just have to find the right unix function, know the return type, and types of the arguments, name of the module to which the function belongs and call it with FFI. Such FFI call looks like this

^ self ffiCall: #( String getcwd (String arg1,256)) module: LibC

The above code is put in ‘getPwdFromFFI: arg1’ method. This calls the getcwd function from pharo using the FFI module. To call this method, I have created a new method ‘getCurrentWorkingDirectory’.

buffer := String new:256.
^ self getPwdViaFFI: buffer.

In the getcwd() function the second argument is the buffer size. This implementation is very basic and it can be better. I tried experimenting a bit with these methods. In the ‘getPwdfromFFI’ instead of hard-coding the buffer size, I have tried to get the size from the argument with (arg1 size). But the FFI module is not recognizing it. I tried to create a temporary variable and assign the arg1 size and then send it, but it was in vain. I tried sending it as String (arg1 size) and Integer (arg1 size)

Anyways, to improve the implementation, I would like to do something like this in Pharo.

long size;
char *buf;
char *ptr;

size = pathconf(".", _PC_PATH_MAX);

if ((buf = (char *)malloc((size_t)size)) != NULL)
ptr = getcwd(buf, (size_t)size);

This will check the appropriate size of the directory name and assigns the buffer size accordingly. This week, I am looking forward to trying this and also implementing a method for windows.




  • Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s