欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
unix程序設計:實(shí)現cp命令 - heiyeluren的blog

最近苦讀《Unix系統編程》便寫(xiě)了一些實(shí)例,逐步增加自己Unix程序設計的能力。

首先來(lái)實(shí)現一個(gè)Unix下常用命令:cp

先看代碼:


#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFSIZE  512
#define PERM  0755

/* copy file function */
int copyfile(const char *name1, const char *name2)
{
 int infile, outfile;
 ssize_t nread;
 char buffer[BUFSIZE];
 /* 打開(kāi)源文件 */
 if ((infile = open(name1, O_RDONLY)) == -1)
  return (-1);
 /* 打開(kāi)目標文件 */
 if ((outfile = open(name2, O_WRONLY|O_CREAT|O_TRUNC, PERM)) == -1)
 {
  close(infile);
  return (-2);
 }
 /* 循環(huán)的把源文件寫(xiě)入目標文件 */
 while ((nread = read(infile, buffer, BUFSIZE)) > 0)
 {
  if (write(outfile, buffer, nread) < nread)
  {
   close(infile);
   close(outfile);
   return (-3);
  }
 }
 /* 關(guān)閉資源 */
 close(infile);
 close(outfile);
 
 if (nread == -1)
  return (-4);
 else
  return (0);
}

main(int argc, char *argv[])
{
 /* 判斷提交的參數 */
 if (argc != 3) {
  printf("Usage: copyfile <file1> <file2>\n");
  exit(1);
 }
 char *file1, *file2;
 file1 = argv[1];
 file2 = argv[2];
 int retcode;
 /* 進(jìn)行復制 */
 retcode = copyfile(file1, file2);
 /* 錯誤信息控制 */
 if (retcode == -1) {
  printf("Open %s failed\n", file1);
  exit(1);
 }
 if (retcode == -2) {
  printf("Open %s failed\n", file2);
  exit(1);
 }
 if (retcode == -3) {
  printf("Read %s buffer failed\n", file1);
  exit(1);
 }
 if (retcode == -4) {
  printf("Write %s buffer failed\n", file2);
  exit(1);
 }

 if (retcode == 0) {
  printf("Copy file succeed!\n");
 }
}



保存為copyfile.c,然后使用gcc來(lái)編譯:gcc -o copyfile copyfile.c

使用命令的格式是:copyfile <file1> <file2>

能夠復制任何文件,不管是ASC還是二進(jìn)制的。其實(shí)根本原理就是調用了三個(gè)Unix下的系統調用:open, read, write,完成基本的IO操作,既然不復雜,我就不解釋了。

本代碼再FreeBSD5.3下編譯通過(guò)。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
把ida的匯編代碼扒到VC中
國家計算機二級c語(yǔ)言考試試題題庫
命令行程序選項解析函數—getopt
fputc(c,out)
fopen 中 按文本讀寫(xiě)與按二進(jìn)制讀寫(xiě) 實(shí)例
高級Unix命令 | 酷殼 - CoolShell.cn
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久