#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Initialisation d'une valeur entière dans le fichier NOMBRE */

#define VAL_INIT	 100

void main (int nbarg, char *tbarg[]) {
  int  df_n, nombre, nbo_ecrits;
  off_t lret ;

 if ((df_n= open ("NOMBRE",O_WRONLY|O_CREAT,0664)) < 0)
	{	perror("ouverture de NOMBRE");
		exit(errno);
	}

 if ((lret=lseek(df_n,(off_t)0,SEEK_SET)) < 0)
	{	perror("lseek sur NOMBRE");
		exit(errno);
	}
 
 if(nbarg<=1) nombre=VAL_INIT;
		else { int j;
		       for (j=0;  j< strlen(tbarg[1]); j++)
			{
        if  ( ! isdigit(tbarg[1][j]))
        {
          printf(" argument %s non numérique \n", tbarg [1]);
          exit(-1);
        } 
			}	
			nombre= atoi(tbarg[1]);
		}
  if ((nbo_ecrits=write(df_n,&nombre,sizeof (int))) < sizeof (int) ) {
    perror("ecriture NOMBRE");
		exit(errno);
  }

 printf("Valeur initiale (INIT) nombre: %d \n",nombre);
 close(df_n);
}