1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: misc.C |
2 | * File: misc.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston |
6 | * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston |
7 | * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> |
7 | * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> |
8 | * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> |
8 | * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> |
9 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
9 | * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com> |
10 | * |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by |
12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation; either version 2 of the License, or |
13 | * the Free Software Foundation; either version 2 of the License, or |
14 | * (at your option) any later version. |
14 | * (at your option) any later version. |
… | |
… | |
246 | rxvt_Str_match (const char *s1, const char *s2) NOTHROW |
246 | rxvt_Str_match (const char *s1, const char *s2) NOTHROW |
247 | { |
247 | { |
248 | int n = strlen (s2); |
248 | int n = strlen (s2); |
249 | |
249 | |
250 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
250 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
251 | } |
|
|
252 | |
|
|
253 | const char * |
|
|
254 | rxvt_Str_skip_space (const char *str) NOTHROW |
|
|
255 | { |
|
|
256 | if (str) |
|
|
257 | while (*str && isspace (*str)) |
|
|
258 | str++; |
|
|
259 | |
|
|
260 | return str; |
|
|
261 | } |
251 | } |
262 | |
252 | |
263 | /* |
253 | /* |
264 | * remove leading/trailing space and strip-off leading/trailing quotes. |
254 | * remove leading/trailing space and strip-off leading/trailing quotes. |
265 | * in place. |
255 | * in place. |
… | |
… | |
425 | free (cs[i]); |
415 | free (cs[i]); |
426 | |
416 | |
427 | free (cs); |
417 | free (cs); |
428 | } |
418 | } |
429 | |
419 | |
430 | /*----------------------------------------------------------------------* |
|
|
431 | * file searching |
|
|
432 | */ |
|
|
433 | |
420 | |
434 | /* #define DEBUG_SEARCH_PATH */ |
|
|
435 | |
421 | |
436 | #ifdef XPM_BACKGROUND |
|
|
437 | /* |
|
|
438 | * search for FILE in the current working directory, and within the |
|
|
439 | * colon-delimited PATHLIST, adding the file extension EXT if required. |
|
|
440 | * |
|
|
441 | * FILE is either semi-colon or zero terminated |
|
|
442 | */ |
|
|
443 | char * |
|
|
444 | rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) NOTHROW |
|
|
445 | { |
|
|
446 | int maxpath, len; |
|
|
447 | const char *p, *path; |
|
|
448 | char name[256]; |
|
|
449 | |
|
|
450 | if (!access (file, R_OK)) /* found (plain name) in current directory */ |
|
|
451 | return strdup (file); |
|
|
452 | |
|
|
453 | /* semi-colon delimited */ |
|
|
454 | if ((p = strchr (file, ';'))) |
|
|
455 | len = (p - file); |
|
|
456 | else |
|
|
457 | len = strlen (file); |
|
|
458 | |
|
|
459 | #ifdef DEBUG_SEARCH_PATH |
|
|
460 | getcwd (name, sizeof (name)); |
|
|
461 | fprintf (stderr, "pwd: \"%s\"\n", name); |
|
|
462 | fprintf (stderr, "find: \"%.*s\"\n", len, file); |
|
|
463 | #endif |
|
|
464 | |
|
|
465 | /* leave room for an extra '/' and trailing '\0' */ |
|
|
466 | maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2); |
|
|
467 | if (maxpath <= 0) |
|
|
468 | return NULL; |
|
|
469 | |
|
|
470 | /* check if we can find it now */ |
|
|
471 | strncpy (name, file, len); |
|
|
472 | name[len] = '\0'; |
|
|
473 | |
|
|
474 | if (!access (name, R_OK)) |
|
|
475 | return strdup (name); |
|
|
476 | if (ext) |
|
|
477 | { |
|
|
478 | strcat (name, ext); |
|
|
479 | if (!access (name, R_OK)) |
|
|
480 | return strdup (name); |
|
|
481 | } |
|
|
482 | for (path = pathlist; path != NULL && *path != '\0'; path = p) |
|
|
483 | { |
|
|
484 | int n; |
|
|
485 | |
|
|
486 | /* colon delimited */ |
|
|
487 | if ((p = strchr (path, ':')) == NULL) |
|
|
488 | p = strchr (path, '\0'); |
|
|
489 | |
|
|
490 | n = (p - path); |
|
|
491 | if (*p != '\0') |
|
|
492 | p++; |
|
|
493 | |
|
|
494 | if (n > 0 && n <= maxpath) |
|
|
495 | { |
|
|
496 | strncpy (name, path, n); |
|
|
497 | if (name[n - 1] != '/') |
|
|
498 | name[n++] = '/'; |
|
|
499 | name[n] = '\0'; |
|
|
500 | strncat (name, file, len); |
|
|
501 | |
|
|
502 | if (!access (name, R_OK)) |
|
|
503 | return strdup (name); |
|
|
504 | if (ext) |
|
|
505 | { |
|
|
506 | strcat (name, ext); |
|
|
507 | if (!access (name, R_OK)) |
|
|
508 | return strdup (name); |
|
|
509 | } |
|
|
510 | } |
|
|
511 | } |
|
|
512 | return NULL; |
|
|
513 | } |
|
|
514 | |
|
|
515 | char * |
|
|
516 | rxvt_File_find (const char *file, const char *ext, const char *path) NOTHROW |
|
|
517 | { |
|
|
518 | char *f; |
|
|
519 | |
|
|
520 | if (file == NULL || *file == '\0') |
|
|
521 | return NULL; |
|
|
522 | |
|
|
523 | f = rxvt_File_search_path (path, file, ext); |
|
|
524 | |
|
|
525 | #ifdef DEBUG_SEARCH_PATH |
|
|
526 | if (f) |
|
|
527 | fprintf (stderr, "found: \"%s\"\n", f); |
|
|
528 | #endif |
|
|
529 | |
|
|
530 | return f; |
|
|
531 | } |
|
|
532 | #endif |
|
|
533 | |
|
|
534 | |
|
|