"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "petidomo-4.0b6/address-db.c" of archive petidomo-4.0b6.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 /*
    2    $Source: /e/ossp/cvs/ossp-pkg/petidomo/address-db.c,v $
    3    $Revision: 1.3 $
    4 
    5    Copyright (C) 2000 by Peter Simons <simons@computer.org>
    6 
    7    This file is part of Petidomo.
    8 
    9    Petidomo is free software; you can redistribute it and/or modify
   10    it under the terms of the GNU General Public License as published by
   11    the Free Software Foundation; either version 2, or (at your option)
   12    any later version.
   13 
   14    Petidomo is distributed in the hope that it will be useful, but
   15    WITHOUT ANY WARRANTY; without even the implied warranty of
   16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   17    General Public License for more details.
   18 */
   19 
   20 #include <string.h>
   21 #include <ctype.h>
   22 #include <errno.h>
   23 #include "libtext/text.h"
   24 #include "petidomo.h"
   25 
   26 /*
   27  * rc ==  0: Address is not on list.
   28  * rc == -1: Error occured while reading the file.
   29  * rc ==  1: Address is on list.
   30  */
   31 int is_address_on_list(const char* file, const char* address)
   32     {
   33     char *         list;
   34     char *         p;
   35     unsigned int   len;
   36     int            rc;
   37 
   38     list = loadfile(file);
   39     if (list == NULL)
   40 	{
   41 	if (errno == ENOENT)
   42 	    return 0;
   43 	else
   44 	    return -1;
   45 	}
   46 
   47     for (len = strlen(address), p = list; *p != '\0'; p = text_find_next_line(p))
   48 	{
   49 	if (strncasecmp(p, address, len) == 0 &&
   50 	    (p == list || p[-1] == '\n') &&
   51 	    (isspace((int)p[len]) || p[len] == '\0'))
   52 	    {
   53 	    break;
   54 	    }
   55 	}
   56 
   57     rc = ((*p != '\0') ? 1 : 0);
   58     free(list);
   59     return rc;
   60     }
   61 
   62 int add_address(const char* file, const char* address)
   63     {
   64     FILE* fh;
   65     int rc = is_address_on_list(file, address);
   66     if (rc != 0)
   67 	return rc;
   68 
   69     fh = fopen(file, "a");
   70     if (fh == NULL)
   71 	{
   72 	syslog(LOG_ERR, "Failed to open file \"%s\" for writing: %s", file, strerror(errno));
   73 	return -1;
   74 	}
   75     fprintf(fh, "%s\n", address);
   76     fclose(fh);
   77     return 0;
   78     }