"Fossies" - the Fresh Open Source Software Archive

Member "mirrordir-0.10.49/vfs/util.c" (23 Nov 1999, 5349 Bytes) of package /linux/misc/old/mirrordir-0.10.49.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "util.c" see the Fossies "Dox" file reference documentation.

    1 /*   This has nothing to do with cryptography. */
    2 #include <config.h>
    3 #include <stdlib.h>
    4 #include <stdio.h>
    5 #include <string.h>
    6 #include <stdarg.h>
    7 #ifdef HAVE_PWD
    8 #include <pwd.h>
    9 #endif
   10 #ifdef HAVE_UNISTD_H
   11 #include <unistd.h>
   12 #endif
   13 #include "util.h"
   14 #include "diffie/compat.h"
   15 #include "mad.h"
   16 
   17 
   18 /* Canonicalize path, and return a new path. Do everything in situ.
   19    The new path differs from path in:
   20     Multiple `/'s are collapsed to a single `/'.
   21     Leading `./'s and trailing `/.'s are removed.
   22     Trailing `/'s are removed.
   23     Non-leading `../'s and trailing `..'s are handled by removing
   24     portions of the path. */
   25 char *canonicalize_pathname (char *path)
   26 {
   27     int i, start;
   28     char stub_char;
   29 
   30     stub_char = (*path == PATH_SEP) ? PATH_SEP : '.';
   31 
   32     /* Walk along path looking for things to compact. */
   33     i = 0;
   34     for (;;) {
   35         if (!path[i])
   36         break;
   37 
   38         while (path[i] && path[i] != PATH_SEP)
   39         i++;
   40 
   41         start = i++;
   42 
   43         /* If we didn't find any slashes, then there is nothing left to do. */
   44         if (!path[start])
   45         break;
   46 
   47 #if defined(__QNX__)
   48         /*
   49         ** QNX accesses the directories of nodes on its peer-to-peer
   50         ** network by prefixing their directories with "//[nid]".
   51         ** We don't want to collapse two '/'s if they're at the start
   52         ** of the path, followed by digits, and ending with a '/'.
   53         */
   54         if (start == 0 && i == 1)
   55         {
   56             char *p = path + 2;
   57             char *q = strchr(p, PATH_SEP);
   58 
   59             if (q > p)
   60             {
   61                 *q = 0;
   62                 if (!strcspn(p, "0123456789"))
   63                 {
   64                     start = q - path;
   65                     i = start + 1;
   66                 }
   67                 *q = PATH_SEP;
   68             }
   69         }
   70 #endif
   71 
   72         /* Handle multiple `/'s in a row. */
   73         while (path[i] == PATH_SEP)
   74         i++;
   75 
   76         if ((start + 1) != i) {
   77         strcpy (path + start + 1, path + i);
   78         i = start + 1;
   79     }
   80 
   81         /* Handle backquoted `/'. */
   82         if (start > 0 && path[start - 1] == '\\')
   83         continue;
   84 
   85         /* Check for trailing `/'. */
   86         if (start && !path[i]) {
   87     zero_last:
   88         path[--i] = '\0';
   89         break;
   90     }
   91 
   92         /* Check for `../', `./' or trailing `.' by itself. */
   93         if (path[i] == '.') {
   94         /* Handle trailing `.' by itself. */
   95         if (!path[i + 1])
   96             goto zero_last;
   97 
   98         /* Handle `./'. */
   99         if (path[i + 1] == PATH_SEP) {
  100             strcpy (path + i, path + i + 1);
  101             i = start;
  102             continue;
  103         }
  104 
  105         /* Handle `../' or trailing `..' by itself. 
  106            Remove the previous ?/ part with the exception of
  107            ../, which we should leave intact. */
  108         if (path[i + 1] == '.' && (path[i + 2] == PATH_SEP || !path[i + 2])) {
  109             while (--start > -1 && path[start] != PATH_SEP);
  110             if (!strncmp (path + start + 1, ".." PATH_SEP_STR, 3))
  111                 continue;
  112             strcpy (path + start + 1, path + i + 2);
  113             i = start;
  114             continue;
  115         }
  116     }
  117     }
  118 
  119     if (!*path) {
  120         *path = stub_char;
  121         path[1] = '\0';
  122     }
  123     return path;
  124 }
  125 
  126 
  127 #ifdef HAVE_MAD
  128 char *mad_copy_strings (char *file, int line, const char *first, ...)
  129 #else
  130 char *copy_strings (const char *first,...)
  131 #endif
  132 {
  133     va_list ap;
  134     int len;
  135     char *data, *result;
  136 
  137     if (!first)
  138     return 0;
  139     
  140     len = strlen (first);
  141     va_start (ap, first);
  142 
  143     while ((data = va_arg (ap, char *))!=0)
  144     len += strlen (data);
  145 
  146     len++;
  147 
  148 #ifdef HAVE_MAD
  149     result = mad_alloc (len, file, line);
  150 #else
  151     result = malloc (len);
  152 #endif
  153     va_end (ap);
  154     va_start (ap, first);
  155     strcpy (result, first);
  156     while ((data = va_arg (ap, char *)) != 0)
  157     strcat (result, data);
  158     va_end (ap);
  159 
  160     return result;
  161 }
  162 
  163 #ifdef HAVE_MAD
  164 void mad_wipe_password (char *passwd, char *file, int line)
  165 #else
  166 void wipe_password (char *passwd)
  167 #endif
  168 {
  169     char *p = passwd;
  170     for (; *p; p++)
  171     *p = 0;
  172 #ifdef HAVE_MAD
  173     mad_free (passwd, file, line);
  174 #else
  175     free (passwd);
  176 #endif
  177 }
  178 
  179 char *unix_error_string (int error_num)
  180 {
  181     static char buffer [256];
  182     char *error_msg;
  183     
  184 #ifdef HAVE_STRERROR
  185     error_msg = strerror (error_num);
  186 #else
  187     extern int sys_nerr;
  188     extern char *sys_errlist [];
  189     if ((0 <= error_num) && (error_num < sys_nerr))
  190     error_msg = sys_errlist[error_num];
  191     else
  192     error_msg = "strange errno";
  193 #endif
  194     sprintf (buffer, "%.200s (%d)", error_msg, error_num);
  195     return buffer;
  196 }
  197 
  198 int exist_file (char *name)
  199 {
  200     return access (name, R_OK) == 0;
  201 }
  202 
  203 void vfs_default_message_callback (char *p,...)
  204 {
  205     va_list ap;
  206     va_start (ap, p);
  207     vprintf (p, ap);
  208     printf ("\n");
  209     va_end (ap);
  210 }
  211 
  212 char *vfs_default_getpasswd_callback (char *a, char *b, char *c, char *host, char *user) /*EK*/
  213 {
  214     return (char *) strdup ((char *) getpass (b));
  215 }
  216 
  217 extern char *ftpfs_anonymous_passwd;
  218 
  219 char *vfs_default_getanonpasswd_callback (char *s)
  220 {
  221     return 0;
  222 }
  223 
  224 int vfs_default_gotinterrupt_callback (void)
  225 {
  226     return 0;
  227 }
  228 
  229 void vfs_set_message_callback (void (*callback) (char *,...))
  230 {
  231     vfs_message_callback = callback;
  232 }
  233 
  234 /*EK* added to extra arguments, host and user name. they may be NULL */
  235 void vfs_set_getpasswd_callback (char *(*callback) (char *, char *, char *, char *, char*)) /*EK*/
  236 {
  237     vfs_getpasswd_callback = callback;
  238 }
  239 
  240 void vfs_set_getanonpasswd_callback (char *(*callback) (char *)) /*EK*/
  241 {
  242     vfs_getanonpasswd_callback = callback;
  243 }
  244 
  245 void vfs_set_gotinterrupt_callback (int (*callback) (void))
  246 {
  247     vfs_gotinterrupt_callback = callback;
  248 }
  249 
  250 
  251 
  252 
  253 
  254