Linux_C实现文档导出至USB

1658阅读 0评论2011-11-09 luozhiyong131
分类:嵌入式

/*

 * 功能:文档导出至USB

 * 描述:将文档复制到指定目录

 * 时间: 2011118     Lzy

 */

 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

/*

 * 功能:新线程运行的函数

 * 描述:提示文档复制正在运行

 */

void *PlayS(void *arg)       

{

    if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL))    //设置线程取消状态

    {

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        pthread_exit("pthread_setcancelstate");

    }

   

    if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL))    //设置线程取消类型

    {

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        pthread_exit("pthread_setcanceltype");

    }

   

    while(1)

    {

        write(STDOUT_FILENO,"*",1);        //正在复制提示信号

        sleep(1);

    }

   

}

 

int main(int argc, char *argv[])

 {

     pthread_t tid;        //定义线程标识符

     char arg[50];

         

     /* 获得要复制文件名 */

     if(argc < 2)

     {

         printf("输入要复制文件名: ");

         exit(1);

     }

                 

      /* 扗载U盘至/mnt目录 */

     if((system("mount /dev/sda1 /mnt")) != 0)

     {

         printf("mount USB failed\n");

         exit(0);

     } 

     

    write(STDOUT_FILENO,"Please Wait",strlen("Please Wait"));  /*打印提示信息*/

    

   if(pthread_create(&tid, NULL, PlayS, NULL))        //创建新线程

    {

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        exit(0);

    }

        

     sprintf(arg,"cp -rf %s /mnt",argv[1]);      

     if(system(arg) < 0)     /* 文件复制 */

     {

         printf("Copy  failed\n");

         exit(0);

     }

     

    if(pthread_cancel(tid))            //终止新线程

    {       

        printf("FUNC %s: %d\n", __FUNCTION__, __LINE__);    //打印错误信息

        exit(0);   

    }

   

    pthread_join(tid, NULL);        //等待新线程退出

         

     /* 制载U盘至/mnt目录 */

     if(system("umount /mnt") < 0)

     {

         printf("umount USB failed\n");

         exit(0);

     }

   

    printf(" Done!\n");

     

    return 0;

 }

源码: usb.rar   

上一篇:Linux_C实现将文件导出至U盘
下一篇:LCD测试程序