[1714] | 1 | /* |
---|
| 2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/delete/col.c,v $ |
---|
| 3 | * $Author: jik $ |
---|
| 4 | * |
---|
| 5 | * This program is part of a package including delete, undelete, |
---|
| 6 | * lsdel, expunge and purge. The software suite is meant as a |
---|
| 7 | * replacement for rm which allows for file recovery. |
---|
| 8 | * |
---|
| 9 | * Copyright (c) 1989 by the Massachusetts Institute of Technology. |
---|
[4505] | 10 | * For copying and distribution information, see the file "mit-copying.h." |
---|
[1714] | 11 | */ |
---|
| 12 | |
---|
| 13 | #if (!defined(lint) && !defined(SABER)) |
---|
[4505] | 14 | static char rcsid_col_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/col.c,v 1.8 1991-02-28 18:42:09 jik Exp $"; |
---|
[1714] | 15 | #endif |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * Note that this function has a lot of options I'm not really using |
---|
| 19 | * because I took it out of other code that needed a lot more |
---|
| 20 | * versatility. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #include <stdio.h> |
---|
[3049] | 24 | #ifdef SYSV |
---|
| 25 | #include <string.h> |
---|
| 26 | #define index strchr |
---|
| 27 | #define rindex strrchr |
---|
| 28 | #else |
---|
[1714] | 29 | #include <strings.h> |
---|
[3049] | 30 | #endif /* SYSV */ |
---|
[2186] | 31 | #include "errors.h" |
---|
| 32 | #include "delete_errs.h" |
---|
[1714] | 33 | #include "col.h" |
---|
[4505] | 34 | #include "mit-copying.h" |
---|
[1714] | 35 | |
---|
| 36 | |
---|
[2239] | 37 | static int calc_string_width(), calc_widths(), num_width(); |
---|
[1714] | 38 | static void trim_strings(); |
---|
| 39 | |
---|
| 40 | int column_array(strings, num_to_print, screen_width, column_width, |
---|
| 41 | number_of_columns, margin, spread_flag, |
---|
| 42 | number_flag, var_col_flag, outfile) |
---|
| 43 | char **strings; |
---|
| 44 | FILE *outfile; |
---|
| 45 | { |
---|
| 46 | char buf[BUFSIZ]; |
---|
| 47 | int updown, leftright, height; |
---|
| 48 | int string_width; |
---|
| 49 | int numwidth; |
---|
| 50 | |
---|
| 51 | numwidth = num_width(num_to_print); |
---|
| 52 | if (! var_col_flag) { |
---|
| 53 | string_width = calc_string_width(column_width, margin, number_flag, |
---|
| 54 | num_to_print); |
---|
[2186] | 55 | if (string_width <= 0) { |
---|
| 56 | set_error(COL_COLUMNS_TOO_THIN); |
---|
| 57 | error("calc_string_width"); |
---|
| 58 | return error_code; |
---|
[1714] | 59 | } |
---|
| 60 | trim_strings(strings, num_to_print, string_width); |
---|
| 61 | } else if (calc_widths(strings, &screen_width, &column_width, |
---|
| 62 | &number_of_columns, num_to_print, &margin, |
---|
[2186] | 63 | spread_flag, number_flag)) { |
---|
| 64 | error("calc_widths"); |
---|
| 65 | return error_code; |
---|
| 66 | } |
---|
[1714] | 67 | height = num_to_print / number_of_columns; |
---|
| 68 | if (num_to_print % number_of_columns) |
---|
| 69 | height++; |
---|
| 70 | |
---|
| 71 | if (number_flag) for (updown = 0; updown < height; updown++) { |
---|
[1847] | 72 | for (leftright = updown; leftright < num_to_print; ) { |
---|
[1714] | 73 | (void) sprintf(buf, "%*d. %s", numwidth, leftright+1, |
---|
| 74 | strings[leftright]); |
---|
[1847] | 75 | if ((leftright += height) >= num_to_print) |
---|
| 76 | fprintf(outfile, "%s", buf ); |
---|
| 77 | else |
---|
| 78 | fprintf(outfile, "%*s", -column_width, buf); |
---|
[1714] | 79 | } |
---|
| 80 | fprintf(outfile, "\n"); |
---|
| 81 | } else for (updown = 0; updown < height; updown++) { |
---|
[1847] | 82 | for (leftright = updown; leftright < num_to_print; ) { |
---|
[1714] | 83 | (void) sprintf(buf, "%s", strings[leftright]); |
---|
[1847] | 84 | if ((leftright += height) >= num_to_print) |
---|
| 85 | fprintf(outfile, "%s", buf ); |
---|
| 86 | else |
---|
| 87 | fprintf(outfile, "%*s", -column_width, buf); |
---|
[1714] | 88 | } |
---|
| 89 | fprintf(outfile, "\n"); |
---|
| 90 | } |
---|
[2186] | 91 | return 0; |
---|
[1714] | 92 | } |
---|
| 93 | |
---|
| 94 | static int calc_string_width(column_width, margin, number_flag, max_number) |
---|
| 95 | { |
---|
| 96 | int string_width; |
---|
| 97 | |
---|
| 98 | string_width = column_width - margin; |
---|
| 99 | if (number_flag) |
---|
| 100 | string_width = string_width - num_width(max_number) - strlen(". "); |
---|
[2186] | 101 | return string_width; |
---|
[1714] | 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | static void trim_strings(strings, number, width) |
---|
| 106 | char **strings; |
---|
| 107 | { |
---|
| 108 | int loop; |
---|
| 109 | |
---|
| 110 | for (loop = 0; loop < number; loop++) |
---|
| 111 | if (strlen(strings[loop]) > width) |
---|
| 112 | strings[loop][width] = '\0'; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | static int calc_widths(strings, screen_width, column_width, number_of_columns, |
---|
| 117 | num_to_print, margin, spread_flag, number_flag) |
---|
| 118 | int *screen_width, *column_width, *number_of_columns, *margin; |
---|
| 119 | char **strings; |
---|
| 120 | { |
---|
| 121 | int loop; |
---|
| 122 | int maxlen, templen; |
---|
| 123 | int spread; |
---|
| 124 | |
---|
| 125 | maxlen = templen = 0; |
---|
| 126 | for (loop = 0; loop < num_to_print; loop++) |
---|
| 127 | if (maxlen < (templen = strlen(strings[loop]))) |
---|
| 128 | maxlen = templen; |
---|
[2186] | 129 | |
---|
[1714] | 130 | *column_width = maxlen; |
---|
| 131 | |
---|
| 132 | if (number_flag) |
---|
| 133 | *column_width = *column_width + num_width(num_to_print) + |
---|
| 134 | strlen(". "); |
---|
| 135 | |
---|
| 136 | if (! spread_flag) { |
---|
| 137 | *column_width += *margin; |
---|
| 138 | if (! *number_of_columns) { |
---|
| 139 | *number_of_columns = *screen_width / *column_width; |
---|
| 140 | if (! *number_of_columns) { |
---|
| 141 | (*number_of_columns)++; |
---|
| 142 | *column_width -= *margin; |
---|
| 143 | *margin = 0; |
---|
| 144 | *screen_width = *column_width; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | else |
---|
| 148 | *screen_width = *number_of_columns * *column_width; |
---|
| 149 | } else { |
---|
| 150 | if (! *number_of_columns) { |
---|
| 151 | *number_of_columns = *screen_width / (*column_width + *margin); |
---|
| 152 | if (! *number_of_columns) { |
---|
| 153 | (*number_of_columns)++; |
---|
| 154 | *screen_width = *column_width; |
---|
| 155 | *margin = 0; |
---|
| 156 | } |
---|
| 157 | spread = (*screen_width - *number_of_columns * *column_width) |
---|
| 158 | / *number_of_columns; |
---|
| 159 | *column_width += spread; |
---|
| 160 | } |
---|
| 161 | else { |
---|
| 162 | if (*number_of_columns * (*column_width + *margin) > |
---|
| 163 | *screen_width) { |
---|
| 164 | *column_width += *margin; |
---|
| 165 | *screen_width = *column_width; |
---|
| 166 | } else { |
---|
| 167 | spread = (*screen_width - (*number_of_columns * |
---|
| 168 | *column_width)) / |
---|
| 169 | *number_of_columns; |
---|
| 170 | *column_width += spread; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|
[2186] | 174 | return 0; |
---|
[1714] | 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | static int num_width(number) |
---|
[2186] | 181 | int number; |
---|
[1714] | 182 | { |
---|
| 183 | char buf[BUFSIZ]; |
---|
| 184 | |
---|
[2186] | 185 | (void) sprintf(buf, "%d", number); |
---|
| 186 | return strlen(buf); |
---|
[1714] | 187 | } |
---|