1 /*
2 * lftp - file transfer program
3 *
4 * Copyright (c) 1996-2017 by Alexander V. Lukyanov (lav@yars.free.net)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef MISC_H
21 #define MISC_H
22
23 #include "trio.h"
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #ifdef TIME_WITH_SYS_TIME
27 # include <time.h>
28 #endif
29 #include <stdarg.h>
30
31 #include "xstring.h"
32
33 // expands tilde; returns pointer to static data
34 const char *expand_home_relative(const char *);
35
36 // returns ptr to last path element
37 const char *basename_ptr(const char *);
basename_ptr(char * f)38 static inline char *basename_ptr(char *f) {
39 return const_cast<char*>(basename_ptr(const_cast<const char *>(f)));
40 }
41
42 // glues file to dir; returns pointer to static storage
43 const char *dir_file(const char *dir,const char *file);
44
45 // glues file to given url; returns pointer to static storage
46 const char *url_file(const char *url,const char *file);
47
48 const char *output_file_name(const char *src,const char *dst,bool dst_local,
49 const char *dst_base,bool make_dirs);
50
51 const char *squeeze_file_name(const char *name,int w);
52
53 // mkdir -p
54 int create_directories(char *);
55
56 // rm -rf
57 void truncate_file_tree(const char *dir);
58
59 /* returns file descriptor terminal width; -1 on error, 0 on N/A */
60 int fd_width(int fd);
61
62 /* returns true if current pgrp is the foreground pgrp of controlling tty
63 * or if the fg pgrp is unknown */
64 bool in_foreground_pgrp();
65
66 // returns malloc'ed cwd no matter how long it is
67 // returns 0 on error.
68 char *xgetcwd();
69 // store cwd to specified string
70 void xgetcwd_to(xstring& s);
xgetcwd_to(xstring_c & s)71 static inline void xgetcwd_to(xstring_c& s) { s.set_allocated(xgetcwd()); }
72
73 int percent(off_t offset,off_t size);
74
75 #define find_char(buf,len,ch) ((const char *)memchr(buf,ch,len))
76
77 extern const char month_names[][4];
78
79 int parse_month(const char *);
80 int parse_perms(const char *);
81 const char *format_perms(int p);
82 int parse_year_or_time(const char *year_or_time,int *year,int *hour,int *minute);
83 int guess_year(int month,int day,int hour,int minute);
84
85 time_t mktime_from_utc(const struct tm *);
86 time_t mktime_from_tz(struct tm *,const char *tz);
87
88 bool re_match(const char *line,const char *a,int flags=0);
89
90 struct subst_t {
91 char from;
92 const char *to;
93 };
94
95 /* Subst changes escape sequences to given strings, also substitutes \nnn
96 * with corresponding character. Returns allocated buffer to be free'd */
97 xstring& SubstTo(xstring& buf,const char *txt,const subst_t *s);
98
99 /* uses gettimeofday if available */
100 void xgettimeofday(time_t *sec, int *usec);
101
102 /* returns malloc'd date */
103 char *xstrftime(const char *format, const struct tm *tm);
104
105 const char *xhuman(long long n);
106
107 void strip_trailing_slashes(xstring& fn);
108 xstring& dirname_modify(xstring& fn);
109 xstring& dirname(const char *path); // returns a tmp
110
111 /* returns last character of string or \0 if string is empty */
112 char last_char(const char *str);
113
114 int base64_length (int len);
115 void base64_encode (const char *s, char *store, int length);
116
117 bool temporary_network_error(int e);
118
119 CDECL const char *get_home();
120 CDECL const char *get_lftp_config_dir();
121 CDECL const char *get_lftp_data_dir();
122 CDECL const char *get_lftp_cache_dir();
123
124 const char *memrchr(const char *buf,char c,size_t len);
memrchr(char * buf,char c,size_t len)125 static inline char *memrchr(char *buf,char c,size_t len) {
126 return const_cast<char*>(memrchr(const_cast<const char*>(buf),c,len));
127 }
128
129 bool is_shell_special(char c);
130 const xstring& shell_encode(const char *s,int len);
shell_encode(const char * s)131 static inline const xstring& shell_encode(const char *s) { return shell_encode(s,strlen(s)); }
shell_encode(const xstring & s)132 static inline const xstring& shell_encode(const xstring& s) { return shell_encode(s.get(),s.length()); }
133 int remove_tags(char *buf);
134 void rtrim(char *s);
135
136 void random_init();
137 double random01();
138
139 const char *get_nodename();
140
141 const char *xidna_to_ascii(const char *name);
142 bool xtld_name_ok(const char *name);
143
144 bool is_ipv4_address(const char *);
145 bool is_ipv6_address(const char *);
146
147 int lftp_fallocate(int fd,off_t sz);
148
149 void call_dynamic_hook(const char *name);
150
151 #endif // MISC_H
152