lstat test

C/C++,Unix — Dillon @ 5:38 pm

Following an interview question that was extremely hard I went to `man lstat’ and tried to code up a test just based on system documentation. It was not entirely successful, however after a tip-off from an online resource I came up with this.


File: lstat.c

#include 
#include 
#include 
#include 

int main(int c, char** argv) {
        struct stat buf;
        lstat(argv[1],&buf);
        printf("%s is %d bytes\n", argv[1],buf.st_size);

        return 0;
}

When compile like this:
g++ -o lstat lstat.c

It looks like this where yep.txt is a dummy file.
user@box ~/c $ ls -l /tmp/yep.txt
ls -l /tmp/yep.txt
-rw-r--r-- 1 user users 17 Apr 9 23:16 /tmp/yep.txt

user@box ~/c $ ./lstat /tmp/yep.txt
/tmp/yep.txt is 17 bytes

So all this does is really use stat since yep.txt is a file and not a symbolic link. This stupid test just prints out the file size which you can see by the real `ls’ above.

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 SQUARISM | powered by WordPress with Barecity