1 //===- AutoConvert.cpp - Auto conversion between ASCII/EBCDIC -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains functions used for auto conversion between
10 // ASCII/EBCDIC codepages specific to z/OS.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifdef __MVS__
15 
16 #include "llvm/Support/AutoConvert.h"
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 
20 std::error_code llvm::disableAutoConversion(int FD) {
21   static const struct f_cnvrt Convert = {
22       SETCVTOFF,        // cvtcmd
23       0,                // pccsid
24       (short)FT_BINARY, // fccsid
25   };
26   return fcntl(FD, F_CONTROL_CVT, &Convert);
27 }
28 
29 std::error_code llvm::enableAutoConversion(int FD) {
30   struct f_cnvrt Query = {
31       QUERYCVT, // cvtcmd
32       0,        // pccsid
33       0,        // fccsid
34   };
35 
36   if (fcntl(FD, F_CONTROL_CVT, &Query) == -1)
37     return -1;
38 
39   Query.cvtcmd = SETCVTALL;
40   Query.pccsid =
41       (FD == STDIN_FILENO || FD == STDOUT_FILENO || FD == STDERR_FILENO)
42           ? 0
43           : CCSID_UTF_8;
44   // Assume untagged files to be IBM-1047 encoded.
45   Query.fccsid = (Query.fccsid == FT_UNTAGGED) ? CCSID_IBM_1047 : Query.fccsid;
46   return fcntl(FD, F_CONTROL_CVT, &Query);
47 }
48 
49 std::error_code llvm::setFileTag(int FD, int CCSID, bool Text) {
50   assert((!Text || (CCSID != FT_UNTAGGED && CCSID != FT_BINARY)) &&
51          "FT_UNTAGGED and FT_BINARY are not allowed for text files");
52   struct file_tag Tag;
53   Tag.ft_ccsid = CCSID;
54   Tag.ft_txtflag = Text;
55   Tag.ft_deferred = 0;
56   Tag.ft_rsvflags = 0;
57 
58   return fcntl(FD, F_SETTAG, &Tag);
59 }
60 
61 #endif // __MVS__
62