/************************************************************************* * * $Id: triostr.c,v 1.19 2003/03/01 15:34:02 breese Exp $ * * Copyright (C) 2001 Bjorn Reese and Daniel Stenberg. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER. * ************************************************************************/ /************************************************************************* * Include files */ #include #include #include #include #include #include "triodef.h" #include "triostr.h" /************************************************************************* * Definitions */ #if !defined(TRIO_STRING_PUBLIC) # define TRIO_STRING_PUBLIC TRIO_PUBLIC #endif #if !defined(TRIO_STRING_PRIVATE) # define TRIO_STRING_PRIVATE TRIO_PRIVATE #endif #if !defined(NULL) # define NULL 0 #endif #if !defined(NIL) # define NIL ((char)0) #endif #if !defined(FALSE) # define FALSE (1 == 0) # define TRUE (! FALSE) #endif #if !defined(BOOLEAN_T) # define BOOLEAN_T int #endif #if defined(TRIO_COMPILER_SUPPORTS_C99) # define USE_STRTOD # define USE_STRTOF #elif defined(TRIO_COMPILER_MSVC) # define USE_STRTOD #endif #if defined(TRIO_PLATFORM_UNIX) # define USE_STRCASECMP # define USE_STRNCASECMP # if defined(TRIO_PLATFORM_SUNOS) # define USE_SYS_ERRLIST # else # define USE_STRERROR # endif # if defined(TRIO_PLATFORM_QNX) # define strcasecmp(x,y) stricmp(x,y) # define strncasecmp(x,y,n) strnicmp(x,y,n) # endif #elif defined(TRIO_PLATFORM_WIN32) # define USE_STRCASECMP # define strcasecmp(x,y) strcmpi(x,y) #endif #if !(defined(TRIO_PLATFORM_SUNOS)) # define USE_TOLOWER # define USE_TOUPPER #endif /************************************************************************* * Structures */ struct _trio_string_t { char *content; size_t length; size_t allocated; }; /************************************************************************* * Constants */ #if !defined(TRIO_MINIMAL) static TRIO_CONST char rcsid[] = "@(#)$Id: triostr.c,v 1.19 2003/03/01 15:34:02 breese Exp $"; #endif /************************************************************************* * Static String Functions */ #if defined(TRIO_DOCUMENTATION) # include "doc/doc_static.h" #endif /** @addtogroup StaticStrings @{ */ /** Create new string. @param size Size of new string. @return Pointer to string, or NULL if allocation failed. */ TRIO_STRING_PUBLIC char * trio_create TRIO_ARGS1((size), size_t size) { return (char *)TRIO_MALLOC(size); } /** Destroy string. @param string String to be freed. */ TRIO_STRING_PUBLIC void trio_destroy TRIO_ARGS1((string), char *string) { if (string) { TRIO_FREE(string); } } /** Count the number of characters in a string. @param string String to measure. @return Number of characters in @string. */ TRIO_STRING_PUBLIC size_t trio_length TRIO_ARGS1((string), TRIO_CONST char *string) { return strlen(string); } #if !defined(TRIO_MINIMAL) /** Append @p source at the end of @p target. @param target Target string. @param source Source string. @return Boolean value indicating success or failure. @pre @p target must point to a memory chunk with sufficient room to contain the @p target string and @p source string. @pre No boundary checking is performed, so insufficient memory will result in a buffer overrun. @post @p target will be zero terminated. */ TRIO_STRING_PUBLIC int trio_append TRIO_ARGS2((target, source), char *target, TRIO_CONST char *source) { assert(target); assert(source); return (strcat(target, source) != NULL); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /** Append at most @p max characters from @p source to @p target. @param target Target string. @param max Maximum number of characters to append. @param source Source string. @return Boolean value indicating success or failure. @pre @p target must point to a memory chuck with sufficient room to contain the @p target string and the @p source string (at most @p max characters). @pre No boundary checking is performed, so insufficient memory will result in a buffer overrun. @post @p target will be zero terminated. */ TRIO_STRING_PUBLIC int trio_append_max TRIO_ARGS3((target, max, source), char *target, size_t max, TRIO_CONST char *source) { size_t length; assert(target); assert(source); length = trio_length(target); if (max > length) { strncat(target, source, max - length - 1); } return TRUE; } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /** Determine if a string contains a substring. @param string String to be searched. @param substring String to be found. @return Boolean value indicating success or failure. */ TRIO_STRING_PUBLIC int trio_contains TRIO_ARGS2((string, substring), TRIO_CONST char *string, TRIO_CONST char *substring) { assert(string); assert(substring); return (0 != strstr(string, substring)); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /** Copy @p source to @p target. @param target Target string. @param source Source string. @return Boolean value indicating success or failure. @pre @p target must point to a memory chunk with sufficient room to contain the @p source string. @pre No boundary checking is performed, so insufficient memory will result in a buffer overrun. @post @p target will be zero terminated. */ TRIO_STRING_PUBLIC int trio_copy TRIO_ARGS2((target, source), char *target, TRIO_CONST char *source) { assert(target); assert(source); (void)strcpy(target, source); return TRUE; } #endif /* !defined(TRIO_MINIMAL) */ /** Copy at most @p max characters from @p source to @p target. @param target Target string. @param max Maximum number of characters to append. @param source Source string. @return Boolean value indicating success or failure. @pre @p target must point to a memory chunk with sufficient room to contain the @p source string (at most @p max characters). @pre No boundary checking is performed, so insufficient memory will result in a buffer overrun. @post @p target will be zero terminated. */ TRIO_STRING_PUBLIC int trio_copy_max TRIO_ARGS3((target, max, source), char *target, size_t max, TRIO_CONST char *source) { assert(target); assert(source); assert(max > 0); /* Includes != 0 */ (void)strncpy(target, source, max - 1); target[max - 1] = (char)0; return TRUE; } /* * TrioDuplicateMax */ TRIO_STRING_PRIVATE char * TrioDuplicateMax TRIO_ARGS2((source, size), TRIO_CONST char *source, size_t size) { char *target; assert(source); /* Make room f
#!/bin/sh

