⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ramdemo.c

📁 nucleus 文件系统,内核和彩色图形系统,在小系统上非常好用
💻 C
📖 第 1 页 / 共 4 页
字号:

    /*  Create the directory.  */
    if (!NU_Make_Dir(dir_id))
        bail_out(CANT_CREAT_DIR);

    /*  Indicate that the directory was created.  */
    RAM_Directories_Created[task_id]++;

    /*  Check for input.  */
    looping();

    /*  Set the current working directory.  */
    if (!NU_Set_Current_Dir(dir_id))
        bail_out(CANT_SET_WRK_DIR);

    /* Create 20 files */
    for (i = 0; (looping() && i < 20); i++)
    {
        /*  Create a unique file name (note that it must be in upper
            case letters.  */
        fname_1[0] = '\0'; pc_strcat(fname_1,"FILE. ");
        fname_1[5] = (char) ('A'+(char) i);

        /* Now create the file. */
		if ((fd = NU_Open(fname_1,(PO_BINARY|PO_WRONLY|PO_CREAT|PO_TRUNC),
                         (PS_IWRITE | PS_IREAD) ) ) < 0)
            bail_out(CANT_OPEN_FILE);

        /* Write to the file.  */
        if (NU_Write(fd,(UTINY*) wr_buf, (UCOUNT)512) != 512)
            bail_out(CANT_WRITE_FILE);

        /* Close the file.  */
        NU_Close(fd);

        /*  Indicate that a file has been created.  */
        RAM_Files_Created[task_id]++;
    }

    /* Rename the 20 files */
    for (i = 0; (looping() && i < 20); i++)
    {
        /* Create the file names for the current file and the new
           name.  */
        fname_1[0] = '\0';
        pc_strcat(fname_1,"FILE. ");
        fname_1[5] = (char) ('A'+(char) i);
        fname_2[0] = '\0';pc_strcat(fname_2,"NEWFILE. ");
        fname_2[8] = (char) ('A'+(char) i);

        /* Rename the file. */
		if (!NU_Rename(fname_1, fname_2))
            bail_out(CANT_REN_FILE);

        /* Indicate that the file was renamed. */
        RAM_Files_Renamed[task_id]++;
    }

    /* Go back to the root directory. */
	if (!NU_Set_Current_Dir("\\"))
        bail_out(CANT_SET_WRK_DIR);
}


