LCOV - code coverage report
Current view: top level - desktop/unx/source - pagein.c (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 34 60 56.7 %
Date: 2014-04-14 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "file_image.h"
      21             : 
      22             : #include <unistd.h>
      23             : #include <errno.h>
      24             : #include <stdio.h>
      25             : #include <string.h>
      26             : 
      27             : /* do_pagein */
      28     2210260 : static int do_pagein (const char * filename, size_t * size)
      29             : {
      30             :     int result;
      31     2210260 :     file_image image = FILE_IMAGE_INITIALIZER;
      32             : 
      33     2210260 :     if ((result = file_image_open (&image, filename)) != 0)
      34       85010 :         return (result);
      35             : 
      36     2125250 :     if ((result = file_image_pagein (&image)) != 0)
      37             :     {
      38           0 :         fprintf (stderr, "file_image_pagein %s: %s\n", filename, strerror(result));
      39           0 :         goto cleanup_and_leave;
      40             :     }
      41             : 
      42     2125250 :     if (size)
      43             :     {
      44     2125250 :         *size = image.m_size;
      45             :     }
      46             : 
      47             : cleanup_and_leave:
      48     2125250 :     file_image_close (&image);
      49     2125250 :     return (result);
      50             : }
      51             : 
      52             : extern int pagein_execute (int argc, char **argv);
      53             : 
      54             : /* main */
      55       42505 : int pagein_execute (int argc, char **argv)
      56             : {
      57       42505 :     int    i, v = 0;
      58       42505 :     size_t nfiles = 0, nbytes = 0;
      59             : 
      60       42505 :     if (argc < 2)
      61             :     {
      62           0 :         fprintf (
      63             :             stderr,
      64             :             "%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
      65             :             argv[0]);
      66           0 :         return (1);
      67             :     }
      68             : 
      69       85010 :     for (i = 1; i < argc; i++)
      70             :     {
      71       42505 :         FILE   * fp = 0;
      72       42505 :         size_t   k  = 0;
      73             : 
      74       42505 :         if (argv[i][0] == '-')
      75             :         {
      76             :             /* option */
      77           0 :             int j = 1;
      78           0 :             switch (argv[i][j])
      79             :             {
      80             :                 case 'v':
      81             :                     /* verbosity level */
      82           0 :                     for (v += 1, j += 1; argv[i][j]; j++)
      83           0 :                         v += (argv[i][j] == 'v');
      84           0 :                     break;
      85             :                 case 'L':
      86             :                     /* search path */
      87           0 :                     if (chdir (&(argv[i][2])) == -1)
      88           0 :                         fprintf (stderr, "chdir %s: %s\n", &(argv[i][2]), strerror(errno));
      89           0 :                     break;
      90             :                 default:
      91             :                     /* ignored */
      92           0 :                     break;
      93             :             }
      94             : 
      95             :             /* next argv */
      96           0 :             continue;
      97             :         }
      98             : 
      99       42505 :         if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
     100       42505 :         {
     101             :             char fullpath[4096];
     102       42505 :             char *path = NULL;
     103       42505 :             memset(fullpath, 0, sizeof(fullpath));
     104       42505 :             strncpy (fullpath, argv[i] + 1, 3000);
     105       42505 :             if (!(path = strrchr (fullpath, '/')))
     106           0 :                 path = fullpath;
     107             :             else
     108       42505 :                 path++;
     109             : 
     110       42505 :             if ((fp = fopen (&(argv[i][1]), "r")) == 0)
     111             :             {
     112           0 :                 fprintf (stderr, "fopen %s: %s\n", &(argv[i][1]), strerror(errno));
     113           0 :                 continue;
     114             :             }
     115     2295270 :             while (fgets (path, 1024, fp) != 0)
     116             :             {
     117     2210260 :                 path[strlen(path) - 1] = '\0', k = 0;
     118             : 
     119             :                 /* paths relative to the location of the pagein file */
     120     2210260 :                 if (do_pagein (fullpath, &k) == 0)
     121             :                 {
     122             :                     /* accumulate total size */
     123     2125250 :                     nbytes += k;
     124             :                 }
     125             : 
     126     2210260 :                 if (v >= 2)
     127           0 :                     fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
     128     2210260 :                 nfiles += 1;
     129             :             }
     130       42505 :             fclose (fp);
     131             :         }
     132             :         else
     133             :         {
     134           0 :             if (fp != 0)
     135           0 :                 fclose (fp);
     136             : 
     137           0 :             if (do_pagein (argv[i], &k) == 0)
     138             :             {
     139             :                 /* accumulate total size */
     140           0 :                 nbytes += k;
     141             :             }
     142             : 
     143           0 :             if (v >= 2)
     144           0 :                 fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
     145           0 :             nfiles += 1;
     146             :         }
     147             :     }
     148             : 
     149       42505 :     if (v >= 1)
     150           0 :         fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
     151       42505 :     return (0);
     152             : }
     153             : 
     154             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10