path.c

00001 /*
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: path.c,v 1.6 2008/10/29 15:21:26 tho Exp $
00009  */
00010 
00011 #include "klone_conf.h"
00012 #include <sys/stat.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <u/libu.h>
00016 #include <klone/emb.h>
00017 #include <klone/utils.h>
00018 #ifdef HAVE_STRINGS
00019 #include <strings.h>
00020 #endif
00021 
00036 int u_uri_normalize(char *path)
00037 {
00038     enum { SLASH = '/', BACKSLASH = '\\' };
00039     u_string_t *s = NULL;
00040     size_t len;
00041     char delim[2];
00042     char *pp, *tok, *src; 
00043     const char *cs;
00044     int trsl = 0; /* trailing slash */
00045 
00046     dbg_err_if(path == NULL);
00047 
00048     /* convert backslashes to slashes */
00049     for(pp = path; *pp; ++pp)
00050         if(*pp == BACKSLASH)
00051             *pp = SLASH;
00052 
00053     /* must be an absolute path (i.e. must start with a slash) */
00054     dbg_err_if(path[0] != SLASH); 
00055 
00056     if(path[strlen(path)-1] == SLASH)
00057         trsl = 1;
00058 
00059     dbg_err_if(u_snprintf(delim, sizeof(delim), "%c", SLASH));
00060 
00061     dbg_err_if(u_string_create(NULL, 0, &s));
00062 
00063     /* alloc a reasonable buffer immediately */
00064     dbg_err_if(u_string_reserve(s, strlen(path) + 1));
00065 
00066     /* foreach name=value pair... */
00067     for(src = path; (tok = strtok_r(src, delim, &pp)) != NULL; src = NULL)
00068     {
00069         if(!strcmp(tok, ""))
00070             continue; /* double slash */
00071         else if(!strcmp(tok, "."))
00072             continue; /* /./ */
00073         else if(!strcmp(tok, "..")) {
00074             /* eat last dir */
00075             len = u_string_len(s);
00076             cs = u_string_c(s) + u_string_len(s) - 1;
00077             for(; len && *cs != SLASH; --len, --cs)
00078                 continue;
00079             /* crop */
00080             dbg_err_if(u_string_set_length(s, (len ? --len : 0) ));
00081         } else {
00082             dbg_err_if(u_string_aprintf(s, "%c%s", SLASH, tok));
00083         }
00084     }
00085 
00086     if(!u_string_len(s) || trsl)
00087         dbg_err_if(u_string_aprintf(s, "%c", SLASH));
00088 
00089     /* copy out */
00090     strcpy(path, u_string_c(s));
00091 
00092     u_string_free(s);
00093 
00094     return 0;
00095 err:
00096     if(s)
00097         u_string_free(s);
00098     return ~0;
00099 }
00100 
00110 int u_path_where_art_thou (const char *fqn, int *where)
00111 {
00112     struct stat sb;
00113     embres_t *dummy;
00114 
00115     dbg_return_if (fqn == NULL, ~0);
00116     dbg_return_if (where == NULL, ~0);
00117 
00118     /* check embfs first */
00119     if (emb_lookup(fqn, &dummy) == 0)
00120     {
00121         *where = U_PATH_IN_EMBFS;
00122         return 0;
00123     }
00124 
00125     /* then the file system */
00126     if (stat(fqn, &sb) == 0)
00127     {
00128         *where = U_PATH_IN_FS;
00129         return 0;
00130     }
00131 
00132     /* then give up */
00133     *where = U_PATH_NOT_FOUND;
00134 
00135     return 0;
00136 }

←Products
Copyright © 2005-2012 - KoanLogic S.r.l. - All rights reserved