"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/str2.c" (20 Jan 1999, 3100 Bytes) of package /linux/misc/old/pidentd-3.0.19.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 "str2.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** str2.c - String to Foo conversion routines.
    3 **
    4 ** Copyright (c) 1997 Peter Eriksson <pen@lysator.liu.se>
    5 **
    6 ** This program is free software; you can redistribute it and/or
    7 ** modify it as you wish - as long as you don't claim that you wrote
    8 ** it.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   13 */
   14 
   15 #include "config.h"
   16 
   17 #include <stdio.h>
   18 #include <stdlib.h>
   19 #include <ctype.h>
   20 #include <string.h>
   21 
   22 #include <pwd.h>
   23 
   24 #include <grp.h>
   25 #include <netdb.h>
   26 
   27 #include "pidentd.h"
   28 
   29 
   30 int
   31 is_int(const char *p)
   32 {
   33     while (isspace((unsigned char) *p))
   34     ++p;
   35        
   36     if (*p == '-')
   37     ++p;
   38 
   39     while (isdigit((unsigned char) *p))
   40     ++p;
   41 
   42     while (isspace((unsigned char) *p))
   43     ++p;
   44 
   45     return (*p == '\0');
   46 }
   47 
   48 
   49 int
   50 str2int(const char *buf, int *out)
   51 {
   52     if (!is_int(buf))
   53     return -1;
   54     
   55     if (sscanf(buf, " %d ", out) != 1)
   56     return -1;
   57 
   58     return 0;
   59 }
   60 
   61 int
   62 str2str(char *buf, char **out)
   63 {
   64     int sep;
   65     char *start, *cp;
   66 
   67     
   68     while (isspace((unsigned char) *buf))
   69     ++buf;
   70 
   71     switch (*buf)
   72     {
   73       case '\0':
   74     *out = NULL;
   75     return 0;
   76 
   77       case '\'':
   78       case '"':
   79     sep = *buf;
   80     start = ++buf;
   81     while (*buf != '\0' && *buf != sep)
   82         ++buf;
   83     if (*buf == '\0')
   84         return -1;
   85     *buf = '\0';
   86     *out = s_strdup(start);
   87     return 0;
   88 
   89       default:
   90     cp = buf;
   91     while (*cp != '\0')
   92         ++cp;
   93     --cp;
   94     while (cp > buf && isspace((unsigned char) *cp))
   95         --cp;
   96     *++cp = '\0';
   97 
   98     *out = s_strdup(buf);
   99     return 0;
  100     }
  101 }
  102 
  103 
  104 int
  105 str2bool(const char *buf,
  106      int *out)
  107 {
  108     while (isspace((unsigned char) *buf))
  109     ++buf;
  110 
  111     if (strcasecmp(buf, "true") == 0 ||
  112     strcasecmp(buf, "on") == 0 ||
  113     strcasecmp(buf, "enable") == 0 ||
  114     strcasecmp(buf, "enabled") == 0 ||
  115     strcasecmp(buf, "yes") == 0)
  116     {
  117     return (*out = 1);
  118     }
  119     
  120     if (strcasecmp(buf, "false") == 0 ||
  121     strcasecmp(buf, "off") == 0 ||
  122     strcasecmp(buf, "disable") == 0 ||
  123     strcasecmp(buf, "disabled") == 0 ||
  124     strcasecmp(buf, "no") == 0)
  125     {
  126     return (*out = 0);
  127     }
  128 
  129     return -1;
  130 }
  131 
  132 
  133 
  134 
  135 int
  136 str2port(const char *str, int *out)
  137 {
  138     struct servent *sp;
  139 
  140 
  141     if (is_int(str))
  142     {
  143     *out = atoi(str);
  144     return 0;
  145     }
  146     
  147     sp = getservbyname(str, "tcp");
  148     if (sp == NULL)
  149     return -1;
  150 
  151 
  152     *out = ntohs(sp->s_port);
  153     return 0;
  154 }
  155 
  156 
  157 
  158 int
  159 str2gid(const char *str, gid_t *out)
  160 {
  161     struct group *gp;
  162 
  163     if (is_int(str))
  164     {
  165     *out = atoi(str);
  166     return 0;
  167     }
  168     
  169     gp = getgrnam(str);
  170     if (gp == NULL)
  171     return -1;
  172 
  173     *out = gp->gr_gid;
  174     return 0;
  175 }
  176 
  177 
  178 
  179 int
  180 str2uid(const char *str, uid_t *uid, gid_t *gid)
  181 {
  182     struct passwd pb, *pp;
  183     char buf[1024];
  184 
  185     
  186     if (is_int(str))
  187     {
  188         /* FIXME: Handle overflow?  */
  189     *uid = atol(str);
  190 
  191     pp = NULL;
  192     (void) s_getpwuid_r(*uid, &pb, buf, sizeof(buf), &pp);
  193     if (pp != NULL)
  194         *gid = pp->pw_gid;
  195     
  196     return 0;
  197     }
  198 
  199     pp = NULL;
  200     (void) s_getpwnam_r(str, &pb, buf, sizeof(buf), &pp);
  201     if (pp == NULL)
  202     return -1;
  203 
  204     *uid = pp->pw_uid;
  205     *gid = pp->pw_gid;
  206 
  207     return 0;
  208 }
  209 
  210