Advertisement
Bladtman

notif.c

Jan 19th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void pHelp();
  6. char* createString(int len);
  7. void notif(char *command);
  8.  
  9. int main (int argc, char *argv[]) {
  10.    
  11.     if(argc !=2 || strcmp(argv[1], "--help") == 0) {
  12.         pHelp();   
  13.     } else {
  14.         notif(argv[1]);
  15.     }
  16.     return 0;
  17. }
  18.  
  19. void notif (char* command) {
  20.     int commLen = strlen(command);
  21.     if(system(command) == 0) { //zero is success for _most_ UNIX binaries, but evaluates to false in C. so == 0 is necessary
  22.         char* buff = createString(commLen +29);
  23.         strcat(buff, "notif finished\" \"Command: '");
  24.         strcat(buff, command);
  25.         strcat(buff, "'\"");
  26.         system(buff);
  27.     } else {
  28.         char* buff = createString(commLen +56);
  29.         strcat(buff, "notif failed\" \"Command: '");
  30.         strcat(buff, command);
  31.         strcat(buff, "' did not execute as expected.\"");
  32.         system(buff);
  33.     }
  34. }
  35.  
  36. char* createString(int len) {
  37.     char* str = malloc(len +14); //Size of len, plus 1 for the string terminator plus the length of the string added in this method (13 currently)
  38.  
  39.         if (!str) { //if malloc returned NULL
  40.             printf("notif couldn't free enough memory,\nterminating");
  41.             exit(EXIT_FAILURE);
  42.         }
  43.         str[0] = '\0';
  44.         strcat (str, "notify-send \""); //specific, because both times this function is used need this as start of the string
  45.         return str;
  46. }
  47.    
  48. void pHelp () {
  49.     printf("usage: notif [CMND...]\n\tCMND being the command to loop over.\n\tCMND shoud be quoted if it contains whitespace characters.\n\n(note that command aliases are currently ignored.)\n\n'notif --help' displays this message\n");
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement