Trattazione di linkat e openat con O_TMPFILE, con programmi di test ed
[gapil.git] / sources / test_initfile.c
diff --git a/sources/test_initfile.c b/sources/test_initfile.c
new file mode 100644 (file)
index 0000000..12dc028
--- /dev/null
@@ -0,0 +1,118 @@
+/* test_initfile.c
+ * 
+ * Copyright (C) 2019 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program test_linkat.c: 
+ * Program to test use of linkat with an O_TMPFILE or linking another file 
+ * using AT_EMPTY_PATH or AT_SYMLINK_FOLLOW on /proc/self/fd/N
+ * giving the option to wait to remove source file
+ *
+ * Author: Simone Piccardi
+ * Oct. 2018
+ *
+ * Usage: testlinkat -h give all info's
+ *
+ ****************************************************************/
+/* 
+ * Include needed headers
+ */
+#define _GNU_SOURCE
+#include <errno.h>       /* error definitions and routines */ 
+#include <stdlib.h>      /* C standard library */
+#include <unistd.h>      /* unix standard library */
+#include <stdio.h>      /* standard I/O library */
+#include <string.h>      /* C strings library */
+#include <limits.h>
+#include <libgen.h>      /* needed for dirname e basename */
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+#include "Gapil.h"
+#include "macros.h"
+
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/* 
+ * Variables definition  
+ */
+    /*
+     * Input section: decode command line parameters 
+     * Use getopt function
+     */
+    int i;
+    opterr = 0;         /* don't want writing to stderr */
+    while ( (i = getopt(argc, argv, "hw:f:")) != -1) {
+       switch (i) {
+       /* 
+        * Handling options 
+        */ 
+       case 'h':   /* help option */
+           printf("Wrong -h option use\n");
+           usage();
+           return -1;
+           break;
+       case '?':   /* unrecognized options */
+           printf("Unrecognized options -%c\n",optopt);
+           usage();
+       default:    /* should not reached */
+           usage();
+       }
+    }
+    /* ***********************************************************
+     * 
+     *          Options processing completed
+     *
+     *               Main code beginning
+     * 
+     * ***********************************************************/
+    /* There must be 1 remaing parameters */
+    if ( (argc-optind) != 2 )  {
+       printf("From %d arguments, removed %d options\n", argc, optind);
+       usage();
+    }
+    char *path, *dir, *file;
+    path = strdup(argv[optind]);
+    file = basename(argv[optind]);
+    dir = dirname(argv[optind]);
+    printf("Destination on dir %s file: %s\n            with path: %s\n",
+          dir, file, path);
+    int newfd, res, count;
+    count = strlen(argv[optind+1]);
+    newfd = open(dir, O_PATH|O_RDWR);
+    res = InitFile(newfd, file, argv[optind+1], count);
+    free(path);
+    return 0;
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+    printf("Program test_initfile : test initfile  \n");
+    printf("Create a link to a tempfile or a link to -f indicated file");
+    printf("Usage:\n");
+    printf("  test_initfile [-h] pathname 'test to be written on pathname' \n");
+    printf("  -h          print this help\n");
+    
+    exit(1);
+}
+