Skip to content

Instantly share code, notes, and snippets.

@kimhoki
Last active November 30, 2015 12:26
Show Gist options
  • Save kimhoki/8dcb5c0a8b5143fbd0be to your computer and use it in GitHub Desktop.
Save kimhoki/8dcb5c0a8b5143fbd0be to your computer and use it in GitHub Desktop.

커맨드 만들기 및 파일 파싱 하기

내내용

###중중젬중제목


  • 목목록
  • makefile.c
  • ioctl.c
  • myapp.c

jni폴더 생성 안에 /Android.mk:

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := myapp 
LOCAL_SRC_FILES := myapp.c

include $(BUILD_EXECUTABLE)

makefile.c:

CC	:= /usr/local/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-eabi-gcc
obj-m := ioctl.o
KDIR  := /Smart4412Linux/Source/kernel/kernel_4412

all:
	make -C $(KDIR) SUBDIRS=$(PWD)

clean:
	rm -rf *.o*.ko
	rm -rf *.mod
	rm -rf *.ko
	rm -rf *.o
	

ioctl.c:

#if 1
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>

#define DEVICE_NAME "ioctl_test"
#define DEVICE_MAJOR 90

int my_open(struct inode *inode, struct file *filp)
{
	printk("my_open()\n");
	return 0;
}
int my_close(struct inode *inode, struct file *filp)
{
	printk("my_colse()\n");
	return 0;
}
//#define LED_ON 1
#define LED_ON _IO('c',1)

long my_ioctl(struct file *filp,unsigned int cmd, unsigned long opt)
{
	printk("my_ioctl()\n");
	switch(cmd)
	{
		case LED_ON : printk("LED_ON\n");break;
	}
	return 0;
}
static struct file_operations fops =
{
	.open		=my_open,
	.release	=my_close,
	.unlocked_ioctl=my_ioctl,
};
static int __init ioctl_init(void)
{
	int result;
	result = register_chrdev(DEVICE_MAJOR, DEVICE_NAME, &fops);
if(result <0 )
{
	printk("unable to register devices %s\n",DEVICE_NAME);
	return result;
}
return 0;
}

static void __exit ioctl_exit(void)
{
	unregister_chrdev(DEVICE_MAJOR,DEVICE_NAME);
	printk("rmmod %s\n",DEVICE_NAME);
}
MODULE_LICENSE("GPL");
module_init(ioctl_init);
module_exit(ioctl_exit);
			
#endif

myapp.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

// stat();
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define ARRAY_LEN(X) (sizeof(X)/sizeof(X[0]))

long ex_m3_2_global_heap_size;

typedef struct// output
{
    char *name;
    char *ext;
    int dot_idx;

}i_filename_type;

typedef struct
{
    int     div_size;
    int     div_count;
    int     div_remain;
    long    org_size;

}o_div_info_type;

typedef struct// input
{
  o_div_info_type o_div_info;
}output_type;

typedef struct// input
{
    char            *i_file_path;
    int             i_file_div;
    i_filename_type i_file_name;

}input_type;

void display( void )
{
  printf("------------------------------------------------------------------\n");
  printf("\t\t\tFile Divied Example\n");
  printf("------------------------------------------------------------------\n\n");
}

void param_parsing( int argc, char **argv )
{
  int i;
  printf("//input parameter parsing ----------------------------------------\n");

  for (i = 0; i < argc; i++)
  {
    printf("argc = %d, argv[%d] = %s\n", i, i, argv[i]);
  }
  printf("\n");
}

void file_name_divide( input_type *input, char **argv )
{
  input->i_file_path = argv[0];
  input->i_file_div  = atoi(argv[2]);
  ex_m3_2_global_heap_size = atoi(argv[3]);
  printf("//file name, file divide count -----------------------------------\n");
  printf("input->i_file_path\t\t= %s <len=%d>\n", input->i_file_path, strlen(input->i_file_path));
  printf("input->i_file_div\t\t= %d\n", input->i_file_div);
  printf("ex_m3_2_global_heap_size\t= %ld byte\n\n", ex_m3_2_global_heap_size);

}

char* heap_memory( void )
{
  char * heap_ptr_buffer;
  printf("//Heap memory cheak ------------------------------------------------------\n");

  heap_ptr_buffer = NULL;
  heap_ptr_buffer = (char*)malloc(ex_m3_2_global_heap_size);
  memset(heap_ptr_buffer, 00, ex_m3_2_global_heap_size);

  if(heap_ptr_buffer == NULL)
  {
    printf("Heap_cheak = fail\n");
  }
  else
  {
    printf("Heap memory check success! malloc<> = %ld\n\n", ex_m3_2_global_heap_size);
  }
  return heap_ptr_buffer;
}

char *file_name_parsing( input_type *input, char **argv )
{
  char *name_ptr;
  char *ext_ptr;
  char *file_name_parsing_str;

  printf("//file name parsing ----------------------------------------------\n");

  file_name_parsing_str = (char *)malloc(sizeof(char)*(strlen(argv[1])));
  strncpy(file_name_parsing_str, argv[1], sizeof(char)*(strlen(argv[1])));

  //printf("xbin = %s\n",file_name_parsing_str);

  ext_ptr = strrchr(file_name_parsing_str, '.');
  input->i_file_name.ext = ext_ptr;
  printf("strcspn\t\t= <len=%d>\n", strlen(file_name_parsing_str)-strlen(ext_ptr));
  strncpy(file_name_parsing_str, argv[1], sizeof(char)*(strlen(file_name_parsing_str)-strlen(ext_ptr)));
  printf("input->i_file_name.name\t\t= '%s' <len=%d>\n", file_name_parsing_str, strlen(file_name_parsing_str));
  //name_ptr=basename(file_name_parsing_str);
  input->i_file_name.name = file_name_parsing_str;

  //ptr = *(file_name_parsing_str-strlen(&ptr));



  //printf("input->i_file_name.name\t\t= '%s' <len=%d>\n", input->i_file_name.name, strlen(input->i_file_name.name));
  printf("input->i_file_name.ext \t\t= '%s' <len=%d>\n", input->i_file_name.ext, strlen(input->i_file_name.ext));
  //printf("input->i_file_name.dot_idx\t = [%d] \n\n", input->i_file_name.dot_idx);

  return file_name_parsing_str;
}

void ori_file_check( output_type *output, char **argv )
{
  struct stat stat_file_info;

  if ( 0 > stat(argv[1], &stat_file_info) )
  {
    perror("Error : ");
    exit(0);
  }

  stat(argv[1], &stat_file_info);
  output->o_div_info.org_size = stat_file_info.st_size;
  printf( "//original file size check ---------------------------------------\n" );
  printf( "file_name = %s \n",argv[1]);
  printf( "file_size = %ld \n\n",output->o_div_info.org_size);
}

void output_divide_size_check( output_type *output, char** argv )
{
    output->o_div_info.div_count = atoi(argv[2]);
    output->o_div_info.div_size = output->o_div_info.org_size / output->o_div_info.div_count;
    output->o_div_info.div_remain = output->o_div_info.org_size % output->o_div_info.div_count;
    printf("file divide size check------------------\n");
    printf("output->o_div_info.org_size = %ld \n", output->o_div_info.org_size);
    printf("output->o_div_info.div_size = %d\n", output->o_div_info.div_size);
    printf("output->o_div_info.org_count = %d\n", output->o_div_info.div_count);
    printf("output->o_div_info.org_remain = %d\n\n", output->o_div_info.div_remain);
}

void process_check( input_type *input, output_type *output, char **argv, char *buffer )
{
  char *dest_name;
  int i,j ;
  int heap_div_count;
  int heap_remain;

  FILE *fd_src;
  FILE *fd_dest_div;

  printf("file divide process start------------------\n");
  dest_name = (char *)malloc(sizeof(char)*(10));
  fd_src = fopen(argv[1],"rb");

  if (fd_src == NULL)
  {
    free(dest_name);
    perror("Can't find orignal file: ");
    exit(0);
  }

  printf("div_remain = %d\n\n", output->o_div_info.div_remain);
  heap_div_count = output->o_div_info.div_size / ex_m3_2_global_heap_size;
  printf("heap_div_count %d \n", heap_div_count);
  heap_remain = (output->o_div_info.div_size % ex_m3_2_global_heap_size);
  printf("heap_remain %d \n", heap_remain);

// 힙사이즈가 원본 사이즈보다 작으면 힙으로 나누어서 처리
  if( ex_m3_2_global_heap_size <= output->o_div_info.org_size)
  {
    // 분할한 개수 많큼  i = 1
    for(i=1; i <= output->o_div_info.div_count ; i++)
    {
      //파일명 생성
      snprintf(dest_name,10,"%s_%d.%s", input->i_file_name.name, input->i_file_name.dot_idx++, input->i_file_name.ext);
      printf("\t\tdest_name %s \n", dest_name);

      fd_dest_div = fopen(dest_name,"wb");

      if (fd_dest_div == NULL)
      {
        printf("Can't find dest_div file : %s\n",dest_name);
        free(dest_name);
        exit(0);
      }
      // 힙 사이즈 만큼 분할 해서 복사
      for(j=heap_div_count-1; j>=0;  j--)
      {
        fseek (fd_dest_div, 0, SEEK_END);
        fread (buffer,1,ex_m3_2_global_heap_size, fd_src);
        fwrite(buffer, 1, ex_m3_2_global_heap_size, fd_dest_div);
        //printf("%d\t", ftell(fd_src));
        //printf("\rloop=%3d\t\t", j);
      }

      // 힙 나머지 처리
      if(heap_remain != 0)
      {
        fread (buffer, 1, heap_remain, fd_src);
        fwrite(buffer, 1, heap_remain, fd_dest_div);
      }

      printf("\t\t<cur_ptr=%ld>\n\n", ftell(fd_src));

      fclose(fd_dest_div);
    }
  }
// 오리지널 사이즈가 힙사이즈보다 작으면 나눈 사이즈 만큼 한번에 복사
  else
  {
    for(i=1; i <= output->o_div_info.div_count ; i++)
    {
      snprintf(dest_name,10,"%s_%d.%s", input->i_file_name.name, input->i_file_name.dot_idx++, input->i_file_name.ext);
      printf("\t\tdest_name %s \n", dest_name);

      fd_dest_div = fopen(dest_name,"wb");

      if (fd_dest_div == NULL)
      {
        printf("Can't find dest_div file : %s\n",dest_name);
        free(dest_name);
        exit(0);
      }

      fseek (fd_dest_div, 0, SEEK_END);
      fread (buffer,1,output->o_div_info.div_size, fd_src);
      fwrite(buffer, 1, output->o_div_info.div_size, fd_dest_div);
      printf("\t\t<cur_ptr=%ld>\t", ftell(fd_src));

      fclose(fd_dest_div);
      printf("\n\n");
    }
  }
// div_remain 처리
  if(output->o_div_info.div_remain != 0)
  {
    snprintf(dest_name,10,"%s_%d.%s", input->i_file_name.name, input->i_file_name.dot_idx, input->i_file_name.ext);
    printf("\t\tdest_name %s \n", dest_name);

    fd_dest_div = fopen(dest_name,"wb");

    if (fd_dest_div == NULL)
    {
      printf("Can't find dest_div file : %s\n",dest_name);
      free(dest_name);
      exit(0);
    }

    fseek (fd_dest_div, 0, SEEK_END);
    fread (buffer,1,output->o_div_info.div_remain, fd_src);
    fwrite(buffer, 1,output->o_div_info.div_remain, fd_dest_div);
    printf("\t\t<cur_ptr=%ld>\t", ftell(fd_src));
    printf("\ndiv_remain = %d\n", output->o_div_info.div_remain);
    fclose(fd_dest_div);
  }
  free(dest_name);
  fclose(fd_src);
}

int main( int argc,char *argv[] )
{
  input_type inputz;
  output_type outz;

  char *heap_ptr_buff;
  char *file_name_parsing_buf;

  if(argc < 3)
  {
    printf("Usage : %s x.bin 4 1000 \n",argv[0]);
    exit(0);
  }

  display();
  param_parsing(argc,argv);
  file_name_divide(&inputz, argv);
  heap_ptr_buff= heap_memory();
  file_name_parsing_buf=file_name_parsing(&inputz, argv);
  ori_file_check(&outz,argv);
  output_divide_size_check(&outz,argv);
  process_check(&inputz,&outz,argv,heap_ptr_buff);

  free(heap_ptr_buff);
  free(file_name_parsing_buf);

  return 0;
}


/*
int main( void)
{
   char   *str = "forum.falinux.com";
   char   *temp;

   temp = strrchr( str, 'f');
   printf( "%s\n", temp);

   return 0;
}*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment