Prova pratica 2017.02.17

From Sistemi Operativi
Jump to navigation Jump to search

[link al testo]

Esercizio 1

Scrivere un programma ager in grado di invecchiare file.
Il programma deve poter prendere una lista di file come parametri o nessun parametro, nel qual caso invecchierà
tutti I file della directory corrente. “invecchiare” significa predatare il tempo di ultimo accesso e di modifica di 10
giorni.
Esempio:
$ ls -l file
-rw-r--r-- 1 renzo renzo 0 Feb 17 09:07 file
$ ./ager file
$ ls -l file
-rw-r--r-- 1 renzo renzo 0 Feb 7 09:07 file

Soluzione di FedericoB

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <utime.h>

int main(int argc, char* argv[]) {
    if (argc>1) {
        int i=0;
        for (i=1;i<argc;i++) {
            struct stat structstat;
            if (stat(argv[i],&structstat)==0) {
                //read last access time
                time_t atim = structstat.st_atim.tv_sec;
                //read last modification time
                time_t mtim = structstat.st_mtim.tv_sec;
                atim -= 10 * 24 * 60 * 60;
                mtim -= 10 * 24 * 60 * 60;
                //create structure for utime
                struct utimbuf structutimbuf = {atim, mtim};
                //call utime for setting the new date
                if (utime(argv[i], &structutimbuf)!=0) {
                    //if utime failed print errno
                    perror(NULL);
                }
            } else {
                //if stat failed print errno
                perror(NULL);
            }
        }
    }
    return 0;
}

Esercizio 2

Completare l’esercizio 1 ponendo una gestione opportuna dei parametri in linea comando che preveda di
poter modificare solo la data di accesso o quella di modifica, di definire il tempo di “invecchiamento” e un help.
Si possono inserire altri parametri ritenuti opportuni.

Soluzione di FedericoB

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <utime.h>

int main(int argc, char* argv[]) {
    int access = 0;
    int modification = 0;
    int c;
    int index = 0;
    time_t time = 10 * 24 * 60 * 60;
    while ((c = getopt(argc, argv, "amt:h")) != -1) {
        switch (c) {
            case 'a': access=1; break;
            case 'm': modification=1; break;
            case 't':
                time = strtol(optarg,NULL,10);
                break;
            case 'h':
            case '?':
                printf("usage: ager [-a] [-m] [-t time] [file ...]");
            default:
                abort ();
        }
    }
    for (index = optind; index < argc; index++) {
        struct stat structstat;
        if (stat(argv[index], &structstat) == 0) {
            //read last access time
            time_t atim = structstat.st_atim.tv_sec;
            //read last modification time
            time_t mtim = structstat.st_mtim.tv_sec;
            if (!modification || access) {
                atim -= time;
            }
            if (!access || modification) {
                mtim -= time;
            }
            //create structure for utime
            struct utimbuf structutimbuf = {atim, mtim};
            //call utime for setting the new date
            if (utime(argv[index], &structutimbuf) != 0) {
                //if utime failed print errno
                perror(NULL);
            }
        } else {
            //if stat failed print errno
            perror(NULL);
        }
    }
    return 0;
}

Esercizio 3

Il programma deve elencare I file della directory corrente divisi per suffisso (e al termine l’elenco di quelli privi di
suffisso.
es:
.c: primo.c var.c main.c
.h: primo.h const.h
.odt: relazione.odt
makefile README COPYING

Soluzione di FedericoB

import os
from pathlib import Path

nosuffix = []
suffix = {}
for root, dirs, files in os.walk("."):
    for filename in files:
      path = Path(filename)
      if (len(path.suffix)>0):
        suffix.setdefault(path.suffix,[])
        suffix[path.suffix].append(filename)
      else:
        nosuffix.append(filename)
for k in suffix.keys():
    print(k, end=": ")
    for v in suffix[k]:
        print(v,end=" ")
    print("")
for v in nosuffix: print(v, end=" ")