ProvaPratica 2009.02.12

From Sistemi Operativi
Revision as of 15:02, 28 April 2014 by Stefano 92 (talk | contribs)
Jump to navigation Jump to search

http://www.cs.unibo.it/~renzo/so/compiti/2009-02-12.pdf


Esercizio 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void usage()
{
	printf("usage: ./invarg.out EXEC_NAME [ARG1] [ARG2] ... [ARGN]\n");
}

int main(int argc,char* argv[])
{
   char* tmp_i,*tmp_x;
   int x=(argc-1),i=2;
   if (argc<2)
   {
   		usage();
   		return(1);
   }
   while(i<x)
   {
       tmp_i=argv[i];
       tmp_x=argv[x];
       argv[i]=tmp_x;
       argv[x]=tmp_i;
       i++;
       x--;
   }
	for(i=0;i<argc-1;i++)
		argv[i]=argv[i+1];
	
	argv[argc-1]=NULL;
	argc--;
	execvp(argv[0],argv);
        //equivalentemente avrei potuto lanciare execvp(argv[1],argv+1) evitando di modificare argv[]
	return 0;

}

stefano92