Skip to content

Instantly share code, notes, and snippets.

@aliou
Created November 24, 2012 16:12
Show Gist options
  • Save aliou/4140328 to your computer and use it in GitHub Desktop.
Save aliou/4140328 to your computer and use it in GitHub Desktop.
url_encode
/*
** main.c for osef in /exam//rendu/ex_5
**
** Made by aliou diallo
** Login <[email protected]>
**
** Started on Sat Nov 24 11:08:38 2012 aliou diallo
** Last update Sat Nov 24 11:34:57 2012 aliou diallo
*/
void my_putcc(char c)
{
write(1, &c, 1);
}
int is_unsafe(char c)
{
return (c == '{' || c == '}' || c == '|' || c == '\\' || c == '^' ||
c == '~' || c == '[' || c == ']' || c == '`' || c == '+' || c == '<'
|| c == '>' || c == '"' || c == '#' || c == '%' || c == ';'
|| c == '/' || c == '?' || c == ':' || c == '@' || c == '='
|| c == '&' || c == '\'');
}
int print_hex(char c)
{
int nb;
nb = (int) c;
my_putcc('%');
my_putcc("0123456789ABCDEF"[nb / 16]);
my_putcc("0123456789ABCDEF"[nb % 16]);
}
void url_encode(char *str)
{
while (*str)
{
if (*str == ' ')
my_putcc('+');
else if (is_unsafe(*str))
print_hex(*str);
else
my_putcc(*str);
*str++;
}
}
int main(int ac, char **av)
{
if (ac == 2)
url_encode(av[1]);
my_putcc('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment