Blame src/misc.h

Packit 8f70b4
/*
Packit 8f70b4
 * lftp - file transfer program
Packit 8f70b4
 *
Packit 8f70b4
 * Copyright (c) 1996-2017 by Alexander V. Lukyanov (lav@yars.free.net)
Packit 8f70b4
 *
Packit 8f70b4
 * This program is free software; you can redistribute it and/or modify
Packit 8f70b4
 * it under the terms of the GNU General Public License as published by
Packit 8f70b4
 * the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
 * (at your option) any later version.
Packit 8f70b4
 *
Packit 8f70b4
 * This program is distributed in the hope that it will be useful,
Packit 8f70b4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
 * GNU General Public License for more details.
Packit 8f70b4
 *
Packit 8f70b4
 * You should have received a copy of the GNU General Public License
Packit 8f70b4
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8f70b4
 */
Packit 8f70b4
Packit 8f70b4
#ifndef MISC_H
Packit 8f70b4
#define MISC_H
Packit 8f70b4
Packit 8f70b4
#include "trio.h"
Packit 8f70b4
#include <sys/types.h>
Packit 8f70b4
#include <sys/time.h>
Packit 8f70b4
#ifdef TIME_WITH_SYS_TIME
Packit 8f70b4
# include <time.h>
Packit 8f70b4
#endif
Packit 8f70b4
#include <stdarg.h>
Packit 8f70b4
Packit 8f70b4
#include "xstring.h"
Packit 8f70b4
Packit 8f70b4
// expands tilde; returns pointer to static data
Packit 8f70b4
const char *expand_home_relative(const char *);
Packit 8f70b4
Packit 8f70b4
// returns ptr to last path element
Packit 8f70b4
const char *basename_ptr(const char *);
Packit 8f70b4
static inline char *basename_ptr(char *f) {
Packit 8f70b4
   return const_cast<char*>(basename_ptr(const_cast<const char *>(f)));
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
// glues file to dir; returns pointer to static storage
Packit 8f70b4
const char *dir_file(const char *dir,const char *file);
Packit 8f70b4
Packit 8f70b4
// glues file to given url; returns pointer to static storage
Packit 8f70b4
const char *url_file(const char *url,const char *file);
Packit 8f70b4
Packit 8f70b4
const char *output_file_name(const char *src,const char *dst,bool dst_local,
Packit 8f70b4
			     const char *dst_base,bool make_dirs);
Packit 8f70b4
Packit 8f70b4
const char *squeeze_file_name(const char *name,int w);
Packit 8f70b4
Packit 8f70b4
// mkdir -p
Packit 8f70b4
int   create_directories(char *);
Packit 8f70b4
Packit 8f70b4
// rm -rf
Packit 8f70b4
void  truncate_file_tree(const char *dir);
Packit 8f70b4
Packit 8f70b4
/* returns file descriptor terminal width; -1 on error, 0 on N/A */
Packit 8f70b4
int fd_width(int fd);
Packit 8f70b4
Packit 8f70b4
/* returns true if current pgrp is the foreground pgrp of controlling tty
Packit 8f70b4
 * or if the fg pgrp is unknown */
Packit 8f70b4
bool in_foreground_pgrp();
Packit 8f70b4
Packit 8f70b4
// returns malloc'ed cwd no matter how long it is
Packit 8f70b4
// returns 0 on error.
Packit 8f70b4
char *xgetcwd();
Packit 8f70b4
// store cwd to specified string
Packit 8f70b4
void xgetcwd_to(xstring& s);
Packit 8f70b4
static inline void xgetcwd_to(xstring_c& s) { s.set_allocated(xgetcwd()); }
Packit 8f70b4
Packit 8f70b4
int percent(off_t offset,off_t size);
Packit 8f70b4
Packit 8f70b4
#define find_char(buf,len,ch) ((const char *)memchr(buf,ch,len))
Packit 8f70b4
Packit 8f70b4
extern const char month_names[][4];
Packit 8f70b4
Packit 8f70b4
int parse_month(const char *);
Packit 8f70b4
int parse_perms(const char *);
Packit 8f70b4
const char *format_perms(int p);
Packit 8f70b4
int parse_year_or_time(const char *year_or_time,int *year,int *hour,int *minute);
Packit 8f70b4
int guess_year(int month,int day,int hour,int minute);
Packit 8f70b4
Packit 8f70b4
time_t mktime_from_utc(const struct tm *);
Packit 8f70b4
time_t mktime_from_tz(struct tm *,const char *tz);
Packit 8f70b4
Packit 8f70b4
bool re_match(const char *line,const char *a,int flags=0);
Packit 8f70b4
Packit 8f70b4
struct subst_t {
Packit 8f70b4
   char from;
Packit 8f70b4
   const char *to;
Packit 8f70b4
};
Packit 8f70b4
Packit 8f70b4
/* Subst changes escape sequences to given strings, also substitutes \nnn
Packit 8f70b4
 * with corresponding character. Returns allocated buffer to be free'd */
Packit 8f70b4
xstring& SubstTo(xstring& buf,const char *txt,const subst_t *s);
Packit 8f70b4
Packit 8f70b4
/* uses gettimeofday if available */
Packit 8f70b4
void xgettimeofday(time_t *sec, int *usec);
Packit 8f70b4
Packit 8f70b4
/* returns malloc'd date */
Packit 8f70b4
char *xstrftime(const char *format, const struct tm *tm);
Packit 8f70b4
Packit 8f70b4
const char *xhuman(long long n);
Packit 8f70b4
Packit 8f70b4
void strip_trailing_slashes(xstring& fn);
Packit 8f70b4
xstring& dirname_modify(xstring& fn);
Packit 8f70b4
xstring& dirname(const char *path);  // returns a tmp
Packit 8f70b4
Packit 8f70b4
/* returns last character of string or \0 if string is empty */
Packit 8f70b4
char last_char(const char *str);
Packit 8f70b4
Packit 8f70b4
int  base64_length (int len);
Packit 8f70b4
void base64_encode (const char *s, char *store, int length);
Packit 8f70b4
Packit 8f70b4
bool temporary_network_error(int e);
Packit 8f70b4
Packit 8f70b4
CDECL const char *get_home();
Packit 8f70b4
CDECL const char *get_lftp_config_dir();
Packit 8f70b4
CDECL const char *get_lftp_data_dir();
Packit 8f70b4
CDECL const char *get_lftp_cache_dir();
Packit 8f70b4
Packit 8f70b4
const char *memrchr(const char *buf,char c,size_t len);
Packit 8f70b4
static inline char *memrchr(char *buf,char c,size_t len) {
Packit 8f70b4
   return const_cast<char*>(memrchr(const_cast<const char*>(buf),c,len));
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
bool is_shell_special(char c);
Packit 8f70b4
const xstring& shell_encode(const char *s,int len);
Packit 8f70b4
static inline const xstring& shell_encode(const char *s) { return shell_encode(s,strlen(s)); }
Packit 8f70b4
static inline const xstring& shell_encode(const xstring& s) { return shell_encode(s.get(),s.length()); }
Packit 8f70b4
void remove_tags(char *buf);
Packit 8f70b4
void rtrim(char *s);
Packit 8f70b4
Packit 8f70b4
void random_init();
Packit 8f70b4
double random01();
Packit 8f70b4
Packit 8f70b4
const char *get_nodename();
Packit 8f70b4
Packit 8f70b4
const char *xidna_to_ascii(const char *name);
Packit 8f70b4
bool xtld_name_ok(const char *name);
Packit 8f70b4
Packit 8f70b4
bool is_ipv4_address(const char *);
Packit 8f70b4
bool is_ipv6_address(const char *);
Packit 8f70b4
Packit 8f70b4
int lftp_fallocate(int fd,off_t sz);
Packit 8f70b4
Packit 8f70b4
void call_dynamic_hook(const char *name);
Packit 8f70b4
Packit 8f70b4
#endif // MISC_H