Advertisement
Guest User

Untitled

a guest
Feb 7th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. /**
  2.  * Application: basic-webserver, Helpers.cpp
  3.  * Author: Project, UNX511, Team A
  4.  * Description: Contains helper functions
  5.  * A webserver that has been coded from scratch.
  6.  * It uses Linux's socket library to deal with connections.
  7.  *
  8.  * This application is licensed under Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
  9.  * See this for more information: http://creativecommons.org/licenses/by-nc-sa/3.0/
  10.  */
  11.  
  12. #include "Helpers.h"
  13.  
  14. void preventZombies() {
  15.    signal(SIGHUP, SIG_IGN); // Ignore terminal hangups
  16.    signal(SIGCLD, SIG_IGN); // Ignore child death
  17.    setpgrp();               // Break away from process group
  18. }
  19.  
  20. char* runCmd(const char* cmd){
  21.    FILE* fp;
  22.    char* out = NULL;
  23.    char* more_out;
  24.    char ch;
  25.    int count = 0;
  26.  
  27.   /* Open the command for reading. */
  28.    fp = popen(cmd, "r");
  29.    if (fp == NULL) {
  30.       printf("Failed to run command\n" );
  31.       return 0;
  32.    }
  33.  
  34.    /* Read the output a line at a time - output it. */
  35.    ch = fgetc(fp);
  36.    while(ch != '\n' && ch != EOF){
  37.       more_out = (char*) realloc(out, ++count); // no need to free more_out
  38.  
  39.       if(more_out != NULL){
  40.          out = more_out;
  41.          out[count-1] = ch;
  42.       }
  43.       ch = fgetc(fp);
  44.    }
  45.  
  46.    pclose(fp);
  47.    return out;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement