-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_returns.sh
More file actions
executable file
·84 lines (70 loc) · 2.32 KB
/
Copy pathupdate_returns.sh
File metadata and controls
executable file
·84 lines (70 loc) · 2.32 KB
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
# !/usr/bin/env bash
# Process all C files recursively
find . -type f -name '*.c' | while read -r file; do
echo "Processing: $file"
modified=false
# 1. Ensure stdlib.h is included
if ! grep -qE '#include\s*<stdlib\.h>' "$file"; then
awk '
BEGIN { last_include=0 }
/^#include/ { last_include=NR }
{ lines[NR]=$0 }
END {
for (i=1; i<=NR; i++) {
print lines[i]
if (i == last_include) {
print "#include <stdlib.h>"
}
}
}
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
modified=true
fi
# 2. Process the main function properly
awk '
BEGIN {
in_main = 0
brace_level = 0
found_return = 0
}
{
line = $0
# Detect main function start (loose match)
if (line ~ /int[[:space:]]+main[[:space:]]*\(.*\)/) {
in_main = 1
}
if (in_main) {
# Track braces
brace_level += gsub(/\{/, "{", line)
brace_level -= gsub(/\}/, "}", line)
# Replace return 0 and 1
gsub(/return[[:space:]]+0[[:space:]]*;/, "return EXIT_SUCCESS;", line)
gsub(/return[[:space:]]+1[[:space:]]*;/, "return EXIT_FAILURE;", line)
# Detect if already a good return
if (line ~ /return[[:space:]]+(EXIT_SUCCESS|EXIT_FAILURE)[[:space:]]*;/) {
found_return = 1
}
# If line is just a closing brace and we are at end of main without return
if (brace_level == 0 && found_return == 0 && line ~ /^[[:space:]]*\}[[:space:]]*$/) {
print " return EXIT_SUCCESS;"
found_return = 1
in_main = 0
}
# If line is closing brace and found return
else if (brace_level == 0 && found_return == 1 && line ~ /^[[:space:]]*\}[[:space:]]*$/) {
in_main = 0
}
}
print line
}
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
modified=true
if [ "$modified" = true ]; then
echo "Modified: $file"
fi
done
echo ""
echo "Formatting all code with clang-format..."
# 3. clang-format everything properly
find . -type f -name '*.c' -exec clang-format -i {} +
echo "Done!"