Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright (C) 2013 Daniel Hilst Selli
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
- /**
- * File: file-progress.c
- * Desc: linux cp has no progress log, so this is
- * what file-progress is. You pass copy destination
- * and source size in bytes (du -sb origin) and
- * file-progress gives you estimate time arrive
- * percentage progress, and current speed.
- *
- * Compile: gcc file-progress.c -o file-progress
- * Author: Daniel Hilst Selli <[email protected]>
- * License: GPLv2
- */
- /* sample output
- 7% 816709632/10737418240 ETA 2 mins 61mbps
- 8% 885268480/10737418240 ETA 2 mins 65mbps
- 8% 956194816/10737418240 ETA 2 mins 67mbps
- 9% 1029414912/10737418240 ETA 2 mins 69mbps
- 10% 1090318336/10737418240 ETA 2 mins 58mbps
- 10% 1155309568/10737418240 ETA 2 mins 61mbps
- 11% 1224249344/10737418240 ETA 2 mins 65mbps
- 12% 1294929920/10737418240 ETA 2 mins 67mbps
- 12% 1354788864/10737418240 ETA 2 mins 57mbps
- 13% 1438244864/10737418240 ETA 1 mins 79mbps
- 14% 1517338624/10737418240 ETA 1 mins 75mbps
- 14% 1592406016/10737418240 ETA 2 mins 71mbps
- 15% 1666785280/10737418240 ETA 2 mins 70mbps
- 16% 1721761792/10737418240 ETA 2 mins 52mbps
- 16% 1791979520/10737418240 ETA 2 mins 66mbps
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- static struct stat st;
- #define pexit(msg) do { \
- perror(msg); \
- exit(EXIT_FAILURE); \
- } while (0)
- static char *suffixes[] = {
- "bps",
- "kbps",
- "mbps",
- NULL,
- };
- int main(int argc, char **argv)
- {
- long long total;
- long long percent;
- long long oldsize = 0;
- if (argc < 2) {
- fprintf(stderr, "Usage: %s FILE TOTAL_FILE_SIZE_IN_BYTES\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- total = strtol(argv[2], NULL, 0);
- do {
- int error;
- long long speed ;
- int interval = 1;
- unsigned long long etasecs; /* estimate time arrive in seconds */
- int i = 0;
- error = stat(argv[1], &st);
- if (error)
- pexit("stat");
- percent = st.st_size * 100 / total;
- speed = (st.st_size - oldsize) / interval; /* in bytes per second */
- etasecs = (total - st.st_size) / speed;
- for (i = 0; speed >= 1024 && suffixes[i+1]; i++, speed /= 1024)
- ;
- printf("%ld%% %u/%ld ETA %d %s %d%s\n",
- percent,
- st.st_size,
- total,
- (etasecs > 60 ? etasecs / 60 : etasecs),
- (etasecs > 60 ? "mins" : "secs"),
- speed,
- suffixes[i]);
- oldsize = st.st_size;
- sleep(interval);
- } while (percent < 100);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment