/*----------------------------------------------------------------------------*\ * lm - 31-08-2001 - http://www.incubus.co.uk * * Check if a given file exists- and if it can be opened for read/write access * cc checkfile -Wall -o checkfile ; ./checkfile * * Updated 27/11/2007 - Improved code and style. \*----------------------------------------------------------------------------*/ /****************************************************************************** * * * 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. * * * ******************************************************************************/ /*----------------------------------------------------------------------------*\ * System includes. \*----------------------------------------------------------------------------*/ #include #include #include #include #include /*----------------------------------------------------------------------------*\ * Function Prototypes. \*----------------------------------------------------------------------------*/ int GetDirName (char *path, int dirlen, char *dir); /*----------------------------------------------------------------------------*\ * Main. \*----------------------------------------------------------------------------*/ int main (int argc, char *argv[]) { char file_exist = 0; char dirname[256] = ""; char buffer [256] = ""; FILE *f_ptr = NULL; DIR *dir_ptr = NULL; struct dirent *dirent_ptr; /*-- Ensure a file name has been provoided ------------------------*/ if (!(--argc)) { fprintf(stderr,"Missing argument - checkfile dir/filename \n"); return (EXIT_FAILURE); } /*-- Get directory from given filename ----------------------------*/ if ( GetDirName(argv[1], 255, dirname) ) { fprintf(stderr,"Missing argument - checkfile dir/filename \n"); return (EXIT_FAILURE); } /*-- Determine directory name -------------------------------------*/ if ((dir_ptr = opendir(dirname))) { while ((dirent_ptr = readdir(dir_ptr))) { if (strcmp(dirent_ptr->d_name, basename(argv[1]))==0) { file_exist = 1; break; } } closedir(dir_ptr); } if (file_exist) { printf ("File Exists => PASS\n"); sprintf(buffer, "%s/%s", dirname, basename(argv[1])); if( (f_ptr = fopen( buffer, "r")) != 0) { fprintf(stdout,"File Open READ => PASS\n"); fclose (f_ptr); if( (f_ptr = fopen( buffer, "r+")) != 0) { fprintf(stdout,"File Open WRITE => PASS\n"); fclose (f_ptr); }else fprintf(stdout,"File Open WRITE => FAIL\n"); }else fprintf(stdout,"File Open READ => FAIL\n"); }else printf("File Exists => FAIL\n"); return EXIT_SUCCESS; } /*----------------------------------------------------------------------------*\ * Get Directory Name. \*----------------------------------------------------------------------------*/ int GetDirName (char *path, int dirlen, char *dir) { int count; int len; if (!strcmp(basename(path),path)) strcpy(dir, "./"); /* Current Working Directory */ else { /*-- Determine the length of the directory element -------*/ len = strlen(path) - strlen(basename(path)); /*-- Ensure this will not over run the given buffer ------*/ if (len > dirlen) return -1; /*-- Copy directory element to dir buffer ----------------*/ for (count = 0; count < len; count++) dir[count] = path[count]; dir[count-1] = '\0'; } return 0; }
© Copyright 1999-2008 @incubus. All Rights Reserved. All trademarks acknowledged. incubus.co.uk || zenithpaints.co.uk || rankinstine.co.uk