MSG() {
cat << EOF

# Fetchsetup is a shell script for creating a .fetchmailrc file, that will be
# used by the program "fetchmail" to connect to your mail domain and retrieve
# your mail.
# This script is linux specific, so it may not work on another system.
# Kent Robotti <krobot@erols.com> (3-31-99)

EOF
}

if [ "$(id -ur)" != "0" ]; then
    echo >&2 "$0: You need to be root [found $(id -un)] to run this script."
    echo >&2 "You could login as root"
    echo >&2 "You could also try one of these: # sudo fetchsetup"
    echo >&2 "                                 # su root -c fetchsetup"
    exit 1
fi

MSG
echo -n "Continue? (Y/n) : "
read ans
if [ "$ans" = "n" -o "$ans" = "N" ]; then
    echo "Cancelled."
    exit 0
fi

stty erase "^?" 2>/dev/null

echo
echo "Remote mail site?: pop.boo.com   <Your service providers mail domain name>"
echo -n "Remote mail site?: "
read SITE
echo
echo "Protocol?: pop3   <My service provider uses the 'pop3' mail protocol>"
echo "Protocol?: auto   <If not sure put: auto>"
echo "Choices: apop auto etrn imap imap-gss imap-k4 kpop pop2 pop3 rpop sdps"
echo -n "Protocol?: "
read PROTO
echo
echo "Remote username?: jerry   <My username or login is jerry>"
echo -n "Remote username?: "
read USR
echo
echo "Remote password?: ?       <What's the password for?: $USR>"
echo -n "Remote password?: "
read PASS

echo
echo -n "Create $HOME/.fetchmailrc file? (Y/n) : "
read ans
if [ "$ans" = "n" -o "$ans" = "N" ]; then
    echo
    echo "Fetchsetup cancelled."
    echo
    exit 0
fi

echo 'poll "'$SITE'"' > $HOME/.fetchmailrc
echo "protocol $PROTO" >> $HOME/.fetchmailrc
echo 'username "'$USR'"' >> $HOME/.fetchmailrc
echo 'password "'$PASS'"' >> $HOME/.fetchmailrc

PROCMAIL=`type -all procmail | sed -n "1 p" | cut -d' ' -f3`
SENDMAIL=`type -all sendmail | sed -n "1 p" | cut -d' ' -f3`

if [ ! "$PROCMAIL" = "" ]; then
    echo 'mda "'$PROCMAIL -d %s'"' >> $HOME/.fetchmailrc
    MDA="1"
elif [ ! "$SENDMAIL" = "" ]; then
    echo 'mda "'$SENDMAIL %s'"' >> $HOME/.fetchmailrc
    MDA="2"
else
    MDA="3"
fi

echo >> $HOME/.fetchmailrc
echo
echo "This is your $HOME/.fetchmailrc file."

chmod 600 $HOME/.fetchmailrc

echo
cat $HOME/.fetchmailrc

if [ ! "$MAIL" = "" ]; then
    echo "Fetchmail will retrieve your mail and put it in:"
    echo "$MAIL"
    if [ ! -f "$MAIL" ]; then
	touch $MAIL 2>/dev/null
	chmod 600 $MAIL 2>/dev/null
    fi
fi

echo
if [ "$MDA" = "1" ]; then
    echo "I put that (m)ail (d)elivery (a)gent in .fetchmailrc"
    echo "because i found it on your system, this doesn't mean"
    echo "it's correct or the one you want to use."
    echo
    echo "The first time you run fetchmail, you should run it"
    echo "this way: # fetchmail -k"
    echo
elif [ "$MDA" = "2" ]; then
    echo "You seem to have sendmail, sendmail will be used"
    echo "as the (m)ail (d)elivery (a)gent for fetchmail."
    echo
    echo "WARNING! There's no way to know if sendmail is set up"
    echo "properly for local mail delivery, so the first time you"
    echo "run fetchmail run it this way: # fetchmail -k"
    echo
    echo "If the mail that fetchmail retrieves is not put in your mailbox,"
    echo "you'll know that sendmail is not set up properly for the delivery"
    echo "of local mail."
    echo
elif [ "$MDA" = "3" ]; then
    echo "I Don't know what (m)ail (d)elivery (a)gent you're going to use."
    echo "You need a <mda> to deliver the mail to you, after <fetchmail> retrieves it."
    echo
    echo "Put the <mda> in your .fetchmailrc file, like below."
    echo "password $PASS"
    echo mda '"/usr/bin/procmail -d %s"'
    echo mda '"/usr/sbin/sendmail %s"'
    echo
    echo "The first time you run fetchmail, you should run it"
    echo "this way: # fetchmail -k"
    echo
