~ubuntu-branches/debian/jessie/ccache/jessie

« back to all changes in this revision

Viewing changes to execute.c

  • Committer: Bazaar Package Importer
  • Author(s): Loïc Minier
  • Date: 2010-10-13 17:14:13 UTC
  • mfrom: (5.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101013171413-4ehrj1y89huf069u
Tags: 3.0.1-1ubuntu1
* Merge with Debian; remaining changes:
  - Add gcc/g++-4.4 and -4.5 symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
   Copyright (C) Andrew Tridgell 2002
3
 
   
4
 
   This program is free software; you can redistribute it and/or modify
5
 
   it under the terms of the GNU General Public License as published by
6
 
   the Free Software Foundation; either version 2 of the License, or
7
 
   (at your option) any later version.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 
*/
 
2
 * Copyright (C) Andrew Tridgell 2002
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the Free
 
6
 * Software Foundation; either version 3 of the License, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 
12
 * more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along with
 
15
 * this program; if not, write to the Free Software Foundation, Inc., 51
 
16
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 */
18
18
 
19
19
#include "ccache.h"
20
20
 
 
21
#include <sys/types.h>
 
22
#include <sys/stat.h>
 
23
#include <sys/wait.h>
 
24
#include <fcntl.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
#include <time.h>
 
29
#include <unistd.h>
21
30
 
22
31
/*
23
32
  execute a compiler backend, capturing all output to the given paths
24
33
  the full path to the compiler to run is in argv[0]
25
34
*/
26
 
int execute(char **argv, 
 
35
int execute(char **argv,
27
36
            const char *path_stdout,
28
37
            const char *path_stderr)
29
38
{
30
39
        pid_t pid;
31
40
        int status;
32
41
 
 
42
        cc_log_executed_command(argv);
 
43
 
33
44
        pid = fork();
34
45
        if (pid == -1) fatal("Failed to fork");
35
 
        
 
46
 
36
47
        if (pid == 0) {
37
48
                int fd;
38
49
 
39
50
                unlink(path_stdout);
40
51
                fd = open(path_stdout, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
41
52
                if (fd == -1) {
42
 
                        exit(STATUS_NOCACHE);
 
53
                        exit(1);
43
54
                }
44
55
                dup2(fd, 1);
45
56
                close(fd);
47
58
                unlink(path_stderr);
48
59
                fd = open(path_stderr, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
49
60
                if (fd == -1) {
50
 
                        exit(STATUS_NOCACHE);
 
61
                        exit(1);
51
62
                }
52
63
                dup2(fd, 2);
53
64
                close(fd);
68
79
 
69
80
 
70
81
/*
71
 
  find an executable by name in $PATH. Exclude any that are links to exclude_name 
 
82
  find an executable by name in $PATH. Exclude any that are links to exclude_name
72
83
*/
73
84
char *find_executable(const char *name, const char *exclude_name)
74
85
{
85
96
                path = getenv("PATH");
86
97
        }
87
98
        if (!path) {
88
 
                cc_log("no PATH variable!?\n");
 
99
                cc_log("No PATH variable");
89
100
                return NULL;
90
101
        }
91
102
 
92
103
        path = x_strdup(path);
93
 
        
 
104
 
94
105
        /* search the path looking for the first compiler of the right name
95
106
           that isn't us */
96
107
        for (tok=strtok(path,":"); tok; tok = strtok(NULL, ":")) {
106
117
                        if (S_ISLNK(st1.st_mode)) {
107
118
                                char *buf = x_realpath(fname);
108
119
                                if (buf) {
109
 
                                        char *p = str_basename(buf);
 
120
                                        char *p = basename(buf);
110
121
                                        if (strcmp(p, exclude_name) == 0) {
111
122
                                                /* its a link to "ccache" ! */
112
123
                                                free(p);
127
138
 
128
139
        return NULL;
129
140
}
 
141
 
 
142
void print_command(FILE *fp, char **argv)
 
143
{
 
144
        int i;
 
145
        for (i = 0; argv[i]; i++) {
 
146
                fprintf(fp, "%s%s",  (i == 0) ? "" : " ", argv[i]);
 
147
        }
 
148
        fprintf(fp, "\n");
 
149
}
 
150
 
 
151
void print_executed_command(FILE *fp, char **argv)
 
152
{
 
153
        fprintf(fp, "%s: executing ", MYNAME);
 
154
        print_command(fp, argv);
 
155
}