Ejemplo n.º 1
0
int main()
{
	char str[1024] = "The quick brown fox jumps over the lazy dog";
	printf("Before: %s\n", str);
	escape_space(str);
	printf("After: %s\n", str);
	return 0;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
  struct stat	ffstat;
  char  *inputstr = NULL;
  char  *inbuf = NULL;
  char  *tracktype_str=NULL;
  char  tmpbuf[4096];
  int   inlen = 0, err=0, c=0;
  int   limit_arg = 0;

  if (argc < 2)
    usage(argv[0]);

  while ((c = getopt(argc, argv,"befjt:To:cl:n:s")) != -1)
  {
    switch (c)
    {
    case 'b':
      print_byte_offset=1;
      break;
    case 'e':
      print_epoch_time=1;
      break;
    case 'f':
      print_filename_only=1;
      break;
    case 'j':
      print_julian_time=1;
      break;
    case 'o':
      logfilename=optarg;
      break;
    case 't':
      tracksrch=1;
      tracktype_str=optarg;
      if (atoi(tracktype_str)==1)
        tracktype1=1;
      else if (atoi(tracktype_str)==2)
        tracktype2=1;
      else
        usage(argv[0]);
      break;
    case 'T':
      tracksrch=1;
      tracktype1=1;
      tracktype2=1;
      break;
    case 'c':
    	print_file_hit_count=1;
    	break;
    case 'l':
    	limit_arg = atoi(optarg);
    	if (limit_arg > 0)
    	  limit_file_results = limit_arg;
    	else
    		usage(argv[0]);
    	break;
    case 'n':
    	exclude_extensions = stolower(optarg);
    	break;
    case 's':
    	newstatus = 1;

    	break;
    case 'h':
    default:
      usage(argv[0]);
      break;
    }
  } 

  //do some cleanup to make sure that invalid options don't get combined
  if (logfilename != NULL)
  {
    if (newstatus == 1)
    {
    	print_file_hit_count = 0;
    }
  }
  else
  {
  	newstatus = 0;
  	print_file_hit_count = 0;
  }

  inputstr = argv[optind];
  if (inputstr == NULL)
    usage(argv[0]);

  inlen = strlen(inputstr) + 1;

  if (open_logfile() < 0)
    exit(-1);

  inbuf = (char *)malloc(inlen + 1);

  memset(inbuf, '\0', inlen+1);
  strncpy(inbuf, inputstr, inlen);

  signal_proc();

  init_time = time(NULL);
  printf("\n%s\n", PROG_VER);
  printf("\nLocal start time: %s\n",ctime((time_t *)&init_time));
  if (check_dir(inbuf)) 
  {
#ifdef WINDOWS
    if ((inbuf[strlen(inbuf) - 1]) != '\\')
      inbuf[strlen(inbuf)] = '\\';
#else
    if ((inbuf[strlen(inbuf) - 1]) != '/')
      inbuf[strlen(inbuf)] = '/';
#endif
    proc_dir_list(inbuf);
  }
  else
  {
    err = get_file_stat(inbuf, &ffstat);
    if (err == -1)
    {
      if (errno == ENOENT)
        fprintf(stderr, "File %s not found, can't stat\n", inbuf);
      else
        fprintf(stderr, "Cannot stat file %s; errno=%d\n", inbuf, errno);
      exit (-1);
    }

    if ((ffstat.st_size > 0) && ((ffstat.st_mode & S_IFMT) == S_IFREG))
    {
      memset(&tmpbuf, '\0', 4096);
      if (escape_space(inbuf, tmpbuf) == 0)
      {
        if (logfilename != NULL)
          if (strstr(inbuf, logfilename) != NULL)
          fprintf(stderr, "main: We seem to be hitting our log file, so we'll leave this out of the search -> %s\n", inbuf);
          else
          {
#ifdef DEBUG
            printf("Processing file %s\n",inbuf);
#endif
            ccsrch(inbuf);
          }
        else
        {
#ifdef DEBUG
          printf("Processing file %s\n",inbuf);
#endif
          ccsrch(inbuf);
        }
      }
    }
    else if ((ffstat.st_mode & S_IFMT) == S_IFDIR)
    {
#ifdef WINDOWS
      if ((inbuf[strlen(inbuf) - 1]) != '\\')
        inbuf[strlen(inbuf)] = '\\';
#else
      if ((inbuf[strlen(inbuf) - 1]) != '/')
        inbuf[strlen(inbuf)] = '/';
#endif
      proc_dir_list(inbuf);
    }
    else
      fprintf(stderr, "main: Unknown mode returned-> %x\n", ffstat.st_mode);
  }

  cleanup_shtuff();
  free(inbuf);

  return (0);
}
Ejemplo n.º 3
0
int print_dir_entries(char *dir_name) {
	int dir_name_length = strlen(dir_name), item_length = 0;
	char *path = NULL;
	char *escaped_path = NULL;
	str_list *list = get_file_list(dir_name);
	struct DESKTOP_ENTRY data = { APPLICATION, NULL, NULL, NULL, NULL, NULL, true };

	if (dir_name == NULL || list == NULL) return 1;

	str_list *item = list;
	while (item != NULL) {
		item_length = strlen(item->str);
		path = realloc(path, dir_name_length + 1 + item_length + 1);
		sprintf(path, "%s/%s", dir_name, item->str);
		switch (is_reg_file(path)) {
		case 1:
			if (get_data(&data, path)) { 
				if (data.type == LINK) {
					printf(
						"<item\n"
						"  label=\"%s\"\n"
						"  icon=\"" PAGE_ICON "\">\n"
						"  <action name=\"Execute\">\n"
						"    <execute>%s &quot;%s&quot;</execute>\n"
						"  </action>\n"
						"</item>\n",
						data.name,
						g_browser_name,
						data.url
					);
				} else if (data.type == APPLICATION) {
					printf(
						"<item\n"
					);
					if (data.comment != NULL) {
						printf(
						"  label=\"%s (%s)\"\n", data.name, data.comment
						);
					} else {
						printf(
						"  label=\"%s\"\n", data.name
						);
					}
					printf(
						"  icon=\"%s/%s.png\">\n"
						"  <action name=\"Execute\">\n",
						g_icon_dir,
						data.icon
					);
					if (data.has_gui) {
						printf(
						"    <execute>%s</execute>\n", data.command
						);
					} else {
						printf(
						"    <execute>urxvt -icon &quot;%s/%s.png&quot; -title &quot;%s&quot; -e &quot;%s&quot;</execute>\n",
						g_icon_dir,
						data.command,
					   	data.command,
						data.command
						);
					}
					printf(
						"  </action>\n"
						"</item>\n"
					);
				}
			}
			reset_data(&data);
			break;

		case 0: /* if the path represents a directory */
			escaped_path = escape_space(path);
			printf(
				"<menu\n"
				"  icon=\"" CATEGORY_ICON "\"\n"
				"  id=\"%s\"\n"
				"  label=\"%s\"\n"
				"  execute=\"%s -d%s -b%s -f%s -i%s\" />\n",
				item->str,
				item->str,
				g_cmd_name,
				escaped_path,
				g_browser_name,
				g_filer_name,
				g_icon_dir
			);
			free(escaped_path);
			break;
		}
		item = item->next;
	}
	strlist_free(list);
	free(path);
	return 0;
}
Ejemplo n.º 4
0
int proc_dir_list(char *instr)
{
  DIR            *dirptr;
  struct dirent  *direntptr;
  int             dir_name_len = 0;
  char           *curr_path = NULL;
  struct stat     fstat;
  int             err = 0;
  char            tmpbuf[4096];

  errno = 0;
  dir_name_len = strlen(instr);
  dirptr = opendir(instr);

#ifdef DEBUG
  printf("Checking directory <%s>\n",instr);
#endif

  if (dirptr == NULL)
  {
    fprintf(stderr, "proc_dir_list: Can't open dir %s; errno=%d\n", instr, errno);
    return (1);
  }
  errno = 0;
  curr_path = (char *)malloc(MAXPATH + 1);
  if (curr_path == NULL)
  {
    fprintf(stderr, "proc_dir_list: Can't allocate enough space; errno=%d\n", errno);
    closedir(dirptr);
    return (1);
  }
  memset(curr_path, '\0', MAXPATH+1);
  strncpy(curr_path, instr, MAXPATH);
  errno = 0;
  while ((direntptr = readdir(dirptr)) != NULL)
  {
    /* readdir give us everything and not necessarily in order. This 
       logic is just silly, but it works */
    if (((direntptr->d_name[0] == '.') &&
         (direntptr->d_name[1] == '\0')) ||
        ((direntptr->d_name[0] == '.') &&
         (direntptr->d_name[1] == '.') &&
         (direntptr->d_name[2] == '\0')))
      continue;

    errno = 0;
    strncat(curr_path, direntptr->d_name, MAXPATH);
    err = get_file_stat(curr_path, &fstat);

    if (err == -1)
    {
      if (errno == ENOENT)
        fprintf(stderr, "proc_dir_list: file %s not found, can't stat\n", curr_path);
      else
        fprintf(stderr, "proc_dir_list: Cannot stat file %s; errno=%d\n", curr_path, errno);
      closedir(dirptr);
      return (1);
    }
    if ((fstat.st_mode & S_IFMT) == S_IFDIR)
    {
      strncat(curr_path, "/", MAXPATH);
      proc_dir_list(curr_path);
    } 
    else if ((fstat.st_size > 0) && ((fstat.st_mode & S_IFMT) == S_IFREG))
    {
      memset(&tmpbuf, '\0', 4096);
      if (escape_space(curr_path, tmpbuf) == 0)
      {
        //rest file_hit_count so we can keep track of many hits each file has
        file_hit_count = 0;
        
        if (is_allowed_file_type(curr_path) == 0)
        {
	        /*
	         * kludge, need to clean this up
	         * later else any string matching in the path returns non NULL
	         */
	        if (logfilename != NULL)
	          if (strstr(curr_path, logfilename) != NULL)
	            fprintf(stderr, "We seem to be hitting our log file, so we'll leave this out of the search -> %s\n", curr_path);
	          else
	          {
	            ccsrch(curr_path);
	            if (file_hit_count > 0 && print_file_hit_count == 1)
	              printf("%s: %d hits\n", curr_path, file_hit_count);
	          }
	        else
	        {
	          ccsrch(curr_path);
	        }
        }
      }
    }
#ifdef DEBUG
    else
    {
      if (fstat.st_size > 0)
        fprintf(stderr, "proc_dir_list: Unknown mode returned-> %x for file %s\n", fstat.st_mode,curr_path);
      else
        fprintf(stderr, "Not processing file of size 0 bytes: %s\n", curr_path);
    }
#endif
    curr_path[dir_name_len] = '\0';
  }

  free(curr_path);

  closedir(dirptr);
  return (0);
}