"Fossies" - the Fresh Open Source Software Archive

Member "ms-sys-2.7.0/src/file.c" (24 Oct 2015, 3554 Bytes) of package /linux/privat/ms-sys-2.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "file.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.4.1_vs_2.6.0.

    1 /******************************************************************
    2     Copyright (C) 2009  Henrik Carlqvist
    3 
    4     This program is free software; you can redistribute it and/or modify
    5     it under the terms of the GNU General Public License as published by
    6     the Free Software Foundation; either version 2 of the License, or
    7     (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., 675 Mass Ave, Cambridge, MA 02139, USA.
   17 ******************************************************************/
   18 #include <stdio.h>
   19 #include <string.h>
   20 
   21 #include "nls.h"
   22 #include "file.h"
   23 
   24 #include <unistd.h>
   25 #if defined(__FreeBSD__) || defined(__OpenBSD__)
   26 #include <sys/sysctl.h>
   27 #endif
   28 
   29 int contains_data(FILE *fp, unsigned long ulPosition,
   30           const void *pData, unsigned int uiLen)
   31 {
   32    unsigned char aucBuf[MAX_DATA_LEN];
   33 
   34    if(!read_data(fp, ulPosition, aucBuf, uiLen))
   35       return 0;
   36    if(memcmp(pData, aucBuf, uiLen))
   37       return 0;
   38    return 1;
   39 } /* contains_data */
   40 
   41 int read_data(FILE *fp, unsigned long ulPosition,
   42            void *pData, unsigned int uiLen)
   43 {
   44    unsigned char aucBuf[MAX_DATA_LEN];
   45 
   46    unsigned long start_read = ulPosition;
   47    unsigned long to_read = uiLen;
   48 
   49 #if defined(__FreeBSD__) || defined(__OpenBSD__)
   50    /* cannot read/write directly from/to disk at random offsets */
   51    /* must read/write from/to pagesize boundaries */
   52 
   53    /*Rounding functions for multiples that are powers of two:*/
   54    /*Up:   return ((number + multiple - 1) & ~(multiple - 1));*/
   55    /*Down: return (number & ~(multiple - 1));*/
   56 
   57    unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
   58    start_read = ulPosition & ~(pagesize - 1); /* round down */
   59    to_read = (((ulPosition + uiLen) + pagesize - 1) & ~(pagesize - 1)) - start_read; /* round up */
   60 #endif
   61 
   62    if(to_read > MAX_DATA_LEN)
   63    {
   64       fprintf(stderr, _("Error: contains_data called with uiLen > %d,\n"),
   65           MAX_DATA_LEN);
   66       fprintf(stderr, _("please increase MAX_DATA_LEN in file.h\n"));
   67       return 0;
   68    }
   69    
   70    if(fseek(fp, start_read, SEEK_SET))
   71       return 0;
   72    if(!fread(aucBuf, to_read, 1, fp))
   73       return 0;
   74    memcpy(pData, aucBuf + ulPosition - start_read, uiLen);
   75    return 1;
   76 } /* read_data */
   77 
   78 int write_data(FILE *fp, unsigned long ulPosition,
   79            const void *pData, unsigned int uiLen)
   80 {
   81    unsigned long start_write = ulPosition;
   82    unsigned long to_write = uiLen;
   83 
   84 #if defined (__FreeBSD__) || defined(__OpenBSD__)
   85    /* cannot read/write directly from/to disk at random offsets */
   86    /* must read/write from/to pagesize boundaries */
   87    unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
   88    unsigned char aucBuf[MAX_DATA_LEN];
   89 
   90    start_write = ulPosition & ~(pagesize - 1); /* round down */
   91    to_write = (((ulPosition + uiLen) + pagesize - 1) & ~(pagesize - 1)) - start_write; /* round up */
   92 
   93    if(fseek(fp, start_write, SEEK_SET))
   94       return 0;
   95    if(!fread(aucBuf, to_write, 1, fp))
   96       return 0;
   97 
   98     /* modify in the local buffer first */
   99     memcpy(aucBuf + ulPosition - start_write, pData, uiLen);
  100     pData = aucBuf;
  101 #endif
  102 
  103    if(fseek(fp, start_write, SEEK_SET))
  104       return 0;
  105    if(!fwrite(pData, to_write, 1, fp))
  106       return 0;
  107    return 1;
  108 } /* write_data */