"Fossies" - the Fresh Open Source Software Archive

Member "msmtp-1.8.5/src/tools.h" (11 Jan 2019, 6367 Bytes) of package /linux/privat/msmtp-1.8.5.tar.xz:


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 "tools.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.8.1_vs_1.8.2.

    1 /*
    2  * tools.h
    3  *
    4  * This file is part of msmtp, an SMTP client, and of mpop, a POP3 client.
    5  *
    6  * Copyright (C) 2004, 2005, 2006, 2007, 2011, 2014, 2018, 2019
    7  * Martin Lambers <marlam@marlam.de>
    8  *
    9  *   This program 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 3 of the License, or
   12  *   (at your option) any later version.
   13  *
   14  *   This program is distributed in the hope that it will be useful,
   15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17  *   GNU General Public License for more details.
   18  *
   19  *   You should have received a copy of the GNU General Public License
   20  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   21  */
   22 
   23 #ifndef TOOLS_H
   24 #define TOOLS_H
   25 
   26 #include <time.h> /* time_t */
   27 
   28 #ifdef HAVE_SYSEXITS_H
   29 # include <sysexits.h>
   30 #else
   31 /* exit() exit codes for some BSD system programs.
   32    Copyright (C) 2003, 2006-2011 Free Software Foundation, Inc.
   33    Written by Simon Josefsson based on sysexits(3) man page */
   34 # define EX_OK 0 /* same value as EXIT_SUCCESS */
   35 # define EX_USAGE 64
   36 # define EX_DATAERR 65
   37 # define EX_NOINPUT 66
   38 # define EX_NOUSER 67
   39 # define EX_NOHOST 68
   40 # define EX_UNAVAILABLE 69
   41 # define EX_SOFTWARE 70
   42 # define EX_OSERR 71
   43 # define EX_OSFILE 72
   44 # define EX_CANTCREAT 73
   45 # define EX_IOERR 74
   46 # define EX_TEMPFAIL 75
   47 # define EX_PROTOCOL 76
   48 # define EX_NOPERM 77
   49 # define EX_CONFIG 78
   50 #endif
   51 
   52 #ifndef HAVE_FSEEKO
   53 # ifdef HAVE_FSEEKO64
   54 #  define fseeko(s,o,w) fseeko64(s,o,w)
   55 # else
   56 #  define fseeko(s,o,w) fseek(s,o,w)
   57 # endif
   58 #endif
   59 
   60 /* The path separator character */
   61 #ifdef W32_NATIVE
   62 # define PATH_SEP '\\'
   63 #else
   64 # define PATH_SEP '/'
   65 #endif
   66 
   67 
   68 /*
   69  * exitcode_to_string()
   70  *
   71  * Return the name of a sysexits.h exitcode
   72  */
   73 const char *exitcode_to_string(int exitcode);
   74 
   75 /*
   76  * link() - only for systems that lack it
   77  */
   78 #ifndef HAVE_LINK
   79 int link(const char *path1, const char *path2);
   80 #endif
   81 
   82 /*
   83  * getpass() - only for systems that lack it
   84  */
   85 #ifndef HAVE_GETPASS
   86 char *getpass(const char *prompt);
   87 #endif
   88 
   89 /*
   90  * get_prgname()
   91  *
   92  * Get the program name from an argv[0]-like string.
   93  * Returns a pointer to a static buffer.
   94  */
   95 const char *get_prgname(const char *argv0);
   96 
   97 /*
   98  * get_sysconfdir()
   99  *
  100  * Get the system configuration directory (or something similar, depending
  101  * on the OS). Returns a pointer to an allocated string.
  102  * Cannot fail because it uses safe defaults as fallback.
  103  */
  104 char *get_sysconfdir(void);
  105 
  106 /*
  107  * get_username()
  108  *
  109  * Get the name (login name) of the current user. The returned string is
  110  * allocated.
  111  * The returned string may come from a environment variable and may contain
  112  * all sorts of rubbish!
  113  */
  114 char *get_username(void);
  115 
  116 /*
  117  * get_userconfig()
  118  *
  119  * Get the path of the user config. The returned string is allocated.
  120  * On windows like systems it will return ~/USERCONFFILE, in unix like systems
  121  * it will return the first found file at the following locations:
  122  * - ~/USERCONFFILE
  123  * - $XDG_CONFIG_DIR/PACKAGE_NAME/config
  124  * - ~/.config/PACKAGE_NAME/config
  125  */
  126 char *get_userconfig(const char *userconfigfile);
  127 
  128 /*
  129  * get_homedir()
  130  *
  131  * Get the users home directory (or something similar, depending on the OS).
  132  * Returns a pointer to an allocated string.
  133  * Cannot fail because it uses safe defaults as fallback.
  134  */
  135 char *get_homedir(void);
  136 
  137 /*
  138  * get_filename()
  139  *
  140  * Get the name of file from two components:
  141  * 1) The directory containing the file.
  142  * 2) The name of the file.
  143  * The returned string is allocated.
  144  */
  145 char *get_filename(const char *directory, const char *name);
  146 
  147 /*
  148  * expand_tilde()
  149  *
  150  * Return a new filename in an allocated string, which differs from 'filename'
  151  * in the following way:
  152  * If the first character of 'filename' is '~', it will be replaced by
  153  * the user's home directory.
  154  * If the first character of 'filename' is not '~', the returned string
  155  * will simply be a copy of 'filename'.
  156  */
  157 char *expand_tilde(const char *filename);
  158 
  159 /*
  160  * check_secure()
  161  *
  162  * Checks whether the given file
  163  * 1) is owned by the current user
  164  * 2) has permissions no more than 0600
  165  * The return value is
  166  * 0 if both conditions are met
  167  * 1 if condition 1) is not met
  168  * 2 if condition 2) is not met
  169  * 3 if an error occurred (errno will be set in this case)
  170  */
  171 int check_secure(const char *pathname);
  172 
  173 /*
  174  * lock_file()
  175  *
  176  * Locks a file for reading (if lock_type is TOOLS_LOCK_READ) or writing (if
  177  * lock_type is TOOLS_LOCK_WRITE). Returns 0 in case of success, 1 if the file
  178  * could not be locked before the given timeout (in seconds) because some other
  179  * process holds a lock on the file, and 2 if the file could not be locked due
  180  * to some other error. If 1 or 2 is returned, errno will be set.
  181  */
  182 #define TOOLS_LOCK_READ 0
  183 #define TOOLS_LOCK_WRITE 1
  184 int lock_file(FILE *f, int lock_type, int timeout);
  185 
  186 /*
  187  * string_replace()
  188  *
  189  * Replace all occurrences of 's' in the string 'str' with 'r'.
  190  * The string 'str' must be an allocated string. A pointer to the expanded
  191  * string is returned.
  192  */
  193 char *string_replace(char *str, const char *s, const char *r);
  194 
  195 /*
  196  * sanitize_string()
  197  *
  198  * Replaces all control characters in the string with a question mark
  199  */
  200 char *sanitize_string(char *str);
  201 
  202 /*
  203  * print_fingerprint()
  204  *
  205  * Prints a fingerprint of the given length in bytes in hexadecimal form into a
  206  * buffer. Each input byte is transformed to three output characters (the last
  207  * one is '\0').
  208  */
  209 void print_fingerprint(char *s, const unsigned char *fingerprint, size_t len);
  210 
  211 /*
  212  * print_time_rfc2822()
  213  *
  214  * Print the given time stamp in RFC2822 format into the given buffer.
  215  */
  216 void print_time_rfc2822(time_t t, char rfc2822_timestamp[32]);
  217 
  218 /*
  219  * split_mail_address()
  220  *
  221  * Splits a mail address into a local part (before the last '@') and a domain
  222  * part (after the last '@'). The returned domain_part pointer may be NULL if
  223  * there is no '@' in the address, and both local and domain part may be empty.
  224  */
  225 void split_mail_address(const char *address, char **local_part, char **domain_part);
  226 
  227 /*
  228  * check_hostname_matches_domain()
  229  *
  230  * Checks whether the given host name is within the given domain.
  231  */
  232 int check_hostname_matches_domain(const char *hostname, const char *domain);
  233 
  234 #endif