Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

util.cc

Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <fcntl.h>
00003 #include <netdb.h>
00004 #include <string.h>
00005 #include <sys/param.h>
00006 #include <sys/stat.h>
00007 #include <sys/types.h>
00008 #include <sys/utsname.h>
00009 #include <time.h>
00010 #include <unistd.h>
00011 #include <ctype.h>
00012 
00013 #include"util.h"
00014 
00015 int mkpdir(const char *dirname, int mode)
00016 {
00017         struct stat s;
00018         char buf[MAXPATHLEN];
00019         char *p;
00020         const char *q = dirname;
00021 
00022         if (stat(dirname, &s) < 0) {
00023                 if (errno != ENOENT)
00024                         return -1;
00025                 p = buf;
00026                 if (*q == '/')
00027                         *p++ = *q++;
00028                 while (*q) {
00029                         while (*q && *q != '/')
00030                                 *p++ = *q++;
00031                         *p = '\0';
00032                         if (stat(buf, &s) < 0) {
00033                                 if (errno != ENOENT)
00034                                         return -1;
00035                                 if (mkdir(buf, mode) < 0)
00036                                         return -1;
00037                         } else {
00038                                 if (!(S_ISDIR(s.st_mode))) {
00039                                         errno = ENOTDIR;
00040                                         return -1;
00041                                 }
00042                         }
00043                         while (*q == '/')
00044                                 *p++ = *q++;
00045                 }
00046         }
00047         if (!(S_ISDIR(s.st_mode))) {
00048                 errno = ENOTDIR;
00049                 return -1;
00050         }
00051         return 0;
00052 }
00053 
00054 const char *group2dir(const char *name)
00055 {
00056         static char buf[MAXPATHLEN], *p;
00057         const char *q;
00058 
00059         p = buf;
00060         q = name;
00061         while (((*p) = ((*q != '.') ? *q : '/')) != '\0') {
00062                 p++;
00063                 q++;
00064         }
00065 
00066         return buf;
00067 }
00068 
00069 int matchgroup(const char *pattern, const char *group)
00070 {
00071         char c;
00072         int j, clen, bmlen = 0, bmrej = 0;
00073         int rejrule = 0;
00074 
00075         j = 0;
00076         do {
00077                 clen = 0;
00078                 if (pattern[j] == '!') {
00079                         j++;
00080                         rejrule = 1;
00081                 } else {
00082                         rejrule = 0;
00083                 }
00084                 while ((c = pattern[j]) && c == group[clen]) {
00085                         clen++;
00086                         j++;
00087                 }
00088 
00089                 switch (c) {
00090                 case ',':
00091                 case '\0':
00092                         if (group[clen] != '\0' && !isspace(group[clen])) {
00093                                 clen = -2;
00094                         }
00095                         clen++;
00096                         break;
00097                 case '*':
00098                         clen++;
00099                         break;
00100                 default:
00101                         clen = -1;
00102                         break;
00103                 }
00104 
00105                 if (clen > bmlen) {
00106                         bmlen = clen;
00107                         bmrej = rejrule;
00108                 }
00109 
00110                 while (c && c != ',') {
00111                         j++;
00112                         c = pattern[j];
00113                 }
00114                 j++;
00115         } while (c);
00116 
00117         return bmrej ? -bmlen : bmlen;
00118 }
00119 
00120 const char *getfqdn(void)
00121 {
00122         static char primary_hostname[MAXHOSTNAMELEN];
00123         struct utsname uts;
00124         struct hostent *host;
00125 
00126         if (uname(&uts) < 0)
00127                 return NULL;    // failed
00128         if (strchr(uts.nodename, '.') == NULL &&
00129             (host = gethostbyname(uts.nodename)) != NULL) {
00130                 strcpy(primary_hostname, (char *) host->h_name);
00131         } else {
00132                 strcpy(primary_hostname, uts.nodename);
00133         }
00134 
00135         return primary_hostname;
00136 }

Generated on Fri Aug 20 10:58:08 2004 for NewsCache by doxygen 1.3.6-20040222