void InsertAfter (List *L,address P,address Prec) {
/*	I.S. Prec pastilah elemen list dan bukan elemen terakhir, 
	P sudah dialokasi  
	F.S. Insert P sebagai elemen sesudah elemen beralamat Prec */
	if(FSearch(*L,P))
		printf("Address tidak boleh element dari List yang sudah ada.\n");
	else {
		if(Prec==Last(*L)) InsertLast(L,P);
		else {	
			address Pt = Next(Prec);
			Next(Prec) = P;
			Prev(P) = Prec;
			Next(P) = Pt;
			Prev(Pt) = P;
		}
	}
}
int main()
{
	/* Kamus Lokal */
		List MyList, List2, List3;
		int i;
		infotype isi;
		address P, Prec;
		
	/* Program */
	CreateList (&MyList);
	printf ("Jml Elemen List adalah : %d \n", NbElmt(MyList));
/* Menambah List di awal */
	i = 1;
	while (i <= 5)
	{
		InsVFirst (&MyList, i*5);
		i++;
	}
	printf ("Hasil InsVFirst 5 x adalah : "); PrintInfo (MyList);
	printf ("Node Maksimal = %d ",Max (MyList));
	printf ("Node Minimal = %d ",Min (MyList));
	printf ("Rata-rata = %d \n",Average (MyList));
/* Mencari suatu elemen di list */	

	P = Search(MyList, 15);
printf ("Search yang berhasil (15) : P = %d, Ketemu = %d \n",P,FSearch(MyList,P));
	DelP (&MyList, 15);

/* Insert di Last */
	printf ("Insert di akhir nilai 723 : ");
	InsVLast (&MyList, 723); 
	PrintInfo (MyList);

/* Insert diantara 2 elemen */
	printf ("Insert sebelum elemen 10  : ");
	Prec = SearchPrec (MyList, 10);
	P = Alokasi (2712);
	if (P != Nil)
	{   InsertAfter (&MyList, P, Prec);	}
	PrintInfo (MyList);
	
/* Menghapus elemen List */	
	printf ("\tHasil Delete dari elemen List :\n");
	printf ("Jumlah elemen list = %d \n", NbElmt(MyList));
	DelVFirst (&MyList, &isi);		
	printf ("DelVFirst adalah %d\t", isi);
	
	DelVLast (&MyList, &isi);		
	printf ("DelVLast adalah %d\t", isi);

/* Menghapus elemen di tengah-tengah */
	Prec = SearchPrec (MyList, 10);	/* Node yang akan dihapus */
	if (Prec != Nil)
	{
	    DelAfter (&MyList, &P, Prec);		
	    isi = Info(P);
	    DeAlokasi (P);
	    printf ("DelAfter adalah %d\n", isi);
	}    
	printf ("Hasil setelah delete : ");
        PrintInfo (MyList);
	
	printf ("Insert sebelum elemen 5 : ");
	Prec = SearchPrec (MyList, 5);
	P = Alokasi (-987);
	if (P != Nil)
	{   InsertAfter (&MyList, P, Prec);	}
	PrintInfo (MyList);

/* Invers List */
	printf ("\tHasil Invers dari List yang ada : \n");
	printf ("Versi 1 : ");
	List2 = FInversList (MyList);	
	PrintInfo (List2);
	
	printf ("Versi 2 : ");
	InversList (&MyList);
        PrintInfo (MyList);

/* Copy List */
	printf ("\tHasil Copy dari List yang ada : \n");
	printf("Versi 1 : ");
	CopyList (MyList, &List2);
	PrintInfo (List2);	

	printf ("Versi 2 : ");
	List3 = FCopyList (MyList);
	PrintInfo (List3);	
	
	printf ("Versi 3 : ");
	CpAlokList (MyList, &List2);
	PrintInfo (List2);	

/* Konkat */ 
	printf("\tHasil Konkat Invers dan List asli : \n");
	List2 = FInversList (MyList);	
	Konkat (List2, List3, &MyList);
	printf("Versi 1 : ");
	PrintInfo (MyList);	

	Konkat1 (&List2, &List3, &MyList);
	printf("Versi 2 : ");
	PrintInfo (MyList);	

/* Pecah List */
	PecahList (&List2, &List3, MyList);
	printf ("\tHasil membagi dua list adalah : \n");
	printf ("L1 = "); PrintInfo (List2);
	printf ("L2 = "); PrintInfo (List3);
/* Finishing */	
	P = First(MyList);
	DeAlokasi (P); 
	P = First(List2);
	DeAlokasi (P); 
	P = First(List3);
	DeAlokasi (P); 
	return 0;
}	
Exemple #3
0
DIR *opendir(const char *dirname)
{
  DIR *dir;
  void *ptr;

#if OS_PARM_CHECK
  /*-------------------------------------------------------------------*/
  /* If path is NULL, return NULL.                                     */
  /*-------------------------------------------------------------------*/
  if (dirname == NULL)
  {
    set_errno(ENOTDIR);
    return NULL;
  }
#endif

  /*-------------------------------------------------------------------*/
  /* Acquire exclusive access to upper file system.                    */
  /*-------------------------------------------------------------------*/
  semPend(FileSysSem, WAIT_FOREVER);

  /*-------------------------------------------------------------------*/
  /* Find a free file control block and initialize it.                 */
  /*-------------------------------------------------------------------*/
  for (dir = &Files[0]; dir->ioctl; ++dir)
  {
    /*-----------------------------------------------------------------*/
    /* If no DIR was found, return error.                              */
    /*-----------------------------------------------------------------*/
    if (dir == &Files[DIROPEN_MAX - 1])
    {
      set_errno(ENOMEM);
      semPost(FileSysSem);
      return NULL;
    }
  }
  FsInitFCB(dir, FCB_DIR);

  /*-------------------------------------------------------------------*/
  /* Ensure that path is valid and assign ioctl callback function.     */
  /*-------------------------------------------------------------------*/
  ptr = FSearch(dir, &dirname, ACTUAL_DIR);
  if (ptr == NULL)
  {
    dir->ioctl = NULL;
    semPost(FileSysSem);
    return NULL;
  }

  /*-------------------------------------------------------------------*/
  /* Acquire exclusive access to lower file system.                    */
  /*-------------------------------------------------------------------*/
  dir->acquire(dir, F_READ | F_WRITE);

  /*-------------------------------------------------------------------*/
  /* Call file system specific OPENDIR routine.                        */
  /*-------------------------------------------------------------------*/
  ptr = dir->ioctl(dir, OPENDIR, ptr);

  /*-------------------------------------------------------------------*/
  /* Release exclusive access to lower file system.                    */
  /*-------------------------------------------------------------------*/
  dir->release(dir, F_READ | F_WRITE);

  /*-------------------------------------------------------------------*/
  /* Release file control block if an error occurred.                  */
  /*-------------------------------------------------------------------*/
  if (ptr == NULL)
    dir->ioctl = NULL;

  /*-------------------------------------------------------------------*/
  /* Release upper file system access and return result.               */
  /*-------------------------------------------------------------------*/
  semPost(FileSysSem);
  return ptr;
}
Exemple #4
0
int creatn(const char *path, mode_t mode, size_t size)
{
  int rv = -1;
  void *dir;
  FILE *file;
#if !_PATH_NO_TRUNC
  char trunc_path[PATH_MAX + 1];
#endif

#if OS_PARM_CHECK
  /*-------------------------------------------------------------------*/
  /* If path is NULL, return -1.                                       */
  /*-------------------------------------------------------------------*/
  if (path == NULL)
  {
    set_errno(EFAULT);
    return -1;
  }
#endif

  /*-------------------------------------------------------------------*/
  /* Acquire exclusive access to upper file system.                    */
  /*-------------------------------------------------------------------*/
  semPend(FileSysSem, WAIT_FOREVER);

  /*-------------------------------------------------------------------*/
  /* Find a free file control block and initialize it.                 */
  /*-------------------------------------------------------------------*/
  for (file = &Files[0]; file->ioctl; ++file)
  {
    /*-----------------------------------------------------------------*/
    /* If none are free, return error.                                 */
    /*-----------------------------------------------------------------*/
    if (file == &Files[FOPEN_MAX - 1])
    {
      set_errno(EMFILE);
      semPost(FileSysSem);
      return -1;
    }
  }
  FsInitFCB(file, FCB_FILE);

  /*-------------------------------------------------------------------*/
  /* Ensure path is valid.                                             */
  /*-------------------------------------------------------------------*/
  dir = FSearch(file, &path, PARENT_DIR);
  if (dir == NULL)
    goto end;

  /*-------------------------------------------------------------------*/
  /* If path too long, return error if no truncation, else truncate.   */
  /*-------------------------------------------------------------------*/
  if (strlen(path) > PATH_MAX)
  {
#if _PATH_NO_TRUNC
    set_errno(ENAMETOOLONG);
    goto end;
#else
    strncpy(trunc_path, path, PATH_MAX);
    trunc_path[PATH_MAX] = '\0';
    path = trunc_path;
#endif
  }

  /*-------------------------------------------------------------------*/
  /* Acquire exclusive access to lower file system.                    */
  /*-------------------------------------------------------------------*/
  file->acquire(file, F_READ | F_WRITE);

  /*-------------------------------------------------------------------*/
  /* Call file system specific CREATN routine.                         */
  /*-------------------------------------------------------------------*/
  rv = (int)file->ioctl(file, CREATN, path, mode, size, dir);

  /*-------------------------------------------------------------------*/
  /* Release exclusive access to lower file system.                    */
  /*-------------------------------------------------------------------*/
  file->release(file, F_READ | F_WRITE);

  /*-------------------------------------------------------------------*/
  /* Free control block if error, else set return value.               */
  /*-------------------------------------------------------------------*/
end:
  if (rv == -1)
    file->ioctl = NULL;
  else
    rv = file - &Files[0];

  /*-------------------------------------------------------------------*/
  /* Release exclusive access to upper file system and return result.  */
  /*-------------------------------------------------------------------*/
  semPost(FileSysSem);
  return rv;
}
Exemple #5
0
FILE *freopen(const char *filename, const char *mode, FILE *file)
{
  void *dir;
  FILE *rv = NULL;
#if !_PATH_NO_TRUNC
  char trunc_filename[PATH_MAX + 1];
#endif

#if OS_PARM_CHECK
  /*-------------------------------------------------------------------*/
  /* If file handle is invalid, return NULL.                           */
  /*-------------------------------------------------------------------*/
  if (InvalidStream(file))
  {
    set_errno(EBADF);
    return NULL;
  }

  /*-------------------------------------------------------------------*/
  /* If file name is NULL, return NULL.                                */
  /*-------------------------------------------------------------------*/
  if (filename == NULL)
  {
    file->errcode = EINVAL;
    set_errno(EINVAL);
    return NULL;
  }
#endif

  /*-------------------------------------------------------------------*/
  /* Get exclusive access to upper file system.                        */
  /*-------------------------------------------------------------------*/
  semPend(FileSysSem, WAIT_FOREVER);

  /*-------------------------------------------------------------------*/
  /* Flush and close the file, acquiring and releasing volume access.  */
  /*-------------------------------------------------------------------*/
  file->acquire(file, F_READ | F_WRITE);
  file->ioctl(file, FFLUSH);
  file->ioctl(file, FCLOSE);
  file->release(file, F_READ | F_WRITE);

  /*-------------------------------------------------------------------*/
  /* Verify path of new file. Error exit if path is invalid.           */
  /*-------------------------------------------------------------------*/
  dir = FSearch(file, &filename, PARENT_DIR);
  if (dir == NULL)
    goto end;

  /*-------------------------------------------------------------------*/
  /* If path too long, return error if no truncation, else truncate.   */
  /*-------------------------------------------------------------------*/
  if (strlen(filename) > PATH_MAX)
  {
#if _PATH_NO_TRUNC
    set_errno(ENAMETOOLONG);
    goto end;
#else
    strncpy(trunc_filename, filename, PATH_MAX);
    trunc_filename[PATH_MAX] = '\0';
    filename = trunc_filename;
#endif
  }

  /*-------------------------------------------------------------------*/
  /* Open the file, acquiring and releasing volume access.             */
  /*-------------------------------------------------------------------*/
  file->acquire(file, F_READ | F_WRITE);
  rv = file->ioctl(file, FOPEN, filename, mode, dir);
  file->release(file, F_READ | F_WRITE);

end:
  /*-------------------------------------------------------------------*/
  /* Free control block if error. Release access to upper file system. */
  /*-------------------------------------------------------------------*/
  if (rv == NULL)
    file->ioctl = NULL;
  semPost(FileSysSem);
  return rv;
}
Exemple #6
0
int mkdir(const char *path, mode_t mode)
{
  void *parent;
  int r_value = -1;
  DIR dir;
#if !_PATH_NO_TRUNC
  char trunc_path[PATH_MAX + 1];
#endif

#if OS_PARM_CHECK
  /*-------------------------------------------------------------------*/
  /* If path is NULL, return -1.                                       */
  /*-------------------------------------------------------------------*/
  if (path == NULL)
  {
    set_errno(EFAULT);
    return -1;
  }
#endif

  /*-------------------------------------------------------------------*/
  /* Initialize temporary file control block.                          */
  /*-------------------------------------------------------------------*/
  FsInitFCB(&dir, FCB_DIR);

  /*-------------------------------------------------------------------*/
  /* Get exclusive access to the file system.                          */
  /*-------------------------------------------------------------------*/
  semPend(FileSysSem, WAIT_FOREVER);

  /*-------------------------------------------------------------------*/
  /* Ensure that path is valid.                                        */
  /*-------------------------------------------------------------------*/
  parent = FSearch(&dir, &path, PARENT_DIR);
  if (parent == NULL)
    goto end;

  /*-------------------------------------------------------------------*/
  /* If path too long, return error if no truncation, else truncate.   */
  /*-------------------------------------------------------------------*/
  if (strlen(path) > PATH_MAX)
  {
#if _PATH_NO_TRUNC
    set_errno(ENAMETOOLONG);
    goto end;
#else
    strncpy(trunc_path, path, PATH_MAX);
    trunc_path[PATH_MAX] = '\0';
    path = trunc_path;
#endif
  }

  /*-------------------------------------------------------------------*/
  /* Acquire exclusive access to lower file system.                    */
  /*-------------------------------------------------------------------*/
  dir.acquire(&dir, F_READ | F_WRITE);

  /*-------------------------------------------------------------------*/
  /* Call file system specific MKDIR routine.                          */
  /*-------------------------------------------------------------------*/
  r_value = (int)dir.ioctl(&dir, MKDIR, path, mode, parent, 0, 0);

  /*-------------------------------------------------------------------*/
  /* Release exclusive access to file systems and return result.       */
  /*-------------------------------------------------------------------*/
  dir.release(&dir, F_READ | F_WRITE);
end:
  semPost(FileSysSem);
  return r_value;
}