summaryrefslogtreecommitdiff
path: root/cmd/cmd_seek.c
blob: 2aa0310077cbc3440cdd184481fb16b3743872bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "buf.h"
#include "core.h"

#include "libcmd/cmd.h"
#include "libcmd/cmd_parse.h"

extern file_t *g_file;
extern Buf *g_buf;
extern int g_flags;

int if_decimal(char *s, int sz)
{
	int i=0;
	while (i<sz)
	{
		//printf("%d\n",i);
		if (!isdigit(s[i]))
			return 0;
		i++;
	}
	return 1;
}

int if_hex(char *s, int sz)
{
	int i=0;
	while (i<sz)
	{
		//printf("%d\n",i);
		if (!isxdigit(s[i]))
			return 0;
		i++;
	}
	return 1;
}

int c_seek(  cmd_arg_t *arg  )
{
	int fret;
	int argc = arg->argc;
	char **argv = arg->argv;
	int *type = arg->type;
	int checktype;
	off_t offset;
	int off_type = 0; //-1 seek down, 0 set pos, +1 seek up

	if (argc != 1)
	{
		printf("One argument needed\n");
		return -1;
	}

	if (g_file->fd == 0)
	{
		printf("File descriptor not set\n");
		return -1;
	}

	switch( argv[0][0] )
	{
	case '+':
		off_type = 1;
		break;
	case '-':
		off_type = -1;
		break;
	default:
		off_type = 0;
	}

	//printf("Type %d\n",type[0]);
	checktype = type[0];
	if (checktype == CMDT_WORD)
	{
		if (if_decimal(argv[0]+1,strlen(argv[0])-1))
		{
			checktype = CMDT_INT;
		} else if (if_hex(argv[0]+3,strlen(argv[0])-3))
		{
			checktype = CMDT_HEX;
		}
	}


	//check argument type and convert to offset
	if (checktype == CMDT_INT)
	{
		if (off_type != 0)
		{
			offset = atoi( argv[0] ); //!fix that to strtol at least
		} else 
		{
			offset = atoi( argv[0] );
		}
	} else if (checktype == CMDT_HEX)
	{
		if (off_type != 0)
		{
			offset = strtol( argv[0], NULL, 16);
		} else 
		{
			offset = strtol( argv[0]+3, NULL, 16);
		}
	} else
	{
		//unsupported argument do nothing
		return 0;
	}
	

	if (off_type == 0)
	{
		//g_file offset maybe wrong
		//printf("seekp\n");
		fret = file_seekp( g_file, offset );
	} else
	{
		//printf("seek\n");
		fret = file_seek( g_file, offset );
	}
	if ( fret < 0 )
	{
		printf("Cannot seek postion to %zd\n", offset);
		return -1;
	}

	return 0;
}

int h_seek( cmd_arg_t *arg )
{
	printf("[POS] - move to differnt file offset");
	return 0;
}