check-unixfile.c (2839B)
1 // $Id$ --*- c -*-- 2 3 // Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de> 4 // 5 // This program is free software; you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation; version 2 of the License. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with this program; if not, write to the Free Software 16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 18 19 #ifdef HAVE_CONFIG_H 20 # include <config.h> 21 #endif 22 23 #include "util.h" 24 25 #include <fcntl.h> 26 #include <errno.h> 27 #include <unistd.h> 28 #include <sys/mman.h> 29 #include <limits.h> 30 31 #define ENSC_WRAPPERS_PREFIX "check-unixfile: " 32 #define ENSC_WRAPPERS_FCNTL 1 33 #define ENSC_WRAPPERS_UNISTD 1 34 #include <wrappers.h> 35 36 int wrapper_exit_code = 255; 37 38 static void 39 showHelp(int fd, char const *cmd) 40 { 41 WRITE_MSG(fd, "Usage: "); 42 WRITE_STR(fd, cmd); 43 WRITE_MSG(fd, 44 " [--] <file>+\n\n" 45 "Please report bugs to " PACKAGE_BUGREPORT "\n"); 46 exit(0); 47 } 48 49 static void 50 showVersion() 51 { 52 WRITE_MSG(1, 53 "check-unixfile " VERSION " -- execute some basic fileformat checks\n" 54 "This program is part of " PACKAGE_STRING "\n\n" 55 "Copyright (C) 2005 Enrico Scholz\n" 56 VERSION_COPYRIGHT_DISCLAIMER); 57 exit(0); 58 } 59 60 static bool 61 checkFile(char const *fname) 62 { 63 int fd = Eopen(fname, O_RDONLY, 0); 64 off_t l = Elseek(fd, 0, SEEK_END); 65 char const * data = 0; 66 bool res = true; 67 68 if (l>100*1024*1024) { 69 WRITE_MSG(2, "WARNING: '"); 70 WRITE_STR(2, fname); 71 WRITE_STR(2, "' is too large for a vserver configuration file\n"); 72 res = false; 73 } 74 else if (l>0) { 75 data = mmap(0, l, PROT_READ, MAP_PRIVATE, fd, 0); 76 if (data==MAP_FAILED) { 77 perror("mmap()"); 78 exit(wrapper_exit_code); 79 } 80 81 if (data[l-1]!='\n') { 82 WRITE_MSG(2, "WARNING: '"); 83 WRITE_STR(2, fname); 84 WRITE_MSG(2, "' does not end on newline\n"); 85 res = false; 86 } 87 88 munmap(const_cast(char *)(data), l); 89 } 90 91 Eclose(fd); 92 93 return res; 94 } 95 96 int main(int argc, char *argv[]) 97 { 98 int idx = 1; 99 bool ok = true; 100 bool passed = false; 101 102 if (argc>=2) { 103 if (strcmp(argv[1], "--help") ==0) showHelp(1, argv[0]); 104 if (strcmp(argv[1], "--version")==0) showVersion(); 105 if (strcmp(argv[1], "--") ==0) ++idx; 106 } 107 108 for (; idx<argc; ++idx) 109 if (checkFile(argv[idx])) passed = true; 110 else ok = false; 111 112 if (ok) return 0; 113 else if (passed) return 2; 114 else return 1; 115 }