1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
|
/*
Disk and Directory Handling
int FILEHASEXTENSION(char *filename)
int ISPATHABSOLUTE(char *path)
int NUMDIRCONTENTS(char *path)
int COMPARE_PARENT_PATHS(char *path, char *parent)
int ISPATHDIR(char *path)
int ISLPATHDIR(char *path)
int ISPATHEXECUTABLE(char *path)
char *PathSubHome(char *path)
char **GetDirEntNames(char *parent)
char *ChangeDirRel(char *cpath, char *npath)
void StripAbsolutePath(char *path)
char *PrefixPaths(char *parent, char *child)
char *GetAllocLinkDest(char *link)
char *GetParentDir(char *path)
void SimplifyPath(char *path)
int FileCountLines(char *filename)
int DirHasSubDirs(char *path)
int CopyObject(
char *target,
char *source,
int (*comferm_func)(char *, char *)
)
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
extern int errno;
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#ifndef USE_GETDENTS
# include <dirent.h>
#endif /* not USE_GETDENTS */
#if defined(__linux__)
# include <linux/limits.h>
# ifdef USE_GETDENTS
# include <linux/dirent.h>
# endif
#endif
#include "../include/os.h"
#include "../include/string.h"
#include "../include/disk.h"
int FILEHASEXTENSION(char *filename)
{
char *strptr;
if(filename == NULL)
return(0);
strptr = filename;
strptr += 1; /* Skip first char. */
if(strchr(strptr, '.') == NULL)
return(0);
else
return(1);
}
int ISPATHABSOLUTE(char *path)
{
if(path == NULL)
return(0);
while(isblank(*path))
path++;
/* First non space character a '/' character? */
return(*path == '/');
}
/*
* Returns the number of entries in a dir.
*/
int NUMDIRCONTENTS(char *path)
{
int i, items;
char **dname;
if(path == NULL)
return(0);
if(!ISPATHDIR(path))
return(0);
dname = GetDirEntNames(path);
if(dname == NULL)
return(0);
items = 0;
for(i = 0; dname[i] != NULL; i++)
{
if(dname[i] == NULL)
continue;
/* Skip this and parent notation entries. */
if(!strcmp(dname[i], ".") ||
!strcmp(dname[i], "..")
)
{
/* Free the name. */
free(dname[i]);
dname[i] = NULL;
continue;
}
/* Increment count. */
items++;
/* Free the name. */
free(dname[i]);
dname[i] = NULL;
}
free(dname);
dname = NULL;
return(items);
}
/*
* Returns 1 if path has the parents of parent.
*
* "/usr/X11/bin/xplay" "/usr/X11/bin"
* will return true.
*
* Spaces must be stripped from both!
*/
int COMPARE_PARENT_PATHS(char *path, char *parent)
{
int len_path, len_parent;
if((path == NULL) ||
(parent == NULL)
)
return(0);
len_path = strlen(path);
len_parent = strlen(parent);
/* Cannot compare if path is not absolute. */
if(path[0] != '/')
return(0);
if(parent[0] != '/')
return(0);
/* Reduce length of parent for tailing /'s. */
while(len_parent > 0)
{
if(parent[len_parent - 1] == '/')
len_parent--;
else
break;
}
/* definately not if parent is longer then path. */
if(len_path < len_parent)
return(0);
/* Compare, but just to the length of the parent. */
if(strncmp(path, parent, len_parent))
return(0);
else
return(1);
}
int ISPATHDIR(char *path)
{
struct stat stat_buf;
/* Input error checks. */
if(path == NULL)
return(0);
/* Check if exists. */
if(stat(path, &stat_buf))
return(0);
return((S_ISDIR(stat_buf.st_mode)) ? 1 : 0);
}
int ISLPATHDIR(char *path)
{
struct stat stat_buf;
if(path == NULL)
return(0);
/* Check if exists. */
if(lstat(path, &stat_buf))
return(0);
return((S_ISDIR(stat_buf.st_mode)) ? 1 : 0);
}
int ISPATHEXECUTABLE(char *path)
{
struct stat stat_buf;
if(path == NULL)
return(0);
/* Get stats. */
if(stat(path, &stat_buf))
return(0);
if((S_IXUSR & stat_buf.st_mode) |
(S_IXGRP & stat_buf.st_mode) |
(S_IXOTH & stat_buf.st_mode)
)
return(1);
else
return(0);
}
/*
* Replaces any occurances of "~" in path with user's home
* dir.
*
* Returns statically allocated string.
*/
char *PathSubHome(char *path)
{
int i, n, p, len;
char home_dir[PATH_MAX];
char *strptr;
static char rtn_path[PATH_MAX];
if(path == NULL)
return(NULL);
/* Get home dir. */
strptr = getenv("HOME");
if(strptr == NULL)
return(NULL);
strncpy(home_dir, strptr, PATH_MAX);
home_dir[PATH_MAX - 1] = '\0';
len = strlen(home_dir);
/* Seek ~. */
for(p = 0, n = 1; path[p] != '\0'; p++)
{
if(path[p] == '~')
{
n = 0;
break;
}
}
/* No match? */
if(n)
{
strncpy(rtn_path, path, PATH_MAX);
rtn_path[PATH_MAX - 1] = '\0';
return(rtn_path);
}
/* Copy over first few chars. */
for(i = 0; (i < p) && (i < PATH_MAX); i++)
{
rtn_path[i] = path[i];
}
/* Sub. */
for(i = p, n = 0; (i < PATH_MAX) && (n < len); n++, i++)
{
rtn_path[i] = home_dir[n];
}
/* Ending chars. */
for(i = p + len, n = p + 1; (path[n] != '\0') && (i < PATH_MAX);
i++, n++
)
{
rtn_path[i] = path[n];
}
if(i < PATH_MAX)
rtn_path[i] = '\0';
else
rtn_path[PATH_MAX - 1] = '\0';
return(rtn_path);
}
/*
* Returns a pointer to an array of pointers to strings.
* Last pointer points to NULL.
*
* Returns NULL on error. All contents are dynamically
* allocated and MUST BE FREED by calling program.
*/
#ifdef USE_GETDENTS
char **GetDirEntNames(char *parent)
{
int x, bytes_read, fd;
struct dirent *dirp_origin;
struct dirent *dirp_pos;
char *dirp_raw;
long dirp_memsize;
long next_dirent;
long total_dnames;
char **dname;
/* Error checks. */
if(parent == NULL)
return(NULL);
/* Must be absolute parent. */
x = 0;
while((parent[x] == ' ') ||
(parent[x] == '\t')
)
x++;
if(parent[x] != '/')
return(NULL);
/* Check if parent is a directory. */
if(!ISPATHDIR(parent))
return(NULL);
/* ********************************************************* */
/* Open parent. */
fd = open(parent, O_RDONLY);
if(fd < 0)
return(NULL);
/* Begin reading dirs. */
next_dirent = 0;
total_dnames = 0;
dname = NULL;
dirp_origin = NULL;
dirp_memsize = sizeof(struct dirent);
while(1)
{
/* Reopen file descriptor. */
close(fd); fd = -1;
fd = open(parent, O_RDONLY);
if(fd < 0)
break;
/* Increment number of directory entry names gotten. */
total_dnames++;
/* Allocate more directory entry name pointers. */
dname = (char **)realloc(dname, total_dnames * sizeof(char *));
/* Allocate more dent pointers. */
dirp_origin = (struct dirent *)realloc(dirp_origin,
dirp_memsize);
/* Reset to prevent segfault. */
memset(dirp_origin, 0, dirp_memsize);
/* Get more directory entries. */
bytes_read = getdents((unsigned int)fd, dirp_origin,
dirp_memsize);
if(bytes_read <= 0)
break;
/* Get raw FAT position pointer. */
dirp_raw = (char *)dirp_origin;
dirp_pos = (struct dirent *)(&dirp_raw[next_dirent]);
if(dirp_pos->d_reclen == 0)
break;
/* Get next dir entry position. */
next_dirent += dirp_pos->d_reclen;
dirp_memsize += dirp_pos->d_reclen; /* More memory needed. */
/* Allocate memory for new directory entry name. */
dname[total_dnames - 1] = (char *)calloc(1,
(strlen(dirp_pos->d_name) + 1) * sizeof(char));
strcpy(dname[total_dnames - 1], dirp_pos->d_name);
}
/* Free dents pointer. */
free(dirp_origin); dirp_origin = NULL;
/* Close the directory. */
if(fd > -1)
{
close(fd);
fd = -1;
}
/* Last entry for directory entry names must be NULL. */
if(total_dnames > 0)
dname[total_dnames - 1] = NULL;
else
dname[0] = NULL;
return(dname);
}
#else /* USE_GETDENTS */
char **GetDirEntNames(char *parent)
{
int i, len;
DIR *dir;
struct dirent *de;
char **rtn_names;
/* Error checks. */
if(parent == NULL)
return(NULL);
/* Open dir. */
dir = opendir(parent);
if(dir == NULL)
return(NULL);
/* Allocate rtn_names pointers. */
rtn_names = NULL;
for(i = 0; 1; i++)
{
/* Reallocate rtn_names pointers. */
rtn_names = (char **)realloc(
rtn_names,
(i + 1) * sizeof(char *)
);
if(rtn_names == NULL)
{
closedir(dir);
dir = NULL;
return(NULL);
}
/* Get next dir entry. */
de = (struct dirent *)readdir(dir);
if(de == NULL)
break;
len = strlen(de->d_name);
rtn_names[i] = (char *)calloc(1, (len + 1) * sizeof(char));
if(rtn_names[i] == NULL)
break;
strcpy(rtn_names[i], de->d_name);
}
closedir(dir);
dir = NULL;
/* Set last entry to NULL. */
rtn_names[i] = NULL;
return(rtn_names);
}
#endif /* USE_GETDENTS */
/*
* Performs a `change directory' operation on the given path
* statements cpath and npath.
*
* cpath is treated as your current directory and npath is
* treated as the operation. Examples
*
*
* Returns NULL on error or an allocated NULL terminated string on
* success. The calling function must free the returned pointer.
*/
char *ChangeDirRel(char *cpath, char *npath)
{
int len;
char *rtn_str, *strptr;
/* If both cpath and npath are NULL, return allocated cwd. */
if((cpath == NULL) &&
(npath == NULL)
)
{
len = PATH_MAX;
rtn_str = (char *)malloc((len + 1) * sizeof(char));
if(rtn_str == NULL)
return(NULL);
if(getcwd(rtn_str, len) == NULL)
{
free(rtn_str);
rtn_str = NULL;
return(NULL);
}
return(rtn_str);
}
/* If only npath is NULL, return allocated cpath. */
if((cpath != NULL) && (npath == NULL))
{
len = strlen(cpath);
rtn_str = (char *)malloc((len + 1) * sizeof(char));
if(rtn_str == NULL)
return(NULL);
strncpy(rtn_str, cpath, len);
rtn_str[len] = '\0';
return(rtn_str);
}
/* If only cpath is NULL, return allocated cwd. */
if((npath != NULL) && (cpath == NULL))
{
len = PATH_MAX;
rtn_str = (char *)malloc((len + 1) * sizeof(char));
if(rtn_str == NULL)
return(NULL);
if(getcwd(rtn_str, len) == NULL)
{
free(rtn_str);
rtn_str = NULL;
return(NULL);
}
return(rtn_str);
}
/* If cpath is not absolute, then return cwd. */
if(!ISPATHABSOLUTE(cpath))
{
len = PATH_MAX;
rtn_str = (char *)malloc((len + 1) * sizeof(char));
if(rtn_str == NULL)
return(NULL);
if(getcwd(rtn_str, len) == NULL)
{
free(rtn_str);
rtn_str = NULL;
return(NULL);
}
return(rtn_str);
}
/* If npath is ".", then just return cpath. */
if(!strcmp(npath, "."))
{
len = strlen(cpath);
rtn_str = (char *)malloc((len + 1) * sizeof(char));
if(rtn_str == NULL)
return(NULL);
strncpy(rtn_str, cpath, len);
rtn_str[len] = '\0';
return(rtn_str);
}
/* If npath is an absolute directory, then change to it. */
if(ISPATHABSOLUTE(npath))
{
len = strlen(npath);
rtn_str = (char *)malloc((len + 1) * sizeof(char));
if(rtn_str == NULL)
return(NULL);
strncpy(rtn_str, npath, len);
rtn_str[len] = '\0';
return(rtn_str);
}
/* npath is a relative notation. */
/* Prefix paths. */
strptr = PrefixPaths(cpath, npath);
if(strptr == NULL)
return(NULL);
/* Allocate return string. */
len = strlen(strptr);
rtn_str = (char *)malloc((len + 1) * sizeof(char));
if(rtn_str == NULL)
return(NULL);
strncpy(rtn_str, strptr, len);
rtn_str[len] = '\0';
/* Simplify return path, this removes any "whatever/.."s. */
SimplifyPath(rtn_str);
return(rtn_str);
}
/*
* Strips the pointed storage path of its absolute path,
* leaving it with just the final destination name.
*/
void StripAbsolutePath(char *path)
{
char *strptr1, *strptr2;
if(path == NULL)
return;
if(*path != '/')
return;
strptr1 = path;
/* Seek to end. */
while(*strptr1 != '\0')
strptr1++;
/* Seek back, skipping tailing '/' chars. */
strptr1--;
if(strptr1 < path)
strptr1 = path;
while((strptr1 > path) &&
(*strptr1 == '/')
)
strptr1--;
/* Seek back untill a '/' char. */
if(strptr1 < path)
strptr1 = path;
while((strptr1 > path) &&
(*strptr1 != '/')
)
strptr1--;
strptr1++; /* Go forward one char. */
if(strptr1 < path)
strptr1 = path;
/* Copy child to beginning. */
strptr2 = path;
while(*strptr1 != '\0')
{
*strptr2 = *strptr1;
strptr1++;
strptr2++;
}
*strptr2 = '\0';
/* Make sure path contains atleast "/". */
if(*path == '\0')
{
path[0] = '/';
path[1] = '\0';
}
return;
}
/*
* Returns a statically allocated string containing the
* prefixed paths of the parent and child.
*
* If child is absolute, then the statically allocated string
* will contain the child path.
*
* The parent path must be absolute.
*/
char *PrefixPaths(char *parent, char *child)
{
char *strptr1, *strptr2, *strend;
static char rtn_path[PATH_MAX];
if((parent == NULL) ||
(child == NULL) ||
(parent == child)
)
return("/");
/* If child is absolute, copy child and return. */
if(*child == '/')
{
strncpy(rtn_path, child, PATH_MAX);
rtn_path[PATH_MAX - 1] = '\0';
return(rtn_path);
}
/* Calculate strend. */
strend = rtn_path + PATH_MAX;
/* Copy parent. */
strncpy(rtn_path, parent, PATH_MAX);
rtn_path[PATH_MAX - 1] = '\0';
/* Seek to end of rtn_path. */
strptr1 = rtn_path;
while(*strptr1 != '\0')
strptr1++;
/* Insert deliminating '/' char. */
if(strptr1 > rtn_path)
{
if(*(strptr1 - 1) != '/')
{
*strptr1 = '/';
strptr1++;
}
}
else
{
strptr1 = rtn_path;
}
/* Copy child. */
strptr2 = child;
while((strptr1 < strend) &&
(*strptr2 != '\0')
)
{
*strptr1 = *strptr2;
strptr1++;
strptr2++;
}
if(strptr1 < strend)
*strptr1 = '\0';
else
rtn_path[PATH_MAX - 1] = '\0';
/* Make sure rtn_path contains atleast "/". */
if(*rtn_path == '\0')
{
rtn_path[0] = '/';
rtn_path[1] = '\0';
}
return(rtn_path);
}
/*
* Returns an allocated string containing the link's destination
* or NULL on error or if link is not really a link.
*
* The pointer returned must be free()ed at by the
* calling function.
*/
char *GetAllocLinkDest(char *link)
{
int bytes_read;
char *dest;
struct stat stat_buf;
if(link == NULL)
return(NULL);
if(lstat(link, &stat_buf))
return(NULL);
if(!S_ISLNK(stat_buf.st_mode))
return(NULL);
/* Allocate dest buffer. */
dest = (char *)calloc(1, (PATH_MAX + NAME_MAX + 1) * sizeof(char));
if(dest == NULL)
return(NULL);
/* Read link's destination. */
bytes_read = readlink(link, dest, PATH_MAX + NAME_MAX);
if(bytes_read <= 0)
{
/* Empty dest buffer, return an allocated but empty string. */
dest[0] = '\0';
return(dest);
}
if(bytes_read > PATH_MAX + NAME_MAX)
bytes_read = PATH_MAX + NAME_MAX;
if(bytes_read < 0)
bytes_read = 0;
/* Must NULL terminate. */
dest[bytes_read] = '\0';
return(dest);
}
/*
* Returns the number of lines in the file filename.
*/
int FileCountLines(char *filename)
{
FILE *fp;
int i, lines = 0;
fp = fopen(filename, "r");
if(fp == NULL)
return(lines);
/* Start counting lines. */
i = fgetc(fp);
while(i != EOF)
{
if(((char)i == '\r') ||
((char)i == '\n')
)
lines++;
i = fgetc(fp);
}
/* Close file. */
fclose(fp);
return(lines);
}
/*
* Returns 1 if path is a dir and contains sub dirs.
*
* The given path must be an absolute path.
*/
int DirHasSubDirs(char *path)
{
char *strptr;
DIR *dir;
struct dirent *dent;
char tmp_path[PATH_MAX + NAME_MAX];
int status = 0;
/* Error checks. */
if(path == NULL) return(status);
if(!ISPATHDIR(path)) return(status);
/* Open dir. */
dir = opendir(path);
if(dir == NULL) return(status);
/* Scan through dirs. */
dent = (struct dirent *)readdir(dir);
while(dent != NULL)
{
/* Skip special dir notations. */
if(!strcmp(dent->d_name, ".") ||
!strcmp(dent->d_name, "..")
)
{
dent = (struct dirent *)readdir(dir);
continue;
}
strptr = PrefixPaths(path, dent->d_name);
if(strptr == NULL) continue;
strncpy(tmp_path, strptr, PATH_MAX + NAME_MAX);
tmp_path[PATH_MAX + NAME_MAX - 1] = '\0';
if(ISPATHDIR(tmp_path))
{
status = 1;
break;
}
dent = (struct dirent *)readdir(dir);
}
/* Close dir. */
closedir(dir);
return(status);
}
/*
* Returns a statically allocated string containing the parent
* of the given absolute path.
*/
char *GetParentDir(char *path)
{
int i;
char *strptr1, *strptr2;
static char rtn_path[PATH_MAX];
if(path == NULL)
return("/");
i = 0;
strptr1 = path;
strptr2 = rtn_path;
while((*strptr1 != '\0') && (i < PATH_MAX))
{
*strptr2 = *strptr1;
strptr1++;
strptr2++;
i++;
}
if(i < PATH_MAX)
*strptr2 = '\0';
else
rtn_path[PATH_MAX - 1] = '\0';
strptr2--;
if(strptr2 < rtn_path)
strptr2 = rtn_path;
while(*strptr2 == '/')
{
*strptr2 = '\0';
strptr2--;
}
/* Removing the child. */
while((strptr2 > rtn_path) &&
(*strptr2 != '/')
)
strptr2--;
if(strptr2 < rtn_path)
strptr2 = rtn_path;
*strptr2 = '\0';
/* Make sure rtn_path contains atleast "/". */
if(*rtn_path == '\0')
{
rtn_path[0] = '/';
rtn_path[1] = '\0';
}
return(rtn_path);
}
/*
* Simplifies path statement path by removing:
*
* 1. Any embedded "whatever/.." in it.
*
* Path must be absolute.
*/
void SimplifyPath(char *path)
{
char *strptr1, *strptr2, *strptr3;
if(path == NULL)
return;
/* Look for a "/..". */
strptr1 = strstr(path, "/..");
while(strptr1 != NULL)
{
/* Seek next '/' char or end. */
strptr2 = strptr1 + 1;
while((*strptr2 != '\0') &&
(*strptr2 != '/')
)
strptr2++;
/* Seek prev '/' char or start. */
strptr3 = strptr1 - 1;
while((strptr3 > path) &&
(*strptr3 != '/')
)
strptr3--;
if(strptr3 < path)
strptr3 = path;
/* Copy segment. */
while(*strptr2 != '\0')
{
*strptr3 = *strptr2;
strptr2++;
strptr3++;
}
*strptr3 = '\0';
/* Look for another "/.." if any. */
strptr1 = strstr(path, "/..");
}
/* Make sure path contains atleast a "/". */
if(*path == '\0')
{
path[0] = '/';
path[1] = '\0';
}
return;
}
/*
* Coppies object source to target.
*
* If target exists and comferm_func is not NULL, then
* comferm_func will be called, given the target and
* source (respectivly) and return is checked.
*
* Return of any true value from comferm_func will mean overwrite,
* and return of 0 from comferm_func will mean do not overwrite.
*/
int CopyObject(
char *target,
char *source,
int (*comferm_func)(char *, char *)
)
{
int i;
FILE *tar_fp, *src_fp;
struct stat stat_buf;
if((target == NULL) ||
(source == NULL)
)
return(-1);
/* Check if target exists. */
if(!stat(target, &stat_buf))
{
if(comferm_func != NULL)
{
/* Overwrite? */
if(!comferm_func(target, source))
return(-3); /* Don't! */
}
}
/* Truncate target and open source. */
tar_fp = fopen(target, "wb");
if(tar_fp == NULL)
return(-1);
src_fp = fopen(source, "rb");
if(src_fp == NULL)
return(-1);
i = fgetc(src_fp);
while(i != EOF)
{
if(fputc(i, tar_fp) == EOF)
break;
i = fgetc(src_fp);
}
fclose(tar_fp);
fclose(src_fp);
return(0);
}
|