-1

Create a file foo and foo2 with the same time access and permissions using system programming. I am quite clueless about this, I had to create foo and foo2 outside, and now I am unsure whether this is the correct way to do it or not. This is what I have tried:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#include <fcntl.h>

int main()
{
    int fd = open("./foo", O_RDWR);
    
    int fd2 = open("./foo2", O_RDWR);
    if(fd == -1)
    {
        printf("\nError creating foo.");
    }
    else if(fd2 == -1)
    {
        printf("\nError creating foo2.");
    }
    else
    {
        printf("\nFile descripter 1:     %d", fd);  
        printf("\nFile descripter 2:     %d", fd2);
    }
    return 0;
}









  • 2
    You are probably looking for this: https://stackoverflow.com/a/42214603/898348 – Jabberwocky Dec 07 '21 at 09:17
  • "Using system programming" means (e.g.) writing code that understands and modifies the contents of raw physical disk blocks. Using high level APIs built on top of abstractions (which are then provided to user-space), is not system programming. – Brendan Dec 07 '21 at 09:27
  • Does this answer your question? [Linux - modify file modify/access/change time](https://stackoverflow.com/questions/40630695/linux-modify-file-modify-access-change-time) – the busybee Dec 07 '21 at 09:49

1 Answers1

1

As of file creation, the C command is open(), as stated in it's man page (requires some libs, beware):

https://www.man7.org/linux/man-pages/man2/open.2.html

If the user wishes to create or override the file, he may use creat(), which is in reality little more than open() with these flags: O_CREAT|O_WRONLY|O_TRUNC

As for time access, others dwellers may be more helpful; Jabberwocky's link may be of some help.

Archivist
  • 36
  • 1
  • 4
  • *requires some libs* Huh? `open()` is almost always in the implicitly-linked-unless-you-tell-it-otherwise `libc.so` library. – Andrew Henle Dec 07 '21 at 11:25