Ignore:
Timestamp:
06/05/07 10:46:00 (5 years ago)
Author:
wd
Message:

Another milestone. Builds, starts without errors.. then doesn't take
connections, buy hey.

Also made the config file finder a bit smarter about looking for config
files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ithildin/lib/util.c

    r787 r808  
    4141char *canonize_make_human(int size, char type); 
    4242 
    43 /* mmap a file into a single string */ 
     43/* memory map a file into a single string (using malloc, though, not 
     44 * mmap...) */ 
    4445char *mmap_file(char *file) { 
    4546    struct stat sb; 
     
    6364 
    6465    fp = fopen(file, "r"); 
    65     if (fp==NULL) { 
     66    if (fp == NULL) { 
    6667        free(data); 
    6768        return NULL; 
     
    127128} 
    128129 
     130#define PATH_SEPARATOR '/' 
     131/* A function to determine whether a filename given is an absolute path or 
     132 * not.  For unix systems this is a trivial check for a '/', on win32 it 
     133 * requires that we examine the path for drive letter/etc */ 
     134bool is_absolute_path(const char *fname) { 
     135    if (*fname == PATH_SEPARATOR) 
     136        return true; 
     137    return false; 
     138} 
     139 
     140/* get the base/dirname of a file (portably).  these are also provided by 
     141 * some operating systems, and we can happily interoperate with those 
     142 * implementations. */ 
     143char bdname_buf[PATH_MAX]; 
     144char *basename(const char *fname) { 
     145    char *s; 
     146 
     147    if (strlen(fname) >= PATH_MAX) { 
     148        errno = EINVAL; 
     149        return NULL; 
     150    } 
     151 
     152    /* chew off any trailing slashes */ 
     153    strlcpy(bdname_buf, fname, PATH_MAX); 
     154    s = bdname_buf + strlen(fname) - 1; 
     155    while (*s == PATH_SEPARATOR && s >= bdname_buf) 
     156        *s-- = '\0'; 
     157 
     158    if ((s = strrchr(bdname_buf, PATH_SEPARATOR)) == NULL) 
     159        /* no path separator, easy.. */ 
     160        return bdname_buf; 
     161    else { 
     162        /* just move the end of the string to the beginning with memcpy */ 
     163        s++; 
     164        memcpy(bdname_buf, s, strlen(s) + 1); 
     165        return bdname_buf; 
     166    } 
     167} 
     168 
     169char *dirname(const char *fname) { 
     170    char *s; 
     171 
     172    if (strlen(fname) >= PATH_MAX) { 
     173        errno = EINVAL; 
     174        return NULL; 
     175    } 
     176 
     177    /* chew off any trailing slashes */ 
     178    strlcpy(bdname_buf, fname, PATH_MAX); 
     179    s = bdname_buf + strlen(fname) - 1; 
     180    while (*s == PATH_SEPARATOR && s >= bdname_buf) 
     181        *s-- = '\0'; 
     182 
     183    if ((s = strrchr(bdname_buf, PATH_SEPARATOR)) == NULL) { 
     184        strcpy(bdname_buf, "."); 
     185        return bdname_buf; 
     186    } else { 
     187        while (*s == PATH_SEPARATOR && s >= bdname_buf) 
     188            *s-- = '\0'; 
     189        if (*bdname_buf == '\0') 
     190            strcpy(bdname_buf, "."); 
     191        return bdname_buf; 
     192    } 
     193} 
     194 
    129195struct timeval *subtract_timeval(struct timeval tv1, struct timeval tv2) { 
    130196    static struct timeval result; 
Note: See TracChangeset for help on using the changeset viewer.