X-Git-Url: https://git.llucax.com/software/mutt-debian.git/blobdiff_plain/19304f7c526fbe36ba0db2fb80bcaf3bd974d81d..753c3b3409564dc4fe3824cba6272babc6db5fe6:/lib.c?ds=sidebyside diff --git a/lib.c b/lib.c index 146f363..17cc516 100644 --- a/lib.c +++ b/lib.c @@ -220,11 +220,10 @@ int safe_fsync_close (FILE **f) if (fflush (*f) || fsync (fileno (*f))) { r = -1; - fclose (*f); + safe_fclose (f); } else - r = fclose(*f); - *f = NULL; + r = safe_fclose (f); } return r; @@ -345,7 +344,7 @@ void mutt_unlink (const char *s) fwrite (buf, 1, MIN (sizeof (buf), sb.st_size), f); sb.st_size -= MIN (sizeof (buf), sb.st_size); } - fclose (f); + safe_fclose (&f); } } } @@ -739,7 +738,7 @@ int mutt_rx_sanitize_string (char *dest, size_t destlen, const char *src) * If a line ends with "\", this char and the linefeed is removed, * and the next line is read too. */ -char *mutt_read_line (char *s, size_t *size, FILE *fp, int *line) +char *mutt_read_line (char *s, size_t *size, FILE *fp, int *line, int flags) { size_t offset = 0; char *ch; @@ -759,11 +758,14 @@ char *mutt_read_line (char *s, size_t *size, FILE *fp, int *line) } if ((ch = strchr (s + offset, '\n')) != NULL) { - (*line)++; + if (line) + (*line)++; + if (flags & M_EOL) + return s; *ch = 0; if (ch > s && *(ch - 1) == '\r') *--ch = 0; - if (ch == s || *(ch - 1) != '\\') + if (!(flags & M_CONT) || ch == s || *(ch - 1) != '\\') return s; offset = ch - s - 1; } @@ -778,7 +780,8 @@ char *mutt_read_line (char *s, size_t *size, FILE *fp, int *line) if (c == EOF) { /* The last line of fp isn't \n terminated */ - (*line)++; + if (line) + (*line)++; return s; } else @@ -1005,3 +1008,77 @@ mutt_strsysexit(int e) return sysexits_h[i].str; } + +void mutt_debug (FILE *fp, const char *fmt, ...) +{ + va_list ap; + time_t now = time (NULL); + static char buf[23] = ""; + static time_t last = 0; + + if (now > last) + { + strftime (buf, sizeof (buf), "%Y-%m-%d %H:%M:%S", localtime (&now)); + last = now; + } + fprintf (fp, "[%s] ", buf); + va_start (ap, fmt); + vfprintf (fp, fmt, ap); + va_end (ap); +} + +int mutt_atos (const char *str, short *dst) +{ + int rc; + long res; + short tmp; + short *t = dst ? dst : &tmp; + + *t = 0; + + if ((rc = mutt_atol (str, &res)) < 0) + return rc; + if ((short) res != res) + return -2; + + *t = (short) res; + return 0; +} + +int mutt_atoi (const char *str, int *dst) +{ + int rc; + long res; + int tmp; + int *t = dst ? dst : &tmp; + + *t = 0; + + if ((rc = mutt_atol (str, &res)) < 0) + return rc; + if ((int) res != res) + return -2; + + *t = (int) res; + return 0; +} + +int mutt_atol (const char *str, long *dst) +{ + long r; + long *res = dst ? dst : &r; + char *e = NULL; + + /* no input: 0 */ + if (!str || !*str) + { + *res = 0; + return 0; + } + + *res = strtol (str, &e, 10); + if ((*res == LONG_MAX && errno == ERANGE) || + (e && *e != '\0')) + return -1; + return 0; +}