X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-lib%2Ffile.c;h=ed74aada186c8e75f576645b2e984707b3902df2;hp=aee35803fdb7a8234535c125de64ffce6d9bdc47;hb=HEAD;hpb=70e9ad0d77eb1c2d7ef76afdba6825de75d5599f diff --git a/lib-lib/file.c b/lib-lib/file.c index aee3580..ed74aad 100644 --- a/lib-lib/file.c +++ b/lib-lib/file.c @@ -28,6 +28,8 @@ #include "lib-lib.h" +#include + #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 #endif @@ -68,7 +70,7 @@ int safe_open(const char *path, int flags) return -1; } - return (fd); + return fd; } @@ -224,7 +226,7 @@ FILE *safe_fopen(const char *path, const char *mode) flags |= (mode[1] == '+' ? O_RDWR : O_WRONLY); if ((fd = safe_open(path, flags)) < 0) - return (NULL); + return NULL; return fdopen (fd, mode); } @@ -353,7 +355,7 @@ int mutt_copy_bytes(FILE *in, FILE *out, ssize_t size) /****************************************************************************/ -/* ligben-like funcs */ +/* path manipulations */ /****************************************************************************/ const char *mutt_basename(const char *f) @@ -362,6 +364,26 @@ const char *mutt_basename(const char *f) return p ? p + 1 : f; } +ssize_t m_dirname(char *dst, ssize_t dlen, const char *path) +{ + ssize_t plen = m_strlen(path); + + while (plen > 0 && path[plen - 1] == '/') + plen--; + + while (plen > 0 && path[plen - 1] != '/') + plen--; + + while (plen > 0 && path[plen - 1] == '/') + plen--; + + if (plen) + return m_strncpy(dst, dlen, path, plen); + + if (*path == '/') + return m_strcpy(dst, dlen, "/"); + return m_strcpy(dst, dlen, "."); +} char * mutt_concat_path(char *d, ssize_t n, const char *dir, const char *fname) @@ -521,3 +543,31 @@ FILE *m_tempfile(char *dst, ssize_t n, const char *dir, const char *fmt) int fd = m_tempfd(dst, n, dir, fmt); return fd < 0 ? NULL : fdopen(fd, "w+"); } + +/****************************************************************************/ +/* misc */ +/****************************************************************************/ + +/* Decrease a file's modification time by 1 second */ +time_t m_decrease_mtime(const char *path, struct stat *st) +{ + struct utimbuf utim; + struct stat _st; + time_t mtime; + + if (!st) { + if (stat(path, &_st) == -1) + return -1; + st = &_st; + } + + if ((mtime = st->st_mtime) == time(NULL)) { + mtime -= 1; + utim.actime = mtime; + utim.modtime = mtime; + utime(path, &utim); + } + + return mtime; +} +