#include #include #include #include #include #include extern const char *__progname; #include "system.h" #include "profile.h" static int did_p_r = 0; static PROFILE *def_profile; static int lockfd = -1; static const char *mmdir = 0; static const char *folder_dir = 0; static void get_syslock_fd(void) { const char *lockpath; if (lockfd >= 0) return; lockpath = profile_relative_to(system_get_profile(),"syslock",".syslock",system_mmdir()); lockfd = open(lockpath,O_RDWR|O_CREAT,0600); fcntl(lockfd,F_SETFD,1); } const char *system_homedir(void) { const char *home; home = getenv("HOME"); if (home == 0) home = "."; return(home); } const char *system_mmdir(void) { if (! mmdir) mmdir = profile_relative_to(system_get_profile(),"mmdir",".mm",system_homedir()); return(mmdir); } const char *system_folder_dir(void) { if (! folder_dir) folder_dir = profile_relative_to(system_get_profile(),"folders","mail",system_mmdir()); return(folder_dir); } PROFILE *system_get_profile(void) { if (! did_p_r) { def_profile = profile_read(0); if (def_profile == 0) { fprintf(stderr,"%s: no profile!\n",__progname); exit(1); } profile_set_envoverride(def_profile,1); did_p_r = 1; } return(def_profile); } int system_lock(unsigned int how) { int lockop; int srv; switch (how & (SYS_SHARED|SYS_EXCLUSIVE)) { case SYS_SHARED: lockop = LOCK_SH; break; case SYS_EXCLUSIVE: lockop = LOCK_EX; break; default: errno = EINVAL; return(-1); break; } switch (how & (SYS_WAIT|SYS_NOWAIT)) { case SYS_WAIT: break; case SYS_NOWAIT: lockop |= LOCK_NB; break; default: errno = EINVAL; return(-1); break; } get_syslock_fd(); srv = (lockfd < 0) ? -1 : flock(lockfd,lockop); if (srv < 0) { if (how & SYS_FAILEXIT) { fprintf(stderr,"%s: can't get system lock: %s\n",__progname,strerror(errno)); exit(1); } return(-1); } return(0); } void system_unlock(void) { if (lockfd >= 0) flock(lockfd,LOCK_UN); }