Mercurial > gftp.yaz
annotate lib/misc.c @ 516:fbb5a02beddb
2004-7-26 Brian Masney <masneyb@gftp.org>
* lib/config_file.c lib/local.c lib/ftps.c lib/gftp.h lib/httpcommon.h
lib/https.c lib/misc.c lib/protocols.c lib/pty.c lib/rfc2068.c
lib/rfc959.c lib/sshv2.c - various fixes found through a static code
analysis checker. (some signed/unsigned fixes, removed unneeded casts,
removed unneeded variables, more consistency, largefile fixes). Thanks
to Derek M Jones for running the static code analysis for me
* lib/cache.c - s/remove/del_entry/g
* lib/bookmark.c lib/protocols.c (gftp_parse_bookmark) - allow
the third parameter of this function to be a NULL pointer
| author | masneyb |
|---|---|
| date | Tue, 27 Jul 2004 02:46:54 +0000 |
| parents | 39e9945288ea |
| children | bccfdbfaac00 |
| rev | line source |
|---|---|
| 1 | 1 /*****************************************************************************/ |
| 2 /* misc.c - general purpose routines */ | |
| 122 | 3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */ |
| 1 | 4 /* */ |
| 5 /* This program is free software; you can redistribute it and/or modify */ | |
| 6 /* it under the terms of the GNU General Public License as published by */ | |
| 7 /* the Free Software Foundation; either version 2 of the License, or */ | |
| 8 /* (at your option) any later version. */ | |
| 9 /* */ | |
| 10 /* This program is distributed in the hope that it will be useful, */ | |
| 11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
| 12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
| 13 /* GNU General Public License for more details. */ | |
| 14 /* */ | |
| 15 /* You should have received a copy of the GNU General Public License */ | |
| 16 /* along with this program; if not, write to the Free Software */ | |
| 17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
| 18 /*****************************************************************************/ | |
| 19 | |
| 20 #include "gftp.h" | |
| 21 #include "options.h" | |
| 516 | 22 static const char cvsid[] = "$Id$"; |
| 1 | 23 |
| 328 | 24 #ifdef HAVE_INTL_PRINTF |
| 289 | 25 |
| 26 char * | |
| 27 insert_commas (off_t number, char *dest_str, size_t dest_len) | |
| 28 { | |
| 29 if (dest_str != NULL) | |
| 516 | 30 g_snprintf (dest_str, dest_len, GFTP_OFF_T_PRINTF_MOD, number); |
| 289 | 31 else |
| 516 | 32 dest_str = g_strdup_printf (GFTP_OFF_T_PRINTF_MOD, number); |
| 289 | 33 |
| 34 return (dest_str); | |
| 35 } | |
| 36 | |
| 37 #else | |
| 38 | |
| 1 | 39 char * |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
40 insert_commas (off_t number, char *dest_str, size_t dest_len) |
| 1 | 41 { |
| 42 char *frompos, *topos, src[50], *dest; | |
| 460 | 43 size_t num, rem, srclen; |
| 44 int len, i; | |
| 1 | 45 |
| 516 | 46 g_snprintf (src, sizeof (src), GFTP_OFF_T_PRINTF_MOD, number); |
| 220 | 47 |
| 1 | 48 if (dest_str != NULL) |
| 49 *dest_str = '\0'; | |
| 50 | |
| 220 | 51 len = strlen (src); |
| 52 if (len == 0) | |
| 1 | 53 { |
| 54 if (dest_str != NULL) | |
| 249 | 55 { |
| 56 strncpy (dest_str, "0", dest_len); | |
| 57 dest_str[dest_len - 1] = '\0'; | |
| 58 } | |
| 1 | 59 else |
| 56 | 60 dest_str = g_strdup ("0"); |
| 1 | 61 return (dest_str); |
| 62 } | |
| 63 | |
| 64 len += len / 3; | |
| 220 | 65 |
| 1 | 66 if (dest_str != NULL && len > dest_len) |
| 67 { | |
| 68 | |
| 69 for (i=0; i<dest_len - 1; i++) | |
| 70 dest_str[i] = 'X'; | |
| 71 dest_str[dest_len - 1] = '\0'; | |
| 72 return (dest_str); | |
| 73 } | |
| 74 | |
| 75 if (dest_str == NULL) | |
| 76 dest = g_malloc0 (len); | |
| 77 else | |
| 78 dest = dest_str; | |
| 79 | |
| 460 | 80 srclen = strlen (src); |
| 81 num = srclen / 3 - 1; | |
| 82 rem = srclen % 3; | |
| 83 | |
| 1 | 84 frompos = src; |
| 85 topos = dest; | |
| 86 for (i = 0; i < rem; i++) | |
| 87 *topos++ = *frompos++; | |
| 88 | |
| 89 if (*frompos != '\0') | |
| 90 { | |
| 91 if (rem != 0) | |
| 92 *topos++ = ','; | |
| 93 while (num > 0) | |
| 94 { | |
| 95 for (i = 0; i < 3; i++) | |
| 96 *topos++ = *frompos++; | |
| 97 *topos++ = ','; | |
| 98 num--; | |
| 99 } | |
| 100 for (i = 0; i < 3; i++) | |
| 101 *topos++ = *frompos++; | |
| 102 } | |
| 103 *topos = '\0'; | |
| 104 return (dest); | |
| 105 } | |
| 106 | |
| 289 | 107 #endif |
| 1 | 108 |
| 109 char * | |
| 110 alltrim (char *str) | |
| 111 { | |
| 112 char *pos, *newpos; | |
| 113 int diff; | |
| 114 | |
| 115 pos = str + strlen (str) - 1; | |
| 87 | 116 while (pos >= str && (*pos == ' ' || *pos == '\t')) |
| 1 | 117 *pos-- = '\0'; |
| 118 | |
| 119 pos = str; | |
| 120 diff = 0; | |
| 121 while (*pos++ == ' ') | |
| 122 diff++; | |
| 123 | |
| 124 if (diff == 0) | |
| 125 return (str); | |
| 126 | |
| 127 pos = str + diff; | |
| 128 newpos = str; | |
| 129 while (*pos != '\0') | |
| 130 *newpos++ = *pos++; | |
| 131 *newpos = '\0'; | |
| 132 | |
| 133 return (str); | |
| 134 } | |
| 135 | |
| 136 | |
| 137 char * | |
| 138 expand_path (const char *src) | |
| 139 { | |
| 307 | 140 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, *ntoken, |
| 141 tempchar; | |
| 1 | 142 struct passwd *pw; |
| 143 | |
| 144 pw = NULL; | |
| 124 | 145 str = g_strdup (src); |
| 1 | 146 |
| 147 if (*str == '~') | |
| 148 { | |
| 149 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
| 150 pw = getpwuid (geteuid ()); | |
| 151 else | |
| 152 { | |
| 153 if ((pos = strchr (str, '/')) != NULL) | |
| 154 *pos = '\0'; | |
| 155 | |
| 156 pw = getpwnam (str + 1); | |
| 157 | |
| 158 if (pos != NULL) | |
| 159 *pos = '/'; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 endpos = str; | |
| 164 newstr = NULL; | |
| 165 while ((pos = strchr (endpos, '/')) != NULL) | |
| 166 { | |
| 167 pos++; | |
| 204 | 168 |
| 307 | 169 for (ntoken = pos; *ntoken == '/'; ntoken++); |
| 170 | |
| 171 if ((endpos = strchr (ntoken, '/')) == NULL) | |
| 1 | 172 endpos = pos + strlen (pos); |
| 204 | 173 |
| 1 | 174 tempchar = *endpos; |
| 175 *endpos = '\0'; | |
| 204 | 176 |
| 307 | 177 if (strcmp (ntoken, "..") == 0) |
| 1 | 178 { |
| 179 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
| 307 | 180 { |
| 181 *prevpos = '\0'; | |
| 182 if (*newstr == '\0') | |
| 183 { | |
| 184 g_free (newstr); | |
| 185 newstr = NULL; | |
| 186 } | |
| 187 } | |
| 1 | 188 } |
| 307 | 189 else if (strcmp (ntoken, ".") != 0) |
| 1 | 190 { |
| 191 if (newstr == NULL) | |
| 56 | 192 newstr = g_strdup (pos - 1); |
| 1 | 193 else |
| 194 { | |
| 195 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
| 196 g_free (newstr); | |
| 197 newstr = tempstr; | |
| 198 } | |
| 199 } | |
| 204 | 200 |
| 1 | 201 *endpos = tempchar; |
| 202 if (*endpos == '\0') | |
| 203 break; | |
| 204 | 204 |
| 1 | 205 endpos = pos + 1; |
| 206 } | |
| 207 | |
| 204 | 208 if (endpos != NULL && *endpos != '\0' && newstr == NULL) |
| 209 { | |
| 210 if (strcmp (endpos, "..") == 0) | |
| 307 | 211 newstr = g_strdup ("/"); |
| 204 | 212 else |
| 213 newstr = g_strdup (endpos); | |
| 214 } | |
| 215 | |
| 1 | 216 if (newstr == NULL || *newstr == '\0') |
| 217 { | |
| 218 if (newstr != NULL) | |
| 219 g_free (newstr); | |
| 204 | 220 |
| 247 | 221 newstr = g_strdup ("/"); |
| 1 | 222 } |
| 223 | |
| 224 g_free (str); | |
| 225 | |
| 226 if (pw != NULL) | |
| 227 { | |
| 228 if ((pos = strchr (newstr, '/')) == NULL) | |
| 124 | 229 str = g_strdup (pw->pw_dir); |
| 1 | 230 else |
| 247 | 231 str = gftp_build_path (pw->pw_dir, pos, NULL); |
| 1 | 232 |
| 233 g_free (newstr); | |
| 234 newstr = str; | |
| 235 } | |
| 236 | |
| 237 return (newstr); | |
| 238 } | |
| 239 | |
| 240 | |
| 241 void | |
| 242 make_nonnull (char **str) | |
| 243 { | |
| 244 if (*str == NULL) | |
| 245 *str = g_malloc0 (1); | |
| 246 } | |
| 247 | |
| 248 | |
| 87 | 249 /* FIXME - is there a replacement for this */ |
| 1 | 250 int |
| 374 | 251 gftp_match_filespec (const char *filename, const char *filespec) |
| 1 | 252 { |
| 374 | 253 const char *filepos, *wcpos, *pos; |
| 254 char search_str[20], *newpos; | |
| 1 | 255 size_t len, curlen; |
| 256 | |
| 257 if (filename == NULL || *filename == '\0' || | |
| 258 filespec == NULL || *filespec == '\0') | |
| 259 return(1); | |
| 260 | |
| 261 filepos = filename; | |
| 262 wcpos = filespec; | |
| 263 while(1) | |
| 264 { | |
| 265 if (*wcpos == '\0') | |
| 266 return (1); | |
| 267 else if (*filepos == '\0') | |
| 268 return(0); | |
| 269 else if(*wcpos == '?') | |
| 270 { | |
| 271 wcpos++; | |
| 272 filepos++; | |
| 273 } | |
| 274 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
| 275 return(1); | |
| 276 else if(*wcpos == '*') | |
| 277 { | |
| 278 len = sizeof (search_str); | |
| 279 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
| 280 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
| 281 curlen++, *newpos++ = *pos++); | |
| 282 *newpos = '\0'; | |
| 283 | |
| 284 if ((filepos = strstr (filepos, search_str)) == NULL) | |
| 285 return(0); | |
| 286 wcpos += curlen + 1; | |
| 287 filepos += curlen; | |
| 288 } | |
| 289 else if(*wcpos++ != *filepos++) | |
| 290 return(0); | |
| 291 } | |
| 292 return (1); | |
| 293 } | |
| 294 | |
| 295 | |
| 243 | 296 static void |
| 297 gftp_info (void) | |
| 298 { | |
| 260 | 299 int i; |
| 300 | |
| 243 | 301 printf ("%s\n", gftp_version); |
| 302 | |
| 307 | 303 #ifdef _REENTRANT |
| 304 printf ("#define _REENTRANT\n"); | |
| 305 #endif | |
| 306 | |
| 289 | 307 #ifdef _GNU_SOURCE |
| 308 printf ("#define _GNU_SOURCE\n"); | |
| 309 #endif | |
| 310 | |
| 243 | 311 #ifdef _LARGEFILE_SOURCE |
| 260 | 312 printf ("#define _LARGEFILE_SOURCE\n"); |
| 243 | 313 #endif |
| 314 | |
| 315 #ifdef _FILE_OFFSET_BITS | |
| 316 printf ("#define _FILE_OFFSET_BITS %d\n", _FILE_OFFSET_BITS); | |
| 317 #endif | |
| 318 | |
| 319 printf ("sizeof (off_t) = %d\n", sizeof (off_t)); | |
| 320 | |
| 321 #ifdef HAVE_GETADDRINFO | |
| 322 printf ("#define HAVE_GETADDRINFO\n"); | |
| 323 #endif | |
| 324 | |
| 325 #ifdef HAVE_GAI_STRERROR | |
| 326 printf ("#define HAVE_GAI_STRERROR\n"); | |
| 327 #endif | |
| 328 | |
| 329 #ifdef HAVE_GETDTABLESIZE | |
| 330 printf ("#define HAVE_GETDTABLESIZE\n"); | |
| 331 #endif | |
| 332 | |
| 333 #ifdef G_HAVE_GINT64 | |
| 334 printf ("#define G_HAVE_GINT64\n"); | |
| 335 #endif | |
| 336 | |
| 337 #ifdef HAVE_LIBREADLINE | |
| 338 printf ("#define HAVE_LIBREADLINE\n"); | |
| 339 #endif | |
| 340 | |
| 341 #ifdef ENABLE_NLS | |
| 342 printf ("#define ENABLE_NLS\n"); | |
| 343 #endif | |
| 344 | |
| 345 #ifdef HAVE_GETTEXT | |
| 346 printf ("#define HAVE_GETTEXT\n"); | |
| 347 #endif | |
| 348 | |
| 328 | 349 #ifdef HAVE_INTL_PRINTF |
| 350 printf ("#define HAVE_INTL_PRINTF\n"); | |
| 351 #endif | |
| 352 | |
| 243 | 353 printf ("glib version: %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, |
| 354 GLIB_MICRO_VERSION); | |
| 355 | |
| 356 printf ("PTY implementation: %s\n", gftp_get_pty_impl ()); | |
| 357 | |
| 358 #ifdef USE_SSL | |
| 359 printf ("OpenSSL version: 0x%lx\n", OPENSSL_VERSION_NUMBER); | |
| 360 #endif | |
| 260 | 361 |
| 362 printf ("Enabled protocols: "); | |
| 363 for (i=0; gftp_protocols[i].name != NULL; i++) | |
| 364 { | |
| 365 printf ("%s ", gftp_protocols[i].name); | |
| 366 } | |
| 367 printf ("\n"); | |
| 243 | 368 } |
| 369 | |
| 370 | |
| 1 | 371 int |
| 372 gftp_parse_command_line (int *argc, char ***argv) | |
| 373 { | |
| 374 if (*argc > 1) | |
| 375 { | |
| 87 | 376 if (strcmp (argv[0][1], "--help") == 0 || |
| 377 strcmp (argv[0][1], "-h") == 0) | |
| 349 | 378 { |
| 379 gftp_usage (); | |
| 380 exit (0); | |
| 381 } | |
| 87 | 382 else if (strcmp (argv[0][1], "--version") == 0 || |
| 383 strcmp (argv[0][1], "-v") == 0) | |
| 1 | 384 { |
| 349 | 385 printf ("%s\n", gftp_version); |
| 386 exit (0); | |
| 1 | 387 } |
| 243 | 388 else if (strcmp (argv[0][1], "--info") == 0) |
| 389 { | |
| 390 gftp_info (); | |
| 349 | 391 exit (0); |
| 243 | 392 } |
| 1 | 393 } |
| 394 return (0); | |
| 395 } | |
| 396 | |
| 397 | |
| 398 void | |
| 399 gftp_usage (void) | |
| 400 { | |
| 356 | 401 printf (_("usage: gftp " GFTP_URL_USAGE "\n")); |
| 1 | 402 exit (0); |
| 403 } | |
| 404 | |
| 405 | |
| 406 gint | |
| 407 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
| 408 { | |
| 409 return (strcmp ((char *) path1, (char *) path2) == 0); | |
| 410 } | |
| 411 | |
| 412 | |
| 413 guint | |
| 414 string_hash_function (gconstpointer key) | |
| 415 { | |
| 59 | 416 guint ret; |
| 417 int i; | |
| 418 | |
| 419 ret = 0; | |
| 87 | 420 for (i=0; ((char *) key)[i] != '\0' && i < 3; i++) |
| 59 | 421 ret += ((char *) key)[i]; |
| 422 | |
| 423 return (ret); | |
| 1 | 424 } |
| 425 | |
| 426 | |
| 427 void | |
| 428 free_file_list (GList * filelist) | |
| 429 { | |
| 430 gftp_file * tempfle; | |
| 431 GList * templist; | |
| 432 | |
| 433 templist = filelist; | |
| 434 while (templist != NULL) | |
| 435 { | |
| 436 tempfle = templist->data; | |
| 437 free_fdata (tempfle); | |
| 438 templist = templist->next; | |
| 439 } | |
| 440 g_list_free (filelist); | |
| 441 } | |
| 442 | |
| 443 | |
| 444 void | |
| 445 free_fdata (gftp_file * fle) | |
| 446 { | |
| 447 if (fle->file) | |
| 448 g_free (fle->file); | |
| 184 | 449 if (fle->utf8_file) |
| 450 g_free (fle->utf8_file); | |
| 1 | 451 if (fle->user) |
| 452 g_free (fle->user); | |
| 453 if (fle->group) | |
| 454 g_free (fle->group); | |
| 455 if (fle->destfile) | |
| 456 g_free (fle->destfile); | |
| 58 | 457 if (fle->fd > 0) |
| 102 | 458 close (fle->fd); |
| 1 | 459 g_free (fle); |
| 460 } | |
| 461 | |
| 462 | |
| 463 gftp_file * | |
| 464 copy_fdata (gftp_file * fle) | |
| 465 { | |
| 466 gftp_file * newfle; | |
| 467 | |
| 468 newfle = g_malloc0 (sizeof (*newfle)); | |
| 469 memcpy (newfle, fle, sizeof (*newfle)); | |
| 470 | |
| 471 if (fle->file) | |
| 87 | 472 newfle->file = g_strdup (fle->file); |
| 1 | 473 |
| 243 | 474 if (fle->utf8_file) |
| 475 newfle->utf8_file = g_strdup (fle->utf8_file); | |
| 476 | |
| 1 | 477 if (fle->user) |
| 87 | 478 newfle->user = g_strdup (fle->user); |
| 1 | 479 |
| 480 if (fle->group) | |
| 87 | 481 newfle->group = g_strdup (fle->group); |
| 1 | 482 |
| 483 if (fle->destfile) | |
| 87 | 484 newfle->destfile = g_strdup (fle->destfile); |
| 485 | |
| 1 | 486 return (newfle); |
| 487 } | |
| 488 | |
| 489 | |
| 490 int | |
| 491 compare_request (gftp_request * request1, gftp_request * request2, | |
| 492 int compare_dirs) | |
| 493 { | |
| 494 char *strarr[3][2]; | |
| 495 int i, ret; | |
| 496 | |
| 497 ret = 1; | |
| 87 | 498 if (request1->protonum == request2->protonum && |
| 1 | 499 request1->port == request2->port) |
| 500 { | |
| 501 strarr[0][0] = request1->hostname; | |
| 502 strarr[0][1] = request2->hostname; | |
| 503 strarr[1][0] = request1->username; | |
| 504 strarr[1][1] = request2->username; | |
| 505 if (compare_dirs) | |
| 506 { | |
| 507 strarr[2][0] = request1->directory; | |
| 508 strarr[2][1] = request2->directory; | |
| 509 } | |
| 510 else | |
| 511 strarr[2][0] = strarr[2][1] = ""; | |
| 512 | |
| 513 for (i = 0; i < 3; i++) | |
| 514 { | |
| 515 if ((strarr[i][0] && !strarr[i][1]) || | |
| 516 (!strarr[i][0] && strarr[i][1])) | |
| 517 { | |
| 518 ret = 0; | |
| 519 break; | |
| 520 } | |
| 521 | |
| 522 if (strarr[i][0] && strarr[i][1] && | |
| 523 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
| 524 { | |
| 525 ret = 0; | |
| 526 break; | |
| 527 } | |
| 528 } | |
| 529 } | |
| 530 else | |
| 531 ret = 0; | |
| 532 return (ret); | |
| 533 } | |
| 534 | |
| 535 | |
| 129 | 536 gftp_transfer * |
| 537 gftp_tdata_new (void) | |
| 538 { | |
| 227 | 539 #if GLIB_MAJOR_VERSION == 1 |
| 540 static GStaticMutex init_mutex = G_STATIC_MUTEX_INIT; | |
| 541 #endif | |
| 129 | 542 gftp_transfer * tdata; |
| 543 | |
| 544 tdata = g_malloc0 (sizeof (*tdata)); | |
| 227 | 545 |
| 546 #if GLIB_MAJOR_VERSION == 1 | |
| 547 tdata->statmutex = init_mutex; | |
| 548 tdata->structmutex = init_mutex; | |
| 549 #else | |
| 168 | 550 g_static_mutex_init (&tdata->statmutex); |
| 551 g_static_mutex_init (&tdata->structmutex); | |
| 227 | 552 #endif |
| 553 | |
| 129 | 554 return (tdata); |
| 555 } | |
| 556 | |
| 557 | |
| 1 | 558 void |
| 559 free_tdata (gftp_transfer * tdata) | |
| 560 { | |
| 561 if (tdata->fromreq != NULL) | |
| 67 | 562 gftp_request_destroy (tdata->fromreq, 1); |
| 1 | 563 if (tdata->toreq != NULL) |
| 67 | 564 gftp_request_destroy (tdata->toreq, 1); |
| 1 | 565 free_file_list (tdata->files); |
| 566 g_free (tdata); | |
| 567 } | |
| 568 | |
| 569 | |
| 570 gftp_request * | |
| 368 | 571 gftp_copy_request (gftp_request * req) |
| 1 | 572 { |
| 573 gftp_request * newreq; | |
| 574 | |
| 66 | 575 newreq = gftp_request_new (); |
| 1 | 576 |
| 577 if (req->hostname) | |
| 56 | 578 newreq->hostname = g_strdup (req->hostname); |
| 1 | 579 if (req->username) |
| 56 | 580 newreq->username = g_strdup (req->username); |
| 1 | 581 if (req->password) |
| 56 | 582 newreq->password = g_strdup (req->password); |
| 1 | 583 if (req->account) |
| 56 | 584 newreq->account = g_strdup (req->account); |
| 1 | 585 if (req->directory) |
| 56 | 586 newreq->directory = g_strdup (req->directory); |
| 66 | 587 newreq->port = req->port; |
| 588 newreq->use_proxy = req->use_proxy; | |
| 589 newreq->logging_function = req->logging_function; | |
| 151 | 590 newreq->free_hostp = 0; |
| 296 | 591 newreq->hostp = NULL; |
| 151 | 592 |
| 368 | 593 gftp_copy_local_options (&newreq->local_options_vars, |
| 594 &newreq->local_options_hash, | |
| 429 | 595 &newreq->num_local_options_vars, |
| 368 | 596 req->local_options_vars, |
| 597 req->num_local_options_vars); | |
| 1 | 598 |
| 451 | 599 if (req->init != NULL && req->init (newreq) < 0) |
| 173 | 600 { |
| 601 gftp_request_destroy (newreq, 1); | |
| 602 return (NULL); | |
| 603 } | |
| 1 | 604 |
| 309 | 605 gftp_copy_param_options (newreq, req); |
| 606 | |
| 1 | 607 return (newreq); |
| 608 } | |
| 609 | |
| 610 | |
| 16 | 611 static gint |
| 612 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
| 613 { | |
| 614 const gftp_file * f1, * f2; | |
| 615 | |
| 616 f1 = a; | |
| 617 f2 = b; | |
| 618 return (strcmp (f1->file, f2->file)); | |
| 619 } | |
| 620 | |
| 621 | |
| 622 static gint | |
| 623 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 624 { | |
| 625 const gftp_file * f1, * f2; | |
| 626 gint ret; | |
| 627 | |
| 628 f1 = a; | |
| 629 f2 = b; | |
| 630 ret = strcmp (f1->file, f2->file); | |
| 84 | 631 return (ret * -1); |
| 16 | 632 } |
| 633 | |
| 634 | |
| 635 static gint | |
| 636 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
| 637 { | |
| 638 const gftp_file * f1, * f2; | |
| 639 | |
| 640 f1 = a; | |
| 641 f2 = b; | |
| 642 return (strcmp (f1->user, f2->user)); | |
| 643 } | |
| 644 | |
| 645 | |
| 646 static gint | |
| 647 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 648 { | |
| 649 const gftp_file * f1, * f2; | |
| 650 gint ret; | |
| 651 | |
| 652 f1 = a; | |
| 653 f2 = b; | |
| 654 ret = strcmp (f1->user, f2->user); | |
| 84 | 655 return (ret * -1); |
| 16 | 656 } |
| 657 | |
| 658 | |
| 659 static gint | |
| 660 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
| 661 { | |
| 662 const gftp_file * f1, * f2; | |
| 663 | |
| 664 f1 = a; | |
| 665 f2 = b; | |
| 666 return (strcmp (f1->group, f2->group)); | |
| 667 } | |
| 668 | |
| 669 | |
| 670 static gint | |
| 671 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 672 { | |
| 673 const gftp_file * f1, * f2; | |
| 674 gint ret; | |
| 675 | |
| 676 f1 = a; | |
| 677 f2 = b; | |
| 678 ret = strcmp (f1->group, f2->group); | |
| 84 | 679 return (ret * -1); |
| 16 | 680 } |
| 681 | |
| 682 | |
| 683 static gint | |
| 684 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
| 685 { | |
| 686 const gftp_file * f1, * f2; | |
| 687 | |
| 688 f1 = a; | |
| 689 f2 = b; | |
| 499 | 690 if (f1->st_mode < f2->st_mode) |
| 691 return (-1); | |
| 692 else if (f1->st_mode == f2->st_mode) | |
| 693 return (0); | |
| 694 else | |
| 695 return (1); | |
| 16 | 696 } |
| 697 | |
| 698 | |
| 699 static gint | |
| 700 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 701 { | |
| 702 const gftp_file * f1, * f2; | |
| 703 | |
| 704 f1 = a; | |
| 705 f2 = b; | |
| 499 | 706 if (f1->st_mode < f2->st_mode) |
| 707 return (1); | |
| 708 else if (f1->st_mode == f2->st_mode) | |
| 709 return (0); | |
| 710 else | |
| 711 return (-1); | |
| 16 | 712 } |
| 713 | |
| 714 | |
| 715 static gint | |
| 716 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
| 717 { | |
| 718 const gftp_file * f1, * f2; | |
| 719 | |
| 720 f1 = a; | |
| 721 f2 = b; | |
| 722 if (f1->size < f2->size) | |
| 723 return (-1); | |
| 724 else if (f1->size == f2->size) | |
| 725 return (0); | |
| 726 else | |
| 727 return (1); | |
| 728 } | |
| 729 | |
| 730 | |
| 731 static gint | |
| 732 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 733 { | |
| 734 const gftp_file * f1, * f2; | |
| 735 | |
| 736 f1 = a; | |
| 737 f2 = b; | |
| 738 if (f1->size < f2->size) | |
| 739 return (1); | |
| 740 else if (f1->size == f2->size) | |
| 741 return (0); | |
| 742 else | |
| 743 return (-1); | |
| 744 } | |
| 745 | |
| 746 | |
| 747 static gint | |
| 748 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
| 749 { | |
| 750 const gftp_file * f1, * f2; | |
| 751 | |
| 752 f1 = a; | |
| 753 f2 = b; | |
| 754 if (f1->datetime < f2->datetime) | |
| 755 return (-1); | |
| 756 else if (f1->datetime == f2->datetime) | |
| 757 return (0); | |
| 758 else | |
| 759 return (1); | |
| 760 } | |
| 761 | |
| 762 | |
| 763 static gint | |
| 764 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 765 { | |
| 766 const gftp_file * f1, * f2; | |
| 767 | |
| 768 f1 = a; | |
| 769 f2 = b; | |
| 770 if (f1->datetime < f2->datetime) | |
| 771 return (1); | |
| 772 else if (f1->datetime == f2->datetime) | |
| 773 return (0); | |
| 774 else | |
| 775 return (-1); | |
| 776 } | |
| 777 | |
| 778 | |
| 779 GList * | |
| 780 gftp_sort_filelist (GList * filelist, int column, int asds) | |
| 781 { | |
| 782 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
| 783 GCompareFunc sortfunc; | |
| 784 gftp_file * tempfle; | |
| 325 | 785 intptr_t sort_dirs_first; |
| 16 | 786 |
| 422 | 787 if (filelist == NULL) /* nothing to sort */ |
| 788 return (filelist); | |
| 789 | |
| 16 | 790 files = dirs = dotdot = NULL; |
| 791 | |
| 792 if (column == GFTP_SORT_COL_FILE) | |
| 793 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
| 794 else if (column == GFTP_SORT_COL_SIZE) | |
| 795 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
| 796 else if (column == GFTP_SORT_COL_USER) | |
| 797 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
| 798 else if (column == GFTP_SORT_COL_GROUP) | |
| 799 sortfunc = asds ? | |
| 800 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
| 801 else if (column == GFTP_SORT_COL_DATETIME) | |
| 802 sortfunc = asds ? | |
| 122 | 803 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; |
| 804 else if (column == GFTP_SORT_COL_ATTRIBS) | |
| 16 | 805 sortfunc = asds ? |
| 806 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
| 122 | 807 else /* Don't sort */ |
| 808 return (filelist); | |
| 809 | |
| 810 sort_dirs_first = 1; | |
| 811 gftp_lookup_global_option ("sort_dirs_first", &sort_dirs_first); | |
| 16 | 812 |
| 813 for (tempitem = filelist; tempitem != NULL; ) | |
| 814 { | |
| 815 tempfle = tempitem->data; | |
| 816 insitem = tempitem; | |
| 817 tempitem = tempitem->next; | |
| 818 insitem->next = NULL; | |
| 819 | |
| 820 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
| 821 dotdot = insitem; | |
| 499 | 822 else if (sort_dirs_first && S_ISDIR (tempfle->st_mode)) |
| 16 | 823 { |
| 824 insitem->next = dirs; | |
| 825 dirs = insitem; | |
| 826 } | |
| 827 else | |
| 828 { | |
| 829 insitem->next = files; | |
| 830 files = insitem; | |
| 831 } | |
| 832 } | |
| 833 | |
| 834 if (dirs != NULL) | |
| 835 dirs = g_list_sort (dirs, sortfunc); | |
| 836 if (files != NULL) | |
| 837 files = g_list_sort (files, sortfunc); | |
| 838 | |
| 839 filelist = dotdot; | |
| 840 | |
| 841 if (filelist == NULL) | |
| 842 filelist = dirs; | |
| 843 else | |
| 844 filelist = g_list_concat (filelist, dirs); | |
| 845 | |
| 846 if (filelist == NULL) | |
| 847 filelist = files; | |
| 848 else | |
| 849 filelist = g_list_concat (filelist, files); | |
| 850 | |
| 39 | 851 /* I haven't check this, but I'm pretty sure some older versions of glib |
| 852 had a bug that the prev pointer wasn't being sent to NULL */ | |
| 853 filelist->prev = NULL; | |
| 16 | 854 return (filelist); |
| 855 } | |
| 856 | |
| 125 | 857 |
| 131 | 858 char * |
| 859 gftp_gen_ls_string (gftp_file * fle, char *file_prefixstr, char *file_suffixstr) | |
| 860 { | |
| 499 | 861 char *tempstr1, *tempstr2, *ret, tstr[50], *attribs; |
| 131 | 862 struct tm *lt; |
| 863 time_t t; | |
| 864 | |
| 865 lt = localtime (&fle->datetime); | |
| 866 | |
| 499 | 867 attribs = gftp_convert_attributes_from_mode_t (fle->st_mode); |
| 868 tempstr1 = g_strdup_printf ("%10s %8s %8s", attribs, fle->user, fle->group); | |
| 869 g_free (attribs); | |
| 131 | 870 |
| 499 | 871 if (GFTP_IS_SPECIAL_DEVICE (fle->st_mode)) |
| 131 | 872 tempstr2 = g_strdup_printf ("%d, %d", major (fle->size), minor (fle->size)); |
| 873 else | |
| 874 { | |
| 875 #if defined (_LARGEFILE_SOURCE) | |
| 516 | 876 tempstr2 = g_strdup_printf ("%11lld", fle->size); |
| 131 | 877 #else |
| 878 tempstr2 = g_strdup_printf ("%11ld", fle->size); | |
| 879 #endif | |
| 880 } | |
| 881 | |
| 882 time (&t); | |
| 883 | |
| 884 if (fle->datetime > t || t - 3600*24*90 > fle->datetime) | |
| 885 strftime (tstr, sizeof (tstr), "%b %d %Y", lt); | |
| 886 else | |
| 887 strftime (tstr, sizeof (tstr), "%b %d %H:%M", lt); | |
| 888 | |
| 889 if (file_prefixstr == NULL) | |
| 890 file_prefixstr = ""; | |
| 891 if (file_suffixstr == NULL) | |
| 892 file_suffixstr = ""; | |
| 893 | |
| 894 ret = g_strdup_printf ("%s %s %s %s%s%s", tempstr1, tempstr2, tstr, | |
| 184 | 895 file_prefixstr, |
| 896 fle->utf8_file != NULL ? fle->utf8_file : fle->file, | |
| 897 file_suffixstr); | |
| 131 | 898 |
| 899 g_free (tempstr1); | |
| 900 g_free (tempstr2); | |
| 901 | |
| 902 return (ret); | |
| 903 } | |
| 904 | |
| 905 | |
| 125 | 906 #if !defined (HAVE_GETADDRINFO) || !defined (HAVE_GAI_STRERROR) |
| 907 | |
| 908 struct hostent * | |
| 909 r_gethostbyname (const char *name, struct hostent *result_buf, int *h_errnop) | |
| 910 { | |
| 911 static GStaticMutex hostfunclock = G_STATIC_MUTEX_INIT; | |
| 912 struct hostent *hent; | |
| 913 | |
| 914 if (g_thread_supported ()) | |
| 915 g_static_mutex_lock (&hostfunclock); | |
| 916 | |
| 917 if ((hent = gethostbyname (name)) == NULL) | |
| 918 { | |
| 919 if (h_errnop) | |
| 920 *h_errnop = h_errno; | |
| 921 } | |
| 922 else | |
| 923 { | |
| 924 *result_buf = *hent; | |
| 925 hent = result_buf; | |
| 926 } | |
| 927 | |
| 928 if (g_thread_supported ()) | |
| 929 g_static_mutex_unlock (&hostfunclock); | |
| 930 | |
| 931 return (hent); | |
| 932 } | |
| 933 | |
| 934 #endif /* !HAVE_GETADDRINFO */ | |
| 935 | |
| 936 struct servent * | |
| 937 r_getservbyname (const char *name, const char *proto, | |
| 938 struct servent *result_buf, int *h_errnop) | |
| 939 { | |
| 940 static GStaticMutex servfunclock = G_STATIC_MUTEX_INIT; | |
| 941 struct servent *sent; | |
| 942 | |
| 943 if (g_thread_supported ()) | |
| 944 g_static_mutex_lock (&servfunclock); | |
| 945 | |
| 946 if ((sent = getservbyname (name, proto)) == NULL) | |
| 947 { | |
| 948 if (h_errnop) | |
| 949 *h_errnop = h_errno; | |
| 950 } | |
| 951 else | |
| 952 { | |
| 953 *result_buf = *sent; | |
| 954 sent = result_buf; | |
| 955 } | |
| 956 | |
| 957 if (g_thread_supported ()) | |
| 958 g_static_mutex_unlock (&servfunclock); | |
| 959 return (sent); | |
| 960 } | |
| 961 | |
| 168 | 962 |
| 963 char * | |
| 964 base64_encode (char *str) | |
| 965 { | |
| 966 | |
| 967 /* The standard to Base64 encoding can be found in RFC2045 */ | |
| 968 | |
| 969 char *newstr, *newpos, *fillpos, *pos; | |
| 970 unsigned char table[64], encode[3]; | |
| 460 | 971 size_t slen, num; |
| 972 int i; | |
| 168 | 973 |
| 974 for (i = 0; i < 26; i++) | |
| 975 { | |
| 976 table[i] = 'A' + i; | |
| 977 table[i + 26] = 'a' + i; | |
| 978 } | |
| 979 | |
| 980 for (i = 0; i < 10; i++) | |
| 981 table[i + 52] = '0' + i; | |
| 982 | |
| 983 table[62] = '+'; | |
| 207 | 984 table[63] = '/'; |
| 168 | 985 |
| 460 | 986 slen = strlen (str); |
| 987 num = slen / 3; | |
| 988 if (slen % 3 > 0) | |
| 168 | 989 num++; |
| 460 | 990 |
| 168 | 991 newstr = g_malloc (num * 4 + 1); |
| 992 newstr[num * 4] = '\0'; | |
| 993 newpos = newstr; | |
| 994 | |
| 995 pos = str; | |
| 996 while (*pos != '\0') | |
| 997 { | |
| 998 memset (encode, 0, sizeof (encode)); | |
| 999 for (i = 0; i < 3 && *pos != '\0'; i++) | |
| 1000 encode[i] = *pos++; | |
| 1001 | |
| 1002 fillpos = newpos; | |
| 1003 *newpos++ = table[encode[0] >> 2]; | |
| 1004 *newpos++ = table[(encode[0] & 3) << 4 | encode[1] >> 4]; | |
| 1005 *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6]; | |
| 1006 *newpos++ = table[encode[2] & 0x3F]; | |
| 1007 while (i < 3) | |
| 1008 fillpos[++i] = '='; | |
| 1009 } | |
| 1010 return (newstr); | |
| 1011 } | |
| 1012 | |
| 199 | 1013 |
| 1014 void | |
| 1015 gftp_free_bookmark (gftp_bookmarks_var * entry) | |
| 1016 { | |
| 1017 if (entry->hostname) | |
| 1018 g_free (entry->hostname); | |
| 1019 if (entry->remote_dir) | |
| 1020 g_free (entry->remote_dir); | |
| 1021 if (entry->local_dir) | |
| 1022 g_free (entry->local_dir); | |
| 1023 if (entry->user) | |
| 1024 g_free (entry->user); | |
| 1025 if (entry->pass) | |
| 1026 g_free (entry->pass); | |
| 1027 if (entry->acct) | |
| 1028 g_free (entry->acct); | |
| 1029 if (entry->protocol) | |
| 1030 g_free (entry->protocol); | |
| 1031 | |
| 1032 if (entry->local_options_vars != NULL) | |
| 1033 { | |
| 201 | 1034 gftp_config_free_options (entry->local_options_vars, |
| 1035 entry->local_options_hash, | |
| 1036 entry->num_local_options_vars); | |
| 1037 | |
| 199 | 1038 entry->local_options_vars = NULL; |
| 201 | 1039 entry->local_options_hash = NULL; |
| 199 | 1040 entry->num_local_options_vars = 0; |
| 1041 } | |
| 1042 } | |
| 1043 | |
| 201 | 1044 |
| 1045 void | |
| 1046 gftp_shutdown (void) | |
| 1047 { | |
| 203 | 1048 #ifdef WITH_DMALLOC |
| 201 | 1049 gftp_config_vars * cv; |
| 1050 GList * templist; | |
| 203 | 1051 #endif |
| 201 | 1052 |
| 1053 if (gftp_logfd != NULL) | |
| 1054 fclose (gftp_logfd); | |
| 1055 | |
| 1056 gftp_clear_cache_files (); | |
| 1057 | |
| 1058 if (gftp_configuration_changed) | |
| 1059 gftp_write_config_file (); | |
| 1060 | |
| 1061 #ifdef WITH_DMALLOC | |
| 1062 if (gftp_global_options_htable != NULL) | |
| 1063 g_hash_table_destroy (gftp_global_options_htable); | |
| 1064 | |
| 1065 if (gftp_config_list_htable != NULL) | |
| 1066 g_hash_table_destroy (gftp_config_list_htable); | |
| 1067 | |
| 1068 if (gftp_bookmarks_htable != NULL) | |
| 1069 g_hash_table_destroy (gftp_bookmarks_htable); | |
| 1070 | |
| 1071 for (templist = gftp_options_list; | |
| 1072 templist != NULL; | |
| 1073 templist = templist->next) | |
| 1074 { | |
| 1075 cv = templist->data; | |
| 1076 gftp_config_free_options (cv, NULL, -1); | |
| 1077 } | |
| 1078 | |
| 1079 gftp_bookmarks_destroy (gftp_bookmarks); | |
| 1080 | |
| 1081 dmalloc_shutdown (); | |
| 1082 #endif | |
| 1083 | |
| 1084 exit (0); | |
| 1085 } | |
| 1086 | |
| 207 | 1087 |
| 1088 GList * | |
| 1089 get_next_selection (GList * selection, GList ** list, int *curnum) | |
| 1090 { | |
| 1091 gftp_file * tempfle; | |
| 1092 int i, newpos; | |
| 1093 | |
| 1094 newpos = GPOINTER_TO_INT (selection->data); | |
| 1095 i = *curnum - newpos; | |
| 1096 | |
| 1097 if (i < 0) | |
| 1098 { | |
| 1099 while (i != 0) | |
| 1100 { | |
| 1101 tempfle = (*list)->data; | |
| 1102 if (tempfle->shown) | |
| 1103 { | |
| 1104 ++*curnum; | |
| 1105 i++; | |
| 1106 } | |
| 1107 *list = (*list)->next; | |
| 1108 } | |
| 1109 } | |
| 1110 else if (i > 0) | |
| 1111 { | |
| 1112 while (i != 0) | |
| 1113 { | |
| 1114 tempfle = (*list)->data; | |
| 1115 if (tempfle->shown) | |
| 1116 { | |
| 1117 --*curnum; | |
| 1118 i--; | |
| 1119 } | |
| 1120 *list = (*list)->prev; | |
| 1121 } | |
| 1122 } | |
| 1123 | |
| 1124 tempfle = (*list)->data; | |
| 1125 while ((*list)->next && !tempfle->shown) | |
| 1126 { | |
| 1127 *list = (*list)->next; | |
| 1128 tempfle = (*list)->data; | |
| 1129 } | |
| 1130 return (selection->next); | |
| 1131 } | |
| 1132 | |
| 1133 | |
| 227 | 1134 char * |
| 245 | 1135 gftp_build_path (const char *first_element, ...) |
| 227 | 1136 { |
| 1137 const char *element; | |
| 1138 size_t len, retlen; | |
| 1139 int add_separator; | |
| 1140 va_list args; | |
| 1141 char *ret; | |
| 1142 | |
| 245 | 1143 g_return_val_if_fail (first_element != NULL, NULL); |
| 227 | 1144 |
| 247 | 1145 ret = g_strdup (first_element); |
| 1146 retlen = strlen (ret); | |
| 227 | 1147 |
| 1148 va_start (args, first_element); | |
| 247 | 1149 for (element = va_arg (args, char *); |
| 227 | 1150 element != NULL; |
| 1151 element = va_arg (args, char *)) | |
| 1152 { | |
| 1153 len = strlen (element); | |
| 1154 | |
| 422 | 1155 if (len == 0) |
| 1156 continue; | |
| 1157 | |
| 369 | 1158 if (retlen > 0 && (ret[retlen - 1] == '/' || element[0] == '/')) |
| 227 | 1159 add_separator = 0; |
| 1160 else | |
| 1161 { | |
| 1162 add_separator = 1; | |
| 1163 len++; | |
| 1164 } | |
| 1165 | |
| 1166 retlen += len; | |
| 1167 ret = g_realloc (ret, retlen + 1); | |
| 1168 | |
| 1169 if (add_separator) | |
| 245 | 1170 strncat (ret, "/", retlen); |
| 1171 | |
| 1172 strncat (ret, element, retlen); | |
| 227 | 1173 } |
| 1174 | |
| 1175 return (ret); | |
| 1176 } | |
| 1177 | |
| 244 | 1178 |
| 1179 off_t | |
| 1180 gftp_parse_file_size (char *str) | |
| 1181 { | |
| 1182 #if defined (_LARGEFILE_SOURCE) | |
| 1183 return (strtoll (str, NULL, 10)); | |
| 1184 #else | |
| 1185 return (strtol (str, NULL, 10)); | |
| 1186 #endif | |
| 1187 } | |
| 1188 | |
| 290 | 1189 |
| 1190 void | |
| 1191 gftp_locale_init (void) | |
| 1192 { | |
| 1193 #ifdef HAVE_GETTEXT | |
| 1194 | |
| 1195 setlocale (LC_ALL, ""); | |
| 1196 textdomain ("gftp"); | |
| 320 | 1197 bindtextdomain ("gftp", LOCALE_DIR); |
| 290 | 1198 |
| 1199 #if GLIB_MAJOR_VERSION > 1 | |
| 1200 bind_textdomain_codeset ("gftp", "UTF-8"); | |
| 1201 #endif | |
| 1202 | |
| 320 | 1203 #endif /* HAVE_GETTEXT */ |
| 290 | 1204 } |
| 1205 | |
| 330 | 1206 /* Very primary encryption/decryption to make the passwords unreadable |
| 1207 with 'cat ~/.gftp/bookmarks'. | |
| 1208 | |
| 1209 Each character is separated in two nibbles. Then each nibble is stored | |
| 1210 under the form 01xxxx01. The resulted string is prefixed by a '$'. | |
| 1211 */ | |
| 1212 | |
| 1213 | |
| 1214 char * | |
| 1215 gftp_scramble_password (const char *password) | |
| 1216 { | |
| 1217 char *newstr, *newpos; | |
| 1218 | |
| 1219 if (strcmp (password, "@EMAIL@") == 0) | |
| 1220 return (g_strdup (password)); | |
| 1221 | |
| 460 | 1222 newstr = g_malloc (strlen (password) * 2 + 2); |
| 330 | 1223 newpos = newstr; |
| 1224 | |
| 1225 *newpos++ = '$'; | |
| 1226 | |
| 1227 while (*password != 0) | |
| 1228 { | |
| 1229 *newpos++ = ((*password >> 2) & 0x3c) | 0x41; | |
| 1230 *newpos++ = ((*password << 2) & 0x3c) | 0x41; | |
| 1231 password++; | |
| 1232 } | |
| 1233 *newpos = 0; | |
| 1234 | |
| 1235 return (newstr); | |
| 1236 } | |
| 1237 | |
| 1238 | |
| 1239 char * | |
| 1240 gftp_descramble_password (const char *password) | |
| 1241 { | |
| 1242 const char *passwordpos; | |
| 1243 char *newstr, *newpos; | |
| 1244 int error; | |
| 1245 | |
| 1246 if (*password != '$') | |
| 1247 return (g_strdup (password)); | |
| 1248 | |
| 1249 passwordpos = password + 1; | |
| 1250 newstr = g_malloc (strlen (passwordpos) / 2 + 1); | |
| 1251 newpos = newstr; | |
| 1252 | |
| 1253 error = 0; | |
| 1254 while (*passwordpos != '\0' && (*passwordpos + 1) != '\0') | |
| 1255 { | |
| 1256 if ((*passwordpos & 0xc3) != 0x41 || | |
| 1257 (*(passwordpos + 1) & 0xc3) != 0x41) | |
| 1258 { | |
| 1259 error = 1; | |
| 1260 break; | |
| 1261 } | |
| 1262 | |
| 1263 *newpos++ = ((*passwordpos & 0x3c) << 2) | | |
| 1264 ((*(passwordpos + 1) & 0x3c) >> 2); | |
| 1265 | |
| 1266 passwordpos += 2; | |
| 1267 } | |
| 1268 | |
| 1269 if (error) | |
| 1270 { | |
| 1271 g_free (newstr); | |
| 1272 return (g_strdup (password)); | |
| 1273 } | |
| 1274 | |
| 1275 *newpos = '\0'; | |
| 1276 return (newstr); | |
| 1277 } | |
| 1278 | |
| 378 | 1279 |
| 1280 int | |
| 1281 gftp_get_transfer_action (gftp_request * request, gftp_file * fle) | |
| 1282 { | |
| 1283 intptr_t overwrite_default; | |
| 1284 | |
| 1285 gftp_lookup_request_option (request, "overwrite_default", &overwrite_default); | |
| 1286 | |
| 1287 if (overwrite_default) | |
| 1288 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
| 1289 else if (fle->startsize == fle->size) | |
| 1290 fle->transfer_action = GFTP_TRANS_ACTION_SKIP; | |
| 1291 else if (fle->startsize > fle->size) | |
| 1292 fle->transfer_action = GFTP_TRANS_ACTION_OVERWRITE; | |
| 1293 else | |
| 1294 fle->transfer_action = GFTP_TRANS_ACTION_RESUME; | |
| 1295 | |
| 1296 return (fle->transfer_action); | |
| 1297 } | |
| 1298 | |
| 483 | 1299 |
| 1300 char * | |
| 1301 gftp_get_share_dir (void) | |
| 1302 { | |
| 1303 static char *gftp_share_dir = NULL; | |
| 1304 char *envval; | |
| 1305 | |
| 1306 if (gftp_share_dir == NULL) | |
| 1307 { | |
| 1308 envval = getenv ("GFTP_SHARE_DIR"); | |
| 1309 | |
| 1310 if (envval != NULL && *envval != '\0') | |
| 1311 gftp_share_dir = g_strdup (envval); | |
| 1312 else | |
| 1313 gftp_share_dir = SHARE_DIR; | |
| 1314 } | |
| 1315 | |
| 1316 return (gftp_share_dir); | |
| 1317 } | |
| 1318 |