fi
sible for deallocating @p buffer yourself. */ TRIO_STRING_PUBLIC void trio_xstring_set TRIO_ARGS2((self, buffer), trio_string_t *self, char *buffer) { assert(self); trio_destroy(self->content); self->content = trio_duplicate(buffer); } #endif /* !defined(TRIO_MINIMAL) */ /* * trio_string_size */ TRIO_STRING_PUBLIC int trio_string_size TRIO_ARGS1((self), trio_string_t *self) { assert(self); return self->allocated; } /* * trio_string_terminate */ TRIO_STRING_PUBLIC void trio_string_terminate TRIO_ARGS1((self), trio_string_t *self) { trio_xstring_append_char(self, 0); } #if !defined(TRIO_MINIMAL) /** Append the second string to the first. @param self Dynamic string to be modified. @param other Dynamic string to copy from. @return Boolean value indicating success or failure. */ TRIO_STRING_PUBLIC int trio_string_append TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { size_t length; assert(self); assert(other); length = self->length + other->length; if (!TrioStringGrowTo(self, length)) goto error; trio_copy(&self->content[self->length], other->content); self->length = length; return TRUE; error: return FALSE; } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_append */ TRIO_STRING_PUBLIC int trio_xstring_append TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { size_t length; assert(self); assert(other); length = self->length + trio_length(other); if (!TrioStringGrowTo(self, length)) goto error; trio_copy(&self->content[self->length], other); self->length = length; return TRUE; error: return FALSE; } #endif /* !defined(TRIO_MINIMAL) */ /* * trio_xstring_append_char */ TRIO_STRING_PUBLIC int trio_xstring_append_char TRIO_ARGS2((self, character), trio_string_t *self, char character) { assert(self); if ((int)self->length >= trio_string_size(self)) { if (!TrioStringGrow(self, 0)) goto error; } self->content[self->length] = character; self->length++; return TRUE; error: return FALSE; } #if !defined(TRIO_MINIMAL) /** Search for the first occurrence of second parameter in the first. @param self Dynamic string to be modified. @param other Dynamic string to copy from. @return Boolean value indicating success or failure. */ TRIO_STRING_PUBLIC int trio_string_contains TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { assert(self); assert(other); return trio_contains(self->content, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_contains */ TRIO_STRING_PUBLIC int trio_xstring_contains TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { assert(self); assert(other); return trio_contains(self->content, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_copy */ TRIO_STRING_PUBLIC int trio_string_copy TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { assert(self); assert(other); self->length = 0; return trio_string_append(self, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_copy */ TRIO_STRING_PUBLIC int trio_xstring_copy TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { assert(self); assert(other); self->length = 0; return trio_xstring_append(self, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_duplicate */ TRIO_STRING_PUBLIC trio_string_t * trio_string_duplicate TRIO_ARGS1((other), trio_string_t *other) { trio_string_t *self; assert(other); self = TrioStringAlloc(); if (self) { self->content = TrioDuplicateMax(other->content, other->length); if (self->content) { self->length = other->length; self->allocated = self->length + 1; } else { self->length = self->allocated = 0; } } return self; } #endif /* !defined(TRIO_MINIMAL) */ /* * trio_xstring_duplicate */ TRIO_STRING_PUBLIC trio_string_t * trio_xstring_duplicate TRIO_ARGS1((other), TRIO_CONST char *other) { trio_string_t *self; assert(other); self = TrioStringAlloc(); if (self) { self->content = TrioDuplicateMax(other, trio_length(other)); if (self->content) { self->length = trio_length(self->content); self->allocated = self->length + 1; } else { self->length = self->allocated = 0; } } return self; } #if !defined(TRIO_MINIMAL) /* * trio_string_equal */ TRIO_STRING_PUBLIC int trio_string_equal TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { assert(self); assert(other); return trio_equal(self->content, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_equal */ TRIO_STRING_PUBLIC int trio_xstring_equal TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { assert(self); assert(other); return trio_equal(self->content, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_equal_max */ TRIO_STRING_PUBLIC int trio_string_equal_max TRIO_ARGS3((self, max, other), trio_string_t *self, size_t max, trio_string_t *other) { assert(self); assert(other); return trio_equal_max(self->content, max, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_equal_max */ TRIO_STRING_PUBLIC int trio_xstring_equal_max TRIO_ARGS3((self, max, other), trio_string_t *self, size_t max, TRIO_CONST char *other) { assert(self); assert(other); return trio_equal_max(self->content, max, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_equal_case */ TRIO_STRING_PUBLIC int trio_string_equal_case TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { assert(self); assert(other); return trio_equal_case(self->content, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_equal_case */ TRIO_STRING_PUBLIC int trio_xstring_equal_case TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { assert(self); assert(other); return trio_equal_case(self->content, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_equal_case_max */ TRIO_STRING_PUBLIC int trio_string_equal_case_max TRIO_ARGS3((self, max, other), trio_string_t *self, size_t max, trio_string_t *other) { assert(self); assert(other); return trio_equal_case_max(self->content, max, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_equal_case_max */ TRIO_STRING_PUBLIC int trio_xstring_equal_case_max TRIO_ARGS3((self, max, other), trio_string_t *self, size_t max, TRIO_CONST char *other) { assert(self); assert(other); return trio_equal_case_max(self->content, max, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_format_data_max */ TRIO_STRING_PUBLIC size_t trio_string_format_date_max TRIO_ARGS4((self, max, format, datetime), trio_string_t *self, size_t max, TRIO_CONST char *format, TRIO_CONST struct tm *datetime) { assert(self); return trio_format_date_max(self->content, max, format, datetime); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_index */ TRIO_STRING_PUBLIC char * trio_string_index TRIO_ARGS2((self, character), trio_string_t *self, int character) { assert(self); return trio_index(self->content, character); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_index_last */ TRIO_STRING_PUBLIC char * trio_string_index_last TRIO_ARGS2((self, character), trio_string_t *self, int character) { assert(self); return trio_index_last(self->content, character); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_length */ TRIO_STRING_PUBLIC int trio_string_length TRIO_ARGS1((self), trio_string_t *self) { assert(self); if (self->length == 0) { self->length = trio_length(self->content); } return self->length; } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_lower */ TRIO_STRING_PUBLIC int trio_string_lower TRIO_ARGS1((self), trio_string_t *self) { assert(self); return trio_lower(self->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_match */ TRIO_STRING_PUBLIC int trio_string_match TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { assert(self); assert(other); return trio_match(self->content, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_match */ TRIO_STRING_PUBLIC int trio_xstring_match TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { assert(self); assert(other); return trio_match(self->content, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_match_case */ TRIO_STRING_PUBLIC int trio_string_match_case TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { assert(self); assert(other); return trio_match_case(self->content, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_match_case */ TRIO_STRING_PUBLIC int trio_xstring_match_case TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { assert(self); assert(other); return trio_match_case(self->content, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_substring */ TRIO_STRING_PUBLIC char * trio_string_substring TRIO_ARGS2((self, other), trio_string_t *self, trio_string_t *other) { assert(self); assert(other); return trio_substring(self->content, other->content); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_xstring_substring */ TRIO_STRING_PUBLIC char * trio_xstring_substring TRIO_ARGS2((self, other), trio_string_t *self, TRIO_CONST char *other) { assert(self); assert(other); return trio_substring(self->content, other); } #endif /* !defined(TRIO_MINIMAL) */ #if !defined(TRIO_MINIMAL) /* * trio_string_upper */ TRIO_STRING_PUBLIC int trio_string_upper TRIO_ARGS1((self), trio_string_t *self) { assert(self); return trio_upper(self->content); } #endif /* !defined(TRIO_MINIMAL) */ /** @} End of DynamicStrings */