[1714] | 1 | /* |
---|
| 2 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/delete/col.c,v $ |
---|
[10977] | 3 | * $Author: danw $ |
---|
[1714] | 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)) |
---|
[10977] | 14 | static char rcsid_col_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/col.c,v 1.9 1997-12-31 22:35:57 danw 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 | #include <string.h> |
---|
[2186] | 25 | #include "errors.h" |
---|
| 26 | #include "delete_errs.h" |
---|
[1714] | 27 | #include "col.h" |
---|
[4505] | 28 | #include "mit-copying.h" |
---|
[1714] | 29 | |
---|
| 30 | |
---|
[2239] | 31 | static int calc_string_width(), calc_widths(), num_width(); |
---|
[1714] | 32 | static void trim_strings(); |
---|
| 33 | |
---|
| 34 | int column_array(strings, num_to_print, screen_width, column_width, |
---|
| 35 | number_of_columns, margin, spread_flag, |
---|
| 36 | number_flag, var_col_flag, outfile) |
---|
| 37 | char **strings; |
---|
| 38 | FILE *outfile; |
---|
| 39 | { |
---|
| 40 | char buf[BUFSIZ]; |
---|
| 41 | int updown, leftright, height; |
---|
| 42 | int string_width; |
---|
| 43 | int numwidth; |
---|
| 44 | |
---|
| 45 | numwidth = num_width(num_to_print); |
---|
| 46 | if (! var_col_flag) { |
---|
| 47 | string_width = calc_string_width(column_width, margin, number_flag, |
---|
| 48 | num_to_print); |
---|
[2186] | 49 | if (string_width <= 0) { |
---|
| 50 | set_error(COL_COLUMNS_TOO_THIN); |
---|
| 51 | error("calc_string_width"); |
---|
| 52 | return error_code; |
---|
[1714] | 53 | } |
---|
| 54 | trim_strings(strings, num_to_print, string_width); |
---|
| 55 | } else if (calc_widths(strings, &screen_width, &column_width, |
---|
| 56 | &number_of_columns, num_to_print, &margin, |
---|
[2186] | 57 | spread_flag, number_flag)) { |
---|
| 58 | error("calc_widths"); |
---|
| 59 | return error_code; |
---|
| 60 | } |
---|
[1714] | 61 | height = num_to_print / number_of_columns; |
---|
| 62 | if (num_to_print % number_of_columns) |
---|
| 63 | height++; |
---|
| 64 | |
---|
| 65 | if (number_flag) for (updown = 0; updown < height; updown++) { |
---|
[1847] | 66 | for (leftright = updown; leftright < num_to_print; ) { |
---|
[1714] | 67 | (void) sprintf(buf, "%*d. %s", numwidth, leftright+1, |
---|
| 68 | strings[leftright]); |
---|
[1847] | 69 | if ((leftright += height) >= num_to_print) |
---|
| 70 | fprintf(outfile, "%s", buf ); |
---|
| 71 | else |
---|
| 72 | fprintf(outfile, "%*s", -column_width, buf); |
---|
[1714] | 73 | } |
---|
| 74 | fprintf(outfile, "\n"); |
---|
| 75 | } else for (updown = 0; updown < height; updown++) { |
---|
[1847] | 76 | for (leftright = updown; leftright < num_to_print; ) { |
---|
[1714] | 77 | (void) sprintf(buf, "%s", strings[leftright]); |
---|
[1847] | 78 | if ((leftright += height) >= num_to_print) |
---|
| 79 | fprintf(outfile, "%s", buf ); |
---|
| 80 | else |
---|
| 81 | fprintf(outfile, "%*s", -column_width, buf); |
---|
[1714] | 82 | } |
---|
| 83 | fprintf(outfile, "\n"); |
---|
| 84 | } |
---|
[2186] | 85 | return 0; |
---|
[1714] | 86 | } |
---|
| 87 | |
---|
| 88 | static int calc_string_width(column_width, margin, number_flag, max_number) |
---|
| 89 | { |
---|
| 90 | int string_width; |
---|
| 91 | |
---|
| 92 | string_width = column_width - margin; |
---|
| 93 | if (number_flag) |
---|
| 94 | string_width = string_width - num_width(max_number) - strlen(". "); |
---|
[2186] | 95 | return string_width; |
---|
[1714] | 96 | } |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | static void trim_strings(strings, number, width) |
---|
| 100 | char **strings; |
---|
| 101 | { |
---|
| 102 | int loop; |
---|
| 103 | |
---|
| 104 | for (loop = 0; loop < number; loop++) |
---|
| 105 | if (strlen(strings[loop]) > width) |
---|
| 106 | strings[loop][width] = '\0'; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | static int calc_widths(strings, screen_width, column_width, number_of_columns, |
---|
| 111 | num_to_print, margin, spread_flag, number_flag) |
---|
| 112 | int *screen_width, *column_width, *number_of_columns, *margin; |
---|
| 113 | char **strings; |
---|
| 114 | { |
---|
| 115 | int loop; |
---|
| 116 | int maxlen, templen; |
---|
| 117 | int spread; |
---|
| 118 | |
---|
| 119 | maxlen = templen = 0; |
---|
| 120 | for (loop = 0; loop < num_to_print; loop++) |
---|
| 121 | if (maxlen < (templen = strlen(strings[loop]))) |
---|
| 122 | maxlen = templen; |
---|
[2186] | 123 | |
---|
[1714] | 124 | *column_width = maxlen; |
---|
| 125 | |
---|
| 126 | if (number_flag) |
---|
| 127 | *column_width = *column_width + num_width(num_to_print) + |
---|
| 128 | strlen(". "); |
---|
| 129 | |
---|
| 130 | if (! spread_flag) { |
---|
| 131 | *column_width += *margin; |
---|
| 132 | if (! *number_of_columns) { |
---|
| 133 | *number_of_columns = *screen_width / *column_width; |
---|
| 134 | if (! *number_of_columns) { |
---|
| 135 | (*number_of_columns)++; |
---|
| 136 | *column_width -= *margin; |
---|
| 137 | *margin = 0; |
---|
| 138 | *screen_width = *column_width; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | else |
---|
| 142 | *screen_width = *number_of_columns * *column_width; |
---|
| 143 | } else { |
---|
| 144 | if (! *number_of_columns) { |
---|
| 145 | *number_of_columns = *screen_width / (*column_width + *margin); |
---|
| 146 | if (! *number_of_columns) { |
---|
| 147 | (*number_of_columns)++; |
---|
| 148 | *screen_width = *column_width; |
---|
| 149 | *margin = 0; |
---|
| 150 | } |
---|
| 151 | spread = (*screen_width - *number_of_columns * *column_width) |
---|
| 152 | / *number_of_columns; |
---|
| 153 | *column_width += spread; |
---|
| 154 | } |
---|
| 155 | else { |
---|
| 156 | if (*number_of_columns * (*column_width + *margin) > |
---|
| 157 | *screen_width) { |
---|
| 158 | *column_width += *margin; |
---|
| 159 | *screen_width = *column_width; |
---|
| 160 | } else { |
---|
| 161 | spread = (*screen_width - (*number_of_columns * |
---|
| 162 | *column_width)) / |
---|
| 163 | *number_of_columns; |
---|
| 164 | *column_width += spread; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
[2186] | 168 | return 0; |
---|
[1714] | 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | static int num_width(number) |
---|
[2186] | 175 | int number; |
---|
[1714] | 176 | { |
---|
| 177 | char buf[BUFSIZ]; |
---|
| 178 | |
---|
[2186] | 179 | (void) sprintf(buf, "%d", number); |
---|
| 180 | return strlen(buf); |
---|
[1714] | 181 | } |
---|