C関数を使用して分割コマンドを実行するには?

C関数を使用して分割コマンドを実行するには?

C言語機能でLinuxコマンド「parted」を実行したいですか?

私はLinux Ubuntu、Eclipseを使用しています。

ありがとうございます!

ベストアンサー1

理論的には、Cプログラムでは次の行を追加する必要があります。

int res = system("/bin/parted <options>");

Cプログラムはroot権限で(またはを実行して)実行する必要がありますsudo。このres変数にはコマンドの結果が含まれます(man system詳細情報を参照)。

代わりに、execコマンドシリーズを使用できます(man exec詳細については、参考資料を参照)。

/dev/sdbたとえば、ディスクのパーティションテーブルを読み取る必要があります。

#include <stdlib.h>

int main(int argc, char **argv)
{
     int res = 0;
     res = system("/bin/parted -s /dev/sdb print > /var/log/mypartedlist.txt");
     if (res == -1) /* command not executed  */
        exit(1);
     else /* command ok */
     {
          if (WIFEXITED(res))
          {
              if (WEXITSTATUS(res) == 0)
                  printf("Command executed ok\n");
              else
                  printf("Command had a trouble\n");
          }
          else
          {
              printf("Problems running system\n");
              exit(2);
          }
     }   
}

おすすめ記事