Skip to content

Instantly share code, notes, and snippets.

@dibyasonu
Created January 30, 2022 18:22
Show Gist options
  • Save dibyasonu/ab5ea01bd31df5201daf71f5f84d7c92 to your computer and use it in GitHub Desktop.
Save dibyasonu/ab5ea01bd31df5201daf71f5f84d7c92 to your computer and use it in GitHub Desktop.
Create .swp file manually which is compatible with nano and vim.
import os
LOCKSIZE = 1024
lockdata = bytearray(LOCKSIZE)
file_name = 'balance.dat'
mypid = os.getppid()
def strncopy(parent_value, child_value, start):
if start >= len(parent_value):
parent_value.extend(bytearray(start-len(parent_value)+len(child_value)))
for index in range(len(child_value)):
parent_value[start+index] = child_value[index]
return parent_value
lockdata[0] = 0x62
lockdata[1] = 0x30
lockdata = strncopy(lockdata, bytearray("nano 4.8", encoding='utf-8'), 2)
lockdata[24] = mypid % 256
lockdata[25] = (mypid//256) % 256
lockdata[26] = (mypid//(256*256)) % 256
lockdata[27] = mypid//(256*256*256)
lockdata = strncopy(lockdata, bytearray(os.getlogin(), encoding='utf-8'), 28)
lockdata = strncopy(lockdata, bytearray(os.uname()[1], encoding='utf-8'), 68)
lockdata = strncopy(lockdata, bytearray(file_name, encoding='utf-8'), 108)
lockdata[1007] = 0x55
lockdata = bytes(lockdata)
# print(len(lockdata))
with open(f".{file_name}.swp", "wb") as f:
f.write(lockdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment