summaryrefslogtreecommitdiffstats
path: root/progress.c
blob: d4c8f7fc81492de2787e8f524eedc3d29dbcd4f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* $Header: /home/ksheff/src/e2tools/RCS/progress.c,v 0.1 2002/06/26 11:17:28 ksheff Exp $ */
/*
 * progress.c
 *
 * Copyright (C) 2002 Keith W Sheffield.  This file may be redistributed
 * under the terms of the GNU Public License.
 *
 */

#ifndef PROGRESS_C
#define PROGRESS_C
#endif

/* Description */
/*
 *
 *
 */
/*
 * $Log: progress.c,v $
 * Revision 0.1  2002/06/26 11:17:28  ksheff
 * Initial revision
 *
 */

/* Feature Test Switches */
/* System Headers */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>

/* Macros */
#define PROG_FILE_SIZE 50
#define PROG_CLEAR \
"                                                                           \r"
/* Local Variables */

static char prog_file[PROG_FILE_SIZE+1];
static struct stat *prog_sbuf = NULL;
static long prog_start;
static long prog_time;

/* Name:	init_progress()
 *
 * Description:
 *
 * This function initializes the progress status variables for a particular
 * file operation
 * .
 *
 * Algorithm:
 *
 * Determine the size of the file name
 * Copy the last PROG_FILE_SIZE characters of the filename
 * Copy the pointer to the file statistics structure
 *
 * Global Variables:
 *
 * char prog_file[PROG_FILE_SIZE+1];        The file being operated on
 * struct stat *prog_sbuf;                  prog_file's information
 * long prog_start;                          The initial start time
 * long prog_time;                          The last update time
 *
 * Arguments:
 *
 * char *file;                The file to watch
 * struct stat *sbuf;         The file's information
 *
 * Return Values:
 *
 * None
 *
 * Author: Keith W. Sheffield
 * Date:   06/26/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
void init_progress(char *file, struct stat *sbuf)
{
  int len;
  struct timeval tv;

  if (file != NULL && sbuf != NULL)
    {
      if (strcmp(file, "-") == 0)
        file = "<stdin>";
      
      len = strlen(file);
      if (len > PROG_FILE_SIZE)
        {
          file += (len - PROG_FILE_SIZE);
          len = PROG_FILE_SIZE;
        }
      memset(prog_file, ' ', PROG_FILE_SIZE);
      memcpy(prog_file, file, len);
      prog_file[PROG_FILE_SIZE] = '\0';
      prog_sbuf = sbuf;
      gettimeofday(&tv, NULL);
      prog_start = prog_time = tv.tv_sec;
    }
  else
    {
      prog_file[0] = '\0';
      prog_sbuf = NULL;
      prog_start = prog_time = 0;
    }
  
} /* end of init_progress */ 

/* Name:	update_progress()
 *
 * Description:
 *
 * This function updates the current file progress display.
 *
 * Algorithm:
 *
 * Get the system time in seconds.
 * If different from the last update
 *     Save the system time
 *     Display the file name, current byte total, and total file size
 *
 * Global Variables:
 *
 * char prog_file[PROG_FILE_SIZE+1];        The file being operated on
 * struct stat *prog_sbuf;                  prog_file's information
 * long prog_time;                          The last update time
 *
 * Arguments:
 *
 * unsigned long num_bytes;          The number of bytes transferred
 *
 * Return Values:
 *
 * None
 *
 * Author: Keith W. Sheffield
 * Date:   06/26/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
void update_progress(unsigned long num_bytes)
{
  struct timeval tv;
  gettimeofday(&tv, NULL);

  if (prog_sbuf != NULL && prog_time != tv.tv_sec)
    {
      prog_time = tv.tv_sec;
      fprintf(stderr, "%s %10ld / %10ld\r", prog_file, num_bytes,
              prog_sbuf->st_size);
      fflush(stderr);
    }
} /* end of update_progress */ 

/* Name:	finish_progress()
 *
 * Description:
 *
 * This function clears the current progress line if needed.
 *
 * Algorithm:
 *
 * Check to see if prog_start is different than prog_time
 *     Clear the progress line
 *
 * Global Variables:
 *
 *
 * Arguments:
 *
 * None
 *
 * Return Values:
 *
 * None
 *
 * Author: Keith W. Sheffield
 * Date:   06/26/2002
 *
 * Modification History:
 *
 * MM/DD/YY      Name               Description
 *
 */
void finish_progress()
{
  if (prog_start != prog_time)
    fputs(PROG_CLEAR, stderr);
  
} /* end of finish_progress */