small clean ups.
[smatch.git] / smatch_files.c
blobfcf0f2162efae7e69fdba0667d6d99d30f6cff96
1 /*
2 * sparse/smatch_files.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <string.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include "parse.h"
14 #include "smatch.h"
16 static int open_file(const char *filename)
18 int fd;
19 char *buf = malloc(256);
21 fd = open(filename, O_RDONLY);
22 if (fd >= 0)
23 goto exit;
24 if (!data_dir)
25 goto exit;
26 strncpy(buf, data_dir, 254);
27 buf[255] = '\0';
28 strncat(buf, filename, 254 - strlen(buf));
29 fd = open(buf, O_RDONLY);
30 if (fd >= 0)
31 goto exit;
33 exit:
34 free(buf);
35 return fd;
38 struct token *get_tokens_file(const char *filename)
40 int fd;
41 struct token *token;
43 if (option_no_data)
44 return NULL;
45 fd = open_file(filename);
46 if (fd < 0)
47 return NULL;
48 token = tokenize(filename, fd, NULL, NULL);
49 close(fd);
50 return token;