Skip to content

Instantly share code, notes, and snippets.

@cwchentw
Last active February 9, 2019 07:13
Show Gist options
  • Save cwchentw/a6ec222885a2be1bf54c146de5b47e26 to your computer and use it in GitHub Desktop.
Save cwchentw/a6ec222885a2be1bf54c146de5b47e26 to your computer and use it in GitHub Desktop.
Pass separators to C programs (Apache 2.0)
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#define SEP "\r\n"
#else
#define SEP "\n"
#endif
int main(int argc, char *argv[])
{
if (argc < 2) {
return 0;
}
size_t index = 0;
char *sep;
if (strcmp(argv[1], "-s") == 0) {
if (argc < 3) {
fprintf(stderr, "No valid separator\n");
return 1;
}
sep = argv[2];
index = 3;
}
else {
sep = SEP;
index = 1;
}
for (size_t i = index; i < argc; i++) {
if (i < argc -1) {
printf("%s%s", argv[i], sep);
}
else {
printf("%s", argv[i]);
}
}
printf("%s", SEP);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment