LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (http://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Help needed (http://www.linuxquestions.org/questions/linux-kernel-70/help-needed-4175439781/)

mithun_linux 12-03-2012 04:24 AM

Help needed
 
Hi ppl i have a 20MB binary file .. i need a code to read the first 188 bytes of data and put that in a buffer and second 188 bytes of data into second buffer and so on.. i have to print first 188 byte and second 188 bytes and so on .. please help me out

pan64 12-03-2012 04:43 AM

what language do you prefer? what have you tried so far?

mithun_linux 12-03-2012 04:53 AM

like i prefer C language .. the thing which i have tried so far is ... its getting printed but i dont know to find the EOF in my file..


#define __KERNEL_SYSCALLS__
#include <linux/version.h>
#include <linux/syscalls.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/smp_lock.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/string.h>
#include <linux/file.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <asm/errno.h>
#include <asm/io.h>
static void read_file(char *filename)
{
//int fd;
struct file *fd;
loff_t pos =0;
unsigned char buf[1514];
int packets =0;

mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);

fd = filp_open("file name with path", O_RDONLY, 0);
if (fd >= 0) {
printk(KERN_INFO);
while (vfs_read(fd, buf,1316,&pos) == 1316)
{
int i;
printk("Packets %d\n",packets);
printk("###########\n");
for(i=0;i<188;i++)
{
printk("%02x ", buf[i]);
if(!(i % 20)) printk("\n");


}
printk("\n");
printk("\n");
pos += 188;
if(packets++ == 7800)
break;

}
filp_close(fd,NULL);
printk("\n");
}
else
printk("Error \n");

set_fs(old_fs);
}

static int __init init(void)
{
printk("init\n");
//read_file("/etc/shadow");
read_file("file name with path");
return 0;
}

static void __exit exit(void)
{ }

MODULE_LICENSE("GPL");
module_init(init);
module_exit(exit);

pan64 12-03-2012 05:22 AM

I think vfs_read returns the number of bytes read. If it was less than the required (or 0) it means EOF

mithun_linux 12-04-2012 04:27 AM

The file which i am having is an .ts extension file i dont know to print the data because of unknown file format i am getting some junk data when i run the code can u please help me in this ppl??

#include <stdio.h>

main()
{
FILE *file;
char *buffer;
unsigned long fileLen;

file = fopen("kdg_20MB_wrappable.ts", "rb");
if (!file)
{
fprintf(stderr, "can't open file %s", "kdg_20MB_wrappable.ts");
exit(1);
}

fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);

buffer=(char *)malloc(fileLen+1);

if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
exit(1);
}

fread(buffer, fileLen, 188, file);
fclose(file);

printf ("value is %s \n", &buffer);
dump_buffer(&buffer, fileLen);
//free(buffer);
}

dump_buffer(void *buffer, int buffer_size)
{
int i;

for(i = 0;i < buffer_size;++i)
printf("%c", ((char *)buffer)[i]);
}

pan64 12-04-2012 10:01 AM

I would gladly help you just I do not know how. What do you really want to achieve?
you can use the command file <filename> to identify the type of the file, .ts is probably QT transport stream: http://filext.com/file-extension/TS, but I'm not really sure
You would need special software to handle such files.

linosaurusroot 12-04-2012 10:22 AM

Why all the kernel stuff? Why not open() and read() in userspace?

mithun_linux 12-27-2012 03:15 AM

But these transport stream file ".ts" is not getting opened in the user space with the functions
open() / fopen() / fread()... im confused :(:(

pan64 12-27-2012 04:38 AM

use perror to print error messages:
http://www.java-samples.com/showtuto...tutorialid=597

vsselvaraj 02-28-2013 09:25 AM

Getting error.. :(
 
Same program i executed .. Please help me to solve the following errors .. I am new to kernal level file operations...


gcc -I/usr/src/linux-headers-3.2.0-23-generic/arch/x86/include -I/usr/src/linux-headers-3.2.0-23-generic/include/ testfile1.cpp
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:67:0,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/types.h:13:2: warning: #warning "Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders" [-Wcpp]
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/bitops.h:455:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/bitops.h:22,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:52,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/asm-generic/bitops/fls64.h:33:2: error: #error BITS_PER_LONG not 32 or 64
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:52:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/bitops.h:164:2: error: #error BITS_PER_LONG not 32 or 64
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/processor.h:26:0,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/thread_info.h:22,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:53,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/cpumask.h:148:37: error: "and" may not appear in macro parameter list
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/div64.h:57:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/math64.h:5,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/processor.h:29,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/thread_info.h:22,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:53,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/asm-generic/div64.h:54:3: error: #error do_div() does not yet support the C64
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/thread_info.h:22:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:53,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/processor.h:157:0: warning: "cache_line_size" redefined [enabled by default]
/usr/src/linux-headers-3.2.0-23-generic/include/linux/cache.h:64:0: note: this is the location of the previous definition
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:212:2: error: #error "Unknown RCU implementation specified to kernel configuration"
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/irq.h:20:0,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/hardirq.h:5,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/hardirq.h:7,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/ftrace_event.h:7,
from /usr/src/linux-headers-3.2.0-23-generic/include/trace/syscall.h:6,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:77,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/gfp.h:214:2: error: #error ZONES_SHIFT too large to create GFP_ZONE_TABLE integer
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/ktime.h:25:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/timer.h:5,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/workqueue.h:8,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/kmod.h:26,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/module.h:13,
from testfile1.cpp:4:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/module.h:22:0,
from testfile1.cpp:4:
/usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/module.h:59:2: error: #error unknown processor family
testfile1.cpp:6:28: fatal error: linux/smp_lock.h: No such file or directory
compilation terminated.


All times are GMT -5. The time now is 09:29 PM.