Skip to content

Commit 5df786d

Browse files
committed
Update examples
1 parent de11687 commit 5df786d

6 files changed

Lines changed: 74 additions & 46 deletions

File tree

examples/fdisk.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,29 @@
4949

5050
class Position(cstruct.MemCStruct):
5151
__byte_order__ = cstruct.LITTLE_ENDIAN
52-
__struct__ = """
53-
unsigned char head;
54-
unsigned char sector;
55-
unsigned char cyl;
52+
__def__ = """
53+
struct {
54+
unsigned char head;
55+
unsigned char sector;
56+
unsigned char cyl;
57+
}
5658
"""
5759

5860

5961
class Partition(cstruct.MemCStruct):
6062
__byte_order__ = cstruct.LITTLE_ENDIAN
61-
__struct__ = """
63+
__def__ = """
6264
#define ACTIVE_FLAG 0x80
63-
64-
unsigned char status; /* 0x80 - active */
65-
struct Position start;
66-
unsigned char partition_type;
67-
struct Position end;
68-
unsigned int start_sect; /* starting sector counting from 0 */
69-
unsigned int sectors; /* nr of sectors in partition */
65+
typedef struct Position Position;
66+
67+
struct {
68+
unsigned char status; /* 0x80 - active */
69+
Position start;
70+
unsigned char partition_type;
71+
Position end;
72+
unsigned int start_sect; /* starting sector counting from 0 */
73+
unsigned int sectors; /* nr of sectors in partition */
74+
}
7075
"""
7176

7277
@property
@@ -96,7 +101,7 @@ def __str__(self):
96101

97102
class MBR(cstruct.MemCStruct):
98103
__byte_order__ = cstruct.LITTLE_ENDIAN
99-
__struct__ = """
104+
__def__ = """
100105
#define MBR_SIZE 512
101106
#define MBR_DISK_SIGNATURE_SIZE 4
102107
#define MBR_USUALY_NULLS_SIZE 2
@@ -106,11 +111,15 @@ class MBR(cstruct.MemCStruct):
106111
#define MBR_PARTITIONS_SIZE (sizeof(Partition) * MBR_PARTITIONS_NUM)
107112
#define MBR_UNUSED_SIZE (MBR_SIZE - MBR_DISK_SIGNATURE_SIZE - MBR_USUALY_NULLS_SIZE - MBR_PARTITIONS_SIZE - MBR_SIGNATURE_SIZE)
108113
109-
char unused[MBR_UNUSED_SIZE];
110-
unsigned char disk_signature[MBR_DISK_SIGNATURE_SIZE];
111-
unsigned char usualy_nulls[MBR_USUALY_NULLS_SIZE];
112-
struct Partition partitions[MBR_PARTITIONS_NUM];
113-
uint16 signature;
114+
typedef struct Partition Partition;
115+
116+
struct {
117+
char unused[MBR_UNUSED_SIZE];
118+
unsigned char disk_signature[MBR_DISK_SIGNATURE_SIZE];
119+
unsigned char usualy_nulls[MBR_USUALY_NULLS_SIZE];
120+
Partition partitions[MBR_PARTITIONS_NUM];
121+
uint16 signature;
122+
}
114123
"""
115124

116125
@property

examples/fdisk.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
cd "$(dirname "$0")/.." || exit
3+
python -m examples.fdisk $*

examples/flexible_array.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77

88
class FlexArray(MemCStruct):
9-
__struct__ = """
10-
int length;
11-
uint32 checksum;
12-
long data[];
9+
__def__ = """
10+
struct {
11+
int length;
12+
uint32 checksum;
13+
long data[];
14+
}
1315
"""
1416

1517
def set_length(self, length):

examples/flexible_array.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
cd "$(dirname "$0")/.." || exit
3+
python -m examples.flexible_array $*

examples/who.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
from cstruct import parse, getdef, typedef, MemCStruct, NATIVE_ORDER
3+
from cstruct import parse, getdef, MemCStruct, NATIVE_ORDER
44
from pathlib import Path
55
import argparse
66
import sys
@@ -30,24 +30,28 @@
3030
#define UT_LINESIZE 32
3131
#define UT_NAMESIZE 32
3232
#define UT_HOSTSIZE 256
33+
34+
typedef int pid_t;
35+
typedef long time_t;
3336
"""
3437
)
3538

36-
typedef("int", "pid_t")
37-
typedef("long", "time_t")
38-
3939

4040
class ExitStatus(MemCStruct):
41-
__struct__ = """
42-
short e_termination; /* Process termination status. */
43-
short e_exit; /* Process exit status. */
41+
__def__ = """
42+
struct ExitStatus {
43+
short e_termination; /* Process termination status. */
44+
short e_exit; /* Process exit status. */
45+
}
4446
"""
4547

4648

4749
class Timeval(MemCStruct):
48-
__struct__ = """
49-
int32_t tv_sec; /* Seconds. */
50-
int32_t tv_usec; /* Microseconds. */
50+
__def__ = """
51+
struct {
52+
int32_t tv_sec; /* Seconds. */
53+
int32_t tv_usec; /* Microseconds. */
54+
}
5155
"""
5256

5357

@@ -57,21 +61,25 @@ def str_from_c(string):
5761

5862
class Utmp(MemCStruct):
5963
__byte_order__ = NATIVE_ORDER
60-
__struct__ = """
61-
short ut_type; /* Type of record */
62-
pid_t ut_pid; /* PID of login process */
63-
char ut_line[UT_LINESIZE]; /* Device name of tty - "/dev/" */
64-
char ut_id[4]; /* Terminal name suffix, or inittab(5) ID */
65-
char ut_user[UT_NAMESIZE]; /* Username */
66-
char ut_host[UT_HOSTSIZE]; /* Hostname for remote login, or kernel version for run-level messages */
67-
struct ExitStatus ut_exit; /* Exit status of a process marked as DEAD_PROCESS; not used by Linux init (1 */
68-
int32_t ut_session; /* Session ID (getsid(2)), used for windowing */
64+
__def__ = """
65+
typedef struct ExitStatus ExitStatus;
66+
6967
struct {
70-
int32_t tv_sec; /* Seconds */
71-
int32_t tv_usec; /* Microseconds */
72-
} ut_tv; /* Time entry was made */
73-
int32_t ut_addr_v6[4]; /* Internet address of remote host; IPv4 address uses just ut_addr_v6[0] */
74-
char __unused[20]; /* Reserved for future use */
68+
short ut_type; /* Type of record */
69+
pid_t ut_pid; /* PID of login process */
70+
char ut_line[UT_LINESIZE]; /* Device name of tty - "/dev/" */
71+
char ut_id[4]; /* Terminal name suffix, or inittab(5) ID */
72+
char ut_user[UT_NAMESIZE]; /* Username */
73+
char ut_host[UT_HOSTSIZE]; /* Hostname for remote login, or kernel version for run-level messages */
74+
ExitStatus ut_exit; /* Exit status of a process marked as DEAD_PROCESS; not used by Linux init (1 */
75+
int32_t ut_session; /* Session ID (getsid(2)), used for windowing */
76+
struct {
77+
int32_t tv_sec; /* Seconds */
78+
int32_t tv_usec; /* Microseconds */
79+
} ut_tv; /* Time entry was made */
80+
int32_t ut_addr_v6[4]; /* Internet address of remote host; IPv4 address uses just ut_addr_v6[0] */
81+
char __unused[20]; /* Reserved for future use */
82+
}
7583
"""
7684

7785
@property

examples/who.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
cd "$(dirname "$0")/.." || exit
3+
python -m examples.who $*

0 commit comments

Comments
 (0)