chdir back up one level
| 2918 | |
| 2919 | // chdir back up one level |
| 2920 | void Query::deselect() |
| 2921 | { |
| 2922 | if (selected_file_.empty()) |
| 2923 | { |
| 2924 | if (flag_stdin) |
| 2925 | { |
| 2926 | message("cannot chdir .. because standard input is searched"); |
| 2927 | return; |
| 2928 | } |
| 2929 | |
| 2930 | if (!Static::arg_files.empty()) |
| 2931 | { |
| 2932 | message("cannot chdir .. because file or directory arguments are present"); |
| 2933 | return; |
| 2934 | } |
| 2935 | |
| 2936 | #ifdef OS_WIN |
| 2937 | if (dirs_.size() == 3 && dirs_.at(1) == ':' && dirs_.at(2) == PATHSEPCHR) |
| 2938 | return; |
| 2939 | #else |
| 2940 | if (dirs_ == PATHSEPSTR) |
| 2941 | return; |
| 2942 | #endif |
| 2943 | |
| 2944 | if (dirs_.empty()) |
| 2945 | { |
| 2946 | char *cwd = getcwd0(); |
| 2947 | if (cwd != NULL) |
| 2948 | { |
| 2949 | size_t n = strlen(cwd); |
| 2950 | dirs_.assign(cwd); |
| 2951 | wdir_.assign(cwd); |
| 2952 | if (n == 0 || cwd[n-1] != PATHSEPCHR) |
| 2953 | dirs_.append(PATHSEPSTR); |
| 2954 | free(cwd); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | if (dirs_.empty()) |
| 2959 | { |
| 2960 | if (chdir("..") < 0) |
| 2961 | return; |
| 2962 | |
| 2963 | dirs_.assign(".." PATHSEPSTR); |
| 2964 | } |
| 2965 | else |
| 2966 | { |
| 2967 | dirs_.pop_back(); |
| 2968 | |
| 2969 | size_t n = dirs_.find_last_of(PATHSEPCHR); |
| 2970 | |
| 2971 | if (n != std::string::npos && dirs_.front() == PATHSEPCHR) |
| 2972 | { |
| 2973 | if (chdir(dirs_.substr(0, n + 1).c_str()) < 0) |
| 2974 | return; |
| 2975 | |
| 2976 | dirs_.resize(n + 1); |
| 2977 | } |