Debian Codegraph @

listings/appendix-b/create-file.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 int main (int argc, char* argv[])
14 {
15 /* The path at which to create the new file. */
16 char* path = argv[1];
17 /* The permissions for the new file. */
18 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
19
20 /* Create the file. */
21 int fd = open (path, O_WRONLY | O_EXCL | O_CREAT, mode);
22 if (fd == -1) {
23 /* An error occurred. Print an error message and bail. */
24 perror ("open");
25 return 1;
26 }
27
28 return 0;
29 }