Mercurial > gftp.yaz
annotate lib/misc.c @ 56:a12bcbc2fce4
2002-11-11 Brian Masney <masneyb@gftp.org>
* src/gtk/dnd.c - fixes to DnD code
* src/gtk/gftp-gtk.[ch] - added main_thread_id variable
* src/gtk/misc-gtk.c (ftp_log) - don't check the user_data to see if
we're in a child thread, instead compare the value of pthread_self()
with main_thread_id
* src/gtk/chmod_dialog.c src/gtk/delete_dialog.c src/gtk/menu-items.c
src/gtk/mkdir_dialog.c src/gtk/rename_dialog.c src/gtk/transfer.c -
don't set user_data to 0x1 if we're in a child thread
* lib/gftp.h src/gtk/misc-gtk.c src/text/gftp-text.c - make
r_getservbyname() available even if HAVE_GERADDRINFO is defined
* lib/misc.c (make_ssh_exec_args) - if port is zero, lookup the default
port for the ssh service
* lib/protocols.c (gftp_connect_server) - if the port is zero, store
the default port for that protocol there
* src/gtk/transfer.c - added function update_window_transfer_bytes().
Be able to update the directory download progress in window1 now
* lib/config_file.c lib/misc.c lib/protocols.c lib/ssh.c lib/sshv2.c
src/text/gftp-text.c - use g_strdup() instead of g_strconcat() where
needed
| author | masneyb |
|---|---|
| date | Mon, 11 Nov 2002 23:16:12 +0000 |
| parents | 4bcfaf6307b5 |
| children | c01d91c10f6c |
| rev | line source |
|---|---|
| 1 | 1 /*****************************************************************************/ |
| 2 /* misc.c - general purpose routines */ | |
| 3 /* Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org> */ | |
| 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" | |
| 33 | 22 static const char cvsid[] = "$Id$"; |
| 1 | 23 |
| 24 char * | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
25 insert_commas (off_t number, char *dest_str, size_t dest_len) |
| 1 | 26 { |
| 27 char *frompos, *topos, src[50], *dest; | |
| 28 int len, num, rem, i; | |
| 29 | |
| 30 if (dest_str != NULL) | |
| 31 *dest_str = '\0'; | |
| 56 | 32 len = (number > 0 ? log10 (number) : 0) + 2; |
| 1 | 33 |
| 34 if (len <= 0) | |
| 35 { | |
| 36 if (dest_str != NULL) | |
| 37 strncpy (dest_str, "0", dest_len); | |
| 38 else | |
| 56 | 39 dest_str = g_strdup ("0"); |
| 1 | 40 return (dest_str); |
| 41 } | |
| 42 | |
| 43 len += len / 3; | |
| 44 if (dest_str != NULL && len > dest_len) | |
| 45 { | |
| 46 | |
| 47 for (i=0; i<dest_len - 1; i++) | |
| 48 dest_str[i] = 'X'; | |
| 49 dest_str[dest_len - 1] = '\0'; | |
| 50 return (dest_str); | |
| 51 } | |
| 52 | |
| 53 if (dest_str == NULL) | |
| 54 dest = g_malloc0 (len); | |
| 55 else | |
| 56 dest = dest_str; | |
| 57 | |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
58 #if defined (_LARGEFILE_SOURCE) |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
59 g_snprintf (src, sizeof (src), "%lld", number); |
|
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
60 #else |
| 1 | 61 g_snprintf (src, sizeof (src), "%ld", number); |
|
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
13
diff
changeset
|
62 #endif |
| 1 | 63 |
| 64 num = strlen (src) / 3 - 1; | |
| 65 rem = strlen (src) % 3; | |
| 66 frompos = src; | |
| 67 topos = dest; | |
| 68 for (i = 0; i < rem; i++) | |
| 69 *topos++ = *frompos++; | |
| 70 | |
| 71 if (*frompos != '\0') | |
| 72 { | |
| 73 if (rem != 0) | |
| 74 *topos++ = ','; | |
| 75 while (num > 0) | |
| 76 { | |
| 77 for (i = 0; i < 3; i++) | |
| 78 *topos++ = *frompos++; | |
| 79 *topos++ = ','; | |
| 80 num--; | |
| 81 } | |
| 82 for (i = 0; i < 3; i++) | |
| 83 *topos++ = *frompos++; | |
| 84 } | |
| 85 *topos = '\0'; | |
| 86 return (dest); | |
| 87 } | |
| 88 | |
| 89 | |
| 90 long | |
| 91 file_countlf (int filefd, long endpos) | |
| 92 { | |
| 93 char tempstr[255]; | |
| 94 long num, mypos; | |
| 95 ssize_t n; | |
| 96 int i; | |
| 97 | |
| 98 mypos = num = 0; | |
| 99 lseek (filefd, 0, SEEK_SET); | |
| 100 while ((n = read (filefd, tempstr, sizeof (tempstr))) > 0) | |
| 101 { | |
| 102 for (i = 0; i < n; i++) | |
| 103 { | |
| 104 if ((tempstr[i] == '\n') && (i > 0) && (tempstr[i - 1] != '\r') | |
| 105 && (endpos == 0 || mypos + i <= endpos)) | |
| 106 ++num; | |
| 107 } | |
| 108 mypos += n; | |
| 109 } | |
| 110 lseek (filefd, 0, SEEK_SET); | |
| 111 return (num); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 char * | |
| 116 alltrim (char *str) | |
| 117 { | |
| 118 char *pos, *newpos; | |
| 119 int diff; | |
| 120 | |
| 121 pos = str + strlen (str) - 1; | |
| 122 while (pos >= str && *pos == ' ') | |
| 123 *pos-- = '\0'; | |
| 124 | |
| 125 pos = str; | |
| 126 diff = 0; | |
| 127 while (*pos++ == ' ') | |
| 128 diff++; | |
| 129 | |
| 130 if (diff == 0) | |
| 131 return (str); | |
| 132 | |
| 133 pos = str + diff; | |
| 134 newpos = str; | |
| 135 while (*pos != '\0') | |
| 136 *newpos++ = *pos++; | |
| 137 *newpos = '\0'; | |
| 138 | |
| 139 return (str); | |
| 140 } | |
| 141 | |
| 142 | |
| 143 char * | |
| 144 expand_path (const char *src) | |
| 145 { | |
| 146 char *str, *pos, *endpos, *prevpos, *newstr, *tempstr, tempchar; | |
| 147 struct passwd *pw; | |
| 148 | |
| 149 pw = NULL; | |
| 150 str = g_malloc (strlen (src) + 1); | |
| 151 strcpy (str, src); | |
| 152 | |
| 153 if (*str == '~') | |
| 154 { | |
| 155 if (*(str + 1) == '/' || *(str + 1) == '\0') | |
| 156 pw = getpwuid (geteuid ()); | |
| 157 else | |
| 158 { | |
| 159 if ((pos = strchr (str, '/')) != NULL) | |
| 160 *pos = '\0'; | |
| 161 | |
| 162 pw = getpwnam (str + 1); | |
| 163 | |
| 164 if (pos != NULL) | |
| 165 *pos = '/'; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 endpos = str; | |
| 170 newstr = NULL; | |
| 171 while ((pos = strchr (endpos, '/')) != NULL) | |
| 172 { | |
| 173 pos++; | |
| 174 while (*pos == '/') | |
| 175 pos++; | |
| 176 if ((endpos = strchr (pos, '/')) == NULL) | |
| 177 endpos = pos + strlen (pos); | |
| 178 tempchar = *endpos; | |
| 179 *endpos = '\0'; | |
| 180 if (strcmp (pos, "..") == 0) | |
| 181 { | |
| 182 *(pos - 1) = '\0'; | |
| 183 if (newstr != NULL && (prevpos = strrchr (newstr, '/')) != NULL) | |
| 184 *prevpos = '\0'; | |
| 185 } | |
| 186 else if (strcmp (pos, ".") != 0) | |
| 187 { | |
| 188 if (newstr == NULL) | |
| 56 | 189 newstr = g_strdup (pos - 1); |
| 1 | 190 else |
| 191 { | |
| 192 tempstr = g_strconcat (newstr, pos - 1, NULL); | |
| 193 g_free (newstr); | |
| 194 newstr = tempstr; | |
| 195 } | |
| 196 } | |
| 197 *endpos = tempchar; | |
| 198 if (*endpos == '\0') | |
| 199 break; | |
| 200 endpos = pos + 1; | |
| 201 } | |
| 202 | |
| 203 if (newstr == NULL || *newstr == '\0') | |
| 204 { | |
| 205 if (newstr != NULL) | |
| 206 g_free (newstr); | |
| 207 newstr = g_malloc0 (2); | |
| 208 *newstr = '/'; | |
| 209 } | |
| 210 | |
| 211 g_free (str); | |
| 212 | |
| 213 if (pw != NULL) | |
| 214 { | |
| 215 if ((pos = strchr (newstr, '/')) == NULL) | |
| 216 { | |
| 217 str = g_malloc (strlen (pw->pw_dir) + 1); | |
| 218 strcpy (str, pw->pw_dir); | |
| 219 } | |
| 220 else | |
| 221 str = g_strconcat (pw->pw_dir, pos, NULL); | |
| 222 | |
| 223 g_free (newstr); | |
| 224 newstr = str; | |
| 225 } | |
| 226 | |
| 227 return (newstr); | |
| 228 } | |
| 229 | |
| 230 | |
| 231 void | |
| 232 remove_double_slashes (char *string) | |
| 233 { | |
| 234 char *newpos, *oldpos; | |
| 235 | |
| 236 oldpos = newpos = string; | |
| 237 while (*oldpos != '\0') | |
| 238 { | |
| 239 *newpos++ = *oldpos++; | |
| 240 if (*oldpos == '\0') | |
| 241 break; | |
| 242 while (*(newpos - 1) == '/' && *(oldpos) == '/') | |
| 243 oldpos++; | |
| 244 } | |
| 245 *newpos = '\0'; | |
| 246 if (string[strlen (string) - 1] == '/') | |
| 247 string[strlen (string) - 1] = '\0'; | |
| 248 } | |
| 249 | |
| 250 | |
| 251 void | |
| 252 make_nonnull (char **str) | |
| 253 { | |
| 254 if (*str == NULL) | |
| 255 *str = g_malloc0 (1); | |
| 256 } | |
| 257 | |
| 258 | |
| 259 int | |
| 260 copyfile (char *source, char *dest) | |
| 261 { | |
| 262 FILE *srcfd, *destfd; | |
| 263 char buf[8192]; | |
| 264 size_t n; | |
| 265 | |
| 266 if ((srcfd = fopen (source, "rb")) == NULL) | |
| 267 return (0); | |
| 268 | |
| 269 if ((destfd = fopen (dest, "wb")) == NULL) | |
| 270 { | |
| 271 fclose (srcfd); | |
| 272 return (0); | |
| 273 } | |
| 274 | |
| 275 while ((n = fread (buf, 1, sizeof (buf), srcfd)) > 0) | |
| 276 fwrite (buf, 1, n, destfd); | |
| 277 | |
| 278 fclose (srcfd); | |
| 279 fclose (destfd); | |
| 280 | |
| 281 return (1); | |
| 282 } | |
| 283 | |
| 284 | |
| 285 int | |
| 286 gftp_match_filespec (char *filename, char *filespec) | |
| 287 { | |
| 288 char *filepos, *wcpos, *pos, *newpos, search_str[20]; | |
| 289 size_t len, curlen; | |
| 290 | |
| 291 if (filename == NULL || *filename == '\0' || | |
| 292 filespec == NULL || *filespec == '\0') | |
| 293 return(1); | |
| 294 | |
| 295 filepos = filename; | |
| 296 wcpos = filespec; | |
| 297 while(1) | |
| 298 { | |
| 299 if (*wcpos == '\0') | |
| 300 return (1); | |
| 301 else if (*filepos == '\0') | |
| 302 return(0); | |
| 303 else if(*wcpos == '?') | |
| 304 { | |
| 305 wcpos++; | |
| 306 filepos++; | |
| 307 } | |
| 308 else if(*wcpos == '*' && *(wcpos+1) == '\0') | |
| 309 return(1); | |
| 310 else if(*wcpos == '*') | |
| 311 { | |
| 312 len = sizeof (search_str); | |
| 313 for (pos = wcpos + 1, newpos = search_str, curlen = 0; | |
| 314 *pos != '*' && *pos != '?' && *pos != '\0' && curlen < len; | |
| 315 curlen++, *newpos++ = *pos++); | |
| 316 *newpos = '\0'; | |
| 317 | |
| 318 if ((filepos = strstr (filepos, search_str)) == NULL) | |
| 319 return(0); | |
| 320 wcpos += curlen + 1; | |
| 321 filepos += curlen; | |
| 322 } | |
| 323 else if(*wcpos++ != *filepos++) | |
| 324 return(0); | |
| 325 } | |
| 326 return (1); | |
| 327 } | |
| 328 | |
| 329 | |
| 330 int | |
| 331 gftp_parse_command_line (int *argc, char ***argv) | |
| 332 { | |
| 333 if (*argc > 1) | |
| 334 { | |
| 335 if (strcmp (argv[0][1], "--help") == 0 || strcmp (argv[0][1], "-h") == 0) | |
| 336 gftp_usage (); | |
| 337 else if (strcmp (argv[0][1], "--version") == 0 || strcmp (argv[0][1], "-v") == 0) | |
| 338 { | |
| 339 printf ("%s\n", version); | |
| 340 exit (0); | |
| 341 } | |
| 342 } | |
| 343 return (0); | |
| 344 } | |
| 345 | |
| 346 | |
| 347 void | |
| 348 gftp_usage (void) | |
| 349 { | |
| 350 printf (_("usage: gftp [[ftp://][user:[pass]@]ftp-site[:port][/directory]]\n")); | |
| 351 exit (0); | |
| 352 } | |
| 353 | |
| 354 | |
| 355 char * | |
| 356 get_xpm_path (char *filename, int quit_on_err) | |
| 357 { | |
| 358 char *tempstr, *exfile; | |
| 359 | |
| 360 tempstr = g_strconcat (BASE_CONF_DIR, "/", filename, NULL); | |
| 361 exfile = expand_path (tempstr); | |
| 362 g_free (tempstr); | |
| 363 if (access (exfile, F_OK) != 0) | |
| 364 { | |
| 365 g_free (exfile); | |
| 366 tempstr = g_strconcat (SHARE_DIR, "/", filename, NULL); | |
| 367 exfile = expand_path (tempstr); | |
| 368 g_free (tempstr); | |
| 369 if (access (exfile, F_OK) != 0) | |
| 370 { | |
| 371 g_free (exfile); | |
| 372 exfile = g_strconcat ("/usr/share/icons/", filename, NULL); | |
| 373 if (access (exfile, F_OK) != 0) | |
| 374 { | |
| 375 g_free (exfile); | |
| 376 if (!quit_on_err) | |
| 377 return (NULL); | |
| 378 printf (_("gFTP Error: Cannot find file %s in %s or %s\n"), | |
| 379 filename, SHARE_DIR, BASE_CONF_DIR); | |
| 380 exit (-1); | |
| 381 } | |
| 382 } | |
| 383 } | |
| 384 return (exfile); | |
| 385 } | |
| 386 | |
| 387 | |
| 388 gint | |
| 389 string_hash_compare (gconstpointer path1, gconstpointer path2) | |
| 390 { | |
| 391 return (strcmp ((char *) path1, (char *) path2) == 0); | |
| 392 } | |
| 393 | |
| 394 | |
| 395 guint | |
| 396 string_hash_function (gconstpointer key) | |
| 397 { | |
| 398 return (((char *) key)[0] + ((char *) key)[1] + ((char *) key)[2]); | |
| 399 } | |
| 400 | |
| 401 | |
| 402 void | |
| 403 free_file_list (GList * filelist) | |
| 404 { | |
| 405 gftp_file * tempfle; | |
| 406 GList * templist; | |
| 407 | |
| 408 templist = filelist; | |
| 409 while (templist != NULL) | |
| 410 { | |
| 411 tempfle = templist->data; | |
| 412 free_fdata (tempfle); | |
| 413 templist = templist->next; | |
| 414 } | |
| 415 g_list_free (filelist); | |
| 416 } | |
| 417 | |
| 418 | |
| 419 void | |
| 420 free_fdata (gftp_file * fle) | |
| 421 { | |
| 422 if (fle->file) | |
| 423 g_free (fle->file); | |
| 424 if (fle->user) | |
| 425 g_free (fle->user); | |
| 426 if (fle->group) | |
| 427 g_free (fle->group); | |
| 428 if (fle->attribs) | |
| 429 g_free (fle->attribs); | |
| 430 if (fle->destfile) | |
| 431 g_free (fle->destfile); | |
| 432 if (fle->fd) | |
| 433 fclose (fle->fd); | |
| 434 g_free (fle); | |
| 435 } | |
| 436 | |
| 437 | |
| 438 gftp_file * | |
| 439 copy_fdata (gftp_file * fle) | |
| 440 { | |
| 441 gftp_file * newfle; | |
| 442 | |
| 443 newfle = g_malloc0 (sizeof (*newfle)); | |
| 444 memcpy (newfle, fle, sizeof (*newfle)); | |
| 445 | |
| 446 if (fle->file) | |
| 447 { | |
| 448 newfle->file = g_malloc (strlen (fle->file) + 1); | |
| 449 strcpy (newfle->file, fle->file); | |
| 450 } | |
| 451 | |
| 452 if (fle->user) | |
| 453 { | |
| 454 newfle->user = g_malloc (strlen (fle->user) + 1); | |
| 455 strcpy (newfle->user, fle->user); | |
| 456 } | |
| 457 | |
| 458 if (fle->group) | |
| 459 { | |
| 460 newfle->group = g_malloc (strlen (fle->group) + 1); | |
| 461 strcpy (newfle->group, fle->group); | |
| 462 } | |
| 463 | |
| 464 if (fle->attribs) | |
| 465 { | |
| 466 newfle->attribs = g_malloc (strlen (fle->attribs) + 1); | |
| 467 strcpy (newfle->attribs, fle->attribs); | |
| 468 } | |
| 469 | |
| 470 if (fle->destfile) | |
| 471 { | |
| 472 newfle->destfile = g_malloc (strlen (fle->destfile) + 1); | |
| 473 strcpy (newfle->destfile, fle->destfile); | |
| 474 } | |
| 475 return (newfle); | |
| 476 } | |
| 477 | |
| 478 | |
| 479 void | |
| 480 swap_socks (gftp_request * dest, gftp_request * source) | |
| 481 { | |
| 482 dest->sockfd = source->sockfd; | |
| 483 dest->datafd = source->datafd; | |
| 484 dest->sockfd_write = source->sockfd_write; | |
| 485 dest->cached = 0; | |
| 486 if (!source->always_connected) | |
| 487 { | |
| 488 source->sockfd = NULL; | |
| 489 source->datafd = NULL; | |
| 490 source->sockfd_write = NULL; | |
| 491 source->cached = 1; | |
| 492 } | |
| 493 } | |
| 494 | |
| 495 | |
| 496 int | |
| 497 compare_request (gftp_request * request1, gftp_request * request2, | |
| 498 int compare_dirs) | |
| 499 { | |
| 500 char *strarr[3][2]; | |
| 501 int i, ret; | |
| 502 | |
| 503 ret = 1; | |
| 504 if (strcmp (request1->protocol_name, request2->protocol_name) == 0 && | |
| 505 request1->port == request2->port) | |
| 506 { | |
| 507 strarr[0][0] = request1->hostname; | |
| 508 strarr[0][1] = request2->hostname; | |
| 509 strarr[1][0] = request1->username; | |
| 510 strarr[1][1] = request2->username; | |
| 511 if (compare_dirs) | |
| 512 { | |
| 513 strarr[2][0] = request1->directory; | |
| 514 strarr[2][1] = request2->directory; | |
| 515 } | |
| 516 else | |
| 517 strarr[2][0] = strarr[2][1] = ""; | |
| 518 | |
| 519 for (i = 0; i < 3; i++) | |
| 520 { | |
| 521 if ((strarr[i][0] && !strarr[i][1]) || | |
| 522 (!strarr[i][0] && strarr[i][1])) | |
| 523 { | |
| 524 ret = 0; | |
| 525 break; | |
| 526 } | |
| 527 | |
| 528 if (strarr[i][0] && strarr[i][1] && | |
| 529 strcmp (strarr[i][0], strarr[i][1]) != 0) | |
| 530 { | |
| 531 ret = 0; | |
| 532 break; | |
| 533 } | |
| 534 } | |
| 535 } | |
| 536 else | |
| 537 ret = 0; | |
| 538 return (ret); | |
| 539 } | |
| 540 | |
| 541 | |
| 542 void | |
| 543 free_tdata (gftp_transfer * tdata) | |
| 544 { | |
| 545 if (tdata->statmutex) | |
| 546 g_free (tdata->statmutex); | |
| 547 if (tdata->structmutex) | |
| 548 g_free (tdata->structmutex); | |
| 549 if (tdata->fromreq != NULL) | |
| 550 gftp_request_destroy (tdata->fromreq); | |
| 551 if (tdata->toreq != NULL) | |
| 552 gftp_request_destroy (tdata->toreq); | |
| 553 free_file_list (tdata->files); | |
| 554 g_free (tdata); | |
| 555 } | |
| 556 | |
| 557 | |
| 558 gftp_request * | |
| 559 copy_request (gftp_request * req) | |
| 560 { | |
| 561 gftp_request * newreq; | |
| 562 | |
| 563 newreq = g_malloc0 (sizeof (*newreq)); | |
| 564 memcpy (newreq, req, sizeof (*newreq)); | |
| 565 | |
| 566 if (req->hostname) | |
| 56 | 567 newreq->hostname = g_strdup (req->hostname); |
| 1 | 568 if (req->username) |
| 56 | 569 newreq->username = g_strdup (req->username); |
| 1 | 570 if (req->password) |
| 56 | 571 newreq->password = g_strdup (req->password); |
| 1 | 572 if (req->account) |
| 56 | 573 newreq->account = g_strdup (req->account); |
| 1 | 574 if (req->directory) |
| 56 | 575 newreq->directory = g_strdup (req->directory); |
| 1 | 576 |
| 577 newreq->url_prefix = NULL; | |
| 578 newreq->protocol_name = NULL; | |
| 579 newreq->sftpserv_path = NULL; | |
| 580 newreq->proxy_config = NULL; | |
| 581 newreq->proxy_hostname = NULL; | |
| 582 newreq->proxy_username = NULL; | |
| 583 newreq->proxy_password = NULL; | |
| 584 newreq->proxy_account = NULL; | |
| 585 newreq->last_ftp_response = NULL; | |
| 586 newreq->last_dir_entry = NULL; | |
| 587 newreq->sockfd = NULL; | |
| 588 newreq->sockfd_write = NULL; | |
| 589 newreq->datafd = NULL; | |
| 590 newreq->cachefd = NULL; | |
| 591 newreq->hostp = NULL; | |
| 592 newreq->protocol_data = NULL; | |
| 593 | |
| 594 if (req->proxy_config != NULL) | |
| 56 | 595 newreq->proxy_config = g_strdup (req->proxy_config); |
| 1 | 596 |
| 597 req->init (newreq); | |
| 598 | |
| 599 return (newreq); | |
| 600 } | |
| 601 | |
| 602 | |
| 603 int | |
| 604 ptym_open (char *pts_name) | |
| 605 { | |
| 606 int fd; | |
| 607 | |
| 608 #ifdef __sgi | |
| 609 char *tempstr; | |
| 610 | |
| 611 if ((tempstr = _getpty (&fd, O_RDWR, 0600, 0)) == NULL) | |
| 612 return (-1); | |
| 613 | |
| 614 strcpy (pts_name, tempstr); | |
| 615 return (fd); | |
| 616 | |
| 617 #else /* !__sgi */ | |
| 618 | |
| 619 #ifdef SYSV | |
| 620 | |
| 621 char *tempstr; | |
| 622 | |
| 623 strcpy (pts_name, "/dev/ptmx"); | |
| 624 if ((fd = open (pts_name, O_RDWR)) < 0) | |
| 625 return (-1); | |
| 626 | |
| 627 if (grantpt (fd) < 0) | |
| 628 { | |
| 629 close (fd); | |
| 630 return (-1); | |
| 631 } | |
| 632 | |
| 633 if (unlockpt (fd) < 0) | |
| 634 { | |
| 635 close (fd); | |
| 636 return (-1); | |
| 637 } | |
| 638 | |
| 639 if ((tempstr = ptsname (fd)) == NULL) | |
| 640 { | |
| 641 close (fd); | |
| 642 return (-1); | |
| 643 } | |
| 644 | |
| 645 strcpy (pts_name, tempstr); | |
| 646 return (fd); | |
| 647 | |
| 648 #else /* !SYSV */ | |
| 649 | |
| 650 char *pos1, *pos2; | |
| 651 | |
| 652 strcpy (pts_name, "/dev/ptyXY"); | |
| 653 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++) | |
| 654 { | |
| 655 pts_name[8] = *pos1; | |
| 656 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++) | |
| 657 { | |
| 658 pts_name[9] = *pos2; | |
| 659 if ((fd = open (pts_name, O_RDWR)) < 0) | |
| 660 continue; | |
| 661 pts_name[5] = 't'; | |
| 662 return (fd); | |
| 663 } | |
| 664 } | |
| 665 return (-1); | |
| 666 | |
| 667 #endif | |
| 668 | |
| 669 #endif | |
| 670 | |
| 671 } | |
| 672 | |
| 673 | |
| 674 int | |
| 675 ptys_open (int fdm, char *pts_name) | |
| 676 { | |
| 677 int fds; | |
| 678 | |
| 679 #if !defined (SYSV) && !defined (__sgi) | |
| 680 | |
| 681 chmod (pts_name, S_IRUSR | S_IWUSR); | |
| 682 chown (pts_name, getuid (), -1); | |
| 683 | |
| 684 #endif | |
| 685 | |
| 686 if ((fds = open (pts_name, O_RDWR)) < 0) | |
| 687 { | |
| 688 close (fdm); | |
| 689 return (-1); | |
| 690 } | |
| 691 | |
| 692 #ifdef SYSV | |
| 693 | |
| 694 if (ioctl (fds, I_PUSH, "ptem") < 0) | |
| 695 { | |
| 696 close (fdm); | |
| 697 close (fds); | |
| 698 return (-1); | |
| 699 } | |
| 700 | |
| 701 if (ioctl (fds, I_PUSH, "ldterm") < 0) | |
| 702 { | |
| 703 close (fdm); | |
| 704 close (fds); | |
| 705 return (-1); | |
| 706 } | |
| 707 | |
| 708 if (ioctl (fds, I_PUSH, "ttcompat") < 0) | |
| 709 { | |
| 710 close (fdm); | |
| 711 close (fds); | |
| 712 return (-1); | |
| 713 } | |
| 714 | |
| 715 #endif | |
| 716 | |
| 717 #if !defined(SYSV) && !defined (__sgi) && defined(TIOCSCTTY) && !defined(CIBAUD) | |
| 718 | |
| 719 if (ioctl (fds, TIOCSCTTY, (char *) 0) < 0) | |
| 720 { | |
| 721 close (fdm); | |
| 722 return (-1); | |
| 723 } | |
| 724 | |
| 725 #endif | |
| 726 | |
| 727 return (fds); | |
| 728 } | |
| 729 | |
| 730 | |
| 731 int | |
| 732 tty_raw (int fd) | |
| 733 { | |
| 734 struct termios buf; | |
| 735 | |
| 736 if (tcgetattr (fd, &buf) < 0) | |
| 737 return (-1); | |
| 738 | |
| 739 buf.c_iflag |= IGNPAR; | |
| 740 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR); | |
| 741 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL); | |
| 742 #ifdef IEXTEN | |
| 743 buf.c_lflag &= ~(IEXTEN); | |
| 744 #endif | |
| 745 | |
| 746 buf.c_oflag &= ~(OPOST); | |
| 747 buf.c_cc[VMIN] = 1; | |
| 748 buf.c_cc[VTIME] = 0; | |
| 749 | |
| 750 if (tcsetattr (fd, TCSADRAIN, &buf) < 0) | |
| 751 return (-1); | |
| 752 return (0); | |
| 753 } | |
| 754 | |
| 755 | |
| 756 /* We have the caller send us a pointer to a string so we can write the port | |
| 757 into it. It makes it easier so we don't have to worry about freeing it | |
| 758 later on, the caller can just send us an auto variable, The string | |
| 759 should be at least 6 chars. I know this is messy... */ | |
| 760 | |
| 761 char ** | |
| 762 make_ssh_exec_args (gftp_request * request, char *execname, | |
| 763 int use_sftp_subsys, char *portstring) | |
| 764 { | |
| 765 char **args, *oldstr, *tempstr; | |
| 56 | 766 struct servent serv_struct; |
| 1 | 767 int i, j; |
| 768 | |
| 56 | 769 args = g_malloc (sizeof (char *) * (num_ssh_extra_params + 15)); |
| 1 | 770 |
| 771 args[0] = ssh_prog_name != NULL && *ssh_prog_name != '\0' ? | |
| 772 ssh_prog_name : "ssh"; | |
| 773 i = 1; | |
| 56 | 774 tempstr = g_strdup (args[0]); |
| 1 | 775 |
| 776 if (ssh_extra_params_list != NULL) | |
| 777 { | |
| 778 for (j=0; ssh_extra_params_list[j] != NULL; j++) | |
| 779 { | |
| 780 oldstr = tempstr; | |
| 781 args[i++] = ssh_extra_params_list[j]; | |
| 782 tempstr = g_strconcat (oldstr, " ", ssh_extra_params_list[j], NULL); | |
| 783 g_free (oldstr); | |
| 784 } | |
| 785 } | |
| 786 | |
| 787 oldstr = tempstr; | |
| 788 tempstr = g_strconcat (oldstr, " -e none", NULL); | |
| 789 g_free (oldstr); | |
| 790 args[i++] = "-e"; | |
| 791 args[i++] = "none"; | |
| 792 | |
| 793 if (request->username && *request->username != '\0') | |
| 794 { | |
| 795 oldstr = tempstr; | |
| 796 tempstr = g_strconcat (oldstr, " -l ", request->username, NULL); | |
| 797 g_free (oldstr); | |
| 798 args[i++] = "-l"; | |
| 799 args[i++] = request->username; | |
| 800 } | |
| 801 | |
| 802 if (request->port != 0) | |
| 803 { | |
| 804 g_snprintf (portstring, 6, "%d", request->port); | |
| 805 oldstr = tempstr; | |
| 806 tempstr = g_strconcat (oldstr, " -p ", portstring, NULL); | |
| 807 g_free (oldstr); | |
| 808 args[i++] = "-p"; | |
| 809 args[i++] = portstring; | |
| 810 } | |
| 56 | 811 else |
| 812 { | |
| 813 if (!r_getservbyname ("ssh", "tcp", &serv_struct, NULL)) | |
| 814 request->port = 22; | |
| 815 else | |
| 816 request->port = ntohs (serv_struct.s_port); | |
| 817 } | |
| 1 | 818 |
| 819 if (use_sftp_subsys) | |
| 820 { | |
| 821 oldstr = tempstr; | |
| 822 tempstr = g_strconcat (oldstr, " ", request->hostname, " -s sftp", NULL); | |
| 823 g_free (oldstr); | |
| 824 args[i++] = request->hostname; | |
| 825 args[i++] = "-s"; | |
| 826 args[i++] = "sftp"; | |
| 827 args[i] = NULL; | |
| 828 } | |
| 829 else | |
| 830 { | |
| 831 oldstr = tempstr; | |
| 832 tempstr = g_strconcat (oldstr, " ", request->hostname, " \"", execname, | |
| 833 "\"", NULL); | |
| 834 g_free (oldstr); | |
| 835 args[i++] = request->hostname; | |
| 836 args[i++] = execname; | |
| 837 args[i] = NULL; | |
| 838 } | |
| 839 | |
| 840 request->logging_function (gftp_logging_misc, request->user_data, | |
| 841 _("Running program %s\n"), tempstr); | |
| 842 g_free (tempstr); | |
| 843 return (args); | |
| 844 } | |
| 845 | |
| 846 | |
| 847 char * | |
| 848 ssh_start_login_sequence (gftp_request * request, int fd) | |
| 849 { | |
| 850 size_t rem, len, diff, lastdiff; | |
| 851 int flags, wrotepw, ok; | |
| 852 struct timeval tv; | |
| 853 char *tempstr; | |
| 854 fd_set rdfds; | |
| 855 ssize_t rd; | |
| 856 | |
| 857 rem = len = 100; | |
| 858 tempstr = g_malloc0 (len); | |
| 859 diff = lastdiff = 0; | |
| 860 wrotepw = 0; | |
| 861 ok = 1; | |
| 862 | |
| 863 if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | |
| 864 { | |
| 865 g_free (tempstr); | |
| 866 return (NULL); | |
| 867 } | |
| 868 | |
| 869 if (fcntl (fd, F_SETFL, flags | O_NONBLOCK) < 0) | |
| 870 { | |
| 871 g_free (tempstr); | |
| 872 return (NULL); | |
| 873 } | |
| 874 | |
| 41 | 875 errno = 0; |
| 1 | 876 while (1) |
| 877 { | |
| 878 FD_ZERO (&rdfds); | |
| 879 FD_SET (fd, &rdfds); | |
| 880 tv.tv_sec = 5; | |
| 881 tv.tv_usec = 0; | |
| 882 if (select (fd + 1, &rdfds, NULL, NULL, &tv) < 0) | |
| 883 { | |
| 41 | 884 if (errno == EINTR && !request->cancel) |
| 1 | 885 continue; |
| 886 ok = 0; | |
| 887 break; | |
| 888 } | |
| 889 | |
| 890 if ((rd = read (fd, tempstr + diff, rem - 1)) < 0) | |
| 891 { | |
| 41 | 892 if (errno == EINTR && !request->cancel) |
| 1 | 893 continue; |
| 894 ok = 0; | |
| 895 break; | |
| 896 } | |
| 897 else if (rd == 0) | |
| 898 { | |
| 899 ok = 0; | |
| 900 break; | |
| 901 } | |
| 902 tempstr[diff + rd] = '\0'; | |
| 903 rem -= rd; | |
| 904 diff += rd; | |
| 905 if (rem <= 1) | |
| 906 { | |
| 907 tempstr = g_realloc (tempstr, len + 100); | |
| 908 tempstr[diff] = '\0'; | |
| 909 request->logging_function (gftp_logging_recv, request->user_data, | |
| 910 "%s", tempstr + lastdiff); | |
| 911 lastdiff = diff; | |
| 912 len += 100; | |
| 913 rem = 100; | |
| 914 } | |
| 915 | |
| 916 if (!wrotepw && | |
| 917 strlen (tempstr) > 11 && strcmp (tempstr + strlen (tempstr) - 10, | |
| 918 "password: ") == 0) | |
| 919 { | |
| 920 wrotepw = 1; | |
| 921 write (fd, request->password, strlen (request->password)); | |
| 922 write (fd, "\n", 1); | |
| 923 } | |
| 924 | |
| 925 else if (!wrotepw && | |
| 926 (strstr (tempstr, "Enter passphrase for RSA key") != NULL || | |
| 927 strstr (tempstr, "Enter passphrase for key '") != NULL)) | |
| 928 { | |
| 929 wrotepw = 1; | |
| 930 write (fd, request->password, strlen (request->password)); | |
| 931 write (fd, "\n", 1); | |
| 932 } | |
| 933 else if (strlen (tempstr) >= 5 && | |
| 934 strcmp (tempstr + strlen (tempstr) - 5, "xsftp") == 0) | |
| 935 break; | |
| 936 } | |
| 937 | |
| 938 tempstr[diff] = '\0'; | |
| 939 request->logging_function (gftp_logging_recv, request->user_data, | |
| 940 "%s\n", tempstr + lastdiff); | |
| 941 | |
| 942 if (ok && fcntl (fd, F_SETFL, flags) < 0) | |
| 943 ok = 0; | |
| 944 | |
| 945 if (!ok) | |
| 946 { | |
| 947 g_free (tempstr); | |
| 948 return (NULL); | |
| 949 } | |
| 950 | |
| 951 return (tempstr); | |
| 952 } | |
| 953 | |
| 954 | |
| 955 #ifdef G_HAVE_GINT64 | |
| 956 | |
| 957 gint64 | |
| 958 hton64 (gint64 val) | |
| 959 { | |
| 960 gint64 num; | |
| 961 char *pos; | |
| 962 | |
| 963 num = 0; | |
| 964 pos = (char *) # | |
| 965 pos[0] = (val >> 56) & 0xff; | |
| 966 pos[1] = (val >> 48) & 0xff; | |
| 967 pos[2] = (val >> 40) & 0xff; | |
| 968 pos[3] = (val >> 32) & 0xff; | |
| 969 pos[4] = (val >> 24) & 0xff; | |
| 970 pos[5] = (val >> 16) & 0xff; | |
| 971 pos[6] = (val >> 8) & 0xff; | |
| 972 pos[7] = val & 0xff; | |
| 973 return (num); | |
| 974 } | |
| 975 | |
| 976 #endif | |
| 977 | |
| 16 | 978 |
| 979 static gint | |
| 980 gftp_file_sort_function_as (gconstpointer a, gconstpointer b) | |
| 981 { | |
| 982 const gftp_file * f1, * f2; | |
| 983 | |
| 984 f1 = a; | |
| 985 f2 = b; | |
| 986 return (strcmp (f1->file, f2->file)); | |
| 987 } | |
| 988 | |
| 989 | |
| 990 static gint | |
| 991 gftp_file_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 992 { | |
| 993 const gftp_file * f1, * f2; | |
| 994 gint ret; | |
| 995 | |
| 996 f1 = a; | |
| 997 f2 = b; | |
| 998 ret = strcmp (f1->file, f2->file); | |
| 999 if (ret < 0) | |
| 1000 ret = 1; | |
| 1001 else if (ret > 0) | |
| 1002 ret = -1; | |
| 1003 return (ret); | |
| 1004 } | |
| 1005 | |
| 1006 | |
| 1007 static gint | |
| 1008 gftp_user_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1009 { | |
| 1010 const gftp_file * f1, * f2; | |
| 1011 | |
| 1012 f1 = a; | |
| 1013 f2 = b; | |
| 1014 return (strcmp (f1->user, f2->user)); | |
| 1015 } | |
| 1016 | |
| 1017 | |
| 1018 static gint | |
| 1019 gftp_user_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1020 { | |
| 1021 const gftp_file * f1, * f2; | |
| 1022 gint ret; | |
| 1023 | |
| 1024 f1 = a; | |
| 1025 f2 = b; | |
| 1026 ret = strcmp (f1->user, f2->user); | |
| 1027 if (ret < 0) | |
| 1028 ret = 1; | |
| 1029 else if (ret > 0) | |
| 1030 ret = -1; | |
| 1031 return (ret); | |
| 1032 } | |
| 1033 | |
| 1034 | |
| 1035 static gint | |
| 1036 gftp_group_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1037 { | |
| 1038 const gftp_file * f1, * f2; | |
| 1039 | |
| 1040 f1 = a; | |
| 1041 f2 = b; | |
| 1042 return (strcmp (f1->group, f2->group)); | |
| 1043 } | |
| 1044 | |
| 1045 | |
| 1046 static gint | |
| 1047 gftp_group_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1048 { | |
| 1049 const gftp_file * f1, * f2; | |
| 1050 gint ret; | |
| 1051 | |
| 1052 f1 = a; | |
| 1053 f2 = b; | |
| 1054 ret = strcmp (f1->group, f2->group); | |
| 1055 if (ret < 0) | |
| 1056 ret = 1; | |
| 1057 else if (ret > 0) | |
| 1058 ret = -1; | |
| 1059 return (ret); | |
| 1060 } | |
| 1061 | |
| 1062 | |
| 1063 static gint | |
| 1064 gftp_attribs_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1065 { | |
| 1066 const gftp_file * f1, * f2; | |
| 1067 | |
| 1068 f1 = a; | |
| 1069 f2 = b; | |
| 1070 return (strcmp (f1->attribs, f2->attribs)); | |
| 1071 } | |
| 1072 | |
| 1073 | |
| 1074 static gint | |
| 1075 gftp_attribs_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1076 { | |
| 1077 const gftp_file * f1, * f2; | |
| 1078 gint ret; | |
| 1079 | |
| 1080 f1 = a; | |
| 1081 f2 = b; | |
| 1082 ret = strcmp (f1->attribs, f2->attribs); | |
| 1083 if (ret < 0) | |
| 1084 ret = 1; | |
| 1085 else if (ret > 0) | |
| 1086 ret = -1; | |
| 1087 return (ret); | |
| 1088 } | |
| 1089 | |
| 1090 | |
| 1091 static gint | |
| 1092 gftp_size_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1093 { | |
| 1094 const gftp_file * f1, * f2; | |
| 1095 | |
| 1096 f1 = a; | |
| 1097 f2 = b; | |
| 1098 if (f1->size < f2->size) | |
| 1099 return (-1); | |
| 1100 else if (f1->size == f2->size) | |
| 1101 return (0); | |
| 1102 else | |
| 1103 return (1); | |
| 1104 } | |
| 1105 | |
| 1106 | |
| 1107 static gint | |
| 1108 gftp_size_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1109 { | |
| 1110 const gftp_file * f1, * f2; | |
| 1111 | |
| 1112 f1 = a; | |
| 1113 f2 = b; | |
| 1114 if (f1->size < f2->size) | |
| 1115 return (1); | |
| 1116 else if (f1->size == f2->size) | |
| 1117 return (0); | |
| 1118 else | |
| 1119 return (-1); | |
| 1120 } | |
| 1121 | |
| 1122 | |
| 1123 static gint | |
| 1124 gftp_datetime_sort_function_as (gconstpointer a, gconstpointer b) | |
| 1125 { | |
| 1126 const gftp_file * f1, * f2; | |
| 1127 | |
| 1128 f1 = a; | |
| 1129 f2 = b; | |
| 1130 if (f1->datetime < f2->datetime) | |
| 1131 return (-1); | |
| 1132 else if (f1->datetime == f2->datetime) | |
| 1133 return (0); | |
| 1134 else | |
| 1135 return (1); | |
| 1136 } | |
| 1137 | |
| 1138 | |
| 1139 static gint | |
| 1140 gftp_datetime_sort_function_ds (gconstpointer a, gconstpointer b) | |
| 1141 { | |
| 1142 const gftp_file * f1, * f2; | |
| 1143 | |
| 1144 f1 = a; | |
| 1145 f2 = b; | |
| 1146 if (f1->datetime < f2->datetime) | |
| 1147 return (1); | |
| 1148 else if (f1->datetime == f2->datetime) | |
| 1149 return (0); | |
| 1150 else | |
| 1151 return (-1); | |
| 1152 } | |
| 1153 | |
| 1154 | |
| 1155 GList * | |
| 1156 gftp_sort_filelist (GList * filelist, int column, int asds) | |
| 1157 { | |
| 1158 GList * files, * dirs, * dotdot, * tempitem, * insitem; | |
| 1159 GCompareFunc sortfunc; | |
| 1160 gftp_file * tempfle; | |
| 1161 | |
| 1162 files = dirs = dotdot = NULL; | |
| 1163 | |
| 1164 if (column == GFTP_SORT_COL_FILE) | |
| 1165 sortfunc = asds ? gftp_file_sort_function_as : gftp_file_sort_function_ds; | |
| 1166 else if (column == GFTP_SORT_COL_SIZE) | |
| 1167 sortfunc = asds ? gftp_size_sort_function_as : gftp_size_sort_function_ds; | |
| 1168 else if (column == GFTP_SORT_COL_USER) | |
| 1169 sortfunc = asds ? gftp_user_sort_function_as : gftp_user_sort_function_ds; | |
| 1170 else if (column == GFTP_SORT_COL_GROUP) | |
| 1171 sortfunc = asds ? | |
| 1172 gftp_group_sort_function_as : gftp_group_sort_function_ds; | |
| 1173 else if (column == GFTP_SORT_COL_DATETIME) | |
| 1174 sortfunc = asds ? | |
| 1175 gftp_datetime_sort_function_as : gftp_datetime_sort_function_ds; else /* GFTP_SORT_COL_ATTRIBS */ | |
| 1176 sortfunc = asds ? | |
| 1177 gftp_attribs_sort_function_as : gftp_attribs_sort_function_ds; | |
| 1178 | |
| 1179 for (tempitem = filelist; tempitem != NULL; ) | |
| 1180 { | |
| 1181 tempfle = tempitem->data; | |
| 1182 insitem = tempitem; | |
| 1183 tempitem = tempitem->next; | |
| 1184 insitem->next = NULL; | |
| 1185 | |
| 1186 if (dotdot == NULL && strcmp (tempfle->file, "..") == 0) | |
| 1187 dotdot = insitem; | |
| 1188 else if (sort_dirs_first && tempfle->isdir) | |
| 1189 { | |
| 1190 insitem->next = dirs; | |
| 1191 dirs = insitem; | |
| 1192 } | |
| 1193 else | |
| 1194 { | |
| 1195 insitem->next = files; | |
| 1196 files = insitem; | |
| 1197 } | |
| 1198 } | |
| 1199 | |
| 1200 if (dirs != NULL) | |
| 1201 dirs = g_list_sort (dirs, sortfunc); | |
| 1202 if (files != NULL) | |
| 1203 files = g_list_sort (files, sortfunc); | |
| 1204 | |
| 1205 filelist = dotdot; | |
| 1206 | |
| 1207 if (filelist == NULL) | |
| 1208 filelist = dirs; | |
| 1209 else | |
| 1210 filelist = g_list_concat (filelist, dirs); | |
| 1211 | |
| 1212 if (filelist == NULL) | |
| 1213 filelist = files; | |
| 1214 else | |
| 1215 filelist = g_list_concat (filelist, files); | |
| 1216 | |
| 39 | 1217 /* I haven't check this, but I'm pretty sure some older versions of glib |
| 1218 had a bug that the prev pointer wasn't being sent to NULL */ | |
| 1219 filelist->prev = NULL; | |
| 16 | 1220 return (filelist); |
| 1221 } | |
| 1222 |
