Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C - Linux System Call like touch

  • 12-10-2012 12:19AM
    #1
    Registered Users, Registered Users 2 Posts: 6,889 ✭✭✭


    I'm sure many of you are familiar with the "touch" command on Linux. 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;
    }
    

    Not sure if this should go here or the Linux forum, so I'll post it there too.


Comments

  • Registered Users, Registered Users 2 Posts: 45 irishd


    creat() ?

    Adds: or better, mknod() ?


  • Registered Users, Registered Users 2 Posts: 949 ✭✭✭moycullen14


    tolosenc wrote: »
    I'm sure many of you are familiar with the "touch" command on Linux. 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;
    }
    

    Not sure if this should go here or the Linux forum, so I'll post it there too.
    This isn't doing what touch does at all.

    The intention of touch is to alter access & modification times on files - one effect is that a file is created IF AND ONLY IF it doesn't exist.

    your code changes the contents of a file - in the case above it wipes all the data.

    touch is based around creat & stat/fstat system calls

    Have a look at:
    http://src.gnu-darwin.org/src/usr.bin/touch/touch.c.html


Advertisement