From 37250a0568d5b1d689d5c59f983e9be228ed49c2 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sat, 15 Apr 2000 18:00:00 +0200 Subject: Version 0.03 - Actual record/replay now works. - Dropped the idea of different "recording qualities" (a 36GB harddisk is able to store some 18 hours in full quality, so we don't really need that). - Termination signals are now caught and the program cleans up before exiting. - Support for CICAM. --- tools.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 8 deletions(-) (limited to 'tools.c') diff --git a/tools.c b/tools.c index 5a21a93..e356e0f 100644 --- a/tools.c +++ b/tools.c @@ -4,18 +4,46 @@ * See the main source file 'osm.c' for copyright information and * how to reach the author. * - * $Id: tools.c 1.2 2000/03/05 14:33:58 kls Exp $ + * $Id: tools.c 1.3 2000/04/15 15:10:05 kls Exp $ */ +#define _GNU_SOURCE #include "tools.h" +#include #include #include #include #include #include +#include #define MaxBuffer 1000 +int SysLogLevel = 3; + +void writechar(int filedes, char c) +{ + write(filedes, &c, sizeof(c)); +} + +void writeint(int filedes, int n) +{ + write(filedes, &n, sizeof(n)); +} + +char readchar(int filedes) +{ + char c; + read(filedes, &c, 1); + return c; +} + +bool readint(int filedes, int &n) +{ + //XXX timeout!! + return read(filedes, &n, sizeof(n)); +} + char *readline(FILE *f) { static char buffer[MaxBuffer]; @@ -30,36 +58,83 @@ char *readline(FILE *f) int time_ms(void) { + static time_t t0 = 0; struct timeval t; - if (gettimeofday(&t, NULL) == 0) - return t.tv_sec * 1000 + t.tv_usec / 1000; + if (gettimeofday(&t, NULL) == 0) { + if (t0 == 0) + t0 = t.tv_sec; // this avoids an overflow (we only work with deltas) + return (t.tv_sec - t0) * 1000 + t.tv_usec / 1000; + } return 0; } -bool MakeDirs(const char *FileName) +void delay_ms(int ms) +{ + int t0 = time_ms(); + while (time_ms() - t0 < ms) + ; +} + +bool MakeDirs(const char *FileName, bool IsDirectory) { bool result = true; char *s = strdup(FileName); char *p = s; if (*p == '/') p++; - while ((p = strchr(p, '/')) != NULL) { - *p = 0; + while ((p = strchr(p, '/')) != NULL || IsDirectory) { + if (p) + *p = 0; struct stat fs; if (stat(s, &fs) != 0 || !S_ISDIR(fs.st_mode)) { isyslog(LOG_INFO, "creating directory %s", s); if (mkdir(s, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) { - esyslog(LOG_ERR, "ERROR while creating directory %s: %s", s, strerror(errno)); + esyslog(LOG_ERR, "ERROR: %s: %s", s, strerror(errno)); result = false; break; } } - *p++ = '/'; + if (p) + *p++ = '/'; + else + break; } delete s; return result; } +bool RemoveFileOrDir(const char *FileName) +{ + struct stat st; + if (stat(FileName, &st) == 0) { + if (S_ISDIR(st.st_mode)) { + DIR *d = opendir(FileName); + if (d) { + struct dirent *e; + while ((e = readdir(d)) != NULL) { + if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { + char *buffer; + asprintf(&buffer, "%s/%s", FileName, e->d_name); + if (remove(buffer) < 0) + esyslog(LOG_ERR, "ERROR: %s: %s", buffer, strerror(errno)); + delete buffer; + } + } + closedir(d); + } + else { + esyslog(LOG_ERR, "ERROR: %s: %s", FileName, strerror(errno)); + return false; + } + } + if (remove(FileName) == 0) + return true; + } + else + esyslog(LOG_ERR, "ERROR: %s: %s", FileName, strerror(errno)); + return false; +} + // --- cListObject ----------------------------------------------------------- cListObject::cListObject(void) -- cgit v1.2.3