/************************************************************************/
/*                                                                      */
/*  FUNCTION                           "write_test"                     */
/*                                                                      */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      This routine scans the whole dirve specified by drive_id.  It   */
/*      then increments a counter indicating that it found a file.      */
/*                                                                      */
/*      The routine simulates recursion with a local stack (to reduce)  */
/*      the stack requirements of the function).                        */
/*                                                                      */
/*  AUTHOR                                                              */
/*                      Accelerated Technology                          */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      nuf_task_0                          Initiator task.             */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*      pc_set_default_drive                Sets RAM as default drive. */
/*      error_log                           Records an error.           */
/*      pc_mpath                            Make a path name.           */
/*      pc_strcat                           Concatenate a sting.        */
/*      nu_strcmp                              Compare two strings.        */
/*      NU_Set_Current_Dir                  Indicates current directory.*/
/*      NU_Is_Dir                           Determines if the entry is  */
/*                                          a directory.                */
/*      NU_Get_First                        Gets the first file in a    */
/*                                          directory.                  */
/*      mkname                              Constructs a valid file name*/
/*      NU_Get_Next                         Gets the next file in a     */
/*                                          directory started by        */
/*                                          NU_Get_First.               */
/*      NU_Done                             Cleans up after searching   */
/*                                          a directory.                */
/*      looping                             Checks for user input.      */
/*      bail_out                            Error reporting routine.    */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*      task_id                             Task makeing the call.      */
/*      drive_id                            Physical ID of the drive.   */
/*      scan_stack                          Local stack for dir names.  */
/*      path                                Current path for search.    */
/*                                                                      */
/*  OUTPUTS                                                             */
/*       NONE                                                           */
/*                                                                      */
/************************************************************************/
BOOL scan_drive( int task_id, char *drive_id, char scan_stack[30][13],
                 char *path) /*__fn__*/
{
char fname_buffer[14];
DSTAT statobj;
int i,stackp;
BOOL found_dir;
BOOL display_files;

    /* Set the default drive to drive_id */
    if (!pc_set_default_drive(drive_id))
        return(error_log(CANT_SELECT));

    /* clear our directory stack and reset the stack pointer */
    for (stackp=0; stackp<30; stackp++)
        scan_stack[stackp][0]='\0';

    /* Initialize our pseudo stack pointer.  */
    stackp = 0;

    /* Loop forever. traversing the file system using our own private "stack"
       when the "stack" pointer drops back to zero we have scanned the whole
       drive and may return */
    while(looping())
    {
        /* Use our stack to get back to the directory we were working in.
           We also build up the path name as we go. */
		if (!NU_Set_Current_Dir("\\"))
            return(error_log(CANT_SET_DEF));

        /*  Start at the root.  */
        path[0]='\\'; path[1] = '\0';

        /*  Go through our entire stack.  */
        for (i = 0; i < stackp; i++)
        {

            /* Set the current directory to the root.  */
            if (!NU_Set_Current_Dir(scan_stack[i]))
                return(error_log(CANT_SET_DEF));

            /* Make a path.  */
            pc_mpath((UTEXT*)path, (UTEXT*)path, (UTEXT*)scan_stack[i]);
        }

        /* If the stack is NULL for this dir we just got here so display files.
           Otherwise we are scanning back to where we left off */
        if (scan_stack[stackp][0])
            display_files = NO;
        else
        {
            RAM_Directories_Scanned[task_id]++;
            display_files = YES;
        }

        /*  Initialize the indicator for determining if a directory has
            been found. */
        found_dir = NO;

        /* Get first entry the directory */
        if (!NU_Get_First(&statobj, "*.*"))
        {
            /* No entries. This is an error in any subdirectories  (they
               should at least have . and .. */
            if (stackp)
                return(error_log(INVALID_DIR));
            return(YES);                     
        }

        do
        {
            /* make into file.nam  */
            mkname(fname_buffer, &statobj);

            /* Make sure we have a directory. */
            if (statobj.fattribute & ADIRENT)
            {
                /* If it's a directory push it on our stack */
                if (fname_buffer[0] != '.')
                {
                    /* If we have a blank entry, put the name on it.  */
                    if (!scan_stack[stackp][0])
                    {
                        /* First time at this level. Push the dir name and
                           break so we can visit this dir (in a recursive 
                           function we would recurse right here */
                        scan_stack[stackp][0] = '\0';

                        /*  Put the name in.  */
                        pc_strcat(scan_stack[stackp],fname_buffer);

                        /*  Indicate that we have a directory.  */
                        found_dir = YES;

                        /*  Look at the next entry. */
                        stackp += 1;

                        /*  If we have gone to far, it is an error.  */
                        if (stackp >= 30)
                        {
                            error_log(TOO_MANY_SUBS);
                            return(NO);
                        }

                        /*  Indicate that the next entry is blank.  */
                        scan_stack[stackp][0] = 0;

                        /*  Process the current one.  */
                        break;
                    }
                    else
                    {
                        /* Here we simulate the continuation after the return
                           from the recursion */
                        if (nu_strcmp(scan_stack[stackp],fname_buffer)==0)
                        {
                            /* 0 the stack so we take the next directory.
                              plus display files again */
                            display_files = YES;
                            scan_stack[stackp][0] = 0;
                        }
                    }
                }
            }
            else if (!(statobj.fattribute & AVOLUME))
            {
                /* It's a file, display it. */
                if (display_files)
                {
                    RAM_Files_Scanned[task_id]++;
                }
            }

		} while (NU_Get_Next(&statobj));

        /* Call NU_Done to free up internal resources used by statobj */
		NU_Done(&statobj);

        /* If found_dir is NO it means we reached the end of the directory
           and do not require a scan of a subdirectory. Pop this directory
           off the stack */
        if (!found_dir)
        {
            /* If at the top level we are done */
            if (!stackp)
                return(YES);

            /* Clean up the stack and decrement the pointer */
             scan_stack[stackp--][0] = 0;
        }
    }
    return(YES);
}


/************************************************************************/
/*                                                                      */
/*  FUNCTION                           "mkname"                         */
/*                                                                      */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      This routine extracts the name and extension from a directory   */
/*      entry and converts it to a file name.                           */
/*                                                                      */
/*  AUTHOR                                                              */
/*                      Accelerated Technology                          */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      write_test                          Performs writes to the disk.*/
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*      NONE                                                            */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*      buffer                              Where file name is to be    */
/*                                          placed.                     */
/*      pstat                               Pointer to directory entry. */
/*                                                                      */
/*  OUTPUTS                                                             */
/*       NONE                                                           */
/*                                                                      */
/************************************************************************/
void mkname(char *buffer, DSTAT *pstat)
{
char *pfr, *pto;

    /*  Get a temporary pointer to the buffer. */
    pto = buffer;

    /*  Get a temporary pointer to the name in the directory entry.  */
    pfr = (char *) pstat->fname;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -