Commit 35e81e3c authored by cdf009's avatar cdf009

Prelab 10 part 1

parent 24c0c966
#
# CSCI 315 Operating Systems Design
# Author: L. Felipe Perrone
# Date: 2011-04-21
# Copyright (c) 2011 Bucknell University
#
# Permission is hereby granted, free of charge, to any individual or
# institution obtaining a copy of this software and associated
# documentation files (the "Software"), to use, copy, modify, and
# distribute without restriction, provided that this copyright and
# permission notice is maintained, intact, in all copies and supporting
# documentation.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL BUCKNELL UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES
# OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# compiler
CC = gcc
CFLAGS = -g -Wall
EXE = file_stat read_dir fdump
all: $(EXE)
file_stat: file_stat.o
$(CC) $(CFLAGS) -o file_stat file_stat.o
file_stat.o: file_stat.c
$(CC) -c $(CFLAGS) file_stat.c
read_dir: read_dir.o
$(CC) -o read_dir read_dir.o
read_dir.o: read_dir.c
$(CC) -c $(CFLAGS) read_dir.c
hexdump.o: hexdump.h hexdump.c
$(CC) $(CFLAGS) -c hexdump.c -o hexdump.o
fdump: fdump.c hexdump.o
$(CC) $(CFLAGS) hexdump.o fdump.c -o fdump
clean:
/bin/rm -f *~ *.o core $(EXE)
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hexdump.h"
int main(int argc, char *argv[]) {
if (argc < 3){
printf("ERROR: INPUT AS: ./fdump filename(C array) offset(unsigned int) size(unsigned int)");
return -1;
}
FILE* file1;
int size1 = 100;
unsigned char *buffer = malloc(size1);
char* file = argv[1];
unsigned int off = atoi(argv[2]);
unsigned int size = atoi(argv[3]);
file1 = fopen(file, "r");
fseek(file1, off, SEEK_SET);
fgets((void*)buffer, size, file1);
hexdump(buffer, size);
fclose(file1);
return 0;
}
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hexdump.h"
char is_printable(unsigned char c) {
if ((c >= 0x20) && (c < 0x7F))
return c;
else
return '.';
}
void hexdump(unsigned char* buffer, unsigned int length) {
int b=0, c=0;
int s, rem;
// b is a counter for the number of bytes (half the number of hex digits)
printf("\n PAYLOAD HEXDUMP:\n");
while (b < length) {
printf("\n %07x:", b);
for (; (b%16<15) && (b<length); b++) {
if (0 == b % 2)
printf(" ");
printf("%02hhx", buffer[b]);
}
if (b < length)
printf("%02hhx ", buffer[b++]);
else { // print a number of spaces to align the remaining text
rem = b % 16;
for (s=0; s < 44 - ((rem*2) + (rem/2) + 1); s++)
printf(" ");
}
for (;(c%16<15) && (c<length); c++) {
printf("%c", is_printable(buffer[c]));
}
if (c<length)
printf("%c", is_printable(buffer[c++]));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment