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

C - Linux System Call like touch

Options
  • 12-10-2012 12:19am
    #1
    Registered Users 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 Posts: 45 irishd


    creat() ?

    Adds: or better, mknod() ?


  • Registered Users Posts: 880 ✭✭✭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