Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

System Call for touch

Options
  • 12-10-2012 12:20am
    #1
    Registered Users Posts: 6,889 ✭✭✭


    I'm sure many of you are familiar with the "touch" command. Essentially "touch program.c" creates a blank file called program.c in the current directory.

    I've been playing around with system calls lately, and was wondering if there was one that did this? I've been operating off the following code in lieu:
    int touch(char* fn)
    {
        FILE *fp;
        fp = fopen(fn, "w");
    
        if(fp == NULL)
        {
            return -1;
        }
    
        fclose(fp);
    
        return 0;
    }
    


Comments

  • Registered Users Posts: 1,606 ✭✭✭djmarkus


    Touch does this...

    open("boom", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
    dup2(3, 0) = 0
    close(3) = 0


    So I assume if there was a system call that did it, touch would be using it :)


  • Registered Users Posts: 326 ✭✭schrodinger


    Which operating system...?


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    There's two ways of doing this:
    1. use the open system call
    2. use the creat system call
    int touch(char* fn)
    {
        int fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    
        if(fd == -1) { return -1; }
    
        close(fd);
    
       return 0;
    }
    

    Since creat() is now considered obsolete, you should use open() for more control. The above open call will open new or existing files for reading and writing, truncating to zero bytes if the file already exists; file permissions read+write for owner, nothing for all others. So essentially if you do "touch bleh" and bleh already exists it will zero it out. Granted, touch doesn't behave like this (it simply won't let you touch a file if it already exists), but you can change the open flags for that functionality if you wish - just use O_EXCL and O_CREAT exclusively and it will error if the file exists.

    This is applicable for the Linux kernel. I highly recommend the following book: http://www.amazon.com/Linux-Programming-Interface-System-Handbook/dp/1593272200


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    [-0-] wrote: »
    There's two ways of doing this:
    1. use the open system call
    2. use the creat system call
    int touch(char* fn)
    {
        int fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    
        if(fd == -1) { return -1; }
    
        close(fd);
    
       return 0;
    }
    

    Since creat() is now considered obsolete, you should use open() for more control. The above open call will open new or existing files for reading and writing, truncating to zero bytes if the file already exists; file permissions read+write for owner, nothing for all others. So essentially if you do "touch bleh" and bleh already exists it will zero it out. Granted, touch doesn't behave like this (it simply won't let you touch a file if it already exists), but you can change the open flags for that functionality if you wish - just use O_EXCL and O_CREAT exclusively and it will error if the file exists.

    This is applicable for the Linux kernel. I highly recommend the following book: http://www.amazon.com/Linux-Programming-Interface-System-Handbook/dp/1593272200

    This is the actual call touch uses:
    fd = open (file, O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
    		 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    

    Enjoy. :)


  • Registered Users Posts: 218 ✭✭Tillotson


    Use strace to show system calls:
    $ strace touch test.txt 2>&1 | grep ^open | tail -n 1
    open("test.txt", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
    $
    
    I'm guessing "S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH" is equlivant to 0666 in octal.


  • Advertisement
Advertisement