Difference between revisions of "Prove pratiche 2022"
Jump to navigation
Jump to search
Line 43: | Line 43: | ||
== Esercizio 3 == | == Esercizio 3 == | ||
+ | |||
+ | === Proposta 1 === | ||
+ | |||
+ | Da contorllare [[User:Flecart|Flecart]] ([[User talk:Flecart|talk]]) 14:52, 9 March 2023 (CET) | ||
+ | |||
+ | <syntaxhighlight lang="python"> | ||
+ | import sys | ||
+ | import os | ||
+ | import stat | ||
+ | |||
+ | def is_elf(file): | ||
+ | with open(file, "rb") as f: | ||
+ | magic = f.read()[:4] | ||
+ | if magic == b'\x7fELF': | ||
+ | return True | ||
+ | return False | ||
+ | |||
+ | def count(dir): | ||
+ | if not os.path.isdir(dir): | ||
+ | return 0 | ||
+ | |||
+ | num_bits = 0 | ||
+ | for root, dir, files in os.walk(dir): | ||
+ | for file in files: | ||
+ | end_path = os.path.join(root, file) | ||
+ | if is_elf(end_path): | ||
+ | num_bits += os.lstat(end_path).st_size | ||
+ | |||
+ | return num_bits | ||
+ | |||
+ | if __name__ == "__main__": | ||
+ | num_args = len(sys.argv); | ||
+ | |||
+ | num_bits = 0 | ||
+ | for i in range(1, num_args): | ||
+ | num_bits += count(sys.argv[i]) | ||
+ | |||
+ | if num_args == 1: | ||
+ | num_bits += count("./") | ||
+ | |||
+ | print(f"number of bits is : {num_bits}"); | ||
+ | |||
+ | </syntaxhighlight> | ||
== Esercizio 4 == | == Esercizio 4 == |
Revision as of 14:52, 9 March 2023
2022-09-07
Esercizio 1
Proposta 1
Da controllare, 09/03/2023
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
int main(int argc, char *const argv[]) {
if (argc < 2) {
fprintf(stderr, "Non ci sono abbastanza parametri per lo script\n");
return 1;
}
while (1) {
int wstatus;
if (fork()) {
execvp(argv[1], argv + 1);
} else {
wait(&wstatus);
int status = WEXITSTATUS(wstatus);
printf("return value ***************** %d\n", status);
if (status != 0) {
break;
}
}
}
return 0;
}
Esercizio 2
Esercizio 3
Proposta 1
Da contorllare Flecart (talk) 14:52, 9 March 2023 (CET)
import sys
import os
import stat
def is_elf(file):
with open(file, "rb") as f:
magic = f.read()[:4]
if magic == b'\x7fELF':
return True
return False
def count(dir):
if not os.path.isdir(dir):
return 0
num_bits = 0
for root, dir, files in os.walk(dir):
for file in files:
end_path = os.path.join(root, file)
if is_elf(end_path):
num_bits += os.lstat(end_path).st_size
return num_bits
if __name__ == "__main__":
num_args = len(sys.argv);
num_bits = 0
for i in range(1, num_args):
num_bits += count(sys.argv[i])
if num_args == 1:
num_bits += count("./")
print(f"number of bits is : {num_bits}");