"Fossies" - the Fresh Open Source Software Archive

Member "hatools-2.14/signumber.c" (16 Mar 2010, 3310 Bytes) of package /linux/privat/old/hatools-2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "signumber.c" see the Fossies "Dox" file reference documentation.

    1 /*********************************************************************
    2 hatools (c) 2001,2003,2005-2010 by Markus Winand <mws@fatalmind.com>
    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   17 *********************************************************************/
   18 
   19 
   20 #ifdef HAVE_CONFIG_H
   21 #include "config.h"
   22 #endif
   23 
   24 #include <stdio.h>
   25 #include <stdlib.h>
   26 #include <string.h>
   27 #include <strings.h>
   28 #include <ctype.h>
   29 #include "hatimerun.h"
   30 
   31 const char NONE[] = "NONE";
   32 
   33 #ifndef HAVE_SYS_SIGNAME
   34 
   35 #ifdef HATOOLS_SYS_SIGNAME_H
   36 #include "sys_signame.h"
   37 #else
   38 #include "sys_signame_posix.h"
   39 #endif
   40 
   41 
   42 
   43 int signumber(const char *signame) {
   44     int signum = -1;
   45     int i = 0;
   46 
   47     if (! strncasecmp(signame, "SIG", 3)) {
   48         signame += 3;
   49     }
   50 
   51     if (! strncasecmp(signame, NONE, 4)) {
   52         return 0;
   53     }
   54     
   55     while (signum < 0 && sys_signame2[i] != NULL) {
   56         if (! strcasecmp(signame, sys_signame2[i])) {
   57             signum = sys_signumber2[i];
   58         }
   59         i++;
   60     }
   61     return signum;  
   62 }
   63 
   64 #else   /* ifdef HAVE_SYS_SIGNAME */
   65 
   66 #include <signal.h>
   67 int signumber(const char *signame) {
   68     int i;
   69     int signum = -1;
   70     
   71     if (! strncasecmp(signame, "SIG", 3)) {
   72         signame += 3;
   73     }
   74 
   75     if (! strncasecmp(signame, NONE, 4)) {
   76         return 0;
   77     }
   78 
   79     for (i = 0; (i < NSIG) && (signum < 0); i++) {
   80         if (! strcasecmp(signame, sys_signame[i])) {
   81             signum = i;
   82         }
   83     }
   84     return signum;
   85 }
   86 
   87 #endif
   88 
   89 
   90 #ifndef HAVE_SYS_SIGNAME
   91 #define NUMBER_OF_SIGNALS sys_nsigname2
   92 
   93 int getSigIndex(int signal) {
   94     int index = 0;
   95     while (sys_signumber2[index] != signal && sys_signumber2[index] > 0) {
   96         ++index;
   97     }
   98     return sys_signumber2[index] > 0 ? index : 0;
   99 }
  100 
  101 
  102 #else
  103 #define NUMBER_OF_SIGNALS NSIG
  104 #endif
  105 
  106 void get_sig_nameUC(const int signal, char* dest, const int mxlength) {
  107     if (signal == 0) {
  108         strncpy(dest, NONE, mxlength);  
  109     } else if  (signal >= NUMBER_OF_SIGNALS || signal < 0) {
  110         int rv;
  111         
  112         rv = snprintf(dest, mxlength, "%d", signal);
  113         if ((rv < 0 || rv >= mxlength) && (mxlength > 0)) {
  114             dest[0] = 0;
  115         }
  116         return;
  117     } else {
  118 
  119 #ifndef HAVE_SYS_SIGNAME
  120         /* Offset: -1, because sys_signame2 starts with signal "1" (HUP) at index 0 */
  121         const char* const signame = sys_signame2[getSigIndex(signal)];
  122 #else
  123         const char* const signame = sys_signame[signal];
  124 #endif
  125         if (strlen(signame) + 3 < mxlength) {
  126             int i = 0;
  127             strcpy(dest, "SIG");
  128             while (signame[i] != 0) {
  129                 dest[i+3] = toupper(signame[i]);
  130                 ++i;    
  131             }
  132             dest[i+3] = 0;
  133         } else {
  134             /* leave it unchanged */    
  135         }
  136     }
  137 }
  138 
  139 #define MX_LENGTH 64
  140 void siglist() {
  141     int i;
  142     char signameUC[MX_LENGTH+1];
  143     int end = NUMBER_OF_SIGNALS;
  144 
  145     for (i = 1; i < end; i++) {
  146         get_sig_nameUC(i, signameUC, MX_LENGTH);        
  147         fputs(signameUC, stderr);
  148         fputc('\n', stderr);
  149     }
  150     exit (FAIL_EXITCODE[0]);
  151 }
  152