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

Revision 1714, 5.2 KB checked in by jik, 35 years ago (diff)
Initial revision
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.1 1989-01-27 10:17:11 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
27
28static int calc_string_width();
29static void trim_strings();
30extern char *malloc();
31extern char *whoami;
32
33int column_array(strings, num_to_print, screen_width, column_width,
34                 number_of_columns, margin, spread_flag,
35                 number_flag, var_col_flag, outfile)
36char **strings;
37FILE *outfile;
38{
39     char buf[BUFSIZ];
40     int updown, leftright, height;
41     int string_width;
42     int numwidth;
43     
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);
49          if (string_width < 0) {
50               fprintf(stderr,
51                       "%s: do_wait: your columns aren't wide enough!\n",
52                       whoami);
53               return(1);
54          }
55          trim_strings(strings, num_to_print, string_width);
56     } else if (calc_widths(strings, &screen_width, &column_width,
57                            &number_of_columns, num_to_print, &margin,
58                            spread_flag, number_flag))
59          return(1);
60
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++) {
66          for (leftright = updown; leftright < num_to_print;
67               leftright += height) {
68               (void) sprintf(buf, "%*d. %s", numwidth, leftright+1,
69                              strings[leftright]);
70               fprintf(outfile, "%*s", -column_width, buf);
71          }
72          fprintf(outfile, "\n");
73     } else for (updown = 0; updown < height; updown++) {
74          for (leftright = updown; leftright < num_to_print;
75               leftright += height) {
76               (void) sprintf(buf, "%s", strings[leftright]);
77               fprintf(outfile, "%*s", -column_width, buf);
78          }
79          fprintf(outfile, "\n");
80     }
81     
82     return(0);
83}
84
85static int calc_string_width(column_width, margin, number_flag, max_number)
86{
87     int string_width;
88     
89     string_width = column_width - margin;
90     if (number_flag)
91          string_width = string_width - num_width(max_number) - strlen(". ");
92     return(string_width);
93}
94
95
96static void trim_strings(strings, number, width)
97char **strings;
98{
99     int loop;
100     
101     for (loop = 0; loop < number; loop++)
102          if (strlen(strings[loop]) > width)
103               strings[loop][width] = '\0';
104}
105
106
107static int calc_widths(strings, screen_width, column_width, number_of_columns,
108                       num_to_print, margin, spread_flag, number_flag)
109int *screen_width, *column_width, *number_of_columns, *margin;
110char **strings;
111{
112     int loop;
113     int maxlen, templen;
114     int spread;
115     
116#ifdef DEBUG
117     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);
118#endif
119     maxlen = templen = 0;
120     for (loop = 0; loop < num_to_print; loop++)
121          if (maxlen < (templen = strlen(strings[loop])))
122               maxlen = templen;
123#ifdef DEBUG
124     printf("calc_widths maxlen %d\n", maxlen);
125#endif
126     *column_width = maxlen;
127     
128     if (number_flag)
129          *column_width = *column_width + num_width(num_to_print) +
130               strlen(". ");
131
132     if (! spread_flag) {
133          *column_width += *margin;
134          if (! *number_of_columns) {
135               *number_of_columns = *screen_width / *column_width;
136               if (! *number_of_columns) {
137                    (*number_of_columns)++;
138                    *column_width -= *margin;
139                    *margin = 0;
140                    *screen_width = *column_width;
141               }
142          }
143          else
144               *screen_width = *number_of_columns * *column_width;
145     } else {
146          if (! *number_of_columns) {
147               *number_of_columns = *screen_width / (*column_width + *margin);
148               if (! *number_of_columns) {
149                    (*number_of_columns)++;
150                    *screen_width = *column_width;
151                    *margin = 0;
152               }
153               spread = (*screen_width - *number_of_columns * *column_width)
154                    / *number_of_columns;
155               *column_width += spread;
156          }
157          else {
158               if (*number_of_columns * (*column_width + *margin) >
159                   *screen_width) {
160                    *column_width += *margin;
161                    *screen_width = *column_width;
162               } else {
163                    spread = (*screen_width - (*number_of_columns *
164                                               *column_width)) /
165                                                    *number_of_columns;
166                    *column_width += spread;
167               }
168          }
169     }
170#ifdef DEBUG
171     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);
172#endif
173     return(0);
174}
175
176
177               
178
179static int num_width(number)
180{
181     char buf[BUFSIZ];
182
183     return(strlen(sprintf(buf, "%d", number)));
184}
Note: See TracBrowser for help on using the repository browser.