Skip to content

Instantly share code, notes, and snippets.

@outlyer
Created March 29, 2024 14:10
Show Gist options
  • Save outlyer/420b5b4b93fa9c3cfa0e9548c6e5f66a to your computer and use it in GitHub Desktop.
Save outlyer/420b5b4b93fa9c3cfa0e9548c6e5f66a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char ** argv) {
char c = '\0';
int i = 0;
int j = 1;
/* Do not care if no arguments are passed; escaped nothing is still nothing. */
if (argc < 2) {
return 0;
}
while (j < argc) {
while (i < strlen(argv[j])) {
c = argv[j][i];
/* This switch has no breaks on purpose. */
switch (c) {
case ';':
case '\'':
case ' ':
case '!':
case '"':
case '#':
case '$':
case '&':
case '(':
case ')':
case '|':
case '*':
case ',':
case '<':
case '>':
case '[':
case ']':
case '\\':
case '^':
case '`':
case '{':
case '}':
putchar('\\');
default:
putchar(c);
}
i++;
}
j++;
if (j < argc) {
putchar(' ');
}
i = 0;
}
/* Newline at the end */
putchar('\n');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment