contrib/ksmtpproxy: Fix typo
[navymail.git] / t / xdd
blobe3a4232060319badd521b6a9c7a969ff9026d4ae
1 #!/usr/bin/env python
2 """
3 dd-alike which supports SEEK_SET seeking
4 Copyright (C) 2011 Kirill Smelkov <kirr@navytux.spb.ru>
6 This program is free software: you can Use, Study, Modify and Redistribute it
7 under the terms of the GNU General Public License version 2. This program is
8 distributed WITHOUT ANY WARRANTY. See COPYING file for full License terms.
11 dd's skip uses SEEK_CUR, and also it checks whether current position is >
12 st_size after, and errors if it is, which is not good for us - in our case all
13 files have st_size=0, like in /proc.
15 So here goes small utility
16 $ xdd off1,size1 off2,size2 ... <in >out
17 """
18 import sys
19 from os import read, write, lseek, SEEK_SET
22 # safe read
23 def xread(fd, size):
24 buf = ''
26 while size != 0:
27 chunk = read(fd, size)
28 if not chunk:
29 break
31 buf += chunk
32 size -= len(chunk)
34 return buf
36 # safe write
37 def xwrite(fd, buf):
38 wrote = 0
40 while buf:
41 size = write(fd, buf)
43 buf = buf[size:]
44 wrote += size
46 return wrote
49 def main():
50 # (offset, size)
51 work = []
53 for arg in sys.argv[1:]:
54 offset, size = arg.split(',')
55 offset = int(offset)
56 size = int(size)
57 work.append( (offset, size) )
59 for offset, size in work:
60 lseek(0, offset, SEEK_SET)
61 buf = xread(0, size)
62 xwrite(1, buf)
66 if __name__ == '__main__':
67 main()