X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Ftest_initfile.c;fp=sources%2Ftest_initfile.c;h=12dc0280ec65548a6c7f24f28f6946756ca17f85;hp=0000000000000000000000000000000000000000;hb=4be39dd3718d708ccff65b01a8f634e05b56f3c9;hpb=2535dbace6167c4bcff7dd9c98f4d397bb699afd diff --git a/sources/test_initfile.c b/sources/test_initfile.c new file mode 100644 index 0000000..12dc028 --- /dev/null +++ b/sources/test_initfile.c @@ -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 /* error definitions and routines */ +#include /* C standard library */ +#include /* unix standard library */ +#include /* standard I/O library */ +#include /* C strings library */ +#include +#include /* needed for dirname e basename */ +#include +#include +#include + +#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); +} +