-
Notifications
You must be signed in to change notification settings - Fork 11
/
loc.ksh
92 lines (87 loc) · 1.91 KB
/
loc.ksh
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
#!/bin/ksh
#
# ==============================================================================
# Filename : loc
# Function:
# This script is a handy command to get the total comment lines
# out of the source code. This script works for following type
# of comments only:
# Type Description
# C C language type comments
# CPP C ++ type comments
# SHELL Unix Shell script comments
# Exp: loc C hello.c
# loc CPP hello.cpp
# loc SHELL findAworld.ksh
# ==============================================================================
# Submitter : arjun.singh@hpsglobal.com
# ==============================================================================
#
#
# Tested on AIX 4.3. Some adjustments may be needed for your flavor of Unix.
#
#set -vx
USAGE="Usage:$0 <comment type {C|CPP|SHELL}> <Source program name>"
if [ $# -lt 2 ]
then
echo ${USAGE}
exit 1
fi
TIME=$(date)
awk -v type=$1 '
BEGIN {
loc=0;
comment_line=0;
total_line=0;
stflag="F";
}
{
total_line++
if ( type == "C" )
{
if ( $0 ~ /^[ \t]*\/\*/ )
{
stflag="T"
if ( $0 ~ /\*\/$/ )
{
# print "comment line : ", $0
comment_line++
stflag="F"
}
}
if ( stflag == "T" )
{
comment_line++
# print "comment line : ", $0
if ( $0 ~ /\*\/$/ )
{
# print "comment line : ", $0
stflag="F"
}
}
}
if ( type == "CPP" )
{
if ( $0 ~ /^[ \t]*\/\// )
comment_line++
}
if ( type == "SHELL" )
{
if ( $0 ~ /^[ \t]*\#/ )
comment_line++
}
}
END {
print "Timestamp: ""'"${TIME}"'"
pct=(comment_line*100)/total_line
loc=total_line - comment_line
pct1=(loc*100)/total_line
print "Program statistics --->"
print "-------------------------------"
print "Source Code Type : ", type
print "Source Code Program Name : ", ARGV[1]
print "Total number of lines : ", total_line
print "Total number of commented lines : ", comment_line ,"("pct"%)"
print "Total number of lines of code : ",loc, "("pct1"%)"
}
' $2