LCOV - code coverage report
Current view: top level - desktop/unx/source - pagein.c (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 34 60 56.7 %
Date: 2014-04-11 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        2496 : static int do_pagein (const char * filename, size_t * size)
      29             : {
      30             :     int result;
      31        2496 :     file_image image = FILE_IMAGE_INITIALIZER;
      32             : 
      33        2496 :     if ((result = file_image_open (&image, filename)) != 0)
      34          96 :         return (result);
      35             : 
      36        2400 :     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        2400 :     if (size)
      43             :     {
      44        2400 :         *size = image.m_size;
      45             :     }
      46             : 
      47             : cleanup_and_leave:
      48        2400 :     file_image_close (&image);
      49        2400 :     return (result);
      50             : }
      51             : 
      52             : extern int pagein_execute (int argc, char **argv);
      53             : 
      54             : /* main */
      55          48 : int pagein_execute (int argc, char **argv)
      56             : {
      57          48 :     int    i, v = 0;
      58          48 :     size_t nfiles = 0, nbytes = 0;
      59             : 
      60          48 :     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          96 :     for (i = 1; i < argc; i++)
      70             :     {
      71          48 :         FILE   * fp = 0;
      72          48 :         size_t   k  = 0;
      73             : 
      74          48 :         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          48 :         if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
     100          48 :         {
     101             :             char fullpath[4096];
     102          48 :             char *path = NULL;
     103          48 :             memset(fullpath, 0, sizeof(fullpath));
     104          48 :             strncpy (fullpath, argv[i] + 1, 3000);
     105          48 :             if (!(path = strrchr (fullpath, '/')))
     106           0 :                 path = fullpath;
     107             :             else
     108          48 :                 path++;
     109             : 
     110          48 :             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        2592 :             while (fgets (path, 1024, fp) != 0)
     116             :             {
     117        2496 :                 path[strlen(path) - 1] = '\0', k = 0;
     118             : 
     119             :                 /* paths relative to the location of the pagein file */
     120        2496 :                 if (do_pagein (fullpath, &k) == 0)
     121             :                 {
     122             :                     /* accumulate total size */
     123        2400 :                     nbytes += k;
     124             :                 }
     125             : 
     126        2496 :                 if (v >= 2)
     127           0 :                     fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
     128        2496 :                 nfiles += 1;
     129             :             }
     130          48 :             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          48 :     if (v >= 1)
     150           0 :         fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
     151          48 :     return (0);
     152             : }
     153             : 
     154             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10