1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/*
* Copyright (c) 1997, 1998, 1999, 2000
* Tama Communications Corporation
*
* This file is part of GNU GLOBAL.
*
* GNU GLOBAL 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.
*
* GNU GLOBAL 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
#include <sys/file.h>
#endif
#include "locatestring.h"
#include "die.h"
#include "test.h"
/*
* Decide whether or not the path is binary file.
*
* i) path
* r) 0: is not binary, 1: is binary
*/
static int
is_binary(path)
const char *path;
{
int ip;
char buf[32];
int i, c, size;
ip = open(path, 0);
if (ip < 0)
die("cannot open file '%s' in read mode.", path);
size = read(ip, buf, sizeof(buf));
close(ip);
if (size < 0)
return 1;
if (!strncmp(buf, "!<arch>", 7))
return 1;
for (i = 0; i < size; i++) {
c = (unsigned char)buf[i];
if (c == 0 || c > 127)
return 1;
}
return 0;
}
/*
* test:
*
* i) flags file flags
*
* "f" [ -f path ]
* "d" [ -d path ]
* "r" [ -r path ]
* "s" [ -s path ]
* "w" [ -w path ]
* "x" [ -x path ]
* "b" [ -b path ]
*
* i) path path
* if NULL then previous path.
* r) 0: no, 1: ok
*
* You can specify more than one character. It assumed 'AND' test.
*/
int
test(flags, path)
const char *flags;
const char *path;
{
static struct stat sb;
int c;
if (path != NULL)
if (stat(path, &sb) < 0)
return 0;
while ((c = *flags++) != 0) {
switch (c) {
case 'b':
if (!is_binary(path))
return 0;
break;
case 'f':
if (!S_ISREG(sb.st_mode))
return 0;
break;
case 'd':
if (!S_ISDIR(sb.st_mode))
return 0;
break;
case 'r':
if (access(path, R_OK) < 0)
return 0;
break;
case 's':
if (sb.st_size == 0)
return 0;
break;
case 'w':
if (access(path, W_OK) < 0)
return 0;
break;
case 'x':
#ifdef _WIN32
/* Look at file extension to determine executability */
if (strlen(path) < 5)
return 0;
if (!S_ISREG(sb.st_mode))
return 0;
if (!locatestring(path, ".exe", MATCH_AT_LAST|IGNORE_CASE) &&
!locatestring(path, ".com", MATCH_AT_LAST|IGNORE_CASE) &&
!locatestring(path, ".bat", MATCH_AT_LAST|IGNORE_CASE))
return 0;
#else
if (access(path, X_OK) < 0)
return 0;
#endif
break;
default:
break;
}
}
return 1;
}
|