void print_status(int val, char *path1, char *path2, char *entry) { switch (val) { case D_ONLY: print_only(path1, strlen(path1), entry); break; case D_COMMON: fprintf(buffer, "Common subdirectories: %s%s and %s%s\n", path1, entry ? entry : "", path2, entry ? entry : ""); break; case D_BINARY: fprintf(buffer, "Files %s%s and %s%s differ\n", path1, entry ? entry : "", path2, entry ? entry : ""); break; case D_DIFFER: if (format == D_BRIEF) fprintf(buffer, "Files %s%s and %s%s differ\n", path1, entry ? entry : "", path2, entry ? entry : ""); break; case D_SAME: if (sflag) fprintf(buffer, "Files %s%s and %s%s are identical\n", path1, entry ? entry : "", path2, entry ? entry : ""); break; case D_MISMATCH1: fprintf(buffer, "File %s%s is a directory while file %s%s is a regular file\n", path1, entry ? entry : "", path2, entry ? entry : ""); break; case D_MISMATCH2: fprintf(buffer, "File %s%s is a regular file while file %s%s is a directory\n", path1, entry ? entry : "", path2, entry ? entry : ""); break; case D_SKIPPED1: fprintf(buffer, "File %s%s is not a regular file or directory and was skipped\n", path1, entry ? entry : ""); break; case D_SKIPPED2: fprintf(buffer, "File %s%s is not a regular file or directory and was skipped\n", path2, entry ? entry : ""); break; } }
void msh_echo(char **args, char **env) { int i; i = 1; if (args[1] == NULL) { ft_putchar('\n'); return ; } if (args[1][0] == '$') func_print_env(args[1], env); else { while (args[i]) { print_only(args[i]); i++; } ft_putchar('\n'); } }
/* * Diff directory traversal. Will be called recursively if -r was specified. */ void diffdir(char *p1, char *p2, int flags) { struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL; struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL; size_t dirlen1, dirlen2; char path1[MAXPATHLEN], path2[MAXPATHLEN]; int pos; dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1)); if (dirlen1 >= sizeof(path1) - 1) { warnx("%s: %s", p1, strerror(ENAMETOOLONG)); status = 2; return; } if (path1[dirlen1 - 1] != '/') { path1[dirlen1++] = '/'; path1[dirlen1] = '\0'; } dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2)); if (dirlen2 >= sizeof(path2) - 1) { warnx("%s: %s", p2, strerror(ENAMETOOLONG)); status = 2; return; } if (path2[dirlen2 - 1] != '/') { path2[dirlen2++] = '/'; path2[dirlen2] = '\0'; } /* * Get a list of entries in each directory, skipping "excluded" files * and sorting alphabetically. */ pos = scandir(path1, &dirp1, selectfile, alphasort); if (pos == -1) { if (errno == ENOENT && (Nflag || Pflag)) { pos = 0; } else { warn("%s", path1); goto closem; } } dp1 = dirp1; edp1 = dirp1 + pos; pos = scandir(path2, &dirp2, selectfile, alphasort); if (pos == -1) { if (errno == ENOENT && Nflag) { pos = 0; } else { warn("%s", path2); goto closem; } } dp2 = dirp2; edp2 = dirp2 + pos; /* * If we were given a starting point, find it. */ if (start != NULL) { while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0) dp1++; while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0) dp2++; } /* * Iterate through the two directory lists, diffing as we go. */ while (dp1 != edp1 || dp2 != edp2) { dent1 = dp1 != edp1 ? *dp1 : NULL; dent2 = dp2 != edp2 ? *dp2 : NULL; pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 : strcmp(dent1->d_name, dent2->d_name); if (pos == 0) { /* file exists in both dirs, diff it */ diffit(dent1, path1, dirlen1, path2, dirlen2, flags); dp1++; dp2++; } else if (pos < 0) { /* file only in first dir, only diff if -N */ if (Nflag) diffit(dent1, path1, dirlen1, path2, dirlen2, flags); else if (lflag) dent1->d_status |= D_ONLY; else print_only(path1, dirlen1, dent1->d_name); dp1++; } else { /* file only in second dir, only diff if -N or -P */ if (Nflag || Pflag) diffit(dent2, path1, dirlen1, path2, dirlen2, flags); else if (lflag) dent2->d_status |= D_ONLY; else print_only(path2, dirlen2, dent2->d_name); dp2++; } } if (lflag) { path1[dirlen1] = '\0'; path2[dirlen2] = '\0'; for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) { print_status(dent1->d_status, path1, path2, dent1->d_name); } for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) { if (dent2->d_status == D_ONLY) print_status(dent2->d_status, path2, NULL, dent2->d_name); } } closem: if (dirp1 != NULL) { for (dp1 = dirp1; dp1 < edp1; dp1++) xfree(*dp1); xfree(dirp1); } if (dirp2 != NULL) { for (dp2 = dirp2; dp2 < edp2; dp2++) xfree(*dp2); xfree(dirp2); } }
/* * Diff directory traversal. Will be called recursively if -r was specified. */ void diffdir(char *p1, char *p2, int flags) { struct diffdirent **dirp1, **dirp2, **dp1, **dp2; struct diffdirent *dent1, *dent2; size_t dirlen1, dirlen2; char path1[MAXPATHLEN], path2[MAXPATHLEN]; char *dirbuf1, *dirbuf2; int pos; dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1)); if (dirlen1 >= sizeof(path1) - 1) { warnx("%s: %s", p1, strerror(ENAMETOOLONG)); status = 2; return; } if (path1[dirlen1 - 1] != '/') { path1[dirlen1++] = '/'; path1[dirlen1] = '\0'; } dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2)); if (dirlen2 >= sizeof(path2) - 1) { warnx("%s: %s", p2, strerror(ENAMETOOLONG)); status = 2; return; } if (path2[dirlen2 - 1] != '/') { path2[dirlen2++] = '/'; path2[dirlen2] = '\0'; } /* get a list of the entries in each directory */ dp1 = dirp1 = slurpdir(path1, &dirbuf1, Nflag + Pflag); dp2 = dirp2 = slurpdir(path2, &dirbuf2, Nflag); if (dirp1 == NULL || dirp2 == NULL) return; /* * If we were given a starting point, find it. */ if (start != NULL) { while (*dp1 != NULL && strcmp((*dp1)->d_name, start) < 0) dp1++; while (*dp2 != NULL && strcmp((*dp2)->d_name, start) < 0) dp2++; } /* * Iterate through the two directory lists, diffing as we go. */ while (*dp1 != NULL || *dp2 != NULL) { dent1 = *dp1; dent2 = *dp2; pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 : strcmp(dent1->d_name, dent2->d_name); if (pos == 0) { /* file exists in both dirs, diff it */ diffit(dent1, path1, dirlen1, path2, dirlen2, flags); dp1++; dp2++; } else if (pos < 0) { /* file only in first dir, only diff if -N */ if (Nflag) diffit(dent1, path1, dirlen1, path2, dirlen2, flags); else if (lflag) dent1->d_status |= D_ONLY; else print_only(path1, dirlen1, dent1->d_name); dp1++; } else { /* file only in second dir, only diff if -N or -P */ if (Nflag || Pflag) diffit(dent2, path1, dirlen1, path2, dirlen2, flags); else if (lflag) dent2->d_status |= D_ONLY; else print_only(path2, dirlen2, dent2->d_name); dp2++; } } if (lflag) { path1[dirlen1] = '\0'; path2[dirlen2] = '\0'; for (dp1 = dirp1; (dent1 = *dp1) != NULL; dp1++) { print_status(dent1->d_status, path1, path2, dent1->d_name); } for (dp2 = dirp2; (dent2 = *dp2) != NULL; dp2++) { if (dent2->d_status == D_ONLY) print_status(dent2->d_status, path2, NULL, dent2->d_name); } } if (dirbuf1 != NULL) { xfree(dirp1); xfree(dirbuf1); } if (dirbuf2 != NULL) { xfree(dirp2); xfree(dirbuf2); } }