Jspice3
exec.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3f2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1985 Thomas L. Quarles
5  1993 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 #include "spice.h"
9 #include "cpdefs.h"
10 #define SHORT_SCEDIO
11 #include "scedio.h"
12 
13 #ifdef HAVE_SYS_WAIT_H
14 #include <sys/wait.h>
15 #endif
16 #ifdef HAVE_SIGNAL
17 #include <signal.h>
18 #endif
19 #ifdef HAVE_STAT
20 #include <sys/stat.h>
21 #endif
22 
23 bool
24 tryexec(name, argv)
25 
26 /* fork and execute */
27 char *name;
28 char *argv[];
29 {
30 #ifdef HAVE_FORK
31  int status;
32  int pid, j;
33  RETSIGTYPE (*svint)( ), (*svquit)( ), (*svtstp)( );
34 
35  pid = fork( );
36  if (pid == 0) {
38  (void) execv(name, argv);
39  (void) _exit(120); /* A random value. */
40  /* NOTREACHED */
41  }
42  else {
43  svint = signal(SIGINT, SIG_DFL);
44  svquit = signal(SIGQUIT, SIG_DFL);
45  svtstp = signal(SIGTSTP, SIG_DFL);
46  do {
47  j = wait(&status);
48  } while (j != pid);
49  (void) signal(SIGINT, svint);
50  (void) signal(SIGQUIT, svquit);
51  (void) signal(SIGTSTP, svtstp);
52  }
53  if (WIFEXITED(status) && WEXITSTATUS(status) == 120)
54  return (false);
55  return (true);
56 #else
57  char *s;
58  wordlist *wl;
59 
60  argv++;
61  wl = alloc(wordlist);
62  wl->wl_word = copy(name);
63  if (*argv) {
64  wl->wl_next = wl_build(argv);
65  wl->wl_next->wl_prev = wl;
66  }
67  s = wl_flatten(wl);
68  wl_free(wl);
69  (void) system(s);
70  putc('\n',stdout);
71  tfree(s);
72  return (true);
73 
74 #endif
75 }
76 
77 
78 bool
79 is_exec(file,dir)
80 
81 /* return true if the file can be executed */
82 char *file, *dir;
83 {
84 #ifdef MSDOS
85  char *c;
86 
87  if ((c = index(file,'.')) == NULL)
88  return (false);
89  *c = '\0';
90  c++;
91  if (!cieq(c,"exe") && !cieq(c,"com") && !cieq(c,"bat"))
92  return (false);
93 #else
94  char buf[BSIZE_SP];
95 #ifdef HAVE_STAT
96  struct stat stbuf;
97 
98  (void)sprintf(buf,"%s%c%s",dir,DIR_TERM,file);
99  if (stat(buf,&stbuf) < 0)
100  return (false);
101  if (!(stbuf.st_mode & S_IFREG) || !(stbuf.st_mode & S_IXUSR))
102  return (false);
103 #else
104  (void)sprintf(buf,"%s%c%s",dir,DIR_TERM,file);
105  if (access(buf,1))
106  return (false);
107 #endif
108 #endif
109  return (true);
110 }
static char buf[MAXPROMPT]
Definition: arg.c:18
#define BSIZE_SP
Definition: misc.h:19
int cieq()
Definition: cddefs.h:119
int system(char *str)
Definition: libfuncs.c:85
Definition: library.c:18
#define alloc(type)
Definition: cdmacs.h:21
char * copy()
void wl_free()
bool is_exec(char *file, char *dir)
Definition: exec.c:79
struct wordlist * wl_prev
Definition: cpstd.h:24
void cp_fixdescriptors()
Definition: cshpar.c:454
#define tfree(x)
Definition: cdmacs.h:22
#define NULL
Definition: spdefs.h:121
wordlist * wl_build()
int access(char *pth, int m)
Definition: libfuncs.c:75
static double c
Definition: vectors.c:16
bool tryexec(char *name, argv)
Definition: exec.c:24
Definition: cpstd.h:21
char * index(char *s, char c) const
Definition: string.c:294
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
char * wl_flatten()