xref: /trunk/main/shell/source/unix/misc/uri-encode.c (revision badc9ebb)
1*badc9ebbSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*badc9ebbSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*badc9ebbSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*badc9ebbSAndrew Rist  * distributed with this work for additional information
6*badc9ebbSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*badc9ebbSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*badc9ebbSAndrew Rist  * "License"); you may not use this file except in compliance
9*badc9ebbSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*badc9ebbSAndrew Rist  *
11*badc9ebbSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*badc9ebbSAndrew Rist  *
13*badc9ebbSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*badc9ebbSAndrew Rist  * software distributed under the License is distributed on an
15*badc9ebbSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*badc9ebbSAndrew Rist  * KIND, either express or implied.  See the License for the
17*badc9ebbSAndrew Rist  * specific language governing permissions and limitations
18*badc9ebbSAndrew Rist  * under the License.
19*badc9ebbSAndrew Rist  *
20*badc9ebbSAndrew Rist  *************************************************************/
21*badc9ebbSAndrew Rist 
22*badc9ebbSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <ctype.h>
25cdf0e10cSrcweir #include <errno.h>
26cdf0e10cSrcweir #include <stdio.h>
27cdf0e10cSrcweir #include <stdlib.h>
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir 
main()30cdf0e10cSrcweir int main() {
31cdf0e10cSrcweir     for (;;) {
32cdf0e10cSrcweir         int c;
33cdf0e10cSrcweir         errno = 0;
34cdf0e10cSrcweir         c = getchar();
35cdf0e10cSrcweir         if (c == EOF) {
36cdf0e10cSrcweir             exit(errno == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
37cdf0e10cSrcweir         } else if (isalnum(c) || strchr("!$'()*+,-.:=@_~/\n", c) != NULL) {
38cdf0e10cSrcweir             /* valid RFC 2396 pchar characters + '/' + newline */
39cdf0e10cSrcweir             if (putchar(c) == EOF) {
40cdf0e10cSrcweir                 exit(EXIT_FAILURE);
41cdf0e10cSrcweir             }
42cdf0e10cSrcweir         } else if (printf("%%%02X", (unsigned char) (char) c) < 0) {
43cdf0e10cSrcweir             exit(EXIT_FAILURE);
44cdf0e10cSrcweir         }
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir }
47