File: libc.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (100 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* magic */
// Do not edit the line above.

// RUN: %empty-directory(%t)
// RUN: %target-run-simple-swift %s %t | %FileCheck %s

// REQUIRES: executable_test

// TODO: rdar://problem/33388782
// REQUIRES: CPU=x86_64

#if canImport(Darwin)
  import Darwin
#elseif canImport(Glibc)
  import Glibc
#elseif os(Windows)
  import CRT

  let S_IRUSR: Int32 = ucrt._S_IREAD
  let S_IWUSR: Int32 = 0
  let S_IXUSR: Int32 = 0

  let S_IRGRP: Int32 = 0o0040
  let S_IROTH: Int32 = 0o0004
#else
#error("Unsupported platform")
#endif

let sourcePath = CommandLine.arguments[1]
let tempPath = CommandLine.arguments[2] + "/libc.txt"

// CHECK: Hello world
fputs("Hello world", stdout)

// CHECK: 4294967295
print("\(UINT32_MAX)")

// CHECK: the magic word is ///* magic *///
let sourceFile = open(sourcePath, O_RDONLY)
assert(sourceFile >= 0)
var bytes = UnsafeMutablePointer<CChar>.allocate(capacity: 12)
var readed = read(sourceFile, bytes, 11)
close(sourceFile)
assert(readed == 11)
bytes[11] = CChar(0)
print("the magic word is //\(String(cString: bytes))//")

// CHECK: O_CREAT|O_EXCL returned errno *17*
let errFile = 
  open(sourcePath, O_RDONLY | O_CREAT | O_EXCL)
if errFile != -1 { 
  print("O_CREAT|O_EXCL failed to return an error") 
} else {
  let e = errno
  print("O_CREAT|O_EXCL returned errno *\(e)*") 
}

// CHECK-NOT: error
// CHECK: created mode *{{33216|33060}}* *{{33216|33060}}*
let tempFile = 
  open(tempPath, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IXUSR)
if tempFile == -1 {
  let e = errno
  print("error: open(tempPath \(tempPath)) returned -1, errno \(e)")
  abort()
}
let written = write(tempFile, bytes, 11)
if (written != 11) {
  print("error: write(tempFile) returned \(written), errno \(errno)")
  abort()
}

var err: Int32
var statbuf1 = stat()
err = fstat(tempFile, &statbuf1)
if err != 0 {
  let e = errno
  print("error: fstat returned \(err), errno \(e)")
  abort()
}

close(tempFile)

var statbuf2 = stat()
err = stat(tempPath, &statbuf2)
if err != 0 {
  let e = errno
  print("error: stat returned \(err), errno \(e)")
  abort()
}

print("created mode *\(statbuf1.st_mode)* *\(statbuf2.st_mode)*")

#if os(Windows)
assert(statbuf1.st_mode == S_IFREG | S_IRUSR | S_IRGRP | S_IROTH)
#else
assert(statbuf1.st_mode == S_IFREG | S_IRUSR | S_IWUSR | S_IXUSR)
#endif
assert(statbuf1.st_mode == statbuf2.st_mode)