diff -uNr mc-4.8.10-reference/src/filemanager/find.c mc-4.8.10.no-getline-at/src/filemanager/find.c
old
|
new
|
|
929 | 929 | } |
930 | 930 | |
931 | 931 | /* --------------------------------------------------------------------------------------------- */ |
932 | | /** |
933 | | * get_line_at: |
934 | | * |
935 | | * Returns malloced null-terminated line from file file_fd. |
936 | | * Input is buffered in buf_size long buffer. |
937 | | * Current pos in buf is stored in pos. |
938 | | * n_read - number of read chars. |
939 | | * has_newline - is there newline ? |
940 | | */ |
941 | | |
942 | | static char * |
943 | | get_line_at (int file_fd, char *buf, int buf_size, int *pos, int *n_read, gboolean * has_newline) |
944 | | { |
945 | | char *buffer = NULL; |
946 | | int buffer_size = 0; |
947 | | char ch = 0; |
948 | | int i = 0; |
949 | | |
950 | | while (TRUE) |
951 | | { |
952 | | if (*pos >= *n_read) |
953 | | { |
954 | | *pos = 0; |
955 | | *n_read = mc_read (file_fd, buf, buf_size); |
956 | | if (*n_read <= 0) |
957 | | break; |
958 | | } |
959 | | |
960 | | ch = buf[(*pos)++]; |
961 | | if (ch == '\0') |
962 | | { |
963 | | /* skip possible leading zero(s) */ |
964 | | if (i == 0) |
965 | | continue; |
966 | | break; |
967 | | } |
968 | | |
969 | | if (i >= buffer_size - 1) |
970 | | buffer = g_realloc (buffer, buffer_size += 80); |
971 | | |
972 | | /* Strip newline */ |
973 | | if (ch == '\n') |
974 | | break; |
975 | | |
976 | | buffer[i++] = ch; |
977 | | } |
978 | | |
979 | | *has_newline = (ch != '\0'); |
980 | | |
981 | | if (buffer != NULL) |
982 | | buffer[i] = '\0'; |
983 | | |
984 | | return buffer; |
985 | | } |
986 | | |
987 | | /* --------------------------------------------------------------------------------------------- */ |
988 | 932 | |
989 | 933 | static FindProgressStatus |
990 | 934 | check_find_events (WDialog * h) |
… |
… |
|
1028 | 972 | search_content (WDialog * h, const char *directory, const char *filename) |
1029 | 973 | { |
1030 | 974 | struct stat s; |
1031 | | char buffer[BUF_4K]; |
| 975 | char buffer[BUF_4K]; /* raw input buffer */ |
| 976 | char *strbuf = NULL; /* buffer for fetched string */ |
| 977 | int strbuf_size = 0; |
1032 | 978 | int file_fd; |
1033 | 979 | gboolean ret_val = FALSE; |
1034 | 980 | vfs_path_t *vpath; |
… |
… |
|
1059 | 1005 | int line = 1; |
1060 | 1006 | int pos = 0; |
1061 | 1007 | int n_read = 0; |
1062 | | gboolean has_newline; |
1063 | | char *p = NULL; |
1064 | 1008 | gboolean found = FALSE; |
1065 | 1009 | gsize found_len; |
1066 | 1010 | char result[BUF_MEDIUM]; |
… |
… |
|
1072 | 1016 | line = last_line; |
1073 | 1017 | pos = last_pos; |
1074 | 1018 | } |
1075 | | while (!ret_val |
1076 | | && (p = get_line_at (file_fd, buffer, sizeof (buffer), |
1077 | | &pos, &n_read, &has_newline)) != NULL) |
| 1019 | while (!ret_val) |
1078 | 1020 | { |
| 1021 | char ch = 0; |
| 1022 | int i = 0; |
| 1023 | |
| 1024 | /* read to buffer and get line from there */ |
| 1025 | while (TRUE) |
| 1026 | { |
| 1027 | if (pos >= n_read) |
| 1028 | { |
| 1029 | pos = 0; |
| 1030 | n_read = mc_read (file_fd, buffer, sizeof(buffer)); |
| 1031 | if (n_read <= 0) |
| 1032 | break; |
| 1033 | } |
| 1034 | |
| 1035 | ch = buffer[pos++]; |
| 1036 | if (ch == '\0') |
| 1037 | { |
| 1038 | /* skip possible leading zero(s) */ |
| 1039 | if (i == 0) |
| 1040 | continue; |
| 1041 | break; |
| 1042 | } |
| 1043 | |
| 1044 | if (i >= strbuf_size - 1) |
| 1045 | strbuf = g_realloc (strbuf, strbuf_size += 80); |
| 1046 | |
| 1047 | /* Strip newline */ |
| 1048 | if (ch == '\n') |
| 1049 | break; |
| 1050 | |
| 1051 | strbuf[i++] = ch; |
| 1052 | } |
| 1053 | |
| 1054 | if (i == 0) { |
| 1055 | if (ch == '\0') |
| 1056 | break; |
| 1057 | else /* if (ch == '\n'): do not search in empty strings */ |
| 1058 | goto skip_search; |
| 1059 | } |
| 1060 | strbuf[i] = '\0'; |
| 1061 | |
1079 | 1062 | if (!found /* Search in binary line once */ |
1080 | 1063 | && mc_search_run (search_content_handle, |
1081 | | (const void *) p, 0, strlen (p), &found_len)) |
| 1064 | (const void *) strbuf, 0, i, &found_len)) |
1082 | 1065 | { |
1083 | 1066 | g_snprintf (result, sizeof (result), "%d:%s", line, filename); |
1084 | 1067 | find_add_match (directory, result); |
1085 | 1068 | found = TRUE; |
1086 | 1069 | } |
1087 | | g_free (p); |
1088 | 1070 | |
1089 | 1071 | if (found && options.content_first_hit) |
1090 | 1072 | break; |
1091 | 1073 | |
1092 | | if (has_newline) |
| 1074 | if (ch == '\n') |
1093 | 1075 | { |
| 1076 | skip_search: |
1094 | 1077 | found = FALSE; |
1095 | 1078 | line++; |
1096 | 1079 | } |
… |
… |
|
1117 | 1100 | } |
1118 | 1101 | } |
1119 | 1102 | } |
| 1103 | g_free (strbuf); |
1120 | 1104 | tty_disable_interrupt_key (); |
1121 | 1105 | mc_close (file_fd); |
1122 | 1106 | return ret_val; |