commit 72fd76f10ce717d784f27a915ae7a7e9bbab646b
parent 677acf9f5d5842a59a0b17105351b48b52bcb6fa
Author: Christos Margiolis <christos@margiolis.net>
Date: Sat, 12 Dec 2020 14:46:15 +0200
finishing sh: ex2
Diffstat:
7 files changed, 126 insertions(+), 82 deletions(-)
diff --git a/mips-architecture/lab5_ex1.asm b/mips-architecture/lab5_ex1.asm
@@ -1,4 +1,5 @@
.eqv SYS_PRINT_WORD 1
+.eqv SYS_PRINT_STRING 4
.eqv SYS_READ_STRING 8
.eqv SYS_EXIT 10
@@ -19,14 +20,11 @@ main:
# init strlen counter
li $t0, 0
- # load string to $t1 because we want to use $a0
- la $t1, str
strlen:
- lb $a0, 0($t1)
- beqz $a0, exit
+ lb $t1, str($t0)
+ beqz $t1, exit
addi $t0, $t0, 1
- addi $t1, $t1, 1
j strlen
exit:
@@ -35,7 +33,7 @@ exit:
syscall
li $v0, SYS_PRINT_WORD
- la $a0, 0($t0)
+ la $a0, 0($t0)
syscall
li $v0, SYS_EXIT
diff --git a/mips-architecture/lab5_ex2.asm b/mips-architecture/lab5_ex2.asm
@@ -19,30 +19,26 @@ main:
# init strlen counter
li $t0, 0
- # load string to $t1 because we want to use $a0
- la $t1, str
strlen:
- lb $a0, 0($t1)
- beqz $a0, strrev
+ lb $t1, str($t0)
+ beqz $t1, strrev
addi $t0, $t0, 1
- addi $t1, $t1, 1
j strlen
# subtract 2 so that we skip '\n' and '\0' and get to the
# last character in the string
- addi $t1, $t1, -2
+ addi $t0, $t0, -2
strrev:
# if $t0 is 0 is means we reached str[0]
beqz $t0, exit
# in the first loop, $t1 is at the last actual character,
# so we'll go backwards to get to the beginning
- lb $a0, 0($t1)
li $v0, SYS_PRINT_CHAR
+ lb $a0, str($t0)
syscall
addi $t0, $t0, -1
- addi $t1, $t1, -1
j strrev
exit:
diff --git a/mips-architecture/lab5_ex3.asm b/mips-architecture/lab5_ex3.asm
@@ -1,20 +1,18 @@
.eqv SYS_EXIT 10
.data
- size: .byte 10
arr: .byte 1, 15, 0, -3, 99, 48, -17, -9, 20, 15
.text
.globl main
main:
- # init loop, sum counters and define iterations
+ # init loop $t0, sum $t1 counters
li $t0, 0
li $t1, 0
- lb $t2, size
calcsum:
- beq $t0, $t2, exit
+ beq $t0, 10, exit
lb $t3, arr($t0)
add $t1, $t1, $t3
addi $t0, $t0, 1
diff --git a/sh-os1/ex2/bck b/sh-os1/ex2/bck
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+main() {
+ # allow > 3?
+ test $# -eq 3 || usage
+
+ username=${1}
+ src=${2}
+ dst=${3}
+
+ test ! -z "$(grep -w "^${username}" /etc/passwd)" ||
+ err "'${username}' is not in /etc/passwd"
+ test -e ${src} || err "${src}: no such file or directory"
+ test -e ${dst} || err "${dst}: no such file or directory"
+
+ test -d ${dst} && tar -cvf "${dst}/${src##*/}.tar" ${src}
+ # handle tar.*
+ test ${dst##*.} = "tar" && tar -rf ${dst} ${src}
+}
+
+usage() {
+ echo "usage: ${0##*/} username src dst"
+ exit 1
+}
+
+err() {
+ echo "${0##*/}: ${1}"
+ exit 1
+}
+
+main "$@"
diff --git a/sh-os1/ex2/createpvs b/sh-os1/ex2/createpvs
@@ -3,30 +3,46 @@
main() {
test $# -eq 4 || usage
- rootdir=$1
- ndbdirs=$2
- ndatadirs=$3
- username=$4
-
- test -d "$rootdir" || err "$rootdir: no such directory"
- isnumber $ndbdirs || err "'$ndbdirs' is not a number"
- isnumber $ndatadirs || err "'$ndatadirs' is not a number"
- test ! -z $(grep "$username" /etc/passwd) || err "'$username' is not in /etc/passwd"
-
- # pending...
+ rootdir=${1}
+ ndbdirs=${2}
+ ndatadirs=${3}
+ username=${4}
+
+ test -d "${rootdir}" || err "${rootdir}: no such directory"
+ isnumber ${ndbdirs} || err "'${ndbdirs}' is not a number"
+ isnumber ${ndatadirs} || err "'${ndatadirs}' is not a number"
+ test ! -z "$(grep -w "^${username}" /etc/passwd)" ||
+ err "'${username}' is not in /etc/passwd"
+
+ makedirs "dbfolder" ${ndbdirs}
+ makedirs "datafolder" ${ndatadirs}
}
usage() {
- echo "usage: ${0##*/} [ROOT_DIR] [N_DBDIRS] [N_DATADIRS] [USERNAME]" &&
+ echo "usage: ${0##*/} root_dir num_dbdirs num_datadirs username"
exit 1
}
err() {
- echo "${0##*/}: $1" && exit 1
+ echo "${0##*/}: ${1}"
+ exit 1
}
isnumber() {
- test ! -z $(echo "$1" | grep "^[0-9]\+$")
+ test ! -z "$(echo "${1}" | grep "^[0-9]\+$")"
+}
+
+makedirs() {
+ n=${2}
+ lastdir=$(ls ${rootdir} | grep "${1}" | sed "s/${1}//g" | sort -V | tail -1)
+ test -z ${lastdir} && lastdir=0
+
+ while ! [ $(expr ${n}) = 0 ]; do
+ lastdir=$(expr ${lastdir} + 1)
+ mkdir "${rootdir}/${1}${lastdir}" &&
+ chown ${username}: "${rootdir}/${1}${lastdir}"
+ n=$(expr ${n} - 1)
+ done
}
main "$@"
diff --git a/sh-os1/ex2/searching b/sh-os1/ex2/searching
@@ -2,56 +2,61 @@
main() {
test $# -eq 2 || usage
- isnumber $1
- isnumber $2
+ isnumber ${1}
+ isnumber ${2}
sum1=0; sum2=0; sum3=0; sum4=0; sum5=0
- while [ ! "$cont" = "n" ] ; do
+ while [ ! "${cont}" = "n" ] ; do
read -erp "Directory name: " dir
- test -z "$dir" && echo "${0##*/}: please give a directory name" && exit 1
- test ! -d "$dir" && echo "${0##*/}: '$dir' is not a directory" && exit 1
+ test ! -z "${dir}" || err "please give a directory name"
+ test -d "${dir}" || err "'${dir}' is not a directory"
- echo "Files with permissions $1: "
- find "$dir" -perm $1 2> /dev/null
- sum1=$(($sum1+$(find "$dir" -perm $1 2> /dev/null | wc -l)))
+ echo "Files with permissions ${1}: "
+ find "${dir}" -perm ${1} 2> /dev/null
+ sum1=$((${sum1}+$(find "${dir}" -perm ${1} 2> /dev/null | wc -l)))
echo ""
- echo "Files modified within the last $2 days: "
- find "$dir" -mtime -$2 2> /dev/null
- sum2=$(($sum2+$(find "$dir" -mtime -$2 2> /dev/null | wc -l)))
+ echo "Files modified within the last ${2} days: "
+ find "${dir}" -mtime -${2} 2> /dev/null
+ sum2=$((${sum2}+$(find "${dir}" -mtime -${2} 2> /dev/null | wc -l)))
echo ""
- echo "Subdirectories accessed within the last $2 days: "
- find "$dir" -type d -atime -$2 2> /dev/null
- sum3=$(($sum3+$(find "$dir" -type d -atime -$2 2> /dev/null | wc -l)))
+ echo "Subdirectories accessed within the last ${2} days: "
+ find "${dir}" -type d -atime -${2} 2> /dev/null
+ sum3=$((${sum3}+$(find "${dir}" -type d -atime -${2} 2> /dev/null | wc -l)))
echo ""
echo "Files which all users have read access to: "
- ls -l "$dir" 2> /dev/null | grep "^-r..r..r.."
- sum4=$(($sum4+$(ls -l "$dir" 2> /dev/null | grep "^-r..r..r.." | wc -l)))
+ ls -l "${dir}" 2> /dev/null | grep "^-r..r..r.."
+ sum4=$((${sum4}+$(ls -l "${dir}" 2> /dev/null | grep "^-r..r..r.." | wc -l)))
echo ""
echo "Subdirectories which others have write access to: "
- ls -l "$dir" 2> /dev/null | grep "^d.w..w..w."
- sum5=$(($sum5+$(ls -l "$dir" 2> /dev/null | grep "^d.w..w..w." | wc -l)))
+ ls -l "${dir}" 2> /dev/null | grep "^d.w..w..w."
+ sum5=$((${sum5}+$(ls -l "${dir}" 2> /dev/null | grep "^d.w..w..w." | wc -l)))
echo ""
read -erp "Do you want to continue (y/n)? " cont
- test "$cont" = "n" &&
+ test "${cont}" = "n" &&
printf "Total entries found for:\n1. %s\n2. %s\n3. %s\n4. %s\n5. %s\n" \
- "$sum1" "$sum2" "$sum3" "$sum4" "$sum5"
+ "${sum1}" "${sum2}" "${sum3}" "${sum4}" "${sum5}"
done
}
usage() {
- echo "usage: ${0##*/} [PERMS] [DAYS]" && exit 0
+ echo "usage: ${0##*/} perms days"
+ exit 1
+}
+
+err() {
+ echo "${0##*/}: ${1}"
+ exit 1
}
isnumber() {
- test -z $(echo "$1" | grep "^[0-9]\+$") &&
- echo "${0##*/}: '$1' is not a digit" && exit 1
+ test ! -z "$(echo "${1}" | grep "^[0-9]\+$")" || err "'${1}' is not a digit"
}
main "$@"
diff --git a/sh-os1/ex2/teldb b/sh-os1/ex2/teldb
@@ -3,55 +3,55 @@
emptylnregex="^\ *$"
main() {
- case $1 in
+ case ${1} in
-a) newentry ;;
-l) list ;;
- -s) sortcol $2 ;;
- -c) filter $2 ;;
- -d) deleteln $2 $3 ;;
+ -s) sortcol ${2} ;;
+ -c) filter ${2} ;;
+ -d) deleteln ${2} ${3} ;;
-n) emptyln ;;
*) usage ;;
esac
}
usage() {
- echo "usage: ${0##*/} [-a|-l|-s COL|-c KEYWORD|-d KEYWORD [-b|-r]|-n]"
+ echo "usage: ${0##*/} [-a | -l | -n | -c keyword | -d keyword [-b | -r] | -s col]"
echo ""
echo "options:"
echo "-a add a new entry"
echo "-l list contents of file"
- echo "-s sort based on specified column (e.g ${0##*/} -s 3)"
+ echo "-n count empty lines and ask if they should be deleted"
echo "-c filter keyword"
echo "-d delete lines containing a keyword followed by one of the options below"
- echo " -b replace line with an empty line"
- echo " -r remove line completely"
- echo "-n count empty lines and ask if they should be deleted"
+ echo " -b replaces line with an empty line"
+ echo " -r removes line completely"
+ echo "-s sort based on specified column (e.g ${0##*/} -s 3)"
exit 1
}
errempty() {
- test -z "$1" && usage
+ test ! -z "${1}" || usage
}
skipempty() {
- grep -v "$emptylnregex"
+ grep -v "${emptylnregex}"
}
newentry() {
read -erp "First name: " fname
- errempty "$fname"
+ errempty "${fname}"
read -erp "Last name: " lname
- errempty "$lname"
+ errempty "${lname}"
read -erp "Town: " town
- errempty "$town"
+ errempty "${town}"
read -erp "Phone number: " phone
- errempty "$phone"
+ errempty "${phone}"
- printf "%s %s %s %s\n" "$fname" "$lname" "$town" "$phone" >> catalog
+ printf "%s %s %s %s\n" "${fname}" "${lname}" "${town}" "${phone}" >> catalog
}
list() {
@@ -59,30 +59,30 @@ list() {
}
sortcol() {
- errempty "$1"
- sort -k "$1" catalog | skipempty
+ errempty "${1}"
+ sort -k "${1}" catalog | skipempty
}
filter() {
- errempty "$1"
- grep "$1" catalog
+ errempty "${1}"
+ grep "${1}" catalog
}
deleteln() {
- errempty "$1"
- errempty "$2"
+ errempty "${1}"
+ errempty "${2}"
- case $2 in
- -b) sed -i "s/.*$1.*//" catalog ;;
- -r) sed -i "/$1/d" catalog ;;
+ case ${2} in
+ -b) sed -i "s/.*${1}.*//" catalog ;;
+ -r) sed -i "/${1}/d" catalog ;;
esac
}
emptyln() {
printf "Empty lines in 'catalog': "
- grep "$emptylnregex" catalog | wc -l
+ grep "${emptylnregex}" catalog | wc -l
read -erp "Delete them (y/n)? " del
- test "$del" = "y" && deleteln "$emptylnregex" "-r"
+ test "${del}" = "y" && deleteln "${emptylnregex}" "-r"
}
main "$@"