Changeset 1213 for trunk/src/kash/bltin/kill.c
- Timestamp:
- Oct 7, 2007, 9:13:10 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/bltin/kill.c
r1207 r1213 47 47 48 48 #include <ctype.h> 49 #ifndef __sun__50 #include <err.h>51 #endif52 49 #include <errno.h> 53 #include <signal.h>54 50 #include <stdio.h> 55 51 #include <stdlib.h> 56 52 #include <string.h> 57 #include <termios.h> 58 #include <unistd.h> 59 #include <locale.h> 60 #include <sys/ioctl.h> 53 #include "jobs.h" 54 #include "error.h" 55 #include "shinstance.h" 61 56 62 57 #ifndef HAVE_SYS_SIGNAME … … 65 60 #endif 66 61 67 #ifdef SHELL /* sh (aka ash) builtin */ 68 #define main killcmd 69 #include "bltin/bltin.h" 70 #include "jobs.h" 71 #endif /* SHELL */ 72 73 static void nosig(char *); 74 static void printsignals(FILE *); 62 static int nosig(shinstance *, char *); 63 static void printsignals(shinstance *, struct output *); 75 64 static int signame_to_signum(char *); 76 static void usage(void); 77 int main(int, char *[]); 65 static int usage(shinstance *psh); 78 66 79 67 int 80 main(int argc, char *argv[])68 killcmd(shinstance *psh, int argc, char *argv[]) 81 69 { 82 70 int errors, numsig, pid; 83 71 char *ep; 84 72 85 setprogname(argv[0]);86 setlocale(LC_ALL, "");87 73 if (argc < 2) 88 usage();74 return usage(psh); 89 75 90 76 numsig = SIGTERM; 77 #ifndef HAVE_SYS_SIGNAME 78 init_sys_signame(); 79 #endif 91 80 92 81 argc--, argv++; … … 94 83 argc--, argv++; 95 84 if (argc > 1) 96 usage();85 return usage(psh); 97 86 if (argc == 1) { 98 87 if (isdigit((unsigned char)**argv) == 0) 99 usage();88 return usage(psh); 100 89 numsig = strtol(*argv, &ep, 10); 101 90 if (*ep != '\0') { 102 errx(EXIT_FAILURE, "illegal signal number: %s",91 sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s", 103 92 *argv); 104 93 /* NOTREACHED */ … … 107 96 numsig -= 128; 108 97 if (numsig <= 0 || numsig >= NSIG) 109 nosig(*argv); 110 #ifndef HAVE_SYS_SIGNAME 111 init_sys_signame(); 112 #endif 113 printf("%s\n", sys_signame[numsig]); 114 exit(0); 115 } 116 printsignals(stdout); 117 exit(0); 98 return nosig(psh, *argv); 99 outfmt(psh->out1, "%s\n", sys_signame[numsig]); 100 //sh_exit(psh, 0); 101 return 0; 102 } 103 printsignals(psh, psh->out1); 104 //sh_exit(psh, 0); 105 return 0; 118 106 } 119 107 … … 121 109 argc--, argv++; 122 110 if (argc < 1) { 123 warnx("option requires an argument -- s");124 usage();111 sh_warnx(psh, "option requires an argument -- s"); 112 return usage(psh); 125 113 } 126 114 if (strcmp(*argv, "0")) { 127 115 if ((numsig = signame_to_signum(*argv)) < 0) 128 nosig(*argv);116 return nosig(psh, *argv); 129 117 } else 130 118 numsig = 0; … … 134 122 if (isalpha((unsigned char)**argv)) { 135 123 if ((numsig = signame_to_signum(*argv)) < 0) 136 nosig(*argv);124 return nosig(psh, *argv); 137 125 } else if (isdigit((unsigned char)**argv)) { 138 126 numsig = strtol(*argv, &ep, 10); 139 127 if (!*argv || *ep) { 140 errx(EXIT_FAILURE, "illegal signal number: %s",128 sh_errx(psh, EXIT_FAILURE, "illegal signal number: %s", 141 129 *argv); 142 130 /* NOTREACHED */ 143 131 } 144 132 if (numsig < 0 || numsig >= NSIG) 145 nosig(*argv);133 return nosig(psh, *argv); 146 134 } else 147 nosig(*argv);135 return nosig(psh, *argv); 148 136 argc--, argv++; 149 137 } 150 138 151 139 if (argc == 0) 152 usage();140 return usage(psh); 153 141 154 142 for (errors = 0; argc; argc--, argv++) { 155 #ifdef SHELL156 143 if (*argv[0] == '%') { 157 144 pid = getjobpgrp(psh, *argv); 158 145 if (pid == 0) { 159 warnx("illegal job id: %s", *argv);146 sh_warnx(psh, "illegal job id: %s", *argv); 160 147 errors = 1; 161 148 continue; 162 149 } 163 } else 164 #endif 165 { 150 } else { 166 151 pid = strtol(*argv, &ep, 10); 167 152 if (!**argv || *ep) { 168 warnx("illegal process id: %s", *argv);153 sh_warnx(psh, "illegal process id: %s", *argv); 169 154 errors = 1; 170 155 continue; 171 156 } 172 157 } 173 if ( kill(pid, numsig) == -1) {174 warn("%s", *argv);158 if (sh_kill(psh, pid, numsig) == -1) { 159 sh_warn(psh, "%s", *argv); 175 160 errors = 1; 176 161 } 177 #ifdef SHELL178 162 /* Wakeup the process if it was suspended, so it can 179 163 exit without an explicit 'fg'. */ 180 164 if (numsig == SIGTERM || numsig == SIGHUP) 181 kill(pid, SIGCONT);182 #endif 183 } 184 185 exit(errors);186 /* NOTREACHED */165 sh_kill(psh, pid, SIGCONT); 166 } 167 168 //sh_exit(psh, errors); 169 ///* NOTREACHED */ 170 return errors; 187 171 } 188 172 … … 191 175 { 192 176 int n; 193 #ifndef HAVE_SYS_SIGNAME194 init_sys_signame();195 #endif196 177 if (strncasecmp(sig, "sig", 3) == 0) 197 178 sig += 3; … … 203 184 } 204 185 186 static int 187 nosig(shinstance *psh, char *name) 188 { 189 sh_warnx(psh, "unknown signal %s; valid signals:", name); 190 printsignals(psh, psh->out2); 191 //sh_exit(psh, 1); 192 ///* NOTREACHED */ 193 return 1; 194 } 195 205 196 static void 206 nosig(char *name) 207 { 208 209 warnx("unknown signal %s; valid signals:", name); 210 printsignals(stderr); 211 exit(1); 212 /* NOTREACHED */ 213 } 214 215 static void 216 printsignals(FILE *fp) 197 printsignals(shinstance *psh, struct output *out) 217 198 { 218 199 int sig; 219 int len, nl;200 size_t len, nl; 220 201 const char *name; 221 202 int termwidth = 80; 222 203 223 #ifdef TIOCGWINSZ 224 if (isatty(fileno(fp))) { 225 struct winsize win; 226 if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0) 204 if (shfile_isatty(&psh->fdtab, out->fd)) { 205 sh_winsize win; 206 if (shfile_ioctl(&psh->fdtab, out->fd, TIOCGWINSZ, &win) == 0 && win.ws_col > 0) 227 207 termwidth = win.ws_col; 228 208 } 229 #else230 #ifndef _MSC_VER231 #warning TIOCGWINSZ is not present.232 #endif233 #endif234 #ifndef HAVE_SYS_SIGNAME235 init_sys_signame();236 #endif237 209 238 210 for (len = 0, sig = 1; sig < NSIG; sig++) { … … 241 213 242 214 if (len + nl >= termwidth) { 243 fprintf(fp, "\n");215 outfmt(out, "\n"); 244 216 len = 0; 245 } else 246 if (len != 0) 247 fprintf(fp, " "); 217 } else if (len != 0) 218 outfmt(out, " "); 248 219 len += nl; 249 fprintf(fp, "%s", name);220 outfmt(out, "%s", name); 250 221 } 251 222 if (len != 0) 252 fprintf(fp, "\n");253 } 254 255 static void256 usage( void)257 { 258 259 fprintf(stderr,"usage: %s [-s signal_name] pid ...\n"223 outfmt(out, "\n"); 224 } 225 226 static int 227 usage(shinstance *psh) 228 { 229 outfmt(psh->out2, 230 "usage: %s [-s signal_name] pid ...\n" 260 231 " %s -l [exit_status]\n" 261 232 " %s -signal_name pid ...\n" 262 233 " %s -signal_number pid ...\n", 263 getprogname(), getprogname(), getprogname(), getprogname()); 264 exit(1); 265 /* NOTREACHED */ 266 } 234 psh->commandname, psh->commandname, psh->commandname, psh->commandname); 235 //sh_exit(psh, 1); 236 ///* NOTREACHED */ 237 return 1; 238 }
Note:
See TracChangeset
for help on using the changeset viewer.