danielhilst

file-progress.c

Jul 31st, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. /**
  2.  * Copyright (C) 2013 Daniel Hilst Selli
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /**
  21.  * File: file-progress.c
  22.  * Desc: linux cp has no progress log, so this is
  23.  *       what file-progress is. You pass copy destination
  24.  *       and source size in bytes (du -sb origin) and
  25.  *       file-progress gives you estimate time arrive
  26.  *       percentage progress, and current speed.
  27.  *
  28.  * Compile: gcc file-progress.c -o file-progress
  29.  * Author: Daniel Hilst Selli <[email protected]>
  30.  * License: GPLv2
  31.  */
  32.  
  33. /* sample output
  34. 7% 816709632/10737418240 ETA 2 mins 61mbps
  35. 8% 885268480/10737418240 ETA 2 mins 65mbps
  36. 8% 956194816/10737418240 ETA 2 mins 67mbps
  37. 9% 1029414912/10737418240 ETA 2 mins 69mbps
  38. 10% 1090318336/10737418240 ETA 2 mins 58mbps
  39. 10% 1155309568/10737418240 ETA 2 mins 61mbps
  40. 11% 1224249344/10737418240 ETA 2 mins 65mbps
  41. 12% 1294929920/10737418240 ETA 2 mins 67mbps
  42. 12% 1354788864/10737418240 ETA 2 mins 57mbps
  43. 13% 1438244864/10737418240 ETA 1 mins 79mbps
  44. 14% 1517338624/10737418240 ETA 1 mins 75mbps
  45. 14% 1592406016/10737418240 ETA 2 mins 71mbps
  46. 15% 1666785280/10737418240 ETA 2 mins 70mbps
  47. 16% 1721761792/10737418240 ETA 2 mins 52mbps
  48. 16% 1791979520/10737418240 ETA 2 mins 66mbps
  49. */
  50.  
  51.  
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <sys/stat.h>
  55.  
  56. static struct stat st;
  57.  
  58. #define pexit(msg) do {                         \
  59.                 perror(msg);                    \
  60.                 exit(EXIT_FAILURE);             \
  61.         } while (0)
  62.  
  63. static char *suffixes[] = {
  64.         "bps",
  65.         "kbps",
  66.         "mbps",
  67.         NULL,
  68. };
  69.  
  70. int main(int argc, char **argv)
  71. {
  72.         long long total;
  73.         long long percent;
  74.         long long oldsize = 0;
  75.  
  76.         if (argc < 2) {
  77.                 fprintf(stderr, "Usage: %s FILE TOTAL_FILE_SIZE_IN_BYTES\n", argv[0]);
  78.                 exit(EXIT_FAILURE);
  79.         }
  80.  
  81.         total = strtol(argv[2], NULL, 0);
  82.  
  83.         do {
  84.                 int error;
  85.                 long long speed ;
  86.                 int interval = 1;
  87.                 unsigned long long etasecs; /* estimate time arrive in seconds */
  88.                 int i = 0;
  89.                
  90.                 error = stat(argv[1], &st);
  91.                 if (error)
  92.                         pexit("stat");
  93.  
  94.                 percent = st.st_size * 100 / total;
  95.                 speed = (st.st_size - oldsize) / interval; /* in bytes per second */
  96.                 etasecs = (total - st.st_size) / speed;
  97.                
  98.                 for (i = 0; speed >= 1024 && suffixes[i+1]; i++, speed /= 1024)
  99.                         ;
  100.                
  101.                 printf("%ld%% %u/%ld ETA %d %s %d%s\n",
  102.                        percent,
  103.                        st.st_size,
  104.                        total,
  105.                        (etasecs > 60 ? etasecs / 60 : etasecs),
  106.                        (etasecs > 60 ? "mins" : "secs"),
  107.                        speed,
  108.                        suffixes[i]);
  109.  
  110.                 oldsize = st.st_size;
  111.  
  112.                 sleep(interval);
  113.         } while (percent < 100);
  114.         return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment