1 /*  $Id: misc.h 10283 2018-05-14 12:43:05Z iulius $
2 **
3 **  Miscellaneous utility functions for innfeed.
4 **
5 **  Written by James Brister <brister@vix.com>
6 */
7 
8 #if ! defined ( misc_h__ )
9 #define misc_h__
10 
11 #include "config.h"
12 #include "portable/macros.h"
13 
14 #include <stdarg.h>
15 #include <sys/types.h>
16 
17 /* These typedefs are all here because C is too stupid to let me multiply
18    define typedefs to the same things (as C++ will). Hence I can't redeclare
19    the typedefs to get around recursive header file includes (like host.h and
20    connection.h would need if they contained their own typedefs). */
21 
22 typedef struct article_s *Article ;             /* see article.h */
23 typedef struct buffer_s *Buffer ;               /* see buffer.h */
24 typedef struct commander_s *Commander ;         /* see commander.h */
25 typedef struct config_s *Config ;               /* see config.h */
26 typedef struct connection_s *Connection ;       /* see connection.h */
27 typedef struct endpoint_s *EndPoint ;           /* see endpoint.h */
28 typedef struct host_s *Host ;                   /* see host.h */
29 typedef struct innlistener_s *InnListener ;     /* see innlistener.h */
30 typedef struct tape_s *Tape ;                   /* see tape.h */
31 
32 typedef int TimeoutId ;                         /* see endpoint.h */
33 typedef enum {                                  /* see endpoint.h */
34   IoDone, IoIncomplete, IoFailed, IoEOF, IoProgress
35 } IoStatus ;
36 
37 typedef void (*EndpRWCB) (EndPoint e,           /* see endpoint.h */
38                           IoStatus i, Buffer *b, void *d) ;
39 typedef void (*EndpTCB) (TimeoutId tid, void *d) ; /* see endpoint.h */
40 typedef void (*EndpWorkCbk) (EndPoint ep, void *data) ;
41 
42 
43 /* debugging information */
44 extern unsigned int loggingLevel ;     /* if 0 then d_printf is a no-op */
45 
46 /* used by timeToString with strftime(3) */
47 extern char *timeToStringFormat ;
48 
49 /* the current count of file desccriptors */
50 extern unsigned int openfds ;
51 
52 /* if level <= loggingLevel then print */
53 void d_printf (unsigned int level, const char *fmt, ...)
54     __attribute__((__format__ (printf, 2, 3)));
55 
56 /* for the gethostbyname() error code */
57 const char *host_err_str (void) ;
58 
59 /* parse a response line into it's code and body. *rest will end up pointing
60    into the middle of p */
61 bool getNntpResponse (char *p, int *code, char **rest) ;
62 
63 /* parse out the first field of a nntp response code as a message-id. Caller
64    must free the return value when done. */
65 char *getMsgId (const char *p) ;
66 
67 /* pick out the next non-blank string inside PTR. TAIL is set to point at
68    the first blank (or NULL) after the string. Returns a pointer into PTR */
69 char *findNonBlankString (char *ptr, char **tail) ;
70 
71 /* if fp is not NULL then print to it, otherwise syslog at the level. */
72 void logOrPrint (int level, FILE *fp, const char *fmt, ...)
73     __attribute__((__format__(printf, 3, 4)));
74 
75 /* Error handling functions for use with warn and die. */
76 void error_log_stderr_date(int len, const char *fmt, va_list args, int err)
77     __attribute__((__format__(printf, 2, 0)));
78 
79 /* Do cleanup and then abort, for use with die. */
80 void dump_core(void)
81     __attribute__ ((noreturn));
82 
83 /* Alternate die that doesn't invoke an error handler. */
84 void logAndExit (int exitVal, const char *fmt, ...)
85     __attribute__((noreturn, __format__(printf, 2, 3)));
86 
87 /* return true of the file exists and is a regular file */
88 bool fileExistsP (const char *filename) ;
89 
90 /* return true if file exists and is a directory */
91 bool isDirectory (const char *filename) ;
92 
93 char *mystrtok (char *string, const char *sep) ;
94 
95 /* remove trailing whitespace */
96 void trim_ws (char *string) ;
97 
98 /* locks the peer and returns true or returns false */
99 bool lockPeer (const char *peerName) ;
100 
101 /* return true if the end of string matches tail. */
102 bool endsIn (const char *string, const char *tail) ;
103 
104 /* scribble over then free up the null-terminated string */
105 void freeCharP (char *charp) ;
106 
107 /* append the contents of src to dest. src is removed if append if
108    successful */
109 bool appendFile (const char *dest, const char *src) ;
110 
111 /* return the length of the file reference by the given file descriptor */
112 long fileLength (int fd) ;
113 
114 bool lockFile (const char *fileName) ;
115 void unlockFile (const char *lockfile) ;
116 
117 
118 /* return true if file1 is older than file2 */
119 bool isOlder (const char *file1, const char *file2) ;
120 
121 /* converts val into a printable string */
122 const char *boolToString (bool val) ;
123 
124 /* strftime with "%a %b %d %H:%M:%S %Y" (like ctime without linefeed) */
125 char* timeToString (time_t time, char* buffer, size_t size) ;
126 
127 /* memory leak checker helper. */
128 void addPointerFreedOnExit (char *pointerToFree) ;
129 
130 /* string the file opened by FP to the give SIZE. The NEWNAME is the name
131    of the file to have after truncation. FP will be reopened for writing on
132    the new file and will be positioned at the end */
133 bool shrinkfile (FILE *fp, long size, char *name, const char *mode) ;
134 
135 /* max length of pathname */
136 long pathMax (const char *pathname) ;
137 
138 #define ASSERT(x) do{if (!(x))die("assertion -- %s -- failed in file %s line %d",#x,__FILE__,__LINE__);}while(0)
139 
140 #define INDENT_INCR 5
141 #define INDENT_BUFFER_SIZE 80
142 #if ! defined (MIN)
143 #define MIN(A,B) ((A) < (B) ? (A) : (B))
144 #endif
145 
146 #endif /* misc_h__ */
147