source: trunk/athena/bin/delete/col.c @ 1847

Revision 1847, 5.4 KB checked in by jik, 35 years ago (diff)
patches from the net
Line 
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.
10 * For copying and distribution information, see the file "mit-copyright.h."
11 */
12
13#if (!defined(lint) && !defined(SABER))
14     static char rcsid_col_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/delete/col.c,v 1.3 1989-05-04 09:09:27 jik Exp $";
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>
24#include <strings.h>
25#include "col.h"
26#include "mit-copyright.h"
27
28
29static int calc_string_width();
30static void trim_strings();
31extern char *malloc();
32extern char *whoami;
33
34int column_array(strings, num_to_print, screen_width, column_width,
35                 number_of_columns, margin, spread_flag,
36                 number_flag, var_col_flag, outfile)
37char **strings;
38FILE *outfile;
39{
40     char buf[BUFSIZ];
41     int updown, leftright, height;
42     int string_width;
43     int numwidth;
44     
45
46     numwidth = num_width(num_to_print);
47     if (! var_col_flag) {
48          string_width = calc_string_width(column_width, margin, number_flag,
49                                           num_to_print);
50          if (string_width < 0) {
51               fprintf(stderr,
52                       "%s: do_wait: your columns aren't wide enough!\n",
53                       whoami);
54               return(1);
55          }
56          trim_strings(strings, num_to_print, string_width);
57     } else if (calc_widths(strings, &screen_width, &column_width,
58                            &number_of_columns, num_to_print, &margin,
59                            spread_flag, number_flag))
60          return(1);
61
62     height = num_to_print / number_of_columns;
63     if (num_to_print % number_of_columns)
64          height++;
65     
66     if (number_flag) for (updown = 0; updown < height; updown++) {
67          for (leftright = updown; leftright < num_to_print; ) {
68               (void) sprintf(buf, "%*d. %s", numwidth, leftright+1,
69                              strings[leftright]);
70               if ((leftright += height) >= num_to_print)
71                    fprintf(outfile, "%s", buf );
72               else
73                    fprintf(outfile, "%*s", -column_width, buf);
74          }
75          fprintf(outfile, "\n");
76     } else for (updown = 0; updown < height; updown++) {
77          for (leftright = updown; leftright < num_to_print; ) {
78               (void) sprintf(buf, "%s", strings[leftright]);
79               if ((leftright += height) >= num_to_print)
80                    fprintf(outfile, "%s", buf );
81               else
82                    fprintf(outfile, "%*s", -column_width, buf);
83          }
84          fprintf(outfile, "\n");
85     }
86     
87     return(0);
88}
89
90static int calc_string_width(column_width, margin, number_flag, max_number)
91{
92     int string_width;
93     
94     string_width = column_width - margin;
95     if (number_flag)
96          string_width = string_width - num_width(max_number) - strlen(". ");
97     return(string_width);
98}
99
100
101static void trim_strings(strings, number, width)
102char **strings;
103{
104     int loop;
105     
106     for (loop = 0; loop < number; loop++)
107          if (strlen(strings[loop]) > width)
108               strings[loop][width] = '\0';
109}
110
111
112static int calc_widths(strings, screen_width, column_width, number_of_columns,
113                       num_to_print, margin, spread_flag, number_flag)
114int *screen_width, *column_width, *number_of_columns, *margin;
115char **strings;
116{
117     int loop;
118     int maxlen, templen;
119     int spread;
120     
121#ifdef DEBUG
122     printf("calc_widths starting with screen_width %d column_width %d number_of_columns %d margin %d num_to_print %d spread_flag %d number_flag %d\n", *screen_width, *column_width, *number_of_columns, *margin, num_to_print, spread_flag, number_flag);
123#endif
124     maxlen = templen = 0;
125     for (loop = 0; loop < num_to_print; loop++)
126          if (maxlen < (templen = strlen(strings[loop])))
127               maxlen = templen;
128#ifdef DEBUG
129     printf("calc_widths maxlen %d\n", maxlen);
130#endif
131     *column_width = maxlen;
132     
133     if (number_flag)
134          *column_width = *column_width + num_width(num_to_print) +
135               strlen(". ");
136
137     if (! spread_flag) {
138          *column_width += *margin;
139          if (! *number_of_columns) {
140               *number_of_columns = *screen_width / *column_width;
141               if (! *number_of_columns) {
142                    (*number_of_columns)++;
143                    *column_width -= *margin;
144                    *margin = 0;
145                    *screen_width = *column_width;
146               }
147          }
148          else
149               *screen_width = *number_of_columns * *column_width;
150     } else {
151          if (! *number_of_columns) {
152               *number_of_columns = *screen_width / (*column_width + *margin);
153               if (! *number_of_columns) {
154                    (*number_of_columns)++;
155                    *screen_width = *column_width;
156                    *margin = 0;
157               }
158               spread = (*screen_width - *number_of_columns * *column_width)
159                    / *number_of_columns;
160               *column_width += spread;
161          }
162          else {
163               if (*number_of_columns * (*column_width + *margin) >
164                   *screen_width) {
165                    *column_width += *margin;
166                    *screen_width = *column_width;
167               } else {
168                    spread = (*screen_width - (*number_of_columns *
169                                               *column_width)) /
170                                                    *number_of_columns;
171                    *column_width += spread;
172               }
173          }
174     }
175#ifdef DEBUG
176     printf("calc_widths returning screen_width %d column_width %d number_of_columns %d margin %d\n", *screen_width, *column_width, *number_of_columns, *margin);
177#endif
178     return(0);
179}
180
181
182               
183
184static int num_width(number)
185{
186     char buf[BUFSIZ];
187
188     return(strlen(sprintf(buf, "%d", number)));
189}
Note: See TracBrowser for help on using the repository browser.