Skip to content

Instantly share code, notes, and snippets.

@yury

yury/bpcopy.c Secret

Created May 7, 2018 07:30
Show Gist options
  • Save yury/7a99c559e7b687a861b2f40cba67bbc1 to your computer and use it in GitHub Desktop.
Save yury/7a99c559e7b687a861b2f40cba67bbc1 to your computer and use it in GitHub Desktop.
int pbcopy(int argc, char** argv) {
if (argc == 1) {
// no arguments, listen to stdin
if (currentSession == NULL) {
currentSession = [[sessionParameters alloc] init];
}
const int bufsize = 1024;
char buffer[bufsize];
NSMutableData* data = [[NSMutableData alloc] init];
ssize_t count = 0;
while ((count = read(fileno(thread_stdin), buffer, bufsize-1))) {
[data appendBytes:buffer length:count];
}
NSString* result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!result) {
return 1;
}
[UIPasteboard generalPasteboard].string = result;
} else {
// threre are arguments, concatenate and paste:
char* cmd = concatenateArgv(argv + 1);
[UIPasteboard generalPasteboard].string = @(cmd);
free(cmd);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment